This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1393 from finos/1038-stdout-json-to-use-ndjson-fo…
…rmat 1038 stdout json to use ndjson format
- Loading branch information
Showing
7 changed files
with
105 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
output/src/test/java/com/scottlogic/deg/output/writer/json/JsonOutputWriterFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.scottlogic.deg.output.writer.json; | ||
|
||
import com.scottlogic.deg.common.output.GeneratedObject; | ||
import com.scottlogic.deg.common.profile.FieldBuilder; | ||
import com.scottlogic.deg.common.profile.ProfileFields; | ||
import com.scottlogic.deg.output.writer.DataSetWriter; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
|
||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class JsonOutputWriterFactoryTest { | ||
|
||
@Test | ||
void writer_withNDJSONTrue__shouldOutputNewLineDelimiterRows() throws IOException { | ||
expectJson( | ||
true, | ||
Matchers.equalTo("{\n \"my_field\" : \"my_value\"\n}\n{\n \"my_field\" : \"my_value\"\n}")); | ||
} | ||
|
||
@Test | ||
void writer_withNDJSONFalse__shouldOutputRowsWrappedInAnArray() throws IOException { | ||
expectJson( | ||
false, | ||
Matchers.equalTo("[ {\n \"my_field\" : \"my_value\"\n}, {\n \"my_field\" : \"my_value\"\n} ]")); | ||
} | ||
|
||
private static void expectJson(boolean useNdJson, Matcher<String> matcher) throws IOException { | ||
//Arrange | ||
ProfileFields fields = new ProfileFields(Collections.singletonList(FieldBuilder.createField("my_field"))); | ||
|
||
|
||
// Act | ||
GeneratedObject mockGeneratedObject = mock(GeneratedObject.class); | ||
when(mockGeneratedObject.getFormattedValue(eq(fields.iterator().next()))).thenReturn("my_value"); | ||
String generateJson = generateJson(fields, mockGeneratedObject, useNdJson); | ||
|
||
// Assert | ||
Assert.assertThat(generateJson, matcher); | ||
} | ||
|
||
private static String generateJson(ProfileFields fields, GeneratedObject generatedObject, boolean useNdJson) throws IOException { | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
|
||
try (DataSetWriter writer = new JsonOutputWriterFactory(useNdJson).createWriter(stream, fields)) { | ||
writer.writeRow(generatedObject); | ||
writer.writeRow(generatedObject); | ||
} | ||
|
||
return stream | ||
.toString(StandardCharsets.UTF_8.name()) | ||
.replace("\r\n", "\n"); // normalise line endings between e.g. Windows and Linux | ||
} | ||
} |