TxcImportUtility { @NonNull public ImportSpec buildImportSpec(File dir, String hash, JSONObject json) throws ImportException { ImportSpec spec; try { String deckLabel = json.getString(JsonKeys.DECK_LABEL); String publisher = json.optString(JsonKeys.PUBLISHER); String externalId = json.optString(JsonKeys.EXTERNAL_ID); long timestamp = json.optLong(JsonKeys.TIMESTAMP, -1); String srcLanguageIso = json.optString(JsonKeys.SOURCE_LANGUAGE_ISO, DEFAULT_SOURCE_LANGUAGE_ISO); String sourceLanguageName = json.optString(JsonKeys.SOURCE_LANGUAGE_NAME, NO_SOURCE_LANGUAGE_NAME); boolean locked = json.optBoolean(JsonKeys.LOCKED, false); spec = new ImportSpec(deckLabel, publisher, externalId, timestamp, locked, TranslationCardsISO.getLanguageDisplayName(srcLanguageIso, sourceLanguageName), hash, dir); JSONArray dictionaries = json.optJSONArray(JsonKeys.DICTIONARIES); if (dictionaries == null) { return spec; } addDictionariesToImportSpec(spec, dictionaries); } catch (JSONException e) { throw new ImportException(ImportException.ImportProblem.INVALID_INDEX_FILE, e); } return spec; } TxcImportUtility(LanguageService languageService, DeckRepository deckRepository, TranslationRepository translationRepository, DictionaryRepository dictionaryRepository); ImportSpec prepareImport(Context context, Uri source); void executeImport(ImportSpec importSpec); void abortImport(ImportSpec importSpec); boolean isExistingDeck(ImportSpec importSpec); long getExistingDeckId(ImportSpec importSpec); void loadBundledDeck(); @NonNull ImportSpec buildImportSpec(File dir, String hash, JSONObject json); void loadData(ImportSpec importSpec); void loadAssetData(ImportSpec importSpec); static final String NO_AUDIO; static final String NO_SOURCE_LANGUAGE_NAME; }
@Test public void shouldBuildImportSpecWithDefaultsAndDeckLabel() throws ImportException, JSONException { jsonObjectToLoad.put(JsonKeys.DECK_LABEL, DECK_LABEL); TxcImportUtility.ImportSpec importSpec = txcImportUtility.buildImportSpec(mockFile, HASH, jsonObjectToLoad); assertEquals(DECK_LABEL, importSpec.label); assertEquals("", importSpec.publisher); assertEquals("", importSpec.externalId); assertEquals(-1L, importSpec.timestamp); assertEquals("English", importSpec.srcLanguage); assertFalse(importSpec.locked); assertTrue(importSpec.dictionaries.isEmpty()); assertEquals(mockFile, importSpec.dir); assertEquals(HASH, importSpec.hash); }
@Test public void shouldBuildImportSpecWithPublisherWhenPresent() throws ImportException, JSONException { jsonObjectToLoad.put(JsonKeys.DECK_LABEL, DECK_LABEL); jsonObjectToLoad.put(JsonKeys.PUBLISHER, PUBLISHER); TxcImportUtility.ImportSpec importSpec = txcImportUtility.buildImportSpec(mockFile, HASH, jsonObjectToLoad); assertEquals(DECK_LABEL, importSpec.label); assertEquals(PUBLISHER, importSpec.publisher); assertEquals("", importSpec.externalId); assertEquals(-1L, importSpec.timestamp); assertEquals("English", importSpec.srcLanguage); assertFalse(importSpec.locked); assertTrue(importSpec.dictionaries.isEmpty()); assertEquals(mockFile, importSpec.dir); assertEquals(HASH, importSpec.hash); }
@Test public void shouldBuildImportSpecWithExternalIdWhenPresent() throws ImportException, JSONException { jsonObjectToLoad.put(JsonKeys.DECK_LABEL, DECK_LABEL); jsonObjectToLoad.put(JsonKeys.EXTERNAL_ID, EXTERNAL_ID); TxcImportUtility.ImportSpec importSpec = txcImportUtility.buildImportSpec(mockFile, HASH, jsonObjectToLoad); assertEquals(DECK_LABEL, importSpec.label); assertEquals("", importSpec.publisher); assertEquals(EXTERNAL_ID, importSpec.externalId); assertEquals(-1L, importSpec.timestamp); assertEquals("English", importSpec.srcLanguage); assertFalse(importSpec.locked); assertTrue(importSpec.dictionaries.isEmpty()); assertEquals(mockFile, importSpec.dir); assertEquals(HASH, importSpec.hash); }
@Test public void shouldBuildImportSpecWithTimestampWhenPresent() throws ImportException, JSONException { jsonObjectToLoad.put(JsonKeys.DECK_LABEL, DECK_LABEL); jsonObjectToLoad.put(JsonKeys.TIMESTAMP, TIMESTAMP); TxcImportUtility.ImportSpec importSpec = txcImportUtility.buildImportSpec(mockFile, HASH, jsonObjectToLoad); assertEquals(DECK_LABEL, importSpec.label); assertEquals("", importSpec.publisher); assertEquals("", importSpec.externalId); assertEquals(TIMESTAMP, importSpec.timestamp); assertEquals("English", importSpec.srcLanguage); assertFalse(importSpec.locked); assertTrue(importSpec.dictionaries.isEmpty()); assertEquals(mockFile, importSpec.dir); assertEquals(HASH, importSpec.hash); }
@Test public void shouldBuildImportSpecWithSourceLanguageWhenPresent() throws ImportException, JSONException { jsonObjectToLoad.put(JsonKeys.DECK_LABEL, DECK_LABEL); jsonObjectToLoad.put(JsonKeys.SOURCE_LANGUAGE_ISO, SOURCE_LANGUAGE_ISO); TxcImportUtility.ImportSpec importSpec = txcImportUtility.buildImportSpec(mockFile, HASH, jsonObjectToLoad); assertEquals(DECK_LABEL, importSpec.label); assertEquals("", importSpec.publisher); assertEquals("", importSpec.externalId); assertEquals(-1L, importSpec.timestamp); assertEquals("French", importSpec.srcLanguage); assertFalse(importSpec.locked); assertTrue(importSpec.dictionaries.isEmpty()); assertEquals(mockFile, importSpec.dir); assertEquals(HASH, importSpec.hash); }
@Test public void shouldBuildImportSpecWithLockSet() throws ImportException, JSONException { jsonObjectToLoad.put(JsonKeys.DECK_LABEL, DECK_LABEL); jsonObjectToLoad.put(JsonKeys.LOCKED, true); TxcImportUtility.ImportSpec importSpec = txcImportUtility.buildImportSpec(mockFile, HASH, jsonObjectToLoad); assertEquals(DECK_LABEL, importSpec.label); assertEquals("", importSpec.publisher); assertEquals("", importSpec.externalId); assertEquals(-1L, importSpec.timestamp); assertEquals("English", importSpec.srcLanguage); assertTrue(importSpec.locked); assertTrue(importSpec.dictionaries.isEmpty()); assertEquals(mockFile, importSpec.dir); assertEquals(HASH, importSpec.hash); }
@Test public void shouldBuildImportSpecWithDictionariesWhenPresent() throws ImportException, JSONException { jsonObjectToLoad.put(JsonKeys.DECK_LABEL, DECK_LABEL); JSONArray currentDictionaries = new JSONArray(DICTIONARIES_AS_JSON); jsonObjectToLoad.put(JsonKeys.DICTIONARIES, currentDictionaries); TxcImportUtility.ImportSpec importSpec = txcImportUtility.buildImportSpec(mockFile, HASH, jsonObjectToLoad); assertEquals(DECK_LABEL, importSpec.label); assertEquals("", importSpec.publisher); assertEquals("", importSpec.externalId); assertEquals(-1L, importSpec.timestamp); assertEquals("English", importSpec.srcLanguage); assertFalse(importSpec.locked); assertEquals(3, importSpec.dictionaries.size()); assertEquals("ar", importSpec.dictionaries.get(0).isoCode); assertEquals("Arabic", importSpec.dictionaries.get(0).language); assertEquals(mockFile, importSpec.dir); assertEquals(HASH, importSpec.hash); for (TxcImportUtility.ImportSpecDictionary specDictionary : importSpec.dictionaries) { assertEquals(2, specDictionary.cards.size()); } }
@Test public void shouldBuildImportSpecWithDictionariesWithNoDestinationIsoCode() throws ImportException, JSONException { jsonObjectToLoad.put(JsonKeys.DECK_LABEL, DECK_LABEL); JSONArray currentDictionaries = new JSONArray(DICTIONARY_JSON_NO_ISO_CODE); jsonObjectToLoad.put(JsonKeys.DICTIONARIES, currentDictionaries); TxcImportUtility.ImportSpec importSpec = txcImportUtility.buildImportSpec(mockFile, HASH, jsonObjectToLoad); assertEquals(DECK_LABEL, importSpec.label); assertEquals("", importSpec.publisher); assertEquals("", importSpec.externalId); assertEquals(-1L, importSpec.timestamp); assertEquals("English", importSpec.srcLanguage); assertFalse(importSpec.locked); assertEquals(1, importSpec.dictionaries.size()); assertEquals(LanguageService.INVALID_LANGUAGE_NAME, importSpec.dictionaries.get(0).language); assertEquals(mockFile, importSpec.dir); assertEquals(HASH, importSpec.hash); for (TxcImportUtility.ImportSpecDictionary specDictionary : importSpec.dictionaries) { assertEquals(2, specDictionary.cards.size()); } }