package test;

import java.io.IOException;

import authentication.AuthenticationManager;
import authentication.Authentification;
import authentication.UserExistsException;
import authentication.UserUnknownException;
import authentication.WrongPasswordException;

public class TestAuthenticationSystem {

	public static void main(String[] args) {
		AuthenticationManager am = new Authentification () ;
		// users management
		try {
			am.addUser("bob","123");
			System.out.println("Bob has been added !");
			am.removeUser("bob");
			System.out.println("Bob has been removed !");
			am.removeUser("bob");
			System.out.println("Bob has been removes twice !");
		} catch (UserUnknownException e) {
			System.out.println(e.login +" : user unknown (unable to remove) !");
		} catch (UserExistsException e) {
			System.out.println(e.login +" has already been added !");
		}
		
		// authentication
		try {
			am.addUser("bob","123");
			System.out.println("Bob has been added !");
			am.authentify("bob","123");
			System.out.println("Authentification OK !");
			am.authentify("bob","456");
			System.out.println("Invalid password !");
		} catch (WrongPasswordException e) {
			System.out.println(e.login+" has provided an invalid password !");
		} catch (UserExistsException e) {
			System.out.println(e.login +" has already been added !");
		} catch (UserUnknownException e) {
			System.out.println(e.login +" : user unknown (enable to remove) !");
		}
		
		// persistance
		
		try {
			am.save("users.txt");
			/*AuthenticationManager*/ am = Authentification.load("users.txt");
			am.authentify("bob","123");
			System.out.println("Loading complete !");
		} catch (UserUnknownException e) {
			System.out.println(e.login +" is unknown ! error during the saving/loading.");
		} catch (WrongPasswordException e) {
			System.out.println(e.login+" has provided an invalid password !error during the saving/loading .");
		} catch (IOException e) {
			System.out.println(e);
		} catch (ClassNotFoundException e) {
			System.out.println(e);
		}
	}

}
