### Question:
WriteJsonResult extends AbstractRecordSetWriter implements RecordSetWriter, RawRecordWriter { @Override public Map<String, String> writeRecord(final Record record) throws IOException { if (!isActiveRecordSet()) { generator.flush(); schemaAccess.writeHeader(recordSchema, getOutputStream()); } writeRecord(record, recordSchema, generator, g -> g.writeStartObject(), g -> g.writeEndObject(), true); return schemaAccess.getAttributes(recordSchema); } WriteJsonResult(final ComponentLog logger, final RecordSchema recordSchema, final SchemaAccessWriter schemaAccess, final OutputStream out, final boolean prettyPrint,
        final NullSuppression nullSuppression, final OutputGrouping outputGrouping, final String dateFormat, final String timeFormat, final String timestampFormat); @Override void close(); @Override void flush(); @Override Map<String, String> writeRecord(final Record record); @Override WriteResult writeRawRecord(final Record record); @Override String getMimeType();  }### Answer:
@Test public void testExtraFieldInWriteRecord() throws IOException { final List<RecordField> fields = new ArrayList<>(); fields.add(new RecordField("id", RecordFieldType.STRING.getDataType())); final RecordSchema schema = new SimpleRecordSchema(fields); final Map<String, Object> values = new HashMap<>(); values.put("id", "1"); values.put("name", "John"); final Record record = new MapRecord(schema, values); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final WriteJsonResult writer = new WriteJsonResult(Mockito.mock(ComponentLog.class), schema, new SchemaNameAsAttribute(), baos, false, NullSuppression.NEVER_SUPPRESS, OutputGrouping.OUTPUT_ARRAY, null, null, null)) { writer.beginRecordSet(); writer.writeRecord(record); writer.finishRecordSet(); } final byte[] data = baos.toByteArray(); final String expected = "[{\"id\":\"1\"}]"; final String output = new String(data, StandardCharsets.UTF_8); assertEquals(expected, output); }
@Test public void testMissingFieldInWriteRecord() throws IOException { final List<RecordField> fields = new ArrayList<>(); fields.add(new RecordField("id", RecordFieldType.STRING.getDataType())); fields.add(new RecordField("name", RecordFieldType.STRING.getDataType())); final RecordSchema schema = new SimpleRecordSchema(fields); final Map<String, Object> values = new LinkedHashMap<>(); values.put("id", "1"); final Record record = new MapRecord(schema, values); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final WriteJsonResult writer = new WriteJsonResult(Mockito.mock(ComponentLog.class), schema, new SchemaNameAsAttribute(), baos, false, NullSuppression.NEVER_SUPPRESS, OutputGrouping.OUTPUT_ARRAY, null, null, null)) { writer.beginRecordSet(); writer.writeRecord(record); writer.finishRecordSet(); } final byte[] data = baos.toByteArray(); final String expected = "[{\"id\":\"1\",\"name\":null}]"; final String output = new String(data, StandardCharsets.UTF_8); assertEquals(expected, output); }
@Test public void testMissingAndExtraFieldInWriteRecord() throws IOException { final List<RecordField> fields = new ArrayList<>(); fields.add(new RecordField("id", RecordFieldType.STRING.getDataType())); fields.add(new RecordField("name", RecordFieldType.STRING.getDataType())); final RecordSchema schema = new SimpleRecordSchema(fields); final Map<String, Object> values = new LinkedHashMap<>(); values.put("id", "1"); values.put("dob", "1/1/1970"); final Record record = new MapRecord(schema, values); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final WriteJsonResult writer = new WriteJsonResult(Mockito.mock(ComponentLog.class), schema, new SchemaNameAsAttribute(), baos, false, NullSuppression.NEVER_SUPPRESS, OutputGrouping.OUTPUT_ARRAY, null, null, null)) { writer.beginRecordSet(); writer.writeRecord(record); writer.finishRecordSet(); } final byte[] data = baos.toByteArray(); final String expected = "[{\"id\":\"1\",\"name\":null}]"; final String output = new String(data, StandardCharsets.UTF_8); assertEquals(expected, output); }