Search in sources :

Example 36 with ProductPackage

use of BasicCommonClasses.ProductPackage in project SmartCity-Market by TechnionYP5777.

the class SQLDatabaseConnection method logoutAsCart.

/**
	 * logout cart from system. (remove cartID entry and it grocery list)
	 * 
	 * @param cartID
	 * @throws CriticalError
	 */
private void logoutAsCart(int cartID) throws CriticalError {
    // READ part of transaction
    int listID = getCartListId(cartID);
    PreparedStatement deleteGroceryList = null, deleteCart = null;
    try {
        // WRITE part of transaction
        // get grocery list and returns each product to shelf
        ResultSet groceryListResultSet = getGroceryListResultSetByCartID(cartID);
        groceryListResultSet.first();
        GroceryList groceryList = SQLJsonGenerator.resultSetToGroceryList(groceryListResultSet);
        if (groceryList.getList() != null)
            for (ProductPackage p : groceryList.getList().values()) moveProductPackage(cartID, LOCATIONS_TYPES.CART, LOCATIONS_TYPES.STORE, p, p.getAmount());
        // delete grocery list and cart
        deleteGroceryList = getParameterizedQuery(generateDeleteQuery(GroceriesListsTable.table, BinaryCondition.equalTo(GroceriesListsTable.listIDCol, PARAM_MARK)), listID);
        deleteCart = getParameterizedQuery(generateDeleteQuery(CartsListTable.table, BinaryCondition.equalTo(CartsListTable.listIDCol, PARAM_MARK)), listID);
        log.debug("logoutAsCart: delete groceryList " + listID + ".\n by run query: " + deleteGroceryList);
        deleteGroceryList.executeUpdate();
        log.debug("logoutAsCart: disconnect cart " + cartID + ".\n by run query: " + deleteCart);
        deleteCart.executeUpdate();
    } catch (SQLException e) {
        throw new CriticalError();
    } catch (ProductPackageAmountNotMatch e) {
        log.error("logoutAsCart: trying to return product to shelf but amount not matched to what in the grocery list");
        throw new CriticalError();
    } catch (ProductPackageNotExist e) {
        log.error("logoutAsCart: trying to return product to shelf but product package not exist in grocery list");
        throw new CriticalError();
    } finally {
        closeResources(deleteGroceryList, deleteCart);
    }
}
Also used : ProductPackage(BasicCommonClasses.ProductPackage) SQLException(java.sql.SQLException) GroceryList(BasicCommonClasses.GroceryList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 37 with ProductPackage

use of BasicCommonClasses.ProductPackage in project SmartCity-Market by TechnionYP5777.

the class AddProductPackageToWarehouseTest method addProductPackageToWarehouseSuccessfulTest.

@Test
public void addProductPackageToWarehouseSuccessfulTest() {
    int senderID = 1;
    ProductPackage productPackage = new ProductPackage(new SmartCode(1, null), 1, new Location(0, 0, PlaceInMarket.WAREHOUSE));
    String command = new CommandWrapper(senderID, CommandDescriptor.ADD_PRODUCT_PACKAGE_TO_WAREHOUSE, new Gson().toJson(productPackage, ProductPackage.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.doNothing().when(sqlDatabaseConnection).addProductPackageToWarehouse(senderID, productPackage);
    } catch (CriticalError | ClientNotConnected | ProductNotExistInCatalog e) {
        fail();
    }
    out = commandExecuter.execute(sqlDatabaseConnection);
    assertEquals(ResultDescriptor.SM_OK, out.getResultDescriptor());
}
Also used : SmartCode(BasicCommonClasses.SmartCode) ProductNotExistInCatalog(SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) ProductPackage(BasicCommonClasses.ProductPackage) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) CommandExecuter(CommandHandler.CommandExecuter) Location(BasicCommonClasses.Location) Test(org.junit.Test)

Example 38 with ProductPackage

use of BasicCommonClasses.ProductPackage in project SmartCity-Market by TechnionYP5777.

the class AddProductPackageToWarehouseTest method addProductPackageToWarehouseIllegalProductPackageTest.

@Test
public void addProductPackageToWarehouseIllegalProductPackageTest() {
    assertEquals(ResultDescriptor.SM_ERR, (new CommandExecuter(new CommandWrapper(1, CommandDescriptor.ADD_PRODUCT_PACKAGE_TO_WAREHOUSE, new Gson().toJson("", String.class)).serialize())).execute(sqlDatabaseConnection).getResultDescriptor());
    assertEquals(ResultDescriptor.SM_INVALID_PARAMETER, (new CommandExecuter(new CommandWrapper(1, CommandDescriptor.ADD_PRODUCT_PACKAGE_TO_WAREHOUSE, new Gson().toJson(new ProductPackage(new SmartCode(1, null), -1, new Location(0, 0, PlaceInMarket.WAREHOUSE)), ProductPackage.class)).serialize())).execute(sqlDatabaseConnection).getResultDescriptor());
}
Also used : SmartCode(BasicCommonClasses.SmartCode) ProductPackage(BasicCommonClasses.ProductPackage) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) CommandExecuter(CommandHandler.CommandExecuter) Location(BasicCommonClasses.Location) Test(org.junit.Test)

