From f3bce8b198384e32c84eefd09205df08ee8d2478 Mon Sep 17 00:00:00 2001 From: Cael Date: Wed, 21 Nov 2018 18:25:43 +0000 Subject: [PATCH] adding nullify method to set null a field (#16) --- .../caeldev/builder4test/ElementBuilder.java | 5 ++++ .../caeldev/builder4test/EntityBuilder.java | 5 ++++ .../co/caeldev/builder4test/BuilderTest.java | 14 +++++++++- .../builder4test/ElementBuilderTest.java | 17 +++++++++++ .../builder4test/EntityBuilderTest.java | 28 +++++++++++++++++++ .../FixedSizeListBuilderTest.java | 6 ++++ 6 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java b/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java index a7d3cb5..9cfbf40 100644 --- a/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java @@ -26,6 +26,11 @@ public ElementBuilder override(Field field, U value) { return this; } + public ElementBuilder nullify(Field field) { + this.fields.put(field, Optional.empty()); + return this; + } + public ElementListBuilder end() { return elementListBuilder; } diff --git a/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java b/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java index d1cdbcb..0523e78 100644 --- a/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java @@ -40,6 +40,11 @@ public EntityBuilder override(Field field, V value) { return this; } + public EntityBuilder nullify(Field field) { + lookUp.put(field, null); + return this; + } + public K get() { return creator.build(lookUp); } diff --git a/src/test/java/uk/co/caeldev/builder4test/BuilderTest.java b/src/test/java/uk/co/caeldev/builder4test/BuilderTest.java index 37c4413..56bf7d0 100644 --- a/src/test/java/uk/co/caeldev/builder4test/BuilderTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/BuilderTest.java @@ -1,5 +1,6 @@ package uk.co.caeldev.builder4test; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import uk.co.caeldev.builder4test.impl.PojoBuilder; @@ -9,6 +10,7 @@ class BuilderTest { @Test + @DisplayName("Should be abe to create to different instances of Builder") public void shouldReturnANewInstanceOfBuilder() { Builder instance1 = build(); Builder instance2 = build(); @@ -17,11 +19,21 @@ public void shouldReturnANewInstanceOfBuilder() { } @Test - public void shouldUseDefaultEntity() { + @DisplayName("Should be able to build two different instances of the EntityBuilder") + public void shouldBeAbleBuildDifferentInstancesOfEntityBuilder() { EntityBuilder entityBuilder = build().entity(PojoBuilder.creator); EntityBuilder entityBuilder2 = build().entity(PojoBuilder.creator); assertThat(entityBuilder).isNotEqualTo(entityBuilder2); } + @Test + @DisplayName("Should be able to build two different instances of the ListBuilder") + public void shouldBeAbleBuildDifferentInstancesOfListBuilder() { + ListBuilder listBuilder = build().list(PojoBuilder.creator); + ListBuilder listBuilder2 = build().list(PojoBuilder.creator); + + assertThat(listBuilder).isNotEqualTo(listBuilder2); + } + } \ No newline at end of file diff --git a/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java b/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java index 214f1fa..939aff6 100644 --- a/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java @@ -65,6 +65,23 @@ public void shouldGoBackToElementsListBuilder() { assertThat(elementListBuilder1).isEqualTo(elementListBuilder); } + @Test + @DisplayName("Should be able to nullify a field by not passing the null value") + public void shouldBeAbleToNullifyAField() { + //Given + Field field = new Field<>("default"); + ElementListBuilder elementListBuilder = elementListBuilder(PojoBuilder.creator); + + //When + ElementBuilder elementBuilder = ElementBuilder.elementBuilder(elementListBuilder); + ElementListBuilder elementListBuilder1 = elementBuilder.nullify(field).end(); + + //Then + assertThat(elementBuilder.getFields()).hasSize(1); + assertThat(elementBuilder.getFields().get(field)).isNotPresent(); + assertThat(elementListBuilder1).isEqualTo(elementListBuilder); + } + } \ No newline at end of file diff --git a/src/test/java/uk/co/caeldev/builder4test/EntityBuilderTest.java b/src/test/java/uk/co/caeldev/builder4test/EntityBuilderTest.java index 6e365f2..202ac61 100644 --- a/src/test/java/uk/co/caeldev/builder4test/EntityBuilderTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/EntityBuilderTest.java @@ -1,5 +1,6 @@ package uk.co.caeldev.builder4test; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import uk.co.caeldev.builder4test.impl.Pojo; import uk.co.caeldev.builder4test.impl.PojoBuilder; @@ -9,6 +10,7 @@ class EntityBuilderTest { @Test + @DisplayName("Should build different instances of EntityBuilder") public void shouldBuild() { EntityBuilder entityBuilder = EntityBuilder.entityBuilder(PojoBuilder.creator); EntityBuilder entityBuilder2 = EntityBuilder.entityBuilder(PojoBuilder.creator); @@ -17,6 +19,7 @@ public void shouldBuild() { } @Test + @DisplayName("Should build entity using the default values") public void shouldGetEntityUsingDefaultValues() { Pojo pojo = EntityBuilder.entityBuilder(PojoBuilder.creator).get(); @@ -25,6 +28,7 @@ public void shouldGetEntityUsingDefaultValues() { } @Test + @DisplayName("Should be able to override the default values") public void shouldBindValueAndNotUseDefault() { Pojo pojo = EntityBuilder.entityBuilder(PojoBuilder.creator) .override(PojoBuilder.name, "newNAme") @@ -34,4 +38,28 @@ public void shouldBindValueAndNotUseDefault() { assertThat(pojo.getName()).isEqualTo("newNAme"); assertThat(pojo.getValue()).isEqualTo("newValue"); } + + @Test + @DisplayName("Should be able to set null to a field") + public void shouldBindNullValues() { + Pojo pojo = EntityBuilder.entityBuilder(PojoBuilder.creator) + .override(PojoBuilder.name, null) + .override(PojoBuilder.value, "defaultValue") + .get(); + + assertThat(pojo.getName()).isNull(); + assertThat(pojo.getValue()).isNotNull(); + } + + @Test + @DisplayName("Should be able to set null to a field using nullify method") + public void shouldBindNullValuesUsingNullifyMethod() { + Pojo pojo = EntityBuilder.entityBuilder(PojoBuilder.creator) + .nullify(PojoBuilder.name) + .override(PojoBuilder.value, "defaultValue") + .get(); + + assertThat(pojo.getName()).isNull(); + assertThat(pojo.getValue()).isNotNull(); + } } \ No newline at end of file diff --git a/src/test/java/uk/co/caeldev/builder4test/FixedSizeListBuilderTest.java b/src/test/java/uk/co/caeldev/builder4test/FixedSizeListBuilderTest.java index e397080..36c4fde 100644 --- a/src/test/java/uk/co/caeldev/builder4test/FixedSizeListBuilderTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/FixedSizeListBuilderTest.java @@ -1,5 +1,6 @@ package uk.co.caeldev.builder4test; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import uk.co.caeldev.builder4test.impl.Pojo; @@ -12,6 +13,7 @@ class FixedSizeListBuilderTest { @Test + @DisplayName("Should be able to build distinct instances of FixedSizeListBuilder") public void shouldReturnUniqueInstances() { //When FixedSizeListBuilder builder1 = FixedSizeListBuilder.fixedSizeListBuilder(1, creator); @@ -22,6 +24,7 @@ public void shouldReturnUniqueInstances() { } @Test + @DisplayName("Should be able to build a list using defaults values") public void shouldBuildAListWithDefaultValuesNoRandoms() { //When FixedSizeListBuilder builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator); @@ -36,6 +39,7 @@ public void shouldBuildAListWithDefaultValuesNoRandoms() { } @Test + @DisplayName("Should be able to build a list overriding default values") public void shouldBuildAListWithOverrideConstantValues() { //When FixedSizeListBuilder builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator) @@ -51,6 +55,7 @@ public void shouldBuildAListWithOverrideConstantValues() { } @Test + @DisplayName("Should be able to build a list overriding default with constant and random values") public void shouldBuildAListWithOverrideConstantAndRandomValues() { //When FixedSizeListBuilder builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator) @@ -67,6 +72,7 @@ public void shouldBuildAListWithOverrideConstantAndRandomValues() { } @Test + @DisplayName("Should be able to build a list overriding default with only random values") public void shouldBuildAListWithOverrideRandomValues() { //When FixedSizeListBuilder builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator)