From db22703ec0e62555dc49e3d65d877e9f25a1a440 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Mon, 1 Mar 2021 23:02:03 +0100 Subject: [PATCH 1/4] 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(); } } From 40de4a8dc4925460766870cf05fffebd363f9a12 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Tue, 2 Mar 2021 21:10:14 +0100 Subject: [PATCH 2/4] Fix date parsing --- .../protect/card_locker/importexport/VoucherVaultImporter.java | 2 ++ app/src/test/java/protect/card_locker/ImportExportTest.java | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java b/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java index 837cddc59..317adb65a 100644 --- a/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java @@ -18,6 +18,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Currency; import java.util.Date; +import java.util.TimeZone; import protect.card_locker.DBHelper; import protect.card_locker.FormatException; @@ -53,6 +54,7 @@ public class VoucherVaultImporter implements DatabaseImporter Date expiry = null; if (!jsonCard.isNull("expires")) { @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); expiry = dateFormat.parse(jsonCard.getString("expires")); } diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index e31e048ea..17823f253 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -931,7 +931,7 @@ public class ImportExportTest assertEquals("Department Store", card.store); assertEquals("", card.note); - assertEquals(new Date(1616713200000L), card.expiry); + assertEquals(new Date(1616716800000L), card.expiry); assertEquals(new BigDecimal("3.5"), card.balance); assertEquals(Currency.getInstance("USD"), card.balanceType); assertEquals("26846363", card.cardId); From ffa39000f7813842410d827c987fd2e3c9d2d04e Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Thu, 4 Mar 2021 23:35:44 +0100 Subject: [PATCH 3/4] Add Voucher Vault import to UI --- CHANGELOG.md | 6 + .../java/protect/card_locker/DataFormat.java | 4 +- .../card_locker/ImportExportActivity.java | 44 ++- .../importexport/MultiFormatExporter.java | 2 +- .../importexport/MultiFormatImporter.java | 5 +- app/src/main/res/values/arrays.xml | 8 + app/src/main/res/values/strings.xml | 1 + .../protect/card_locker/ImportExportTest.java | 297 +++++++++--------- 8 files changed, 198 insertions(+), 169 deletions(-) create mode 100644 app/src/main/res/values/arrays.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c7ceefa3..56951fb39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +Changes: + +- Support importing [Voucher Vault](https://github.com/tim-smart/vouchervault/) exports + ## v1.9.2 (2021-02-24) Changes: diff --git a/app/src/main/java/protect/card_locker/DataFormat.java b/app/src/main/java/protect/card_locker/DataFormat.java index 722700f00..4b4b23e08 100644 --- a/app/src/main/java/protect/card_locker/DataFormat.java +++ b/app/src/main/java/protect/card_locker/DataFormat.java @@ -2,7 +2,7 @@ package protect.card_locker; public enum DataFormat { - CSV, - + Catima, + VoucherVault ; } diff --git a/app/src/main/java/protect/card_locker/ImportExportActivity.java b/app/src/main/java/protect/card_locker/ImportExportActivity.java index f5d89238d..cb70b396f 100644 --- a/app/src/main/java/protect/card_locker/ImportExportActivity.java +++ b/app/src/main/java/protect/card_locker/ImportExportActivity.java @@ -35,10 +35,12 @@ public class ImportExportActivity extends AppCompatActivity private static final int PERMISSIONS_EXTERNAL_STORAGE = 1; private static final int CHOOSE_EXPORT_LOCATION = 2; - private static final int CHOOSE_EXPORTED_FILE = 3; + private static final int IMPORT = 3; private ImportExportTask importExporter; + private DataFormat importDataFormat; + @Override protected void onCreate(Bundle savedInstanceState) { @@ -93,7 +95,7 @@ public class ImportExportActivity extends AppCompatActivity @Override public void onClick(View v) { - chooseFileWithIntent(intentGetContentAction, CHOOSE_EXPORTED_FILE); + chooseImportType(intentGetContentAction); } }); @@ -106,12 +108,35 @@ public class ImportExportActivity extends AppCompatActivity @Override public void onClick(View v) { - chooseFileWithIntent(intentPickAction, CHOOSE_EXPORTED_FILE); + chooseImportType(intentPickAction); } }); } - private void startImport(final InputStream target, final Uri targetUri) + private void chooseImportType(Intent baseIntent) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.chooseImportType) + .setItems(R.array.import_types_array, (dialog, which) -> { + switch (which) { + // Catima + case 0: + // Loyalty Card Keychain + case 1: + importDataFormat = DataFormat.Catima; + break; + // Voucher Vault + case 2: + importDataFormat = DataFormat.VoucherVault; + break; + default: + throw new IllegalArgumentException("Unknown DataFormat"); + } + chooseFileWithIntent(baseIntent, IMPORT); + }); + builder.show(); + } + + private void startImport(final InputStream target, final Uri targetUri, final DataFormat dataFormat) { ImportExportTask.TaskCompleteListener listener = new ImportExportTask.TaskCompleteListener() { @@ -123,7 +148,7 @@ public class ImportExportActivity extends AppCompatActivity }; importExporter = new ImportExportTask(ImportExportActivity.this, - DataFormat.CSV, target, listener); + dataFormat, target, listener); importExporter.execute(); } @@ -139,7 +164,7 @@ public class ImportExportActivity extends AppCompatActivity }; importExporter = new ImportExportTask(ImportExportActivity.this, - DataFormat.CSV, target, listener); + DataFormat.Catima, target, listener); importExporter.execute(); } @@ -294,7 +319,7 @@ public class ImportExportActivity extends AppCompatActivity { super.onActivityResult(requestCode, resultCode, data); - if (resultCode != RESULT_OK || (requestCode != CHOOSE_EXPORT_LOCATION && requestCode != CHOOSE_EXPORTED_FILE)) + if (resultCode != RESULT_OK) { Log.w(TAG, "Failed onActivityResult(), result=" + resultCode); return; @@ -336,8 +361,9 @@ public class ImportExportActivity extends AppCompatActivity reader = new FileInputStream(new File(uri.toString())); } - Log.e(TAG, "Starting file export with: " + uri.toString()); - startImport(reader, uri); + Log.e(TAG, "Starting file import with: " + uri.toString()); + + startImport(reader, uri, importDataFormat); } } catch(FileNotFoundException e) diff --git a/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java b/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java index 45f702b4d..f6509d561 100644 --- a/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java @@ -30,7 +30,7 @@ public class MultiFormatExporter switch(format) { - case CSV: + case Catima: exporter = new CsvDatabaseExporter(); break; } diff --git a/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java index 62d5af905..3186440cd 100644 --- a/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java @@ -35,9 +35,12 @@ public class MultiFormatImporter switch(format) { - case CSV: + case Catima: importer = new CsvDatabaseImporter(); break; + case VoucherVault: + importer = new VoucherVaultImporter(); + break; } if (importer != null) diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 000000000..303cc27bf --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,8 @@ + + + + Catima + Loyalty Card Keychain + Voucher Vault + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 71260b998..db69108fa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -151,4 +151,5 @@ Points %s does not seem to be a valid balance. + Import data from which app? diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index 17823f253..11413b6b8 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -37,7 +37,6 @@ 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; @@ -322,34 +321,31 @@ public class ImportExportTest { final int NUM_CARDS = 10; - for(DataFormat format : DataFormat.values()) - { - addLoyaltyCards(NUM_CARDS); + addLoyaltyCards(NUM_CARDS); - ByteArrayOutputStream outData = new ByteArrayOutputStream(); - OutputStreamWriter outStream = new OutputStreamWriter(outData); + ByteArrayOutputStream outData = new ByteArrayOutputStream(); + OutputStreamWriter outStream = new OutputStreamWriter(outData); - // Export data to CSV format - boolean result = MultiFormatExporter.exportData(db, outStream, format); - assertTrue(result); - outStream.close(); + // Export data to CSV format + boolean result = MultiFormatExporter.exportData(db, outStream, DataFormat.Catima); + assertTrue(result); + outStream.close(); - clearDatabase(); + clearDatabase(); - ByteArrayInputStream inData = new ByteArrayInputStream(outData.toByteArray()); - InputStreamReader inStream = new InputStreamReader(inData); + ByteArrayInputStream inData = new ByteArrayInputStream(outData.toByteArray()); + InputStreamReader inStream = new InputStreamReader(inData); - // Import the CSV data - result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); - assertTrue(result); + // Import the CSV data + result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); + assertTrue(result); - assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); + assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); - checkLoyaltyCards(); + checkLoyaltyCards(); - // Clear the database for the next format under test - clearDatabase(); - } + // Clear the database for the next format under test + clearDatabase(); } @Test @@ -357,34 +353,31 @@ public class ImportExportTest { final int NUM_CARDS = 9; - for(DataFormat format : DataFormat.values()) - { - addLoyaltyCardsFiveStarred(); + addLoyaltyCardsFiveStarred(); - ByteArrayOutputStream outData = new ByteArrayOutputStream(); - OutputStreamWriter outStream = new OutputStreamWriter(outData); + ByteArrayOutputStream outData = new ByteArrayOutputStream(); + OutputStreamWriter outStream = new OutputStreamWriter(outData); - // Export data to CSV format - boolean result = MultiFormatExporter.exportData(db, outStream, format); - assertTrue(result); - outStream.close(); + // Export data to CSV format + boolean result = MultiFormatExporter.exportData(db, outStream, DataFormat.Catima); + assertTrue(result); + outStream.close(); - clearDatabase(); + clearDatabase(); - ByteArrayInputStream inData = new ByteArrayInputStream(outData.toByteArray()); - InputStreamReader inStream = new InputStreamReader(inData); + ByteArrayInputStream inData = new ByteArrayInputStream(outData.toByteArray()); + InputStreamReader inStream = new InputStreamReader(inData); - // Import the CSV data - result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); - assertTrue(result); + // Import the CSV data + result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); + assertTrue(result); - assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); + assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); - checkLoyaltyCardsFiveStarred(); + checkLoyaltyCardsFiveStarred(); - // Clear the database for the next format under test - clearDatabase(); - } + // Clear the database for the next format under test + clearDatabase(); } private List groupsToGroupNames(List groups) @@ -404,77 +397,74 @@ public class ImportExportTest final int NUM_CARDS = 10; final int NUM_GROUPS = 3; - for(DataFormat format : DataFormat.values()) - { - addLoyaltyCards(NUM_CARDS); - addGroups(NUM_GROUPS); + addLoyaltyCards(NUM_CARDS); + addGroups(NUM_GROUPS); - List emptyGroup = new ArrayList<>(); + List emptyGroup = new ArrayList<>(); - List groupsForOne = new ArrayList<>(); - groupsForOne.add(db.getGroup("group, \" 1")); + List groupsForOne = new ArrayList<>(); + groupsForOne.add(db.getGroup("group, \" 1")); - List groupsForTwo = new ArrayList<>(); - groupsForTwo.add(db.getGroup("group, \" 1")); - groupsForTwo.add(db.getGroup("group, \" 2")); + List groupsForTwo = new ArrayList<>(); + groupsForTwo.add(db.getGroup("group, \" 1")); + groupsForTwo.add(db.getGroup("group, \" 2")); - List groupsForThree = new ArrayList<>(); - groupsForThree.add(db.getGroup("group, \" 1")); - groupsForThree.add(db.getGroup("group, \" 2")); - groupsForThree.add(db.getGroup("group, \" 3")); + List groupsForThree = new ArrayList<>(); + groupsForThree.add(db.getGroup("group, \" 1")); + groupsForThree.add(db.getGroup("group, \" 2")); + groupsForThree.add(db.getGroup("group, \" 3")); - List groupsForFour = new ArrayList<>(); - groupsForFour.add(db.getGroup("group, \" 1")); - groupsForFour.add(db.getGroup("group, \" 2")); - groupsForFour.add(db.getGroup("group, \" 3")); + List groupsForFour = new ArrayList<>(); + groupsForFour.add(db.getGroup("group, \" 1")); + groupsForFour.add(db.getGroup("group, \" 2")); + groupsForFour.add(db.getGroup("group, \" 3")); - List groupsForFive = new ArrayList<>(); - groupsForFive.add(db.getGroup("group, \" 1")); - groupsForFive.add(db.getGroup("group, \" 3")); + List groupsForFive = new ArrayList<>(); + groupsForFive.add(db.getGroup("group, \" 1")); + groupsForFive.add(db.getGroup("group, \" 3")); - db.setLoyaltyCardGroups(1, groupsForOne); - db.setLoyaltyCardGroups(2, groupsForTwo); - db.setLoyaltyCardGroups(3, groupsForThree); - db.setLoyaltyCardGroups(4, groupsForFour); - db.setLoyaltyCardGroups(5, groupsForFive); + db.setLoyaltyCardGroups(1, groupsForOne); + db.setLoyaltyCardGroups(2, groupsForTwo); + db.setLoyaltyCardGroups(3, groupsForThree); + db.setLoyaltyCardGroups(4, groupsForFour); + db.setLoyaltyCardGroups(5, groupsForFive); - ByteArrayOutputStream outData = new ByteArrayOutputStream(); - OutputStreamWriter outStream = new OutputStreamWriter(outData); + ByteArrayOutputStream outData = new ByteArrayOutputStream(); + OutputStreamWriter outStream = new OutputStreamWriter(outData); - // Export data to CSV format - boolean result = MultiFormatExporter.exportData(db, outStream, format); - assertTrue(result); - outStream.close(); + // Export data to CSV format + boolean result = MultiFormatExporter.exportData(db, outStream, DataFormat.Catima); + assertTrue(result); + outStream.close(); - clearDatabase(); + clearDatabase(); - ByteArrayInputStream inData = new ByteArrayInputStream(outData.toByteArray()); - InputStreamReader inStream = new InputStreamReader(inData); + ByteArrayInputStream inData = new ByteArrayInputStream(outData.toByteArray()); + InputStreamReader inStream = new InputStreamReader(inData); - // Import the CSV data - result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); - assertTrue(result); + // Import the CSV data + result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); + assertTrue(result); - assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); - assertEquals(NUM_GROUPS, db.getGroupCount()); + assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); + assertEquals(NUM_GROUPS, db.getGroupCount()); - checkLoyaltyCards(); - checkGroups(); + checkLoyaltyCards(); + checkGroups(); - assertEquals(groupsToGroupNames(groupsForOne), groupsToGroupNames(db.getLoyaltyCardGroups(1))); - assertEquals(groupsToGroupNames(groupsForTwo), groupsToGroupNames(db.getLoyaltyCardGroups(2))); - assertEquals(groupsToGroupNames(groupsForThree), groupsToGroupNames(db.getLoyaltyCardGroups(3))); - assertEquals(groupsToGroupNames(groupsForFour), groupsToGroupNames(db.getLoyaltyCardGroups(4))); - assertEquals(groupsToGroupNames(groupsForFive), groupsToGroupNames(db.getLoyaltyCardGroups(5))); - assertEquals(emptyGroup, db.getLoyaltyCardGroups(6)); - assertEquals(emptyGroup, db.getLoyaltyCardGroups(7)); - assertEquals(emptyGroup, db.getLoyaltyCardGroups(8)); - assertEquals(emptyGroup, db.getLoyaltyCardGroups(9)); - assertEquals(emptyGroup, db.getLoyaltyCardGroups(10)); + assertEquals(groupsToGroupNames(groupsForOne), groupsToGroupNames(db.getLoyaltyCardGroups(1))); + assertEquals(groupsToGroupNames(groupsForTwo), groupsToGroupNames(db.getLoyaltyCardGroups(2))); + assertEquals(groupsToGroupNames(groupsForThree), groupsToGroupNames(db.getLoyaltyCardGroups(3))); + assertEquals(groupsToGroupNames(groupsForFour), groupsToGroupNames(db.getLoyaltyCardGroups(4))); + assertEquals(groupsToGroupNames(groupsForFive), groupsToGroupNames(db.getLoyaltyCardGroups(5))); + assertEquals(emptyGroup, db.getLoyaltyCardGroups(6)); + assertEquals(emptyGroup, db.getLoyaltyCardGroups(7)); + assertEquals(emptyGroup, db.getLoyaltyCardGroups(8)); + assertEquals(emptyGroup, db.getLoyaltyCardGroups(9)); + assertEquals(emptyGroup, db.getLoyaltyCardGroups(10)); - // Clear the database for the next format under test - clearDatabase(); - } + // Clear the database for the next format under test + clearDatabase(); } @Test @@ -482,32 +472,29 @@ public class ImportExportTest { final int NUM_CARDS = 10; - for(DataFormat format : DataFormat.values()) - { - addLoyaltyCards(NUM_CARDS); + addLoyaltyCards(NUM_CARDS); - ByteArrayOutputStream outData = new ByteArrayOutputStream(); - OutputStreamWriter outStream = new OutputStreamWriter(outData); + ByteArrayOutputStream outData = new ByteArrayOutputStream(); + OutputStreamWriter outStream = new OutputStreamWriter(outData); - // Export into CSV data - boolean result = MultiFormatExporter.exportData(db, outStream, format); - assertTrue(result); - outStream.close(); + // Export into CSV data + boolean result = MultiFormatExporter.exportData(db, outStream, DataFormat.Catima); + assertTrue(result); + outStream.close(); - ByteArrayInputStream inData = new ByteArrayInputStream(outData.toByteArray()); - InputStreamReader inStream = new InputStreamReader(inData); + ByteArrayInputStream inData = new ByteArrayInputStream(outData.toByteArray()); + InputStreamReader inStream = new InputStreamReader(inData); - // Import the CSV data on top of the existing database - result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); - assertTrue(result); + // Import the CSV data on top of the existing database + result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); + assertTrue(result); - assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); + assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); - checkLoyaltyCards(); + checkLoyaltyCards(); - // Clear the database for the next format under test - clearDatabase(); - } + // Clear the database for the next format under test + clearDatabase(); } @Test @@ -523,7 +510,7 @@ public class ImportExportTest OutputStreamWriter outStream = new OutputStreamWriter(outData); // Export data to CSV format - boolean result = MultiFormatExporter.exportData(db, outStream, format); + boolean result = MultiFormatExporter.exportData(db, outStream, DataFormat.Catima); assertTrue(result); clearDatabase(); @@ -537,8 +524,8 @@ public class ImportExportTest ByteArrayInputStream inData = new ByteArrayInputStream((outData.toString() + corruptEntry).getBytes()); InputStreamReader inStream = new InputStreamReader(inData); - // Attempt to import the CSV data - result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); + // Attempt to import the data + result = MultiFormatImporter.importData(db, inStream, format); assertEquals(false, result); assertEquals(0, db.getLoyaltyCardCount()); @@ -566,49 +553,46 @@ public class ImportExportTest final File sdcardDir = Environment.getExternalStorageDirectory(); final File exportFile = new File(sdcardDir, "Catima.csv"); - for(DataFormat format : DataFormat.values()) - { - addLoyaltyCards(NUM_CARDS); + addLoyaltyCards(NUM_CARDS); - TestTaskCompleteListener listener = new TestTaskCompleteListener(); + TestTaskCompleteListener listener = new TestTaskCompleteListener(); - // Export to the file - FileOutputStream fileOutputStream = new FileOutputStream(exportFile); - ImportExportTask task = new ImportExportTask(activity, format, fileOutputStream, listener); - task.execute(); + // Export to the file + FileOutputStream fileOutputStream = new FileOutputStream(exportFile); + ImportExportTask task = new ImportExportTask(activity, DataFormat.Catima, fileOutputStream, listener); + task.execute(); - // Actually run the task to completion - Robolectric.flushBackgroundThreadScheduler(); + // Actually run the task to completion + Robolectric.flushBackgroundThreadScheduler(); - // Check that the listener was executed - assertNotNull(listener.success); - assertEquals(true, listener.success); + // Check that the listener was executed + assertNotNull(listener.success); + assertEquals(true, listener.success); - clearDatabase(); + clearDatabase(); - // Import everything back from the default location + // Import everything back from the default location - listener = new TestTaskCompleteListener(); + listener = new TestTaskCompleteListener(); - FileInputStream fileStream = new FileInputStream(exportFile); + FileInputStream fileStream = new FileInputStream(exportFile); - task = new ImportExportTask(activity, format, fileStream, listener); - task.execute(); + task = new ImportExportTask(activity, DataFormat.Catima, fileStream, listener); + task.execute(); - // Actually run the task to completion - Robolectric.flushBackgroundThreadScheduler(); + // Actually run the task to completion + Robolectric.flushBackgroundThreadScheduler(); - // Check that the listener was executed - assertNotNull(listener.success); - assertEquals(true, listener.success); + // Check that the listener was executed + assertNotNull(listener.success); + assertEquals(true, listener.success); - assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); + assertEquals(NUM_CARDS, db.getLoyaltyCardCount()); - checkLoyaltyCards(); + checkLoyaltyCards(); - // Clear the database for the next format under test - clearDatabase(); - } + // Clear the database for the next format under test + clearDatabase(); } @Test @@ -628,7 +612,7 @@ public class ImportExportTest InputStreamReader inStream = new InputStreamReader(inputStream); // Import the CSV data - boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); + boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); assertTrue(result); assertEquals(1, db.getLoyaltyCardCount()); @@ -666,7 +650,7 @@ public class ImportExportTest InputStreamReader inStream = new InputStreamReader(inputStream); // Import the CSV data - boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); + boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); assertTrue(result); assertEquals(1, db.getLoyaltyCardCount()); @@ -704,7 +688,7 @@ public class ImportExportTest InputStreamReader inStream = new InputStreamReader(inputStream); // Import the CSV data - boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); + boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); assertEquals(false, result); assertEquals(0, db.getLoyaltyCardCount()); @@ -730,7 +714,7 @@ public class ImportExportTest InputStreamReader inStream = new InputStreamReader(inputStream); // Import the CSV data - boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); + boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); assertEquals(true, result); assertEquals(1, db.getLoyaltyCardCount()); @@ -768,7 +752,7 @@ public class ImportExportTest InputStreamReader inStream = new InputStreamReader(inputStream); // Import the CSV data - boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); + boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); assertEquals(true, result); assertEquals(1, db.getLoyaltyCardCount()); @@ -806,7 +790,7 @@ public class ImportExportTest InputStreamReader inStream = new InputStreamReader(inputStream); // Import the CSV data - boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); + boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); assertEquals(true, result); assertEquals(1, db.getLoyaltyCardCount()); @@ -844,7 +828,7 @@ public class ImportExportTest InputStreamReader inStream = new InputStreamReader(inputStream); // Import the CSV data - boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); + boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); assertTrue(result); assertEquals(1, db.getLoyaltyCardCount()); @@ -864,7 +848,7 @@ public class ImportExportTest inStream = new InputStreamReader(inputStream); // Import the CSV data - result = MultiFormatImporter.importData(db, inStream, DataFormat.CSV); + result = MultiFormatImporter.importData(db, inStream, DataFormat.Catima); assertTrue(result); assertEquals(1, db.getLoyaltyCardCount()); @@ -912,7 +896,8 @@ public class ImportExportTest InputStreamReader inStream = new InputStreamReader(inputStream); // Import the Voucher Vault data - new VoucherVaultImporter().importData(db, inStream); + boolean result = MultiFormatImporter.importData(db, inStream, DataFormat.VoucherVault); + assertTrue(result); assertEquals(2, db.getLoyaltyCardCount()); LoyaltyCard card = db.getLoyaltyCard(1); From c13b5dda1811d57076e4825853583b51f9bb5c87 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Thu, 4 Mar 2021 23:45:03 +0100 Subject: [PATCH 4/4] Make Spotbugs happy --- .../protect/card_locker/importexport/MultiFormatExporter.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java b/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java index f6509d561..fedc446e7 100644 --- a/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java @@ -33,6 +33,9 @@ public class MultiFormatExporter case Catima: exporter = new CsvDatabaseExporter(); break; + default: + Log.e(TAG, "Failed to export data, unknown format " + format.name()); + break; } if(exporter != null)