Skip to content

Commit

Permalink
adding nullify method to set null a field (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cael authored Nov 21, 2018
1 parent fee08e5 commit f3bce8b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public <U> ElementBuilder<K> override(Field<U> field, U value) {
return this;
}

public <U> ElementBuilder<K> nullify(Field<U> field) {
this.fields.put(field, Optional.empty());
return this;
}

public ElementListBuilder<K> end() {
return elementListBuilder;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public <V> EntityBuilder<K> override(Field<V> field, V value) {
return this;
}

public <V> EntityBuilder<K> nullify(Field<V> field) {
lookUp.put(field, null);
return this;
}

public K get() {
return creator.build(lookUp);
}
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/uk/co/caeldev/builder4test/BuilderTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
Expand All @@ -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);
}

}
17 changes: 17 additions & 0 deletions src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> field = new Field<>("default");
ElementListBuilder<Pojo> elementListBuilder = elementListBuilder(PojoBuilder.creator);

//When
ElementBuilder<Pojo> elementBuilder = ElementBuilder.elementBuilder(elementListBuilder);
ElementListBuilder<Pojo> elementListBuilder1 = elementBuilder.nullify(field).end();

//Then
assertThat(elementBuilder.getFields()).hasSize(1);
assertThat(elementBuilder.getFields().get(field)).isNotPresent();
assertThat(elementListBuilder1).isEqualTo(elementListBuilder);
}



}
28 changes: 28 additions & 0 deletions src/test/java/uk/co/caeldev/builder4test/EntityBuilderTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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")
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,6 +13,7 @@
class FixedSizeListBuilderTest {

@Test
@DisplayName("Should be able to build distinct instances of FixedSizeListBuilder")
public void shouldReturnUniqueInstances() {
//When
FixedSizeListBuilder<Pojo> builder1 = FixedSizeListBuilder.fixedSizeListBuilder(1, creator);
Expand All @@ -22,6 +24,7 @@ public void shouldReturnUniqueInstances() {
}

@Test
@DisplayName("Should be able to build a list using defaults values")
public void shouldBuildAListWithDefaultValuesNoRandoms() {
//When
FixedSizeListBuilder<Pojo> builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator);
Expand All @@ -36,6 +39,7 @@ public void shouldBuildAListWithDefaultValuesNoRandoms() {
}

@Test
@DisplayName("Should be able to build a list overriding default values")
public void shouldBuildAListWithOverrideConstantValues() {
//When
FixedSizeListBuilder<Pojo> builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator)
Expand All @@ -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<Pojo> builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator)
Expand All @@ -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<Pojo> builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator)
Expand Down

0 comments on commit f3bce8b

Please sign in to comment.