diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/Neo4jItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/Neo4jItemWriter.java index 2a489c992..b628f8349 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/Neo4jItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/Neo4jItemWriter.java @@ -39,6 +39,7 @@ import org.springframework.util.CollectionUtils; *

* * @author Michael Minella + * @author Glenn Renfro * */ public class Neo4jItemWriter implements ItemWriter, InitializingBean { @@ -50,10 +51,21 @@ public class Neo4jItemWriter implements ItemWriter, InitializingBean { private SessionFactory sessionFactory; + /** + * Boolean flag indicating whether the writer should save or delete the item at write + * time. + * @param delete true if write should delete item, false if item should be saved. + * Default is false. + */ public void setDelete(boolean delete) { this.delete = delete; } + /** + * Establish the session factory that will be used to create {@link Session} instances + * for interacting with Neo4j. + * @param sessionFactory sessionFactory to be used. + */ public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/Neo4jItemWriterBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/Neo4jItemWriterBuilder.java new file mode 100644 index 000000000..609fe3f83 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/Neo4jItemWriterBuilder.java @@ -0,0 +1,77 @@ +/* + * 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.neo4j.ogm.session.Session; +import org.neo4j.ogm.session.SessionFactory; + +import org.springframework.batch.item.data.Neo4jItemWriter; +import org.springframework.util.Assert; + +/** + * A builder implementation for the {@link Neo4jItemWriter} + * + * @author Glenn Renfro + * @since 4.0 + * @see Neo4jItemWriter + */ +public class Neo4jItemWriterBuilder { + + private boolean delete = false; + + private SessionFactory sessionFactory; + + /** + * Boolean flag indicating whether the writer should save or delete the item at write + * time. + * @param delete true if write should delete item, false if item should be saved. + * Default is false. + * @return The current instance of the builder + * @see Neo4jItemWriter#setDelete(boolean) + */ + public Neo4jItemWriterBuilder delete(boolean delete) { + this.delete = delete; + + return this; + } + + /** + * Establish the session factory that will be used to create {@link Session} instances + * for interacting with Neo4j. + * @param sessionFactory sessionFactory to be used. + * @return The current instance of the builder + * @see Neo4jItemWriter#setSessionFactory(SessionFactory) + */ + public Neo4jItemWriterBuilder sessionFactory(SessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + + return this; + } + + /** + * Validates and builds a {@link org.springframework.batch.item.data.Neo4jItemWriter}. + * + * @return a {@link Neo4jItemWriter} + */ + public Neo4jItemWriter build() { + Assert.notNull(sessionFactory, "sessionFactory is required."); + Neo4jItemWriter writer = new Neo4jItemWriter<>(); + writer.setDelete(this.delete); + writer.setSessionFactory(this.sessionFactory); + return writer; + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/Neo4jItemWriterBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/Neo4jItemWriterBuilderTests.java new file mode 100644 index 000000000..1931557ef --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/Neo4jItemWriterBuilderTests.java @@ -0,0 +1,94 @@ +/* + * 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.neo4j.ogm.session.Session; +import org.neo4j.ogm.session.SessionFactory; + +import org.springframework.batch.item.data.Neo4jItemWriter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * @author Glenn Renfro + */ +public class Neo4jItemWriterBuilderTests { + + @Mock + private SessionFactory sessionFactory; + @Mock + private Session session; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testBasicWriter() throws Exception{ + Neo4jItemWriter writer = new Neo4jItemWriterBuilder().sessionFactory(this.sessionFactory).build(); + List items = new ArrayList<>(); + items.add("foo"); + items.add("bar"); + + when(this.sessionFactory.openSession()).thenReturn(this.session); + writer.write(items); + + verify(this.session).save("foo"); + verify(this.session).save("bar"); + verify(this.session, never()).delete("foo"); + verify(this.session, never()).delete("bar"); + } + + @Test + public void testBasicDelete() throws Exception{ + Neo4jItemWriter writer = new Neo4jItemWriterBuilder().delete(true).sessionFactory(this.sessionFactory).build(); + List items = new ArrayList<>(); + items.add("foo"); + items.add("bar"); + + when(this.sessionFactory.openSession()).thenReturn(this.session); + writer.write(items); + + verify(this.session).delete("foo"); + verify(this.session).delete("bar"); + verify(this.session, never()).save("foo"); + verify(this.session, never()).save("bar"); + } + + @Test + public void testNoSessionFactory() { + try { + new Neo4jItemWriterBuilder().build(); + fail("SessionFactory was not set but exception was not thrown."); + } catch (IllegalArgumentException iae) { + assertEquals("sessionFactory is required.", iae.getMessage()); + } + } + +}