/** * Validate the configuration and build a new {@link JsonItemReader}. * @return a new instance of the {@link JsonItemReader} */ public JsonItemReader<T> build() { Assert.notNull(this.jsonObjectReader, "A json object reader is required."); Assert.notNull(this.resource, "A resource is required."); if (this.saveState) { Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true."); } JsonItemReader<T> reader = new JsonItemReader<>(this.resource, this.jsonObjectReader); reader.setName(this.name); reader.setStrict(this.strict); reader.setSaveState(this.saveState); reader.setMaxItemCount(this.maxItemCount); reader.setCurrentItemCount(this.currentItemCount); return reader; } }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) { JsonItemReader<Foo> reader = (JsonItemReader<Foo>) tested; reader.setResource(new ByteArrayResource("[]".getBytes())); reader.open(new ExecutionContext()); }
@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 testEmptyResource() throws Exception { JsonItemReader<Trade> itemReader = new JsonItemReaderBuilder<Trade>() .jsonObjectReader(getJsonObjectReader()) .resource(new ByteArrayResource("[]".getBytes())) .name("tradeJsonItemReader") .build(); itemReader.open(new ExecutionContext()); Trade trade = itemReader.read(); Assert.assertNull(trade); }
@Test public void testValidation() { try { new JsonItemReader<>(null, this.jsonObjectReader); fail("A resource is required."); } catch (IllegalArgumentException iae) { assertEquals("The resource must not be null.", iae.getMessage()); } try { new JsonItemReader<>(new ByteArrayResource("[{}]".getBytes()), null); fail("A json object reader is required."); } catch (IllegalArgumentException iae) { assertEquals("The json object reader must not be null.", iae.getMessage()); } }
@Test public void testInvalidResourceFormat() { this.expectedException.expect(ItemStreamException.class); this.expectedException.expectMessage("Failed to initialize the reader"); this.expectedException.expectCause(instanceOf(IllegalStateException.class)); JsonItemReader<Trade> itemReader = new JsonItemReaderBuilder<Trade>() .jsonObjectReader(getJsonObjectReader()) .resource(new ByteArrayResource("{}, {}".getBytes())) .name("tradeJsonItemReader") .build(); itemReader.open(new ExecutionContext()); }
@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 testInvalidResourceContent() throws Exception { this.expectedException.expect(ParseException.class); this.expectedException.expectCause(Matchers.instanceOf(getJsonParsingException())); JsonItemReader<Trade> itemReader = new JsonItemReaderBuilder<Trade>() .jsonObjectReader(getJsonObjectReader()) .resource(new ByteArrayResource("[{]".getBytes())) .name("tradeJsonItemReader") .build(); itemReader.open(new ExecutionContext()); itemReader.read(); } }
/** * Validate the configuration and build a new {@link JsonItemReader}. * @return a new instance of the {@link JsonItemReader} */ public JsonItemReader<T> build() { Assert.notNull(this.jsonObjectReader, "A json object reader is required."); Assert.notNull(this.resource, "A resource is required."); if (this.saveState) { Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true."); } JsonItemReader<T> reader = new JsonItemReader<>(this.resource, this.jsonObjectReader); reader.setName(this.name); reader.setStrict(this.strict); reader.setSaveState(this.saveState); reader.setMaxItemCount(this.maxItemCount); reader.setCurrentItemCount(this.currentItemCount); return reader; } }
.build(); itemReader.open(new ExecutionContext()); Trade trade = itemReader.read(); Assert.assertNotNull(trade); Assert.assertEquals("123", trade.getIsin()); Assert.assertEquals(1, trade.getQuantity()); trade = itemReader.read(); Assert.assertNotNull(trade); Assert.assertEquals("456", trade.getIsin()); Assert.assertEquals(2, trade.getQuantity()); trade = itemReader.read(); Assert.assertNotNull(trade); Assert.assertEquals("789", trade.getIsin()); Assert.assertEquals(3, trade.getQuantity()); trade = itemReader.read(); Assert.assertNotNull(trade); Assert.assertEquals("100", trade.getIsin()); trade = itemReader.read(); Assert.assertNull(trade);