@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); writer = new GemfireItemWriter<>(); writer.setTemplate(template); writer.setItemKeyMapper(new SpELItemKeyMapper<>("bar.val")); writer.afterPropertiesSet(); }
/** * Validates and builds a {@link GemfireItemWriter}. * * @return a {@link GemfireItemWriter} */ public GemfireItemWriter<K, V> build() { Assert.notNull(this.template, "template is required."); Assert.notNull(this.itemKeyMapper, "itemKeyMapper is required."); GemfireItemWriter<K, V> writer = new GemfireItemWriter<>(); writer.setTemplate(this.template); writer.setItemKeyMapper(this.itemKeyMapper); writer.setDelete(this.delete); return writer; } }
@Test public void testWriteNoTransactionNoItems() throws Exception { writer.write(null); verifyZeroInteractions(template); }
@Test public void testAfterPropertiesSet() throws Exception { writer = new GemfireItemWriter<>(); try { writer.afterPropertiesSet(); fail("Expected exception was not thrown"); } catch (IllegalArgumentException iae) { } writer.setTemplate(template); try { writer.afterPropertiesSet(); fail("Expected exception was not thrown"); } catch (IllegalArgumentException iae) { } writer.setItemKeyMapper(new SpELItemKeyMapper<>("foo")); writer.afterPropertiesSet(); }
/** * Validates and builds a {@link GemfireItemWriter}. * * @return a {@link GemfireItemWriter} */ public GemfireItemWriter<K, V> build() { Assert.notNull(this.template, "template is required."); Assert.notNull(this.itemKeyMapper, "itemKeyMapper is required."); GemfireItemWriter<K, V> writer = new GemfireItemWriter<>(); writer.setTemplate(this.template); writer.setItemKeyMapper(this.itemKeyMapper); writer.setDelete(this.delete); return writer; } }
@Test public void testBasicWrite() throws Exception { GemfireItemWriter<String, GemfireItemWriterBuilderTests.Foo> writer = new GemfireItemWriterBuilder<String, GemfireItemWriterBuilderTests.Foo>() .template(this.template).itemKeyMapper(this.itemKeyMapper).build(); writer.write(this.items); verify(this.template).put("val1", items.get(0)); verify(this.template).put("val2", items.get(1)); verify(this.template, never()).remove("val1"); verify(this.template, never()).remove("val2"); }
@Test public void testWriteWithCustomItemKeyMapper() throws Exception { List<Foo> items = new ArrayList<Foo>() { { add(new Foo(new Bar("val1"))); add(new Foo(new Bar("val2"))); } }; writer = new GemfireItemWriter<>(); writer.setTemplate(template); writer.setItemKeyMapper(new Converter<Foo, String>() { @Override public String convert(Foo item) { String index = item.bar.val.replaceAll("val", ""); return "item" + index; } }); writer.afterPropertiesSet(); writer.write(items); verify(template).put("item1", items.get(0)); verify(template).put("item2", items.get(1)); }
@Test public void testBasicDelete() throws Exception { GemfireItemWriter<String, GemfireItemWriterBuilderTests.Foo> writer = new GemfireItemWriterBuilder<String, GemfireItemWriterBuilderTests.Foo>() .template(this.template).delete(true).itemKeyMapper(this.itemKeyMapper).build(); writer.write(this.items); verify(this.template).remove("val1"); verify(this.template).remove("val2"); verify(this.template, never()).put("val1", items.get(0)); verify(this.template, never()).put("val2", items.get(1)); }
@Test public void testBasicWrite() throws Exception { List<Foo> items = new ArrayList<Foo>() { { add(new Foo(new Bar("val1"))); add(new Foo(new Bar("val2"))); } }; writer.write(items); verify(template).put("val1", items.get(0)); verify(template).put("val2", items.get(1)); }