From 9baa7a8d4bab98937b4d2576c346ff99f3617ebf Mon Sep 17 00:00:00 2001 From: Cael Date: Sat, 16 Mar 2019 15:57:59 +0000 Subject: [PATCH] [#14] Adding support for default creators as value and override (#17) --- README.md | 51 ++++++++++- build.gradle | 3 + .../caeldev/builder4test/ElementBuilder.java | 9 +- .../caeldev/builder4test/EntityBuilder.java | 8 +- .../builder4test/FixedSizeListBuilder.java | 9 +- .../uk/co/caeldev/builder4test/LookUp.java | 4 + .../caeldev/builder4test/OverrideField.java | 8 ++ .../integration/BuilderIntegrationTest.java | 88 +++++++++++++++++-- .../builder4test/DefaultLookUpTest.java | 13 --- .../builder4test/ElementBuilderTest.java | 19 ++++ .../builder4test/EntityBuilderTest.java | 15 +++- .../FixedSizeListBuilderTest.java | 23 ++++- .../co/caeldev/builder4test/LookUpTest.java | 49 +++++++++++ .../builder4test/impl/PojoBuilder.java | 14 ++- 14 files changed, 285 insertions(+), 28 deletions(-) create mode 100644 src/main/java/uk/co/caeldev/builder4test/OverrideField.java create mode 100644 src/test/java/uk/co/caeldev/builder4test/LookUpTest.java diff --git a/README.md b/README.md index 0d9bffa..6660095 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,53 @@ Pojo pojo = Builder.build() .get(); ``` +You can nullify a field by just using nullify Method from DSL. + +```java +Pojo pojo = Builder.build() + .entity(PojoBuilder.creator) + .override(PojoBuilder.name, "nameoverrideed") + .nullify(PojoBuilder.value) + .get(); +``` + +## Reuse amd composition of Creators + +if you have already creators and you want to reuse them on other creator, you can achieve by: + +- Setting your existing creator as a default value when you define your new creator, note that you can even override the values of the creator that you are using as default. + +```java + public static Field name = new Field<>(); + public static Creator nameCreator = lookUp -> lookUp.get(name, "test1"); + + public static Field secondName = new Field<>(); + public static Creator creator = lookUp -> lookUp.get(secondName, secondCreator); + + Pojo pojo = Builder.build() + .entity(creator) + .override(name, "test2") + .get(); +``` + +- The other way is by overriding a field using a creator as value: + +```java + +public static Field secondName = new Field<>(); +public static Creator secondCreator = lookUp -> lookUp.get(secondName, "test1"); + +Pojo pojo = Builder.build() + .entity(PojoBuilder.creator) + .override(PojoBuilder.name, "nameoverrideed") + .override(PojoBuilder.secondName, "secondName") + .override(PojoBuilder.value, secondCreator) + .get(); +``` + +- You can also from there override a field of secondCreator but you have to override those before using the secondCreator. + + ## Build a list of entities As easy as is creating an entity with Builder4Test, just use list method from the DSL and add as many elements to the collection as you want. For each element you can override all the fields. @@ -102,7 +149,9 @@ List testSimple = Builder.build() .get(); ``` This code will generate a List of five elements and each element will contain a random value and field. -Using defaults generator provided by Fyodor is easy to generate your random values. +Using defaults generator provided by Fyodor is easy to generate your random values. + +__Note:__ that you can use creators as default values in your collections. ## Credits The library is highly inspired by diff --git a/build.gradle b/build.gradle index cd912a0..92453c5 100644 --- a/build.gradle +++ b/build.gradle @@ -32,6 +32,9 @@ dependencies { testImplementation "org.assertj:assertj-core:3.10.0" + testImplementation "org.mockito:mockito-junit-jupiter:2.25.0" + + testRuntime "org.junit.jupiter:junit-jupiter-engine:5.2.0" } diff --git a/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java b/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java index 9cfbf40..ce71c31 100644 --- a/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java @@ -4,7 +4,8 @@ import java.util.Map; import java.util.Optional; -public class ElementBuilder { +public class ElementBuilder implements OverrideField> { + private final ElementListBuilder elementListBuilder; private final Map fields; @@ -21,11 +22,17 @@ protected Map getFields() { return this.fields; } + @Override public ElementBuilder override(Field field, U value) { this.fields.put(field, Optional.ofNullable(value)); return this; } + @Override + public ElementBuilder override(Field field, Creator creator) { + return override(field, creator.build(new DefaultLookUp(fields))); + } + public ElementBuilder nullify(Field field) { this.fields.put(field, Optional.empty()); return this; diff --git a/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java b/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java index 0523e78..5197aa1 100644 --- a/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java @@ -3,7 +3,7 @@ import java.util.Map; import java.util.Optional; -public class EntityBuilder { +public class EntityBuilder implements OverrideField> { private final Creator creator; private final LookUp lookUp; @@ -35,11 +35,17 @@ protected static EntityBuilder entityBuilder(Creator Creator, LookUp l return new EntityBuilder<>(Creator, lookUp); } + @Override public EntityBuilder override(Field field, V value) { lookUp.put(field, value); return this; } + @Override + public EntityBuilder override(Field field, Creator creator) { + return override(field, creator.build(lookUp)); + } + public EntityBuilder nullify(Field field) { lookUp.put(field, null); return this; diff --git a/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java b/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java index e8db29d..72ef7f9 100644 --- a/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java @@ -9,7 +9,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; -public class FixedSizeListBuilder { +public class FixedSizeListBuilder implements OverrideField>{ private final int size; private final Creator creator; @@ -33,11 +33,18 @@ public FixedSizeListBuilder override(Field field, Generator generat return this; } + @Override public FixedSizeListBuilder override(Field field, U value) { values.put(field, Optional.of(value)); return this; } + @Override + public FixedSizeListBuilder override(Field field, Creator creator) { + override(field, creator.build(new DefaultLookUp(values))); + return this; + } + public List get() { LookUp lookUp = new RandomLookUp(values, generators); return IntStream.rangeClosed(1, size) diff --git a/src/main/java/uk/co/caeldev/builder4test/LookUp.java b/src/main/java/uk/co/caeldev/builder4test/LookUp.java index 6277176..255fd58 100644 --- a/src/main/java/uk/co/caeldev/builder4test/LookUp.java +++ b/src/main/java/uk/co/caeldev/builder4test/LookUp.java @@ -8,4 +8,8 @@ public abstract class LookUp { public abstract V get(Field field, V defaultValue); + public V get(Field field, Creator defaultValue) { + return get(field, defaultValue.build(this)); + } + } diff --git a/src/main/java/uk/co/caeldev/builder4test/OverrideField.java b/src/main/java/uk/co/caeldev/builder4test/OverrideField.java new file mode 100644 index 0000000..c53c1fa --- /dev/null +++ b/src/main/java/uk/co/caeldev/builder4test/OverrideField.java @@ -0,0 +1,8 @@ +package uk.co.caeldev.builder4test; + +public interface OverrideField { + + L override(Field field, Creator creator); + L override(Field field, U value); + +} diff --git a/src/test/java/integration/BuilderIntegrationTest.java b/src/test/java/integration/BuilderIntegrationTest.java index 88f5a16..e7f3b4f 100644 --- a/src/test/java/integration/BuilderIntegrationTest.java +++ b/src/test/java/integration/BuilderIntegrationTest.java @@ -93,14 +93,45 @@ public void shouldOverrideDefaultValuesFromFieldInstantiation() { assertThat(pojo1.getValue()).isEqualTo("overridedValue"); } + + @Test + @DisplayName("should build a pojo successfully using another creators available") + public void shouldOverrideDefaultValuesFromFieldInstantiationUsingAnotherCreator() { + //When + Pojo pojo1 = Builder.build() + .entity(creatorWithPredefinedDefaults) + .override(name2, valueCreator) + .override(value2, "overridedValue") + .get(); + + //Then + assertThat(pojo1.getName()).isEqualTo("test1"); + assertThat(pojo1.getValue()).isEqualTo("overridedValue"); + } + + @Test + @DisplayName("should build a pojo by overriding using a creator and overriding a value of it") + public void shouldOverrideDefaultValuesFromFieldInstantiationUsingAnotherCreator2() { + //When + Pojo pojo1 = Builder.build() + .entity(creatorWithPredefinedCreatorDefaults) + .override(testValue, "overridedValue1") + .override(name2, valueTestCreator) + .get(); + + //Then + assertThat(pojo1.getName()).isEqualTo("overridedValue1"); + assertThat(pojo1.getValue()).isEqualTo("defaultValue"); + } + @Test @DisplayName("should build a pojo successfully setting nulls as values") public void shouldOverrideWithNulls() { //When Pojo pojo = Builder.build() .entity(creator) - .override(name, null) - .override(value, null) + .nullify(name) + .nullify(value) .get(); //Then @@ -120,10 +151,10 @@ public void shouldBuildAListOfTwoEntities() { .list(creator) .elements() .element() - .override(name, "testSiumple") + .override(name, "testSimple") .end() .element() - .override(name, "testSiumple2") + .override(name, "testSimple2") .end() .get(); @@ -133,12 +164,12 @@ public void shouldBuildAListOfTwoEntities() { //And Pojo pojo = testSiumple.get(0); - assertThat(pojo.getName()).isEqualTo("testSiumple"); + assertThat(pojo.getName()).isEqualTo("testSimple"); assertThat(pojo.getValue()).isEqualTo("defaultValue"); //And Pojo pojo1 = testSiumple.get(1); - assertThat(pojo1.getName()).isEqualTo("testSiumple2"); + assertThat(pojo1.getName()).isEqualTo("testSimple2"); assertThat(pojo1.getValue()).isEqualTo("defaultValue"); } @@ -164,6 +195,51 @@ public void shouldBuildAListOfTwoUsingGenerators() { assertThat(testSimple.get(0).getValue()).isNotEqualTo(testSimple.get(1).getValue()); } + @Test + @DisplayName("should build a list of two elements overriding defaults values with creators using size") + public void shouldBuildAListOfTwoUsingCreatorAndSize() { + //Given + int size = 2; + + //When + List testSimple = Builder.build() + .list(creator) + .size(size) + .override(name, valueCreator) + .override(value, valueCreator) + .get(); + + //Then + assertThat(testSimple).isNotEmpty(); + assertThat(testSimple).hasSize(size); + + assertThat(testSimple.get(0).getName()).isEqualTo(testSimple.get(1).getName()); + assertThat(testSimple.get(0).getValue()).isEqualTo(testSimple.get(1).getValue()); + assertThat(testSimple.get(0).getName()).isEqualTo("test1"); + assertThat(testSimple.get(0).getValue()).isEqualTo("test1"); + } + + @Test + @DisplayName("should build a list of one element overriding defaults values with creators") + public void shouldBuildAListOfTwoUsingCreators() { + //When + List testSimple = Builder.build() + .list(creator) + .elements() + .element() + .override(name, valueCreator).end() + .element() + .override(value, valueCreator).end() + .get(); + + //Then + assertThat(testSimple).isNotEmpty(); + assertThat(testSimple).hasSize(2); + + assertThat(testSimple.get(0).getName()).isEqualTo("test1"); + assertThat(testSimple.get(1).getValue()).isEqualTo("test1"); + } + @Test @DisplayName("should build a list of one elements using defaults values when there is no size or elements definitions") public void shouldBuildAListSizeOneWithNoSizeAndNoElementsDefinitions() { diff --git a/src/test/java/uk/co/caeldev/builder4test/DefaultLookUpTest.java b/src/test/java/uk/co/caeldev/builder4test/DefaultLookUpTest.java index f6dfb82..f005f6d 100644 --- a/src/test/java/uk/co/caeldev/builder4test/DefaultLookUpTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/DefaultLookUpTest.java @@ -74,19 +74,6 @@ public void shouldGetNullWhenItHasBeenOverrideWithNull() { assertThat(value).isNull(); } - @Test - @DisplayName("Should retrieve null when there default is null") - public void shouldGetNullWhenThereIsNoValueAndDefaultValueIsNull() { - //Given - Field field = new Field<>(); - - //When - String value = defaultLookUp.get(field, null); - - //Then - assertThat(value).isNull(); - } - @Test @DisplayName("Should retrieve default value declared by using field constructor") public void shouldGetDefaultWhenItUseFieldDefaultValue() { diff --git a/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java b/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java index 939aff6..698185a 100644 --- a/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java @@ -82,6 +82,25 @@ public void shouldBeAbleToNullifyAField() { assertThat(elementListBuilder1).isEqualTo(elementListBuilder); } + @Test + @DisplayName("Should be able to use a creator as default value for a field") + public void shouldBeAbleToUseCreatorAsDefaultValueForAField() { + //Given + Field field = new Field<>("default"); + ElementListBuilder elementListBuilder = elementListBuilder(PojoBuilder.creator); + + + //When + ElementBuilder elementBuilder = ElementBuilder.elementBuilder(elementListBuilder); + ElementListBuilder elementListBuilder1 = elementBuilder.override(field, PojoBuilder.valueCreator).end(); + + //Then + assertThat(elementBuilder.getFields()).hasSize(1); + assertThat(elementBuilder.getFields().get(field)).isPresent(); + assertThat(elementBuilder.getFields().get(field).get()).isEqualTo("test1"); + 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 202ac61..52757b6 100644 --- a/src/test/java/uk/co/caeldev/builder4test/EntityBuilderTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/EntityBuilderTest.java @@ -43,7 +43,7 @@ public void shouldBindValueAndNotUseDefault() { @DisplayName("Should be able to set null to a field") public void shouldBindNullValues() { Pojo pojo = EntityBuilder.entityBuilder(PojoBuilder.creator) - .override(PojoBuilder.name, null) + .nullify(PojoBuilder.name) .override(PojoBuilder.value, "defaultValue") .get(); @@ -62,4 +62,17 @@ public void shouldBindNullValuesUsingNullifyMethod() { assertThat(pojo.getName()).isNull(); assertThat(pojo.getValue()).isNotNull(); } + + @Test + @DisplayName("Should be able to a creator as a value of the field") + public void shouldBeAbleToSetACreatorAsValue() { + Pojo pojo = EntityBuilder.entityBuilder(PojoBuilder.creator) + .nullify(PojoBuilder.name) + .override(PojoBuilder.value, PojoBuilder.valueCreator) + .get(); + + assertThat(pojo.getName()).isNull(); + assertThat(pojo.getValue()).isNotNull(); + assertThat(pojo.getValue()).isEqualTo("test1"); + } } \ 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 36c4fde..046c8bd 100644 --- a/src/test/java/uk/co/caeldev/builder4test/FixedSizeListBuilderTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/FixedSizeListBuilderTest.java @@ -7,7 +7,10 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -import static uk.co.caeldev.builder4test.impl.PojoBuilder.*; +import static uk.co.caeldev.builder4test.impl.PojoBuilder.creator; +import static uk.co.caeldev.builder4test.impl.PojoBuilder.name; +import static uk.co.caeldev.builder4test.impl.PojoBuilder.value; +import static uk.co.caeldev.builder4test.impl.PojoBuilder.valueCreator; import static uk.org.fyodor.generators.RDG.string; class FixedSizeListBuilderTest { @@ -84,4 +87,22 @@ public void shouldBuildAListWithOverrideRandomValues() { assertThat(pojos.get(0).getName()).isNotEqualTo(pojos.get(1).getName()); assertThat(pojos.get(0).getValue()).isNotEqualTo(pojos.get(1).getValue()); } + + @Test + @DisplayName("Should be able to build a list overriding default with creators") + public void shouldBuildAListWithOverrideCreatorValues() { + //When + FixedSizeListBuilder builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator) + .override(name, valueCreator).override(value, valueCreator); + List pojos = builder.get(); + + //Then + assertThat(pojos).hasSize(2); + assertThat(pojos.get(0).getName()).isEqualTo(pojos.get(1).getName()); + assertThat(pojos.get(0).getValue()).isEqualTo(pojos.get(1).getValue()); + + //And + assertThat(pojos.get(0).getName()).isEqualTo("test1"); + assertThat(pojos.get(0).getValue()).isEqualTo("test1"); + } } \ No newline at end of file diff --git a/src/test/java/uk/co/caeldev/builder4test/LookUpTest.java b/src/test/java/uk/co/caeldev/builder4test/LookUpTest.java new file mode 100644 index 0000000..62cca44 --- /dev/null +++ b/src/test/java/uk/co/caeldev/builder4test/LookUpTest.java @@ -0,0 +1,49 @@ +package uk.co.caeldev.builder4test; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +class LookUpTest { + + @Mock + private Creator creator; + + @Test + @DisplayName("Should Create two different instance of ElementListBuilder") + public void shouldUseCreatorAsValue() { + //Given + LookUp testLookUp = new TestLookUp(); + + //When + testLookUp.get(new Field(), creator); + + //Then + verify(creator).build(any(LookUp.class)); + } + + class TestLookUp extends LookUp { + + + @Override + protected void put(Field field, V value) { + } + + @Override + public V get(Field field) { + return null; + } + + @Override + public V get(Field field, V defaultValue) { + return null; + } + } + +} \ No newline at end of file diff --git a/src/test/java/uk/co/caeldev/builder4test/impl/PojoBuilder.java b/src/test/java/uk/co/caeldev/builder4test/impl/PojoBuilder.java index af14f6d..ff37a3a 100644 --- a/src/test/java/uk/co/caeldev/builder4test/impl/PojoBuilder.java +++ b/src/test/java/uk/co/caeldev/builder4test/impl/PojoBuilder.java @@ -7,14 +7,22 @@ public class PojoBuilder { public static Field name = new Field<>(); public static Field value = new Field<>(); + public static Creator creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"), + lookUp.get(value, "defaultValue")); + public static Field name2 = new Field<>("defaultName"); public static Field value2 = new Field<>("defaultValue"); + public static Creator creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2), + lookUp.get(value2)); - public static Creator creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"), - lookUp.get(value, "defaultValue")); + public static Creator valueCreator = lookUp -> "test1"; - public static Creator creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2), + public static Field testValue = new Field<>(); + public static Creator valueTestCreator = lookUp -> lookUp.get(testValue, "test1"); + + public static Creator creatorWithPredefinedCreatorDefaults = lookUp -> new Pojo(lookUp.get(name2, valueTestCreator), lookUp.get(value2)); + }