Search in sources :

Example 1 with CustomerNotConnected

use of CustomerContracts.ACustomerExceptions.CustomerNotConnected in project SmartCity-Market by TechnionYP5777.

the class RemoveProductFromCartTest method setup.

@Before
public void setup() {
    PropertyConfigurator.configure("../log4j.properties");
    customer = new Customer(clientRequestHandler);
    try {
        Mockito.when(clientRequestHandler.sendRequestWithRespond(new CommandWrapper(CustomerDefs.loginCommandSenderId, CommandDescriptor.ADD_PRODUCT_TO_GROCERY_LIST, Serialization.serialize(pp)).serialize())).thenReturn(new CommandWrapper(ResultDescriptor.SM_OK).serialize());
    } catch (IOException ¢) {
        fail();
    }
    try {
        Mockito.when(clientRequestHandler.sendRequestWithRespond(new CommandWrapper(CustomerDefs.loginCommandSenderId, CommandDescriptor.VIEW_PRODUCT_FROM_CATALOG, Serialization.serialize(sc)).serialize())).thenReturn(new CommandWrapper(ResultDescriptor.SM_OK, Serialization.serialize(catalogProduct)).serialize());
    } catch (IOException ¢) {
        fail();
    }
    try {
        customer.addProductToCart(sc, amount);
    } catch (CriticalError | CustomerNotConnected | AmountBiggerThanAvailable | ProductPackageDoesNotExist | InvalidParameter e1) {
        fail();
    }
}
Also used : InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) CriticalError(SMExceptions.CommonExceptions.CriticalError) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) ICustomer(CustomerContracts.ICustomer) Customer(CustomerImplementations.Customer) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) IOException(java.io.IOException) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) Before(org.junit.Before)

Example 2 with CustomerNotConnected

use of CustomerContracts.ACustomerExceptions.CustomerNotConnected 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);
}
Also used : CriticalError(SMExceptions.CommonExceptions.CriticalError) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) Login(BasicCommonClasses.Login) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 3 with CustomerNotConnected

use of CustomerContracts.ACustomerExceptions.CustomerNotConnected in project SmartCity-Market by TechnionYP5777.

the class RemoveCustomerTest method setup.

@Before
public void setup() {
    PropertyConfigurator.configure("../log4j.properties");
    customer = new RegisteredCustomer(clientRequestHandler);
    try {
        Mockito.when(clientRequestHandler.sendRequestWithRespond(new CommandWrapper(CustomerDefs.loginCommandSenderId, CommandDescriptor.UPDATE_CUSTOMER_PROFILE, Serialization.serialize(customerProfile)).serialize())).thenReturn(new CommandWrapper(ResultDescriptor.SM_OK).serialize());
    } catch (IOException ¢) {
        fail();
    }
    try {
        customer.updateCustomerProfile(customerProfile);
    } catch (CustomerNotConnected | InvalidParameter | AuthenticationError | CriticalError e) {
        fail();
    }
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) CriticalError(SMExceptions.CommonExceptions.CriticalError) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) IOException(java.io.IOException) RegisteredCustomer(CustomerImplementations.RegisteredCustomer) Before(org.junit.Before)

Example 4 with CustomerNotConnected

use of CustomerContracts.ACustomerExceptions.CustomerNotConnected in project SmartCity-Market by TechnionYP5777.

the class Customer method resume.

@Override
public void resume(int _id) throws CriticalError, CustomerNotConnected {
    CommandWrapper $ = null;
    String serverResponse;
    log.info("Creating customer Load grocery list command wrapper with id: " + id);
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer((new CommandWrapper(id, CommandDescriptor.LOAD_GROCERY_LIST)).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    try {
        $ = getCommandWrapper(serverResponse);
        resultDescriptorHandler($.getResultDescriptor());
        id = _id;
        groceryList = Serialization.deserialize($.getData(), GroceryList.class);
    } 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();
    }
    /* Restoring Grocery list */
    try {
        loadCartProductCacheAndUpdateCartData();
    } catch (ProductCatalogDoesNotExist | CriticalError | CustomerNotConnected e) {
        log.fatal("Critical bug: Failed to fetch grocery list items from server");
    }
    log.info("load grocery list from server succeed.");
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) GroceryList(BasicCommonClasses.GroceryList) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 5 with CustomerNotConnected

use of CustomerContracts.ACustomerExceptions.CustomerNotConnected in project SmartCity-Market by TechnionYP5777.

the class Customer method registerNewCustomer.

@Override
public void registerNewCustomer(CustomerProfile p) throws CriticalError, InvalidParameter, UsernameAlreadyExists {
    String serverResponse;
    log.info("Creating registerNewCustomer 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.REGISTER_NEW_CUSTOMER, 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 | AuthenticationError | CustomerNotConnected | ProductCatalogDoesNotExist | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    log.info("registerNewCustomer command succeed.");
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Aggregations

CommandWrapper (ClientServerApi.CommandWrapper)7 CustomerNotConnected (CustomerContracts.ACustomerExceptions.CustomerNotConnected)7 CriticalError (SMExceptions.CommonExceptions.CriticalError)7 AmountBiggerThanAvailable (CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable)6 InvalidParameter (CustomerContracts.ACustomerExceptions.InvalidParameter)6 ProductPackageDoesNotExist (CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist)6 AuthenticationError (CustomerContracts.ACustomerExceptions.AuthenticationError)5 ForgotPasswordWrongAnswer (CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer)5 GroceryListIsEmpty (CustomerContracts.ACustomerExceptions.GroceryListIsEmpty)5 InvalidCommandDescriptor (CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor)5 ProductCatalogDoesNotExist (CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)5 SocketTimeoutException (java.net.SocketTimeoutException)5 UsernameAlreadyExists (CustomerContracts.ACustomerExceptions.UsernameAlreadyExists)4 IOException (java.io.IOException)2 Before (org.junit.Before)2 GroceryList (BasicCommonClasses.GroceryList)1 Ingredient (BasicCommonClasses.Ingredient)1 Login (BasicCommonClasses.Login)1 ICustomer (CustomerContracts.ICustomer)1 Customer (CustomerImplementations.Customer)1