Search in sources :

Example 1 with NumberOfConnectionsExceeded

use of SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded 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 NumberOfConnectionsExceeded

use of SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded 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 NumberOfConnectionsExceeded

use of SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded 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 NumberOfConnectionsExceeded

use of SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded 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 NumberOfConnectionsExceeded

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

the class SQLDatabaseConnectionTest method testMovePakageToCartAndRemoveTwice.

@Test
public void testMovePakageToCartAndRemoveTwice() {
    SQLDatabaseConnection sqlConnection = new SQLDatabaseConnection();
    ProductPackage productPackage = new ProductPackage(new SmartCode(barcodeDebug, date112000), 5, locationWarehouse);
    int cartSession = 0;
    try {
        cartSession = sqlConnection.loginCustomer(ClientServerDefs.anonymousCustomerUsername, ClientServerDefs.anonymousCustomerPassword);
        sqlConnection.addProductPackageToWarehouse(null, productPackage);
        sqlConnection.addProductPackageToWarehouse(null, productPackage);
        assertEquals("10", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("0", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.placeProductPackageOnShelves(null, productPackage);
        assertEquals("5", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("5", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.placeProductPackageOnShelves(null, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("10", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.addProductToGroceryList(cartSession, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("5", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.addProductToGroceryList(cartSession, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("0", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
    } catch (CriticalError | ClientNotConnected | ProductNotExistInCatalog | ProductPackageAmountNotMatch | ProductPackageNotExist | AuthenticationError | ClientAlreadyConnected | NumberOfConnectionsExceeded e) {
        fail();
    }
    try {
        sqlConnection.removeProductPackageFromShelves(null, productPackage);
        fail();
    } catch (CriticalError | ClientNotConnected | ProductNotExistInCatalog | ProductPackageAmountNotMatch e) {
        fail();
    } catch (ProductPackageNotExist e) {
    }
    try {
        sqlConnection.removeProductFromGroceryList(cartSession, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("5", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.removeProductFromGroceryList(cartSession, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("10", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.removeProductPackageFromShelves(null, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("5", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.removeProductPackageFromShelves(null, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("0", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.logout(cartSession, ClientServerDefs.anonymousCustomerUsername);
    } catch (CriticalError | ClientNotConnected | ProductNotExistInCatalog | ProductPackageAmountNotMatch | ProductPackageNotExist e) {
        fail();
    }
}
Also used : AuthenticationError(SQLDatabase.SQLDatabaseException.AuthenticationError) SmartCode(BasicCommonClasses.SmartCode) ProductNotExistInCatalog(SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog) ProductPackageAmountNotMatch(SQLDatabase.SQLDatabaseException.ProductPackageAmountNotMatch) SQLDatabaseConnection(SQLDatabase.SQLDatabaseConnection) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) ProductPackage(BasicCommonClasses.ProductPackage) ProductPackageNotExist(SQLDatabase.SQLDatabaseException.ProductPackageNotExist) 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