Deck implements Parcelable { public JSONObject toJSON(String exportedDeckName) throws JSONException { JSONObject json = new JSONObject(); json.put(JsonKeys.DECK_LABEL, exportedDeckName); json.put(JsonKeys.PUBLISHER, author); if (externalId != null) { json.put(JsonKeys.EXTERNAL_ID, externalId); } json.put(JsonKeys.TIMESTAMP, timestamp); json.put(JsonKeys.SOURCE_LANGUAGE_ISO, TranslationCardsISO.getISOCodeForLanguage(sourceLanguage)); json.put(JsonKeys.SOURCE_LANGUAGE_NAME, sourceLanguage); json.put(JsonKeys.LOCKED, locked); json.put(JsonKeys.DICTIONARIES, getDictionariesJSON()); return json; } Deck(String title, String author, String externalId, long dbId, long timestamp,
                boolean locked, String sourceLanguage, Dictionary[] dictionaries);  Deck(String title, String author, String externalId, long timestamp, boolean locked,
                String sourceLanguage);  Deck(); protected  Deck(Parcel in); String getTitle(); String getAuthor(); String getExternalId(); long getDbId(); long getTimestamp(); String getCreationDateString(); String getDeckInformation(); boolean isLocked(); String getSourceLanguageIso(); String getSourceLanguageName(); String getDestinationLanguagesForDisplay(); void setTitle(String title); void setSourceLanguage(String language); void setAuthor(String author); JSONObject toJSON(String exportedDeckName); Map<String, Boolean> getAudioFilePaths(); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); static final Creator<Deck> CREATOR; }
@Test public void shouldWriteOneDictionaryToJson() throws JSONException { Dictionary dictionary = mock(Dictionary.class); Dictionary[] dictionaries = {dictionary}; JSONObject dictionaryJSON = new JSONObject(); when(dictionary.toJSON()).thenReturn(dictionaryJSON); Deck deck = new Deck(NO_VALUE, "author", NO_VALUE, -1, 1454946439262L, false, NO_VALUE, dictionaries); JSONObject jsonObject = deck.toJSON(EXPORTED_DECK_NAME); assertEquals(dictionaryJSON, jsonObject.getJSONArray(JsonKeys.DICTIONARIES).get(0)); }
@Test public void shouldWriteMultipleDictionariesToJson() throws JSONException { Dictionary firstDictionary = mock(Dictionary.class); Dictionary secondDictionary = mock(Dictionary.class); Dictionary[] dictionaries = {firstDictionary, secondDictionary}; JSONObject firstDictionaryJSON = new JSONObject(); JSONObject secondDictionaryJSON = new JSONObject(); when(firstDictionary.toJSON()).thenReturn(firstDictionaryJSON); when(secondDictionary.toJSON()).thenReturn(secondDictionaryJSON); Deck deck = new Deck(NO_VALUE, "author", NO_VALUE, -1, 1454946439262L, false, NO_VALUE, dictionaries); JSONObject jsonObject = deck.toJSON(EXPORTED_DECK_NAME); JSONArray dictionariesArray = jsonObject.getJSONArray(JsonKeys.DICTIONARIES); assertEquals(2, dictionariesArray.length()); assertEquals(firstDictionaryJSON, dictionariesArray.get(0)); assertEquals(secondDictionaryJSON, dictionariesArray.get(1)); }