@Override protected T doRead() throws Exception { return jsonObjectReader.read(); }
@Override protected void doClose() throws Exception { this.jsonObjectReader.close(); }
@Override protected JsonObjectMarshaller<Trade> getJsonObjectMarshaller() { return new JacksonJsonObjectMarshaller<>(); }
@Test public void testJsonWriting() throws Exception { // when this.writer.open(this.executionContext); this.writer.write(this.items); this.writer.close(); // then assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "expected-trades.json"), this.resource.getFile()); }
@Test public void testReadItem() throws Exception { // given Resource resource = new ByteArrayResource("[]".getBytes()); itemReader = new JsonItemReader<>(resource, this.jsonObjectReader); // when itemReader.read(); // then Mockito.verify(this.jsonObjectReader).read(); }
@Override protected ItemReader<Foo> getItemReader() { ByteArrayResource resource = new ByteArrayResource(FOOS.getBytes()); JsonObjectReader<Foo> jsonObjectReader = getJsonObjectReader(); JsonItemReader<Foo> itemReader = new JsonItemReader<>(resource, jsonObjectReader); itemReader.setName("fooJsonItemReader"); return itemReader; }
@Test public void testNonReadableResource() { // given this.expectedException.expect(ItemStreamException.class); this.expectedException.expectMessage("Failed to initialize the reader"); this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class)); this.itemReader = new JsonItemReader<>(new NonReadableResource(), this.jsonObjectReader); // when this.itemReader.open(new ExecutionContext()); // then // expected exception }
@Test public void testNonExistentResource() { // given this.expectedException.expect(ItemStreamException.class); this.expectedException.expectMessage("Failed to initialize the reader"); this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class)); this.itemReader = new JsonItemReader<>(new NonExistentResource(), this.jsonObjectReader); // when this.itemReader.open(new ExecutionContext()); // then // expected exception }
@Test public void testJsonMarshalling() { // given JacksonJsonObjectMarshaller<Foo> jsonObjectMarshaller = new JacksonJsonObjectMarshaller<>(); // when String foo = jsonObjectMarshaller.marshal(new Foo(1, "foo")); // then Assert.assertEquals("{\"id\":1,\"name\":\"foo\"}", foo); }
@Override protected JsonObjectMarshaller<Trade> getJsonObjectMarshallerWithPrettyPrint() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); JacksonJsonObjectMarshaller<Trade> jsonObjectMarshaller = new JacksonJsonObjectMarshaller<>(); jsonObjectMarshaller.setObjectMapper(objectMapper); return jsonObjectMarshaller; }
@Test public void testJsonMarshalling() { // given GsonJsonObjectMarshaller<Foo> jsonObjectMarshaller = new GsonJsonObjectMarshaller<>(); // when String foo = jsonObjectMarshaller.marshal(new Foo(1, "foo")); // then Assert.assertEquals("{\"id\":1,\"name\":\"foo\"}", foo); }
@Override protected JsonObjectReader<Trade> getJsonObjectReader() { return new GsonJsonObjectReader<>(Trade.class); }
@Test(expected = IllegalArgumentException.class) public void resourceMustNotBeNull() { new JsonFileItemWriter<>(null, this.jsonObjectMarshaller); }
@Override protected JsonObjectMarshaller<Trade> getJsonObjectMarshallerWithPrettyPrint() { Gson gson = new GsonBuilder().setPrettyPrinting().create(); GsonJsonObjectMarshaller<Trade> jsonObjectMarshaller = new GsonJsonObjectMarshaller<>(); jsonObjectMarshaller.setGson(gson); return jsonObjectMarshaller; }
@Override protected JsonObjectReader<Trade> getJsonObjectReader() { return new JacksonJsonObjectReader<>(Trade.class); }
@Override protected JsonObjectMarshaller<Trade> getJsonObjectMarshaller() { return new GsonJsonObjectMarshaller<>(); }
@Test public void testJsonWritingWithMultipleWrite() throws Exception { // when this.writer.open(this.executionContext); this.writer.write(this.items); this.writer.write(Arrays.asList(trade3, trade4)); this.writer.close(); // then assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "expected-trades-with-multiple-writes.json"), this.resource.getFile()); }
@Override protected JsonObjectReader<Foo> getJsonObjectReader() { return new GsonJsonObjectReader<>(Foo.class); }
@Test(expected = IllegalArgumentException.class) public void jsonObjectMarshallerMustNotBeNull() { new JsonFileItemWriter<>(this.resource, null); }
@Override protected JsonObjectReader<Foo> getJsonObjectReader() { return new JacksonJsonObjectReader<>(Foo.class); }