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");
}
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");
}
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());
}
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());
}
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();
}
}
}
Aggregations