InputJsonWriter { @Nullable public String write(@Nullable final AbstractInput input) { if (input == null) { return null; } final StringWriter writer = new StringWriter(); try { write(writer, input); } catch (IOException ioe) { Log.w(TAG, ioe.getMessage()); return null; } return writer.toString(); } InputJsonWriter(@NonNull OnInputJsonWriterListener onInputJsonWriterListener); @NonNull String getDateFormat(); void setDateFormat(@NonNull final String dateFormat); @Nullable String write(@Nullable final AbstractInput input); void write(@NonNull final Writer out,
                      @NonNull final AbstractInput input);  }
@Test public void testWrite() throws Exception { final DummyInput input = new DummyInput(InputType.FAUNA); final Observer observer1 = new Observer(1L, "lastname", "firstname"); input.getObservers() .put(observer1.getObserverId(), observer1); final DummyTaxon taxon1 = new DummyTaxon(1L); taxon1.setNameEntered("name1"); taxon1.setCriterionId(4L); taxon1.setComment("comment for taxon 1"); input.getTaxa() .put(taxon1.getId(), taxon1); input.setQualification(new Qualification(2, 140, 4)); final StringWriter writer = new StringWriter(); inputJsonWriter.write(writer, input); verify(onInputJsonWriterListener, atLeastOnce()).writeAdditionalInputData(any(JsonWriter.class), eq(input)); verify(onInputJsonWriterListener, atLeastOnce()).writeAdditionalTaxonData(any(JsonWriter.class), eq(taxon1)); assertNotNull(writer.toString()); @SuppressWarnings("StringBufferReplaceableByString") final String expectedJsonString = new StringBuilder().append('{') .append("\"id\":") .append(input.getInputId()) .append(",\"input_type\":\"") .append(input.getType() .getValue()) .append("\",\"initial_input\":\"nomade\",\"dateobs\":\"") .append(DateFormat.format(inputJsonWriter.getDateFormat(), input.getDate()) .toString()) .append("\",\"qualification\":{") .append("\"organism\":2") .append(",\"protocol\":140") .append(",\"lot\":4}") .append(",\"observers_id\":[") .append(observer1.getObserverId()) .append("],\"observers\":[{\"id\":") .append(observer1.getObserverId()) .append(",\"lastname\":\"") .append(observer1.getLastname()) .append("\",\"firstname\":\"") .append(observer1.getFirstname()) .append("\"}],\"taxons\":[{\"id\":") .append(taxon1.getId()) .append(",\"id_taxon\":") .append(taxon1.getTaxonId()) .append(",\"name_entered\":\"") .append(taxon1.getNameEntered()) .append("\",\"observation\":{\"criterion\":") .append(taxon1.getCriterionId()) .append("},\"comment\":\"") .append(taxon1.getComment()) .append("\"}]}") .toString(); assertEquals(expectedJsonString, writer.toString()); }