Example 39 with ProductPackage

use of BasicCommonClasses.ProductPackage in project SmartCity-Market by TechnionYP5777.

the class AddProductPackageToWarehouseTest method addProductPackageToWarehouseClientNotConnectedTest.

@Test
public void addProductPackageToWarehouseClientNotConnectedTest() {
    int senderID = 1;
    ProductPackage productPackage = new ProductPackage(new SmartCode(1, null), 1, new Location(0, 0, PlaceInMarket.WAREHOUSE));
    String command = new CommandWrapper(senderID, CommandDescriptor.ADD_PRODUCT_PACKAGE_TO_WAREHOUSE, new Gson().toJson(productPackage, ProductPackage.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.doThrow(new ClientNotConnected()).when(sqlDatabaseConnection).addProductPackageToWarehouse(senderID, productPackage);
    } catch (CriticalError | ProductNotExistInCatalog e) {
        fail();
    } catch (ClientNotConnected __) {
    /* Success */
    }
    out = commandExecuter.execute(sqlDatabaseConnection);
    assertEquals(ResultDescriptor.SM_SENDER_IS_NOT_CONNECTED, out.getResultDescriptor());
}
Also used : SmartCode(BasicCommonClasses.SmartCode) ProductNotExistInCatalog(SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) ProductPackage(BasicCommonClasses.ProductPackage) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) CommandExecuter(CommandHandler.CommandExecuter) Location(BasicCommonClasses.Location) Test(org.junit.Test)

Example 40 with ProductPackage

use of BasicCommonClasses.ProductPackage in project SmartCity-Market by TechnionYP5777.

the class AddProductPackageToWarehouseTest method addProductPackageToWarehouseProductNotExistInCatalogTest.

@Test
public void addProductPackageToWarehouseProductNotExistInCatalogTest() {
    int senderID = 1;
    ProductPackage productPackage = new ProductPackage(new SmartCode(1, null), 1, new Location(0, 0, PlaceInMarket.WAREHOUSE));
    String command = new CommandWrapper(senderID, CommandDescriptor.ADD_PRODUCT_PACKAGE_TO_WAREHOUSE, new Gson().toJson(productPackage, ProductPackage.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.doThrow(new ProductNotExistInCatalog()).when(sqlDatabaseConnection).addProductPackageToWarehouse(senderID, productPackage);
    } catch (CriticalError | ClientNotConnected e) {
        fail();
    } catch (ProductNotExistInCatalog __) {
    /* Success */
    }
    out = commandExecuter.execute(sqlDatabaseConnection);
    assertEquals(ResultDescriptor.SM_CATALOG_PRODUCT_DOES_NOT_EXIST, out.getResultDescriptor());
}
Also used : SmartCode(BasicCommonClasses.SmartCode) ProductNotExistInCatalog(SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) ProductPackage(BasicCommonClasses.ProductPackage) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) CommandExecuter(CommandHandler.CommandExecuter) Location(BasicCommonClasses.Location) Test(org.junit.Test)

Aggregations

ProductPackage (BasicCommonClasses.ProductPackage)68 SmartCode (BasicCommonClasses.SmartCode)58 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)55 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)55 ProductNotExistInCatalog (SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog)55 Test (org.junit.Test)55 CommandWrapper (ClientServerApi.CommandWrapper)45 ProductPackageAmountNotMatch (SQLDatabase.SQLDatabaseException.ProductPackageAmountNotMatch)43 ProductPackageNotExist (SQLDatabase.SQLDatabaseException.ProductPackageNotExist)43 Location (BasicCommonClasses.Location)40 Gson (com.google.gson.Gson)38 CommandExecuter (CommandHandler.CommandExecuter)37 SQLDatabaseConnection (SQLDatabase.SQLDatabaseConnection)18 AuthenticationError (SQLDatabase.SQLDatabaseException.AuthenticationError)8 ClientAlreadyConnected (SQLDatabase.SQLDatabaseException.ClientAlreadyConnected)8 NumberOfConnectionsExceeded (SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded)8 CartProduct (BasicCommonClasses.CartProduct)2 CatalogProduct (BasicCommonClasses.CatalogProduct)2 AuthenticationError (CustomerContracts.ACustomerExceptions.AuthenticationError)2 ForgotPasswordWrongAnswer (CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer)2