use of BasicCommonClasses.SmartCode in project SmartCity-Market by TechnionYP5777.
the class SmartcodeParser method codeToSmartcode.
/**
* parse GS1 (EAN128) code to Smartcode
*
* @param scannedCode
* @return
*/
private static SmartCode codeToSmartcode(String scannedCode) {
boolean flgDateFilled = false, flgSerialFilled = false;
SmartCode $ = new SmartCode(0, null);
// move to the smartcode itself
scannedCode = scannedCode.substring(GS1_PREFIX.length());
while (scannedCode.length() != 0) {
// CASE: expiration date field
if (scannedCode.startsWith(EXPIRATION_DATE_IDENTIFIER)) {
// eats the field code
scannedCode = scannedCode.substring(EXPIRATION_DATE_IDENTIFIER.length());
// too small or not all digits
if (flgDateFilled || scannedCode.length() < 6 || !scannedCode.matches("[0-9]{" + EXPIRATION_DATE_LEN + "}" + ".*"))
return null;
// start parsing expiration date
flgDateFilled = true;
int year = Integer.parseInt(scannedCode.substring(0, 2)), month = Integer.parseInt(scannedCode.substring(2, 4)), day = Integer.parseInt(scannedCode.substring(4, 6));
if (month > 12 || month < 1 || day > 31 || day < 1)
return null;
$.setExpirationDate(LocalDate.of(2000 + year, month, day));
// eats the expiration date code
scannedCode = scannedCode.substring(EXPIRATION_DATE_LEN);
continue;
}
if (scannedCode.startsWith(BARCODE_IDENTIFIER)) {
// eats the field code
scannedCode = scannedCode.substring(BARCODE_IDENTIFIER.length());
// too small or not all digits
if (flgSerialFilled)
return null;
// start parsing barcode (serial code)
flgSerialFilled = true;
// search for the end of barcode
int i;
for (i = 0; i < scannedCode.length(); ++i) if (scannedCode.charAt(i) < '0' || scannedCode.charAt(i) > '9')
break;
if (i > BARCODE_MAX_LEN)
return null;
// if we reached the end - fix the offset
$.setBarcode(Long.parseLong((i == scannedCode.length()) ? scannedCode.substring(0) : scannedCode.substring(0, i - 1)));
// eats the barcode
scannedCode = scannedCode.substring(i);
continue;
}
// goto next char
scannedCode = scannedCode.substring(1);
}
return $;
}
use of BasicCommonClasses.SmartCode in project SmartCity-Market by TechnionYP5777.
the class AddProductToGroceryListTest method addProductToGroceryListCriticalErrorTest.
@Test
public void addProductToGroceryListCriticalErrorTest() {
int cartID = 1;
ProductPackage productPackage = new ProductPackage(new SmartCode(1, null), 1, new Location(0, 0, PlaceInMarket.WAREHOUSE));
String command = new CommandWrapper(cartID, CommandDescriptor.ADD_PRODUCT_TO_GROCERY_LIST, new Gson().toJson(productPackage, ProductPackage.class)).serialize();
CommandExecuter commandExecuter = new CommandExecuter(command);
CommandWrapper out;
try {
Mockito.doThrow(new CriticalError()).when(sqlDatabaseConnection).addProductToGroceryList(cartID, productPackage);
} catch (ClientNotConnected | ProductNotExistInCatalog | ProductPackageAmountNotMatch | ProductPackageNotExist e) {
fail();
} catch (CriticalError __) {
/* Successful */
}
out = commandExecuter.execute(sqlDatabaseConnection);
assertEquals(ResultDescriptor.SM_ERR, out.getResultDescriptor());
}
use of BasicCommonClasses.SmartCode in project SmartCity-Market by TechnionYP5777.
the class AddProductToGroceryListTest method addProductToGroceryListIllegalCatalogProductTest.
@Test
public void addProductToGroceryListIllegalCatalogProductTest() {
assertEquals(ResultDescriptor.SM_ERR, (new CommandExecuter(new CommandWrapper(1, CommandDescriptor.ADD_PRODUCT_TO_GROCERY_LIST, new Gson().toJson("", String.class)).serialize())).execute(sqlDatabaseConnection).getResultDescriptor());
assertEquals(ResultDescriptor.SM_INVALID_PARAMETER, (new CommandExecuter(new CommandWrapper(1, CommandDescriptor.ADD_PRODUCT_TO_GROCERY_LIST, new Gson().toJson(new ProductPackage(new SmartCode(1, null), -1, new Location(0, 0, PlaceInMarket.WAREHOUSE)), ProductPackage.class)).serialize())).execute(sqlDatabaseConnection).getResultDescriptor());
}
use of BasicCommonClasses.SmartCode in project SmartCity-Market by TechnionYP5777.
the class AddProductToGroceryListTest method addProductToGroceryListProductPackageAmountNotMatchTest.
@Test
public void addProductToGroceryListProductPackageAmountNotMatchTest() {
int cartID = 1;
ProductPackage productPackage = new ProductPackage(new SmartCode(1, null), 1, new Location(0, 0, PlaceInMarket.WAREHOUSE));
String command = new CommandWrapper(cartID, CommandDescriptor.ADD_PRODUCT_TO_GROCERY_LIST, new Gson().toJson(productPackage, ProductPackage.class)).serialize();
CommandExecuter commandExecuter = new CommandExecuter(command);
CommandWrapper out;
try {
Mockito.doThrow(new ProductPackageAmountNotMatch()).when(sqlDatabaseConnection).addProductToGroceryList(cartID, productPackage);
} catch (ClientNotConnected | CriticalError | ProductNotExistInCatalog | ProductPackageNotExist e) {
fail();
} catch (ProductPackageAmountNotMatch __) {
/* Successful */
}
out = commandExecuter.execute(sqlDatabaseConnection);
assertEquals(ResultDescriptor.SM_PRODUCT_PACKAGE_AMOUNT_BIGGER_THEN_AVAILABLE, out.getResultDescriptor());
}
use of BasicCommonClasses.SmartCode 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());
}
Aggregations