Search in sources :

Example 1 with ClientAlreadyConnected

use of SQLDatabase.SQLDatabaseException.ClientAlreadyConnected in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method loginEmployeeCommand.

private void loginEmployeeCommand(SQLDatabaseConnection c) {
    Login login = null;
    log.info("Login employee command called");
    try {
        login = Serialization.deserialize(inCommandWrapper.getData(), Login.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for login command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    if (!login.isValid()) {
        log.info("Login command failed, username and password can't be empty");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_INVALID_PARAMETER);
        return;
    }
    try {
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK);
        outCommandWrapper.setSenderID(c.loginWorker(login.getUserName(), login.getPassword()));
        try {
            outCommandWrapper.setData(c.getClientType(outCommandWrapper.getSenderID()));
        } catch (ClientNotConnected e) {
            log.fatal("Client is not connected for sender ID " + outCommandWrapper.getSenderID());
        }
        log.info("Login command succeded with sender ID " + outCommandWrapper.getSenderID() + " with client type " + outCommandWrapper.getData());
    } catch (AuthenticationError e) {
        log.info("Login command failed, username dosen't exist or wrong password received");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_USERNAME_DOES_NOT_EXIST_WRONG_PASSWORD);
    } catch (CriticalError e) {
        log.fatal("Login command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientAlreadyConnected e) {
        log.info("Login command failed, user already connected");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_SENDER_IS_ALREADY_CONNECTED);
    } catch (NumberOfConnectionsExceeded e) {
        log.fatal("Login command failed, too much connections (try to increase TRYS_NUMBER)");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    }
    log.info("Login employee with User " + login.getUserName() + " finished");
}
Also used : AuthenticationError(SQLDatabase.SQLDatabaseException.AuthenticationError) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login) NumberOfConnectionsExceeded(SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded) ClientAlreadyConnected(SQLDatabase.SQLDatabaseException.ClientAlreadyConnected)

Example 2 with ClientAlreadyConnected

use of SQLDatabase.SQLDatabaseException.ClientAlreadyConnected in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method loginClientCommand.

private void loginClientCommand(SQLDatabaseConnection c) {
    Login login = null;
    log.info("Login client command called");
    try {
        login = Serialization.deserialize(inCommandWrapper.getData(), Login.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for login command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    if (!login.isValid()) {
        log.info("Login command failed, username and password can't be empty");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_INVALID_PARAMETER);
        return;
    }
    try {
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK);
        int senderID = c.loginCustomer(login.getUserName(), login.getPassword());
        outCommandWrapper.setSenderID(senderID);
        try {
            outCommandWrapper.setData(c.getCustomerProfile(login.getUserName()));
        } catch (ClientNotExist e) {
            log.fatal("Client is not exist with username: " + outCommandWrapper.getSenderID());
        }
        log.info("Login command succeded with sender ID " + outCommandWrapper.getSenderID() + " with client type " + outCommandWrapper.getData());
    } catch (AuthenticationError e) {
        log.info("Login command failed, username dosen't exist or wrong password received");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_USERNAME_DOES_NOT_EXIST_WRONG_PASSWORD);
    } catch (CriticalError e) {
        log.fatal("Login command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientAlreadyConnected e) {
        log.info("Login command failed, user already connected");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_SENDER_IS_ALREADY_CONNECTED);
    } catch (NumberOfConnectionsExceeded e) {
        log.fatal("Login command failed, too much connections (try to increase TRYS_NUMBER)");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    }
    log.info("Login client with User " + login.getUserName() + " finished");
}
Also used : AuthenticationError(SQLDatabase.SQLDatabaseException.AuthenticationError) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login) NumberOfConnectionsExceeded(SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded) ClientAlreadyConnected(SQLDatabase.SQLDatabaseException.ClientAlreadyConnected) ClientNotExist(SQLDatabase.SQLDatabaseException.ClientNotExist)

Example 3 with ClientAlreadyConnected

use of SQLDatabase.SQLDatabaseException.ClientAlreadyConnected in project SmartCity-Market by TechnionYP5777.

the class LoginTest method loginEmployeeCriticalErrorTest.

@Test
public void loginEmployeeCriticalErrorTest() {
    Login login = new Login("unknown", "unknown");
    String command = new CommandWrapper(0, CommandDescriptor.LOGIN_EMPLOYEE, new Gson().toJson(login, Login.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.when(sqlDatabaseConnection.loginWorker(login.getUserName(), login.getPassword())).thenThrow(new CriticalError());
    } catch (NumberOfConnectionsExceeded | ClientAlreadyConnected | CriticalError | AuthenticationError e) {
        fail();
    }
    out = commandExecuter.execute(sqlDatabaseConnection);
    assertEquals(ResultDescriptor.SM_ERR, out.getResultDescriptor());
}
Also used : AuthenticationError(SQLDatabase.SQLDatabaseException.AuthenticationError) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login) NumberOfConnectionsExceeded(SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded) ClientAlreadyConnected(SQLDatabase.SQLDatabaseException.ClientAlreadyConnected) CommandExecuter(CommandHandler.CommandExecuter) Test(org.junit.Test)

