### Question:
StripeSourceTypeModel extends StripeJsonModel { static void putAdditionalFieldsIntoJsonObject( @Nullable JSONObject jsonObject, @Nullable Map<String, Object> additionalFields) { if (jsonObject == null || additionalFields == null || additionalFields.isEmpty()) { return; } for (String key : additionalFields.keySet()) { try { if (additionalFields.get(key) != null) { jsonObject.put(key, additionalFields.get(key)); } } catch (JSONException ignored) { } } } StripeSourceTypeModel(); @NonNull Map<String, Object> getAdditionalFields();  }### Answer:
@Test public void putAdditionalFieldsIntoJsonObject_whenHasFields_putsThemIntoObject() { JSONObject jsonObject = new JSONObject(); Map<String, Object> additionalFields = new HashMap<>(); additionalFields.put("a_key", "a_value"); additionalFields.put("b_key", "b_value"); putAdditionalFieldsIntoJsonObject(jsonObject, additionalFields); assertEquals(2, jsonObject.length()); assertEquals("a_value", jsonObject.optString("a_key")); assertEquals("b_value", jsonObject.optString("b_key")); }
@Test public void putAdditionalFieldsIntoJsonObject_whenHasDuplicateFields_putsThemIntoObject() { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("a_key", "original"); } catch (JSONException unexpected) { fail("Unexpected exception: " + unexpected); } Map<String, Object> additionalFields = new HashMap<>(); additionalFields.put("a_key", "a_value"); additionalFields.put("b_key", "b_value"); putAdditionalFieldsIntoJsonObject(jsonObject, additionalFields); assertEquals(2, jsonObject.length()); assertEquals("a_value", jsonObject.optString("a_key")); assertEquals("b_value", jsonObject.optString("b_key")); }
@Test public void putAdditionalFieldsIntoJsonObject_whenNoFieldsArePassed_doesNothing() { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("a", "a_value"); } catch (JSONException unexpected) { fail("Unexpected error: " + unexpected.getLocalizedMessage()); } Map<String, Object> emptyMap = new HashMap<>(); putAdditionalFieldsIntoJsonObject(jsonObject, emptyMap); assertEquals(1, jsonObject.length()); }
@Test public void putAdditionalFieldsIntoJsonObject_whenNullPassedInEitherArgument_doesNothing() { Map<String, Object> littleMap = new HashMap<String, Object>() {{ put("a", "a_value"); }}; JSONObject jsonObject = new JSONObject(); putAdditionalFieldsIntoJsonObject(null, littleMap); putAdditionalFieldsIntoJsonObject(jsonObject, null); assertEquals(0, jsonObject.length()); }