Search in sources :

Example 96 with CriticalError

use of SQLDatabase.SQLDatabaseException.CriticalError in project SmartCity-Market by TechnionYP5777.

the class ViewCatalogProductTest method viewCatalogProductCriticalErrorTest.

@Test
public void viewCatalogProductCriticalErrorTest() {
    int senderID = 1;
    SmartCode smartCode = new SmartCode(0, null);
    String command = new CommandWrapper(senderID, CommandDescriptor.VIEW_PRODUCT_FROM_CATALOG, new Gson().toJson(smartCode, SmartCode.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.when(sqlDatabaseConnection.getProductFromCatalog(senderID, smartCode.getBarcode())).thenThrow(new CriticalError());
    } catch (ClientNotConnected | ProductNotExistInCatalog | CriticalError e) {
        fail();
    }
    out = commandExecuter.execute(sqlDatabaseConnection);
    assertEquals(ResultDescriptor.SM_ERR, out.getResultDescriptor());
}
Also used : SmartCode(BasicCommonClasses.SmartCode) ProductNotExistInCatalog(SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) CommandExecuter(CommandHandler.CommandExecuter) Test(org.junit.Test)

Example 97 with CriticalError

use of SQLDatabase.SQLDatabaseException.CriticalError in project SmartCity-Market by TechnionYP5777.

the class ViewCatalogProductTest method viewCatalogProductWorkerNotConnectedTest.

@Test
public void viewCatalogProductWorkerNotConnectedTest() {
    int senderID = 1;
    SmartCode smartCode = new SmartCode(0, null);
    String command = new CommandWrapper(senderID, CommandDescriptor.VIEW_PRODUCT_FROM_CATALOG, new Gson().toJson(smartCode, SmartCode.class)).serialize();
    CommandExecuter commandExecuter = new CommandExecuter(command);
    CommandWrapper out;
    try {
        Mockito.when(sqlDatabaseConnection.getProductFromCatalog(senderID, smartCode.getBarcode())).thenThrow(new ClientNotConnected());
    } catch (ProductNotExistInCatalog | ClientNotConnected | CriticalError e) {
        fail();
    }
    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) Gson(com.google.gson.Gson) CommandWrapper(ClientServerApi.CommandWrapper) CommandExecuter(CommandHandler.CommandExecuter) Test(org.junit.Test)

Example 98 with CriticalError

use of SQLDatabase.SQLDatabaseException.CriticalError in project SmartCity-Market by TechnionYP5777.

the class SQLDatabaseConnectionTest method testSimpleAddMoveToCartRemovePakage.

@Test
public void testSimpleAddMoveToCartRemovePakage() {
    SQLDatabaseConnection sqlConnection = new SQLDatabaseConnection();
    ProductPackage productPackage = new ProductPackage(new SmartCode(barcodeDebug, date112000), 5, locationWarehouse);
    int cartSession = 0;
    try {
        cartSession = sqlConnection.loginCustomer(ClientServerDefs.anonymousCustomerUsername, ClientServerDefs.anonymousCustomerPassword);
        sqlConnection.addProductPackageToWarehouse(null, productPackage);
        sqlConnection.placeProductPackageOnShelves(null, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("5", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.addProductToGroceryList(cartSession, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("0", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.removeProductFromGroceryList(cartSession, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("5", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
        sqlConnection.removeProductPackageFromShelves(null, productPackage);
        assertEquals("0", sqlConnection.getProductPackageAmonutInWarehouse(null, productPackage));
        assertEquals("0", sqlConnection.getProductPackageAmonutOnShelves(null, productPackage));
    } catch (CriticalError | ClientNotConnected | ProductNotExistInCatalog | ProductPackageAmountNotMatch | ProductPackageNotExist | AuthenticationError | ClientAlreadyConnected | NumberOfConnectionsExceeded e) {
        fail();
    }
    try {
        sqlConnection.removeProductFromGroceryList(cartSession, productPackage);
        fail();
    } catch (ProductPackageNotExist e) {
    } catch (CriticalError | ClientNotConnected | ProductNotExistInCatalog | ProductPackageAmountNotMatch e) {
        fail();
    }
    try {
        sqlConnection.removeProductPackageFromShelves(null, productPackage);
        fail();
    } catch (ProductPackageNotExist e) {
    } catch (CriticalError | ClientNotConnected | ProductNotExistInCatalog | ProductPackageAmountNotMatch e) {
        fail();
    }
    try {
        sqlConnection.logout(cartSession, ClientServerDefs.anonymousCustomerUsername);
    } catch (ClientNotConnected | CriticalError e) {
        fail();
    }
}
Also used : AuthenticationError(SQLDatabase.SQLDatabaseException.AuthenticationError) SmartCode(BasicCommonClasses.SmartCode) ProductNotExistInCatalog(SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog) ProductPackageAmountNotMatch(SQLDatabase.SQLDatabaseException.ProductPackageAmountNotMatch) SQLDatabaseConnection(SQLDatabase.SQLDatabaseConnection) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) ProductPackage(BasicCommonClasses.ProductPackage) ProductPackageNotExist(SQLDatabase.SQLDatabaseException.ProductPackageNotExist) NumberOfConnectionsExceeded(SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded) ClientAlreadyConnected(SQLDatabase.SQLDatabaseException.ClientAlreadyConnected) Test(org.junit.Test)

Example 99 with CriticalError

use of SQLDatabase.SQLDatabaseException.CriticalError in project SmartCity-Market by TechnionYP5777.

the class SQLJsonGenerator method CostumerProfileToJson.

/**
	 * convert customerProfile from ResultSet to Json representation of product
	 * 
	 * @param customer
	 *            - ResultSet of the customer profile. the
	 *            ResultSet need to point to the username to convert). this
	 *            object will point the next product after returning.
	 * @param customerIngredients
	 *            - ResultSet of the customer\s ingredients (assuming the
	 *            ResultSet ordered by username column) the ResultSet should
	 *            pointing the product to convert, if it has ingredients. if so,
	 *            this object will point the next customer (or after last line) after returning.
	 * @return
	 * @throws CriticalError
	 */
static String CostumerProfileToJson(ResultSet customer, ResultSet customerIngredients) throws CriticalError {
    HashSet<Ingredient> ingredients;
    try {
        //get customer username
        String customerUsername = getStringFromResultset(customer, CustomersTable.customerusernameCol);
        // get all customer ingredients
        ingredients = createIngredientsListForCustomer(customerUsername, customerIngredients);
        String customeraddress = getStringFromResultset(customer, CustomersTable.customerAddressCol), customerCity = getStringFromResultset(customer, CustomersTable.customerCityCol), customerEmail = getStringFromResultset(customer, CustomersTable.customerEmailCol), customerFirstname = getStringFromResultset(customer, CustomersTable.customerFirstnameCol), customerLastname = getStringFromResultset(customer, CustomersTable.customerLastnameCol), customerPhonenumber = getStringFromResultset(customer, CustomersTable.customerPhonenumberCol);
        LocalDate customerBirthdate = customer.getDate(CustomersTable.customerBirthdateCol.getColumnNameSQL()).toLocalDate();
        customer.next();
        return Serialization.serialize(new CustomerProfile(customerUsername, null, customerFirstname, customerLastname, customerPhonenumber, customerEmail, customerCity, customeraddress, customerBirthdate, ingredients, null));
    } catch (SQLException e) {
        throw new SQLDatabaseException.CriticalError();
    }
}
Also used : Ingredient(BasicCommonClasses.Ingredient) SQLException(java.sql.SQLException) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) CustomerProfile(BasicCommonClasses.CustomerProfile) LocalDate(java.time.LocalDate)

Example 100 with CriticalError

use of SQLDatabase.SQLDatabaseException.CriticalError in project SmartCity-Market by TechnionYP5777.

the class Main method main.

public static void main(String[] args) {
    if (!parseArguments(args))
        return;
    /* Setting log properties */
    PropertyConfigurator.configure("../log4j.properties");
    log.setLevel(verbosity);
    CommandProcess commandProcess = new CommandProcess();
    ThreadPooledServer server = null;
    try {
        server = new ThreadPooledServer(port, numOfThreads, commandProcess, serverIP);
    } catch (UnknownHostException e1) {
        log.fatal("Server IP address leads to unknown host, server won't start.");
        return;
    }
    SQLDatabaseConnection connection = new SQLDatabaseConnection();
    log.info("Disconnect from all clients");
    try {
        connection.logoutAllUsers();
        connection.close();
    } catch (CriticalError e1) {
        log.fatal("Disconnect failed");
        return;
    }
    log.info("Starting Server.");
    new Thread(server).start();
    try {
        Thread.sleep(1000 * timeout);
    } catch (InterruptedException e) {
        /* Get interrupt to stop server */
        log.info("Server Got interrupted.");
    }
    log.info("Stopping server.");
    server.stop();
}
Also used : CommandProcess(CommandHandler.CommandProcess) SQLDatabaseConnection(SQLDatabase.SQLDatabaseConnection) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) UnknownHostException(java.net.UnknownHostException) ThreadPooledServer(ClientServerCommunication.ThreadPooledServer)

Aggregations

CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)225 Test (org.junit.Test)186 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)172 CommandWrapper (ClientServerApi.CommandWrapper)162 CommandExecuter (CommandHandler.CommandExecuter)128 Gson (com.google.gson.Gson)118 ProductNotExistInCatalog (SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog)81 SmartCode (BasicCommonClasses.SmartCode)63 SQLDatabaseConnection (SQLDatabase.SQLDatabaseConnection)61 ProductPackage (BasicCommonClasses.ProductPackage)55 IngredientNotExist (SQLDatabase.SQLDatabaseException.IngredientNotExist)45 ProductPackageAmountNotMatch (SQLDatabase.SQLDatabaseException.ProductPackageAmountNotMatch)43 ProductPackageNotExist (SQLDatabase.SQLDatabaseException.ProductPackageNotExist)43 ClientNotExist (SQLDatabase.SQLDatabaseException.ClientNotExist)40 Location (BasicCommonClasses.Location)35 ManufacturerNotExist (SQLDatabase.SQLDatabaseException.ManufacturerNotExist)31 AuthenticationError (SQLDatabase.SQLDatabaseException.AuthenticationError)27 ClientAlreadyConnected (SQLDatabase.SQLDatabaseException.ClientAlreadyConnected)27 NumberOfConnectionsExceeded (SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded)27 CatalogProduct (BasicCommonClasses.CatalogProduct)22