Added builder for GemfireItemWriter

resolves BATCH-2614
This commit is contained in:
Glenn Renfro
2017-05-17 17:11:46 -04:00
committed by Michael Minella
parent 6219de0473
commit 8dd7d1e880
3 changed files with 220 additions and 2 deletions

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.item.data.builder;
import org.springframework.batch.item.data.GemfireItemWriter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.util.Assert;
/**
* A builder implementation for the {@link GemfireItemWriter}
*
* @author Glenn Renfro
* @since 4.0
* @see GemfireItemWriterBuilder
*/
public class GemfireItemWriterBuilder<K, V> {
private GemfireTemplate template;
private Converter<V, K> itemKeyMapper;
private boolean delete;
/**
* Establishes the GemfireTemplate the writer should use.
* @param template the {@link GemfireTemplate} to set.
* @return The current instance of the builder.
* @see GemfireItemWriter#setTemplate(GemfireTemplate)
*/
public GemfireItemWriterBuilder<K, V> template(GemfireTemplate template) {
this.template = template;
return this;
}
/**
* Set the {@link Converter} to use to derive the key from the item.
*
* @param itemKeyMapper the Converter to use.
* @return The current instance of the builder.
* @see GemfireItemWriter#setItemKeyMapper(Converter)
*/
public GemfireItemWriterBuilder<K, V> itemKeyMapper(Converter<V, K> itemKeyMapper) {
this.itemKeyMapper = itemKeyMapper;
return this;
}
/**
* Indicates if the items being passed to the writer are to be saved or removed from
* the data store. If set to false (default), the items will be saved. If set to true,
* the items will be removed.
*
* @param delete removal indicator.
* @return The current instance of the builder.
* @see GemfireItemWriter#setDelete(boolean)
*/
public GemfireItemWriterBuilder<K, V> delete(boolean delete) {
this.delete = delete;
return this;
}
/**
* 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;
}
}

View File

@@ -37,8 +37,6 @@ public class GemfireItemWriterTests {
@Mock
private GemfireTemplate template;
//private PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

View File

@@ -0,0 +1,125 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.item.data.builder;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.batch.item.SpELItemKeyMapper;
import org.springframework.batch.item.data.GemfireItemWriter;
import org.springframework.data.gemfire.GemfireTemplate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/**
* @author Glenn Renfro
*/
public class GemfireItemWriterBuilderTests {
@Mock
private GemfireTemplate template;
private SpELItemKeyMapper<String, GemfireItemWriterBuilderTests.Foo> itemKeyMapper;
private List<GemfireItemWriterBuilderTests.Foo> items;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.items = new ArrayList<GemfireItemWriterBuilderTests.Foo>() {
{
add(new GemfireItemWriterBuilderTests.Foo(new GemfireItemWriterBuilderTests.Bar("val1")));
add(new GemfireItemWriterBuilderTests.Foo(new GemfireItemWriterBuilderTests.Bar("val2")));
}
};
this.itemKeyMapper = new SpELItemKeyMapper<>("bar.val");
}
@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 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 testNullTemplate() {
try {
new GemfireItemWriterBuilder<String, GemfireItemWriterBuilderTests.Foo>().itemKeyMapper(this.itemKeyMapper)
.build();
fail("IllegalArgumentException should have been thrown");
}
catch (IllegalArgumentException iae) {
assertEquals("IllegalArgumentException message did not match the expected result.", "template is required.",
iae.getMessage());
}
}
@Test
public void testNullItemKeyMapper() {
try {
new GemfireItemWriterBuilder<String, GemfireItemWriterBuilderTests.Foo>().template(this.template).build();
fail("IllegalArgumentException should have been thrown");
}
catch (IllegalArgumentException iae) {
assertEquals("IllegalArgumentException message did not match the expected result.",
"itemKeyMapper is required.", iae.getMessage());
}
}
static class Foo {
public GemfireItemWriterBuilderTests.Bar bar;
public Foo(GemfireItemWriterBuilderTests.Bar bar) {
this.bar = bar;
}
}
static class Bar {
public String val;
public Bar(String b1) {
this.val = b1;
}
}
}