use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class Customer method returnProductToShelf.
@Override
public void returnProductToShelf(SmartCode c, int amount) throws AmountBiggerThanAvailable, ProductPackageDoesNotExist, CriticalError, CustomerNotConnected {
String serverResponse;
log.info("Creating REMOVE_PRODUCT_FROM_GROCERY_LIST command wrapper to customer with id: " + id);
establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
try {
serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.REMOVE_PRODUCT_FROM_GROCERY_LIST, Serialization.serialize(new ProductPackage(c, amount, null))).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 | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
log.fatal("Critical bug: this command result isn't supposed to return here");
throw new CriticalError();
}
/* update customer data: groceryList, cartProductCache, totalSum */
if (groceryList == null)
throw new CriticalError();
try {
groceryList.removeProduct(new ProductPackage(c, amount, null));
} catch (ProductNotInList | AmountIsBiggerThanAvailable ¢) {
throw new CriticalError();
}
long barcode = c.getBarcode();
CartProduct cartProduct = cartProductCache.get(barcode);
ProductPackage productPackage = new ProductPackage(c, amount, null);
cartProduct.removeProductPackage(productPackage);
if (cartProduct.getTotalAmount() <= 0)
cartProductCache.remove(barcode);
else
cartProductCache.put(barcode, cartProduct);
totalProductsAmount -= amount;
totalSum -= amount * cartProduct.getCatalogProduct().getPrice();
log.info("REMOVE_PRODUCT_FROM_GROCERY_LIST command succeed.");
}
use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class Customer method getAllIngredients.
@Override
public List<Ingredient> getAllIngredients() throws CriticalError {
String serverResponse;
log.info("Creating getAllIngredients command wrapper");
establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
/* first: getting the product from the server */
try {
serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.GET_ALL_INGREDIENTS).serialize());
} catch (SocketTimeoutException e) {
log.fatal("Critical bug: failed to get respond from server");
throw new CriticalError();
}
terminateCommunication();
CommandWrapper commandWrapper = getCommandWrapper(serverResponse);
try {
resultDescriptorHandler(commandWrapper.getResultDescriptor());
} catch (InvalidCommandDescriptor | CriticalError | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | CustomerNotConnected | ProductCatalogDoesNotExist | InvalidParameter | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
log.fatal("Critical bug: this command result isn't supposed to return here");
throw new CriticalError();
}
log.info("getAllIngredients command succeed.");
return new Gson().fromJson(commandWrapper.getData(), new TypeToken<List<Ingredient>>() {
}.getType());
}
use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class RegisteredCustomer method updateCustomerProfile.
@Override
public void updateCustomerProfile(CustomerProfile p) throws CustomerNotConnected, InvalidParameter, AuthenticationError, CriticalError {
String serverResponse;
log.info("Creating updateCustomerProfile command wrapper to customer with username: " + p.getUserName());
establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
/* first: getting the product from the server */
try {
serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.UPDATE_CUSTOMER_PROFILE, Serialization.serialize(p)).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 | CriticalError | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | ProductCatalogDoesNotExist | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
log.fatal("Critical bug: this command result isn't supposed to return here");
throw new CriticalError();
}
customerProfile = p;
log.info("updateCustomerProfile command succeed.");
}
use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class RegisteredCustomer method removeCustomer.
@Override
public void removeCustomer() throws CustomerNotConnected, InvalidParameter, AuthenticationError, CriticalError {
String serverResponse;
log.info("Creating removeCustomer command wrapper to customer with username: " + customerProfile.getUserName());
establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
/* first: getting the product from the server */
try {
serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.REMOVE_CUSTOMER, Serialization.serialize(customerProfile.getUserName())).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 | CriticalError | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | ProductCatalogDoesNotExist | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
log.fatal("Critical bug: this command result isn't supposed to return here");
throw new CriticalError();
}
log.info("removeCustomer command succeed.");
}
use of SMExceptions.CommonExceptions.CriticalError in project SmartCity-Market by TechnionYP5777.
the class IsFreeCustomerNameTest method isFreeCustomerNameConnectionFailureTest.
@Test
public void isFreeCustomerNameConnectionFailureTest() {
try {
Mockito.when(clientRequestHandler.sendRequestWithRespond(new CommandWrapper(CustomerDefs.loginCommandSenderId, CommandDescriptor.IS_FREE_CUSTOMER_NAME, Serialization.serialize(username)).serialize())).thenThrow(new SocketTimeoutException());
} catch (IOException ¢) {
fail();
}
try {
customer.isFreeUsername(username);
fail();
} catch (CriticalError e) {
/* success */
}
}
Aggregations