From db22703ec0e62555dc49e3d65d877e9f25a1a440 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Mon, 1 Mar 2021 23:02:03 +0100 Subject: [PATCH] Create Voucher Vault import code and test --- .../java/protect/card_locker/DBHelper.java | 6 +- .../protect/card_locker/ImportExportTask.java | 3 + .../CsvDatabaseExporter.java | 6 +- .../CsvDatabaseImporter.java | 6 +- .../{ => importexport}/DatabaseExporter.java | 4 +- .../{ => importexport}/DatabaseImporter.java | 10 +- .../MultiFormatExporter.java | 7 +- .../MultiFormatImporter.java | 13 +- .../importexport/VoucherVaultImporter.java | 130 +++++++++++++++ .../protect/card_locker/ImportExportTest.java | 148 ++++++++++++------ 10 files changed, 276 insertions(+), 57 deletions(-) rename app/src/main/java/protect/card_locker/{ => importexport}/CsvDatabaseExporter.java (95%) rename app/src/main/java/protect/card_locker/{ => importexport}/CsvDatabaseImporter.java (98%) rename app/src/main/java/protect/card_locker/{ => importexport}/DatabaseExporter.java (83%) rename app/src/main/java/protect/card_locker/{ => importexport}/DatabaseImporter.java (60%) rename app/src/main/java/protect/card_locker/{ => importexport}/MultiFormatExporter.java (85%) rename app/src/main/java/protect/card_locker/{ => importexport}/MultiFormatImporter.java (76%) create mode 100644 app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 10a631eca..ce27a76cf 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -20,14 +20,14 @@ public class DBHelper extends SQLiteOpenHelper public static final int ORIGINAL_DATABASE_VERSION = 1; public static final int DATABASE_VERSION = 8; - static class LoyaltyCardDbGroups + public static class LoyaltyCardDbGroups { public static final String TABLE = "groups"; public static final String ID = "_id"; public static final String ORDER = "orderId"; } - static class LoyaltyCardDbIds + public static class LoyaltyCardDbIds { public static final String TABLE = "cards"; public static final String ID = "_id"; @@ -43,7 +43,7 @@ public class DBHelper extends SQLiteOpenHelper public static final String STAR_STATUS = "starstatus"; } - static class LoyaltyCardDbIdsGroups + public static class LoyaltyCardDbIdsGroups { public static final String TABLE = "cardsGroups"; public static final String cardID = "cardId"; diff --git a/app/src/main/java/protect/card_locker/ImportExportTask.java b/app/src/main/java/protect/card_locker/ImportExportTask.java index 8537fbe99..402d57181 100644 --- a/app/src/main/java/protect/card_locker/ImportExportTask.java +++ b/app/src/main/java/protect/card_locker/ImportExportTask.java @@ -13,6 +13,9 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.charset.Charset; +import protect.card_locker.importexport.MultiFormatExporter; +import protect.card_locker.importexport.MultiFormatImporter; + class ImportExportTask extends AsyncTask { private static final String TAG = "Catima"; diff --git a/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java b/app/src/main/java/protect/card_locker/importexport/CsvDatabaseExporter.java similarity index 95% rename from app/src/main/java/protect/card_locker/CsvDatabaseExporter.java rename to app/src/main/java/protect/card_locker/importexport/CsvDatabaseExporter.java index dd8a3037c..0d58f4403 100644 --- a/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CsvDatabaseExporter.java @@ -1,4 +1,4 @@ -package protect.card_locker; +package protect.card_locker.importexport; import android.database.Cursor; @@ -8,6 +8,10 @@ import org.apache.commons.csv.CSVPrinter; import java.io.IOException; import java.io.OutputStreamWriter; +import protect.card_locker.DBHelper; +import protect.card_locker.Group; +import protect.card_locker.LoyaltyCard; + /** * Class for exporting the database into CSV (Comma Separate Values) * format. diff --git a/app/src/main/java/protect/card_locker/CsvDatabaseImporter.java b/app/src/main/java/protect/card_locker/importexport/CsvDatabaseImporter.java similarity index 98% rename from app/src/main/java/protect/card_locker/CsvDatabaseImporter.java rename to app/src/main/java/protect/card_locker/importexport/CsvDatabaseImporter.java index 5ba74393e..c2491bc42 100644 --- a/app/src/main/java/protect/card_locker/CsvDatabaseImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CsvDatabaseImporter.java @@ -1,4 +1,4 @@ -package protect.card_locker; +package protect.card_locker.importexport; import android.database.sqlite.SQLiteDatabase; @@ -15,6 +15,10 @@ import java.util.Currency; import java.util.Date; import java.util.List; +import protect.card_locker.DBHelper; +import protect.card_locker.FormatException; +import protect.card_locker.Group; + /** * Class for importing a database from CSV (Comma Separate Values) * formatted data. diff --git a/app/src/main/java/protect/card_locker/DatabaseExporter.java b/app/src/main/java/protect/card_locker/importexport/DatabaseExporter.java similarity index 83% rename from app/src/main/java/protect/card_locker/DatabaseExporter.java rename to app/src/main/java/protect/card_locker/importexport/DatabaseExporter.java index 94e4949d6..a12a124af 100644 --- a/app/src/main/java/protect/card_locker/DatabaseExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/DatabaseExporter.java @@ -1,8 +1,10 @@ -package protect.card_locker; +package protect.card_locker.importexport; import java.io.IOException; import java.io.OutputStreamWriter; +import protect.card_locker.DBHelper; + /** * Interface for a class which can export the contents of the database * in a given format. diff --git a/app/src/main/java/protect/card_locker/DatabaseImporter.java b/app/src/main/java/protect/card_locker/importexport/DatabaseImporter.java similarity index 60% rename from app/src/main/java/protect/card_locker/DatabaseImporter.java rename to app/src/main/java/protect/card_locker/importexport/DatabaseImporter.java index 33d2797bf..867454385 100644 --- a/app/src/main/java/protect/card_locker/DatabaseImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/DatabaseImporter.java @@ -1,7 +1,13 @@ -package protect.card_locker; +package protect.card_locker.importexport; + +import org.json.JSONException; import java.io.IOException; import java.io.InputStreamReader; +import java.text.ParseException; + +import protect.card_locker.DBHelper; +import protect.card_locker.FormatException; /** * Interface for a class which can import the contents of a stream @@ -15,5 +21,5 @@ public interface DatabaseImporter * @throws IOException * @throws FormatException */ - void importData(DBHelper db, InputStreamReader input) throws IOException, FormatException, InterruptedException; + void importData(DBHelper db, InputStreamReader input) throws IOException, FormatException, InterruptedException, JSONException, ParseException; } diff --git a/app/src/main/java/protect/card_locker/MultiFormatExporter.java b/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java similarity index 85% rename from app/src/main/java/protect/card_locker/MultiFormatExporter.java rename to app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java index 5298e2ad7..45f702b4d 100644 --- a/app/src/main/java/protect/card_locker/MultiFormatExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java @@ -1,10 +1,15 @@ -package protect.card_locker; +package protect.card_locker.importexport; import android.util.Log; import java.io.IOException; import java.io.OutputStreamWriter; +import protect.card_locker.DBHelper; +import protect.card_locker.DataFormat; +import protect.card_locker.importexport.CsvDatabaseExporter; +import protect.card_locker.importexport.DatabaseExporter; + public class MultiFormatExporter { private static final String TAG = "Catima"; diff --git a/app/src/main/java/protect/card_locker/MultiFormatImporter.java b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java similarity index 76% rename from app/src/main/java/protect/card_locker/MultiFormatImporter.java rename to app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java index 6a27af7e4..62d5af905 100644 --- a/app/src/main/java/protect/card_locker/MultiFormatImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java @@ -1,9 +1,18 @@ -package protect.card_locker; +package protect.card_locker.importexport; import android.util.Log; +import org.json.JSONException; + import java.io.IOException; import java.io.InputStreamReader; +import java.text.ParseException; + +import protect.card_locker.DBHelper; +import protect.card_locker.DataFormat; +import protect.card_locker.FormatException; +import protect.card_locker.importexport.CsvDatabaseImporter; +import protect.card_locker.importexport.DatabaseImporter; public class MultiFormatImporter { @@ -38,7 +47,7 @@ public class MultiFormatImporter importer.importData(db, input); return true; } - catch(IOException | FormatException | InterruptedException e) + catch(IOException | FormatException | InterruptedException | JSONException | ParseException e) { Log.e(TAG, "Failed to import data", e); } diff --git a/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java b/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java new file mode 100644 index 000000000..837cddc59 --- /dev/null +++ b/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java @@ -0,0 +1,130 @@ +package protect.card_locker.importexport; + +import android.annotation.SuppressLint; +import android.database.sqlite.SQLiteDatabase; +import android.graphics.Color; + +import com.google.zxing.BarcodeFormat; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.math.BigDecimal; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Currency; +import java.util.Date; + +import protect.card_locker.DBHelper; +import protect.card_locker.FormatException; + +/** + * Class for importing a database from CSV (Comma Separate Values) + * formatted data. + * + * The database's loyalty cards are expected to appear in the CSV data. + * A header is expected for the each table showing the names of the columns. + */ +public class VoucherVaultImporter implements DatabaseImporter +{ + public void importData(DBHelper db, InputStreamReader input) throws IOException, FormatException, JSONException, ParseException { + BufferedReader bufferedReader = new BufferedReader(input); + + StringBuilder sb = new StringBuilder(); + String line; + while ((line = bufferedReader.readLine()) != null) { + sb.append(line); + } + JSONArray jsonArray = new JSONArray(sb.toString()); + + SQLiteDatabase database = db.getWritableDatabase(); + database.beginTransaction(); + + // See https://github.com/tim-smart/vouchervault/issues/4#issuecomment-788226503 for more info + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonCard = jsonArray.getJSONObject(i); + + String store = jsonCard.getString("description"); + + Date expiry = null; + if (!jsonCard.isNull("expires")) { + @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); + expiry = dateFormat.parse(jsonCard.getString("expires")); + } + + BigDecimal balance; + if (!jsonCard.isNull("balance")) { + balance = new BigDecimal(String.valueOf(jsonCard.getDouble("balance"))); + } else { + balance = new BigDecimal("0"); + } + + Currency balanceType = Currency.getInstance("USD"); + + String cardId = jsonCard.getString("code"); + + String barcodeType = null; + + String codeTypeFromJSON = jsonCard.getString("codeType"); + switch (codeTypeFromJSON) { + case "CODE128": + barcodeType = BarcodeFormat.CODE_128.name(); + break; + case "CODE39": + barcodeType = BarcodeFormat.CODE_39.name(); + break; + case "EAN13": + barcodeType = BarcodeFormat.EAN_13.name(); + break; + case "QR": + barcodeType = BarcodeFormat.QR_CODE.name(); + break; + case "TEXT": + break; + default: + throw new FormatException("Unknown barcode type found: " + codeTypeFromJSON); + } + + int headerColor; + + String colorFromJSON = jsonCard.getString("color"); + switch (colorFromJSON) { + case "GREY": + headerColor = Color.GRAY; + break; + case "BLUE": + headerColor = Color.BLUE; + break; + case "GREEN": + headerColor = Color.GREEN; + break; + case "ORANGE": + headerColor = Color.rgb(255, 165, 0); + break; + case "PURPLE": + headerColor = Color.rgb(128, 0, 128); + break; + case "RED": + headerColor = Color.RED; + break; + case "YELLOW": + headerColor = Color.YELLOW; + break; + default: + throw new FormatException("Unknown colour type foun: " + colorFromJSON); + } + + db.insertLoyaltyCard(store, "", expiry, balance, balanceType, cardId, barcodeType, headerColor, 0); + } + + database.setTransactionSuccessful(); + database.endTransaction(); + database.close(); + + bufferedReader.close(); + } +} \ No newline at end of file diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index 2baf9ee73..e31e048ea 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -3,10 +3,12 @@ package protect.card_locker; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import android.graphics.Color; import android.os.Environment; import com.google.zxing.BarcodeFormat; +import org.json.JSONException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -26,11 +28,17 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.nio.charset.StandardCharsets; +import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; +import java.util.Currency; import java.util.Date; import java.util.List; +import protect.card_locker.importexport.MultiFormatExporter; +import protect.card_locker.importexport.MultiFormatImporter; +import protect.card_locker.importexport.VoucherVaultImporter; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -112,20 +120,65 @@ public class ImportExportTest boolean result = (id != -1); assertTrue(result); + LoyaltyCard card = db.getLoyaltyCard((int) id); + assertEquals("No Expiry", card.store); + assertEquals("", card.note); + assertEquals(null, card.expiry); + assertEquals(new BigDecimal("0"), card.balance); + assertEquals(null, card.balanceType); + assertEquals(BARCODE_DATA, card.cardId); + assertEquals(BARCODE_TYPE, card.barcodeType); + assertEquals(Integer.valueOf(0), card.headerColor); + assertEquals(0, card.starStatus); + id = db.insertLoyaltyCard("Past", "", new Date((long) 1), new BigDecimal("0"), null, BARCODE_DATA, BARCODE_TYPE, 0, 0); result = (id != -1); assertTrue(result); + card = db.getLoyaltyCard((int) id); + assertEquals("Past", card.store); + assertEquals("", card.note); + assertTrue(card.expiry.before(new Date())); + assertEquals(new BigDecimal("0"), card.balance); + assertEquals(null, card.balanceType); + assertEquals(BARCODE_DATA, card.cardId); + assertEquals(BARCODE_TYPE, card.barcodeType); + assertEquals(Integer.valueOf(0), card.headerColor); + assertEquals(0, card.starStatus); + id = db.insertLoyaltyCard("Today", "", new Date(), new BigDecimal("0"), null, BARCODE_DATA, BARCODE_TYPE, 0, 0); result = (id != -1); assertTrue(result); + card = db.getLoyaltyCard((int) id); + assertEquals("Today", card.store); + assertEquals("", card.note); + assertTrue(card.expiry.before(new Date(new Date().getTime()+86400))); + assertTrue(card.expiry.after(new Date(new Date().getTime()-86400))); + assertEquals(new BigDecimal("0"), card.balance); + assertEquals(null, card.balanceType); + assertEquals(BARCODE_DATA, card.cardId); + assertEquals(BARCODE_TYPE, card.barcodeType); + assertEquals(Integer.valueOf(0), card.headerColor); + assertEquals(0, card.starStatus); + // This will break after 19 January 2038 // If someone is still maintaining this code base by then: I love you - id = db.insertLoyaltyCard("Future", "", new Date(2147483648L), new BigDecimal("0"), null, BARCODE_DATA, BARCODE_TYPE, 0, 0); + id = db.insertLoyaltyCard("Future", "", new Date(2147483648000L), new BigDecimal("0"), null, BARCODE_DATA, BARCODE_TYPE, 0, 0); result = (id != -1); assertTrue(result); + card = db.getLoyaltyCard((int) id); + assertEquals("Future", card.store); + assertEquals("", card.note); + assertTrue(card.expiry.after(new Date(new Date().getTime()+86400))); + assertEquals(new BigDecimal("0"), card.balance); + assertEquals(null, card.balanceType); + assertEquals(BARCODE_DATA, card.cardId); + assertEquals(BARCODE_TYPE, card.barcodeType); + assertEquals(Integer.valueOf(0), card.headerColor); + assertEquals(0, card.starStatus); + assertEquals(4, db.getLoyaltyCardCount()); } @@ -830,59 +883,62 @@ public class ImportExportTest clearDatabase(); } + @Test + public void importVoucherVault() throws IOException, FormatException, JSONException, ParseException { + String jsonText = "[\n" + + " {\n" + + " \"uuid\": \"ae1ae525-3f27-481e-853a-8c30b7fa12d8\",\n" + + " \"description\": \"Clothes Store\",\n" + + " \"code\": \"123456\",\n" + + " \"codeType\": \"CODE128\",\n" + + " \"expires\": null,\n" + + " \"removeOnceExpired\": true,\n" + + " \"balance\": null,\n" + + " \"color\": \"GREY\"\n" + + " },\n" + + " {\n" + + " \"uuid\": \"29a5d3b3-eace-4311-a15c-4c7e6a010531\",\n" + + " \"description\": \"Department Store\",\n" + + " \"code\": \"26846363\",\n" + + " \"codeType\": \"CODE39\",\n" + + " \"expires\": \"2021-03-26T00:00:00.000\",\n" + + " \"removeOnceExpired\": true,\n" + + " \"balance\": 3.5,\n" + + " \"color\": \"PURPLE\"\n" + + " }\n" + + "]"; - private void checkLoyaltyCardsExpiry() - { - Cursor cursor = db.getLoyaltyCardCursor(); - cursor.moveToNext(); - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cursor); - assertEquals("Never", card.store); + ByteArrayInputStream inputStream = new ByteArrayInputStream(jsonText.getBytes(StandardCharsets.UTF_8)); + InputStreamReader inStream = new InputStreamReader(inputStream); + + // Import the Voucher Vault data + new VoucherVaultImporter().importData(db, inStream); + assertEquals(2, db.getLoyaltyCardCount()); + + LoyaltyCard card = db.getLoyaltyCard(1); + + assertEquals("Clothes Store", card.store); assertEquals("", card.note); assertEquals(null, card.expiry); assertEquals(new BigDecimal("0"), card.balance); - assertEquals(null, card.balanceType); - assertEquals(BARCODE_DATA, card.cardId); - assertEquals(BARCODE_TYPE, card.barcodeType); - assertEquals(Integer.valueOf(0), card.headerColor); + assertEquals(Currency.getInstance("USD"), card.balanceType); + assertEquals("123456", card.cardId); + assertEquals(BarcodeFormat.CODE_128.name(), card.barcodeType); assertEquals(0, card.starStatus); + assertEquals(Color.GRAY, (long) card.headerColor); - cursor.moveToNext(); - card = LoyaltyCard.toLoyaltyCard(cursor); - assertEquals("Past", card.store); + card = db.getLoyaltyCard(2); + + assertEquals("Department Store", card.store); assertEquals("", card.note); - assertTrue(card.expiry.before(new Date())); - assertEquals(new BigDecimal("0"), card.balance); - assertEquals(null, card.balanceType); - assertEquals(BARCODE_DATA, card.cardId); - assertEquals(BARCODE_TYPE, card.barcodeType); - assertEquals(Integer.valueOf(0), card.headerColor); + assertEquals(new Date(1616713200000L), card.expiry); + assertEquals(new BigDecimal("3.5"), card.balance); + assertEquals(Currency.getInstance("USD"), card.balanceType); + assertEquals("26846363", card.cardId); + assertEquals(BarcodeFormat.CODE_39.name(), card.barcodeType); assertEquals(0, card.starStatus); + assertEquals(Color.rgb(128, 0, 128), (long) card.headerColor); - cursor.moveToNext(); - card = LoyaltyCard.toLoyaltyCard(cursor); - assertEquals("Today", card.store); - assertEquals("", card.note); - assertTrue(card.expiry.before(new Date(new Date().getTime()+86400))); - assertTrue(card.expiry.after(new Date(new Date().getTime()-86400))); - assertEquals(new BigDecimal("0"), card.balance); - assertEquals(null, card.balanceType); - assertEquals(BARCODE_DATA, card.cardId); - assertEquals(BARCODE_TYPE, card.barcodeType); - assertEquals(Integer.valueOf(0), card.headerColor); - assertEquals(0, card.starStatus); - - cursor.moveToNext(); - card = LoyaltyCard.toLoyaltyCard(cursor); - assertEquals("Future", card.store); - assertEquals("", card.note); - assertTrue(card.expiry.after(new Date(new Date().getTime()+86400))); - assertEquals(new BigDecimal("0"), card.balance); - assertEquals(null, card.balanceType); - assertEquals(BARCODE_DATA, card.cardId); - assertEquals(BARCODE_TYPE, card.barcodeType); - assertEquals(Integer.valueOf(0), card.headerColor); - assertEquals(0, card.starStatus); - - cursor.close(); + clearDatabase(); } }