Example 4 with ClientAlreadyConnected

use of SQLDatabase.SQLDatabaseException.ClientAlreadyConnected in project SmartCity-Market by TechnionYP5777.

the class LoginTest method loginEmployeeSuccessfulTest.

@Test
public void loginEmployeeSuccessfulTest() {
    Login login = new Login("admin", "admin");
    String command = new CommandWrapper(0, CommandDescriptor.LOGIN_EMPLOYEE, new Gson().toJson(login, Login.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.when(sqlDatabaseConnection.loginWorker(login.getUserName(), login.getPassword())).thenReturn(EMPLOYEE_SENDER_ID);
    } catch (NumberOfConnectionsExceeded | ClientAlreadyConnected | CriticalError | AuthenticationError e) {
        fail();
    }
    out = commandExecuter.execute(sqlDatabaseConnection);
    assertEquals(ResultDescriptor.SM_OK, out.getResultDescriptor());
}
Also used : AuthenticationError(SQLDatabase.SQLDatabaseException.AuthenticationError) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login) NumberOfConnectionsExceeded(SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded) ClientAlreadyConnected(SQLDatabase.SQLDatabaseException.ClientAlreadyConnected) CommandExecuter(CommandHandler.CommandExecuter) Test(org.junit.Test)

Example 5 with ClientAlreadyConnected

use of SQLDatabase.SQLDatabaseException.ClientAlreadyConnected in project SmartCity-Market by TechnionYP5777.

the class SQLDatabaseConnectionTest method testWorkerCanLoginWithNewPassword.

@Test
public void testWorkerCanLoginWithNewPassword() {
    SQLDatabaseConnection sqlConnection = new SQLDatabaseConnection();
    try {
        sqlConnection.addWorker(null, new Login(workerName, workerName), new ForgotPasswordData("", ""));
    } catch (CriticalError | ClientAlreadyExist | ClientNotConnected e) {
        fail();
    }
    try {
        sqlConnection.setPasswordWorker(workerName, "newPass");
        //try to login with new password
        int sessionID = sqlConnection.loginWorker(workerName, "newPass");
        sqlConnection.logout(sessionID, workerName);
    } catch (CriticalError | ClientNotExist | AuthenticationError | ClientAlreadyConnected | NumberOfConnectionsExceeded | ClientNotConnected e1) {
        fail();
    } finally {
        try {
            sqlConnection.removeWorker(null, workerName);
        } catch (CriticalError | ClientNotExist | ClientNotConnected e) {
            e.printStackTrace();
        }
    }
}
Also used : AuthenticationError(SQLDatabase.SQLDatabaseException.AuthenticationError) SQLDatabaseConnection(SQLDatabase.SQLDatabaseConnection) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) ForgotPasswordData(BasicCommonClasses.ForgotPasswordData) Login(BasicCommonClasses.Login) ClientNotExist(SQLDatabase.SQLDatabaseException.ClientNotExist) ClientAlreadyExist(SQLDatabase.SQLDatabaseException.ClientAlreadyExist) NumberOfConnectionsExceeded(SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded) ClientAlreadyConnected(SQLDatabase.SQLDatabaseException.ClientAlreadyConnected) Test(org.junit.Test)

Aggregations

AuthenticationError (SQLDatabase.SQLDatabaseException.AuthenticationError)27 ClientAlreadyConnected (SQLDatabase.SQLDatabaseException.ClientAlreadyConnected)27 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)27 NumberOfConnectionsExceeded (SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded)27 Test (org.junit.Test)25 SQLDatabaseConnection (SQLDatabase.SQLDatabaseConnection)19 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)19 ProductNotExistInCatalog (SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog)10 Login (BasicCommonClasses.Login)9 ProductPackage (BasicCommonClasses.ProductPackage)8 SmartCode (BasicCommonClasses.SmartCode)8 CommandWrapper (ClientServerApi.CommandWrapper)8 ProductPackageAmountNotMatch (SQLDatabase.SQLDatabaseException.ProductPackageAmountNotMatch)8 ProductPackageNotExist (SQLDatabase.SQLDatabaseException.ProductPackageNotExist)8 Gson (com.google.gson.Gson)7 CommandExecuter (CommandHandler.CommandExecuter)6 ClientNotExist (SQLDatabase.SQLDatabaseException.ClientNotExist)4 ClientAlreadyExist (SQLDatabase.SQLDatabaseException.ClientAlreadyExist)3 GroceryListIsEmpty (SQLDatabase.SQLDatabaseException.GroceryListIsEmpty)3 ForgotPasswordData (BasicCommonClasses.ForgotPasswordData)1