use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class Customer method login.
@Override
public void login(String username, String password, boolean updateProductPictures) throws CriticalError, AuthenticationError {
CommandWrapper cmdwrppr = null;
String serverResponse = null;
log.info("Creating login command wrapper for customer");
establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
try {
serverResponse = sendRequestWithRespondToServer((new CommandWrapper(CustomerDefs.loginCommandSenderId, CommandDescriptor.LOGIN_CUSTOMER, Serialization.serialize(new Login(username, password)))).serialize());
} catch (SocketTimeoutException e) {
log.fatal("Critical bug: failed to get respond from server");
throw new CriticalError();
}
terminateCommunication();
cmdwrppr = getCommandWrapper(serverResponse);
try {
resultDescriptorHandler(cmdwrppr.getResultDescriptor());
} catch (InvalidCommandDescriptor | InvalidParameter | CustomerNotConnected | ProductCatalogDoesNotExist | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
log.fatal("Critical bug: this command result isn't supposed to return here");
throw new CriticalError();
}
id = cmdwrppr.getSenderID();
if (this instanceof RegisteredCustomer)
customerProfile = Serialization.deserialize(cmdwrppr.getData(), CustomerProfile.class);
//check for new product pictures asynchronous
if (updateProductPictures)
new UpdateProductPictures().start();
log.info("Customer Login to server as succeed. Client id is: " + id);
}
use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class Customer method viewCatalogProduct.
@Override
public CatalogProduct viewCatalogProduct(SmartCode c) throws CriticalError, CustomerNotConnected, ProductCatalogDoesNotExist {
String serverResponse;
log.info("Creating viewProductFromCatalog (in order to addPtoductToCart) command wrapper to customer with id: " + id);
establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
/* first: getting the product from the server */
try {
serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.VIEW_PRODUCT_FROM_CATALOG, Serialization.serialize(c)).serialize());
} catch (SocketTimeoutException e) {
log.fatal("Critical bug: failed to get respond from server");
throw new CriticalError();
}
terminateCommunication();
CommandWrapper $ = getCommandWrapper(serverResponse);
try {
resultDescriptorHandler($.getResultDescriptor());
} catch (InvalidCommandDescriptor | InvalidParameter | CriticalError | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
log.fatal("Critical bug: this command result isn't supposed to return here");
throw new CriticalError();
}
log.info("viewProductFromCatalog command succeed.");
return Serialization.deserialize($.getData(), CatalogProduct.class);
}
use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class Customer method logout.
@Override
public void logout() throws CustomerNotConnected, CriticalError {
String serverResponse;
log.info("Creating customer logout command wrapper with id: " + id);
establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
try {
serverResponse = sendRequestWithRespondToServer((new CommandWrapper(id, CommandDescriptor.LOGOUT)).serialize());
} catch (SocketTimeoutException e) {
log.fatal("Critical bug: failed to get respond from server");
throw new CriticalError();
}
terminateCommunication();
try {
resultDescriptorHandler(getCommandWrapper(serverResponse).getResultDescriptor());
} catch (InvalidCommandDescriptor | InvalidParameter | ProductCatalogDoesNotExist | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
log.fatal("Critical bug: this command result isn't supposed to return here");
throw new CriticalError();
}
customerProfile = null;
log.info("logout from server succeed.");
}
use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class ForgotPasswordHandler method sendRequestWithRespondToServer.
private String sendRequestWithRespondToServer(String request) throws CriticalError {
establishCommunication();
log.info("Sending command to server");
try {
String $ = this.clientRequestHandler.sendRequestWithRespond(request);
terminateCommunication();
return $;
} catch (IOException e) {
log.fatal("Sending logout command to server encounter sever fault : " + e.getMessage());
terminateCommunication();
throw new CriticalError();
}
}
use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class ViewProductFromCatalogTest method ViewProductFromCatalogSuccessfulTest.
@Test
public void ViewProductFromCatalogSuccessfulTest() {
CatalogProduct testCatalogProduct = null, catalogProduct = new CatalogProduct(1234567890, "name", null, new Manufacturer(1, "Manufacturer"), "description", 22.0, null, null);
CommandWrapper commandWrapper = new CommandWrapper(ResultDescriptor.SM_OK, Serialization.serialize(catalogProduct));
try {
Mockito.when(clientRequestHandler.sendRequestWithRespond(new CommandWrapper(WorkerDefs.loginCommandSenderId, CommandDescriptor.VIEW_PRODUCT_FROM_CATALOG, Serialization.serialize(new SmartCode(1234567890, null))).serialize())).thenReturn(commandWrapper.serialize());
} catch (IOException ¢) {
fail();
}
try {
testCatalogProduct = worker.viewProductFromCatalog(1234567890);
} catch (InvalidParameter | CriticalError | EmployeeNotConnected | ProductNotExistInCatalog | ConnectionFailure ¢) {
fail();
}
assertEquals(testCatalogProduct.getBarcode(), 1234567890);
assertEquals(testCatalogProduct.getManufacturer().getId(), 1);
assertEquals(testCatalogProduct.getManufacturer().getName(), "Manufacturer");
assertEquals(testCatalogProduct.getName(), "name");
}
Aggregations