diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilder.java new file mode 100644 index 000000000..1a79bb249 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilder.java @@ -0,0 +1,96 @@ +/* + * 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.MongoItemWriter; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.util.Assert; + +/** + * A builder implementation for the {@link MongoItemWriter} + * + * @author Glenn Renfro + * @since 4.0 + * @see MongoItemWriter + */ +public class MongoItemWriterBuilder { + + private MongoOperations template; + + private String collection; + + private boolean delete = false; + + /** + * 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 MongoItemWriter#setDelete(boolean) + */ + public MongoItemWriterBuilder delete(boolean delete) { + this.delete = delete; + + return this; + } + + /** + * Set the {@link MongoOperations} to be used to save items to be written. + * + * @param template the template implementation to be used. + * @return The current instance of the builder + * @see MongoItemWriter#setTemplate(MongoOperations) + */ + public MongoItemWriterBuilder template(MongoOperations template) { + this.template = template; + + return this; + } + + /** + * Set the name of the Mongo collection to be written to. + * + * @param collection the name of the collection. + * @return The current instance of the builder + * @see MongoItemWriter#setCollection(String) + * + */ + public MongoItemWriterBuilder collection(String collection) { + this.collection = collection; + + return this; + } + + /** + * Validates and builds a {@link MongoItemWriter}. + * + * @return a {@link MongoItemWriter} + */ + public MongoItemWriter build() { + Assert.notNull(this.template, "template is required."); + + MongoItemWriter writer = new MongoItemWriter<>(); + writer.setTemplate(this.template); + writer.setDelete(this.delete); + writer.setCollection(this.collection); + + return writer; + } + +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilderTests.java new file mode 100644 index 000000000..579f72450 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilderTests.java @@ -0,0 +1,103 @@ +/* + * 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.data.MongoItemWriter; +import org.springframework.data.mongodb.core.MongoOperations; + +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 MongoItemWriterBuilderTests { + @Mock + private MongoOperations template; + + private List items; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + this.items = new ArrayList() { + { + add("foo"); + add("bar"); + } + }; + } + + @Test + public void testBasicWrite() throws Exception { + MongoItemWriter writer = new MongoItemWriterBuilder().template(this.template).build(); + writer.write(this.items); + + verify(this.template).save(this.items.get(0)); + verify(this.template).save(this.items.get(1)); + verify(this.template, never()).remove(this.items.get(0)); + verify(this.template, never()).remove(this.items.get(1)); + } + + @Test + public void testDelete() throws Exception { + MongoItemWriter writer = new MongoItemWriterBuilder().template(this.template).delete(true) + .build(); + + writer.write(this.items); + + verify(this.template).remove(this.items.get(0)); + verify(this.template).remove(this.items.get(1)); + verify(this.template, never()).save(this.items.get(0)); + verify(this.template, never()).save(this.items.get(1)); + } + + @Test + public void testWriteToCollection() throws Exception { + MongoItemWriter writer = new MongoItemWriterBuilder().collection("collection") + .template(this.template).build(); + + writer.write(this.items); + + verify(this.template).save(this.items.get(0), "collection"); + verify(this.template).save(this.items.get(1), "collection"); + verify(this.template, never()).remove(this.items.get(0), "collection"); + verify(this.template, never()).remove(this.items.get(1), "collection"); + } + + @Test + public void testNullTemplate() { + try { + new MongoItemWriterBuilder<>().build(); + fail("IllegalArgumentException should have been thrown"); + } + catch (IllegalArgumentException iae) { + assertEquals("IllegalArgumentException message did not match the expected result.", "template is required.", + iae.getMessage()); + } + } +}