Search in sources :

Example 41 with CriticalError

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.");
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) AmountIsBiggerThanAvailable(CommonDefs.GroceryListExceptions.AmountIsBiggerThanAvailable) CriticalError(SMExceptions.CommonExceptions.CriticalError) ProductPackage(BasicCommonClasses.ProductPackage) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductNotInList(CommonDefs.GroceryListExceptions.ProductNotInList) CartProduct(BasicCommonClasses.CartProduct) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 42 with CriticalError

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());
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) Ingredient(BasicCommonClasses.Ingredient) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) TypeToken(com.google.gson.reflect.TypeToken) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 43 with CriticalError

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.");
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) CriticalError(SMExceptions.CommonExceptions.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper)

Example 44 with CriticalError

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.");
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) CriticalError(SMExceptions.CommonExceptions.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper)

Example 45 with CriticalError

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 */
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) CriticalError(SMExceptions.CommonExceptions.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

CriticalError (SMExceptions.CommonExceptions.CriticalError)45 CommandWrapper (ClientServerApi.CommandWrapper)39 InvalidCommandDescriptor (EmployeeDefs.AEmployeeException.InvalidCommandDescriptor)22 ParamIDAlreadyExists (EmployeeDefs.AEmployeeException.ParamIDAlreadyExists)22 WorkerAlreadyExists (EmployeeDefs.AEmployeeException.WorkerAlreadyExists)22 AuthenticationError (EmployeeDefs.AEmployeeException.AuthenticationError)21 EmployeeAlreadyConnected (EmployeeDefs.AEmployeeException.EmployeeAlreadyConnected)21 IngredientStillInUse (EmployeeDefs.AEmployeeException.IngredientStillInUse)21 ManfacturerStillInUse (EmployeeDefs.AEmployeeException.ManfacturerStillInUse)21 ProductAlreadyExistInCatalog (EmployeeDefs.AEmployeeException.ProductAlreadyExistInCatalog)21 ProductStillForSale (EmployeeDefs.AEmployeeException.ProductStillForSale)21 WorkerDoesNotExist (EmployeeDefs.AEmployeeException.WorkerDoesNotExist)21 AmountBiggerThanAvailable (EmployeeDefs.AEmployeeException.AmountBiggerThanAvailable)20 ParamIDDoesNotExist (EmployeeDefs.AEmployeeException.ParamIDDoesNotExist)20 ProductPackageDoesNotExist (EmployeeDefs.AEmployeeException.ProductPackageDoesNotExist)19 ProductNotExistInCatalog (EmployeeDefs.AEmployeeException.ProductNotExistInCatalog)17 SocketTimeoutException (java.net.SocketTimeoutException)14 AuthenticationError (CustomerContracts.ACustomerExceptions.AuthenticationError)10 ForgotPasswordWrongAnswer (CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer)10 InvalidCommandDescriptor (CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor)10