Dictionary implements Parcelable { protected JSONObject toJSON() throws JSONException { JSONObject jsonObject = new JSONObject(); jsonObject.put(JsonKeys.DICTIONARY_DEST_ISO_CODE, TranslationCardsISO.getISOCodeForLanguage(language)); jsonObject.put(JsonKeys.DICTIONARY_DEST_NAME, language); jsonObject.put(JsonKeys.CARDS, createJSONTranslationsArray()); return jsonObject; } Dictionary(String language, Translation[] translations, long dbId);  Dictionary(String language); protected  Dictionary(Parcel in); String getLanguage(); int getTranslationCount(); Translation getTranslation(int index); Translation getTranslationBySourcePhrase(String sourcePhrase); long getDbId(); @Override String toString(); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); static final Creator<Dictionary> CREATOR; }
@Test public void shouldWriteTranslationsToJSON() throws JSONException { Translation firstTranslation = mock(Translation.class); Translation secondTranslation = mock(Translation.class); Dictionary dictionary = new Dictionary("", new Translation[]{firstTranslation, secondTranslation}, 1L); JSONObject firstTranslationAsJSON = new JSONObject(); JSONObject secondTranslationAsJSON = new JSONObject(); when(firstTranslation.toJSON()).thenReturn(firstTranslationAsJSON); when(secondTranslation.toJSON()).thenReturn(secondTranslationAsJSON); JSONObject jsonObject = dictionary.toJSON(); JSONArray cardsArray = jsonObject.getJSONArray(JsonKeys.CARDS); assertEquals(2, cardsArray.length()); assertEquals(firstTranslationAsJSON, cardsArray.getJSONObject(0)); assertEquals(secondTranslationAsJSON, cardsArray.getJSONObject(1)); }