From fee08e57d01a45d0c837a5b99045ceffa9901e3d Mon Sep 17 00:00:00 2001 From: Cael Date: Wed, 21 Nov 2018 13:12:45 +0000 Subject: [PATCH] Improvements on the DSL and remove some unexpected methods from it. (#15) --- README.md | 2 +- .../caeldev/builder4test/DefaultLookUp.java | 4 +- .../caeldev/builder4test/ElementBuilder.java | 22 +-- .../builder4test/ElementListBuilder.java | 42 ++++++ ...Builder.java => FixedSizeListBuilder.java} | 12 +- .../co/caeldev/builder4test/ListBuilder.java | 25 +--- .../uk/co/caeldev/builder4test/LookUp.java | 8 +- .../co/caeldev/builder4test/RandomLookUp.java | 4 +- .../integration/BuilderIntegrationTest.java | 126 +++++++++++------- .../builder4test/ElementBuilderTest.java | 70 ++++++++++ .../builder4test/ElementListBuilderTest.java | 79 +++++++++++ ...est.java => FixedSizeListBuilderTest.java} | 14 +- .../caeldev/builder4test/ListBuilderTest.java | 67 ++-------- 13 files changed, 317 insertions(+), 158 deletions(-) create mode 100644 src/main/java/uk/co/caeldev/builder4test/ElementListBuilder.java rename src/main/java/uk/co/caeldev/builder4test/{RandomListBuilder.java => FixedSizeListBuilder.java} (69%) create mode 100644 src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java create mode 100644 src/test/java/uk/co/caeldev/builder4test/ElementListBuilderTest.java rename src/test/java/uk/co/caeldev/builder4test/{RandomListBuilderTest.java => FixedSizeListBuilderTest.java} (79%) diff --git a/README.md b/README.md index 467aeaa..0d9bffa 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ In the example below we are creating a list of two elements overriding the fiend ```java List testSiumple = Builder.build() - .list(creator) + .list(creator).elements() .element() .override(name, "testSiumple") .end() diff --git a/src/main/java/uk/co/caeldev/builder4test/DefaultLookUp.java b/src/main/java/uk/co/caeldev/builder4test/DefaultLookUp.java index 45c16b8..2cbe00a 100644 --- a/src/main/java/uk/co/caeldev/builder4test/DefaultLookUp.java +++ b/src/main/java/uk/co/caeldev/builder4test/DefaultLookUp.java @@ -6,7 +6,7 @@ import static java.util.Objects.isNull; -public class DefaultLookUp implements LookUp { +public class DefaultLookUp extends LookUp { private Map values; @@ -19,7 +19,7 @@ protected DefaultLookUp(Map values) { } @Override - public void put(Field field, V value) { + protected void put(Field field, V value) { values.put(field, Optional.ofNullable(value)); } diff --git a/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java b/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java index f7ff773..a7d3cb5 100644 --- a/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java @@ -5,16 +5,20 @@ import java.util.Optional; public class ElementBuilder { - private final ListBuilder listBuilder; + private final ElementListBuilder elementListBuilder; private final Map fields; - private ElementBuilder(ListBuilder listBuilder) { - this.listBuilder = listBuilder; + private ElementBuilder(ElementListBuilder elementListBuilder) { + this.elementListBuilder = elementListBuilder; this.fields = new HashMap<>(); } - protected static ElementBuilder elementBuilder(ListBuilder listBuilder) { - return new ElementBuilder<>(listBuilder); + public static ElementBuilder elementBuilder(ElementListBuilder elementListBuilder) { + return new ElementBuilder<>(elementListBuilder); + } + + protected Map getFields() { + return this.fields; } public ElementBuilder override(Field field, U value) { @@ -22,11 +26,7 @@ public ElementBuilder override(Field field, U value) { return this; } - protected Map getFields() { - return this.fields; - } - - public ListBuilder end() { - return listBuilder; + public ElementListBuilder end() { + return elementListBuilder; } } diff --git a/src/main/java/uk/co/caeldev/builder4test/ElementListBuilder.java b/src/main/java/uk/co/caeldev/builder4test/ElementListBuilder.java new file mode 100644 index 0000000..c832dc2 --- /dev/null +++ b/src/main/java/uk/co/caeldev/builder4test/ElementListBuilder.java @@ -0,0 +1,42 @@ +package uk.co.caeldev.builder4test; + +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +public class ElementListBuilder { + + private final Creator creator; + private final List> elements; + + private ElementListBuilder(Creator creator) { + this.creator = creator; + this.elements = new ArrayList<>(); + } + + public static ElementListBuilder elementListBuilder(Creator creator) { + return new ElementListBuilder<>(creator); + } + + public ElementBuilder element() { + ElementBuilder elementBuilder = ElementBuilder.elementBuilder(this); + this.elements.add(elementBuilder.getFields()); + return elementBuilder; + } + + public List get() { + + if (elements.isEmpty()) { + return Lists.newArrayList(EntityBuilder.entityBuilder(creator).get()); + } + + return elements.stream() + .filter(it -> it.size() != 0) + .map(it -> EntityBuilder.entityBuilder(creator, it).get()) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/uk/co/caeldev/builder4test/RandomListBuilder.java b/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java similarity index 69% rename from src/main/java/uk/co/caeldev/builder4test/RandomListBuilder.java rename to src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java index 53d175e..e8db29d 100644 --- a/src/main/java/uk/co/caeldev/builder4test/RandomListBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java @@ -9,14 +9,14 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; -public class RandomListBuilder { +public class FixedSizeListBuilder { private final int size; private final Creator creator; private final Map generators; private final Map values; - private RandomListBuilder(int size, Creator creator) { + private FixedSizeListBuilder(int size, Creator creator) { this.size = size; this.creator = creator; values = new HashMap<>(); @@ -24,16 +24,16 @@ private RandomListBuilder(int size, Creator creator) { } - protected static RandomListBuilder randomListBuilder(int size, Creator creator) { - return new RandomListBuilder<>(size, creator); + protected static FixedSizeListBuilder fixedSizeListBuilder(int size, Creator creator) { + return new FixedSizeListBuilder<>(size, creator); } - public RandomListBuilder override(Field field, Generator generator) { + public FixedSizeListBuilder override(Field field, Generator generator) { generators.put(field, generator); return this; } - public RandomListBuilder override(Field field, U value) { + public FixedSizeListBuilder override(Field field, U value) { values.put(field, Optional.of(value)); return this; } diff --git a/src/main/java/uk/co/caeldev/builder4test/ListBuilder.java b/src/main/java/uk/co/caeldev/builder4test/ListBuilder.java index 8ea177e..67fddaa 100644 --- a/src/main/java/uk/co/caeldev/builder4test/ListBuilder.java +++ b/src/main/java/uk/co/caeldev/builder4test/ListBuilder.java @@ -1,39 +1,22 @@ package uk.co.caeldev.builder4test; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; - public class ListBuilder { private final Creator creator; - private final List> elements; private ListBuilder(Creator creator) { this.creator = creator; - this.elements = new ArrayList<>(); } protected static ListBuilder listBuilder(Creator creator) { return new ListBuilder<>(creator); } - public ElementBuilder element() { - ElementBuilder elementBuilder = ElementBuilder.elementBuilder(this); - this.elements.add(elementBuilder.getFields()); - return elementBuilder; - } - - public List get() { - return elements.stream() - .filter(it -> it.size() != 0) - .map(it -> EntityBuilder.entityBuilder(creator, it).get()) - .collect(Collectors.toList()); + public ElementListBuilder elements() { + return ElementListBuilder.elementListBuilder(creator); } - public RandomListBuilder size(int size) { - return RandomListBuilder.randomListBuilder(size, creator); + public FixedSizeListBuilder size(int size) { + return FixedSizeListBuilder.fixedSizeListBuilder(size, creator); } } diff --git a/src/main/java/uk/co/caeldev/builder4test/LookUp.java b/src/main/java/uk/co/caeldev/builder4test/LookUp.java index 7f85cf0..6277176 100644 --- a/src/main/java/uk/co/caeldev/builder4test/LookUp.java +++ b/src/main/java/uk/co/caeldev/builder4test/LookUp.java @@ -1,11 +1,11 @@ package uk.co.caeldev.builder4test; -public interface LookUp { +public abstract class LookUp { - void put(Field field, V value); + protected abstract void put(Field field, V value); - V get(Field field); + public abstract V get(Field field); - V get(Field field, V defaultValue); + public abstract V get(Field field, V defaultValue); } diff --git a/src/main/java/uk/co/caeldev/builder4test/RandomLookUp.java b/src/main/java/uk/co/caeldev/builder4test/RandomLookUp.java index 291497d..f0ad97f 100644 --- a/src/main/java/uk/co/caeldev/builder4test/RandomLookUp.java +++ b/src/main/java/uk/co/caeldev/builder4test/RandomLookUp.java @@ -8,7 +8,7 @@ import static java.util.Objects.isNull; import static java.util.Objects.nonNull; -public class RandomLookUp implements LookUp { +public class RandomLookUp extends LookUp { private Map values; private Map generators; @@ -19,7 +19,7 @@ protected RandomLookUp(Map values, Map genera } @Override - public void put(Field field, V value) { + protected void put(Field field, V value) { values.put(field, Optional.ofNullable(value)); } diff --git a/src/test/java/integration/BuilderIntegrationTest.java b/src/test/java/integration/BuilderIntegrationTest.java index df9f438..88f5a16 100644 --- a/src/test/java/integration/BuilderIntegrationTest.java +++ b/src/test/java/integration/BuilderIntegrationTest.java @@ -1,5 +1,7 @@ package integration; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import uk.co.caeldev.builder4test.Builder; import uk.co.caeldev.builder4test.impl.Pojo; @@ -13,6 +15,7 @@ public class BuilderIntegrationTest { @Test + @DisplayName("should build a pojo successfully using defaults values") public void shouldBuildPojo() { //When Pojo pojo = Builder.build().entity(creator).get(); @@ -23,6 +26,7 @@ public void shouldBuildPojo() { } @Test + @DisplayName("should build a pojo successfully when override default values") public void shouldBuildPojoOverridingValues() { //When Pojo pojo = Builder.build() @@ -37,6 +41,7 @@ public void shouldBuildPojoOverridingValues() { } @Test + @DisplayName("should build different pojo successfully when overrides the default values for both objects") public void shouldBuilTwoDifferentInstancesWithDifferentValues() { //When Pojo pojo1 = Builder.build() @@ -61,6 +66,7 @@ public void shouldBuilTwoDifferentInstancesWithDifferentValues() { } @Test + @DisplayName("should build pojo using defaults values sets by field constructor") public void shouldUseDefaultValuesFromFieldInstantiation() { //When Pojo pojo1 = Builder.build() @@ -73,6 +79,7 @@ public void shouldUseDefaultValuesFromFieldInstantiation() { } @Test + @DisplayName("should build a pojo successfully overriding defaults values sets by constructor") public void shouldOverrideDefaultValuesFromFieldInstantiation() { //When Pojo pojo1 = Builder.build() @@ -87,6 +94,7 @@ public void shouldOverrideDefaultValuesFromFieldInstantiation() { } @Test + @DisplayName("should build a pojo successfully setting nulls as values") public void shouldOverrideWithNulls() { //When Pojo pojo = Builder.build() @@ -100,53 +108,75 @@ public void shouldOverrideWithNulls() { assertThat(pojo.getValue()).isNull(); } - @Test - public void shouldBuildAListOfTwoEntities() { - - //When - List testSiumple = Builder.build() - .list(creator) - .element() - .override(name, "testSiumple") - .end() - .element() - .override(name, "testSiumple2") - .end() - .get(); - - //Then - assertThat(testSiumple).isNotEmpty(); - assertThat(testSiumple).hasSize(2); - - //And - Pojo pojo = testSiumple.get(0); - assertThat(pojo.getName()).isEqualTo("testSiumple"); - assertThat(pojo.getValue()).isEqualTo("defaultValue"); - - //And - Pojo pojo1 = testSiumple.get(1); - assertThat(pojo1.getName()).isEqualTo("testSiumple2"); - assertThat(pojo1.getValue()).isEqualTo("defaultValue"); - } - - @Test - public void shouldBuildAListOfTwoUsingGenerators() { - //Given - int size = 2; - - //When - List testSimple = Builder.build() - .list(creator) - .size(size) - .override(name, string()) - .override(value, string()) - .get(); - - //Then - assertThat(testSimple).isNotEmpty(); - assertThat(testSimple).hasSize(size); - - assertThat(testSimple.get(0).getName()).isNotEqualTo(testSimple.get(1).getName()); - assertThat(testSimple.get(0).getValue()).isNotEqualTo(testSimple.get(1).getValue()); + @Nested + class ListGenerationTest { + + @Test + @DisplayName("should build a list of two elements overriding defaults values") + public void shouldBuildAListOfTwoEntities() { + + //When + List testSiumple = Builder.build() + .list(creator) + .elements() + .element() + .override(name, "testSiumple") + .end() + .element() + .override(name, "testSiumple2") + .end() + .get(); + + //Then + assertThat(testSiumple).isNotEmpty(); + assertThat(testSiumple).hasSize(2); + + //And + Pojo pojo = testSiumple.get(0); + assertThat(pojo.getName()).isEqualTo("testSiumple"); + assertThat(pojo.getValue()).isEqualTo("defaultValue"); + + //And + Pojo pojo1 = testSiumple.get(1); + assertThat(pojo1.getName()).isEqualTo("testSiumple2"); + assertThat(pojo1.getValue()).isEqualTo("defaultValue"); + } + + @Test + @DisplayName("should build a list of two elements overriding defaults values with random generators") + public void shouldBuildAListOfTwoUsingGenerators() { + //Given + int size = 2; + + //When + List testSimple = Builder.build() + .list(creator) + .size(size) + .override(name, string()) + .override(value, string()) + .get(); + + //Then + assertThat(testSimple).isNotEmpty(); + assertThat(testSimple).hasSize(size); + + assertThat(testSimple.get(0).getName()).isNotEqualTo(testSimple.get(1).getName()); + assertThat(testSimple.get(0).getValue()).isNotEqualTo(testSimple.get(1).getValue()); + } + + @Test + @DisplayName("should build a list of one elements using defaults values when there is no size or elements definitions") + public void shouldBuildAListSizeOneWithNoSizeAndNoElementsDefinitions() { + //When + List testSimple = Builder.build() + .list(creator).elements().get(); + + //Then + assertThat(testSimple).isNotEmpty(); + assertThat(testSimple).hasSize(1); + + assertThat(testSimple.get(0).getName()).isEqualTo("defaultName"); + assertThat(testSimple.get(0).getValue()).isEqualTo("defaultValue"); + } } } diff --git a/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java b/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java new file mode 100644 index 0000000..214f1fa --- /dev/null +++ b/src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java @@ -0,0 +1,70 @@ +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; + +import static org.assertj.core.api.Assertions.assertThat; +import static uk.co.caeldev.builder4test.ElementListBuilder.elementListBuilder; + +class ElementBuilderTest { + + @Test + @DisplayName("Should create different instances of the builder") + public void shouldCreateDifferentInstances() { + //When + ElementBuilder elementBuilder = ElementBuilder.elementBuilder(elementListBuilder(PojoBuilder.creator)); + ElementBuilder elementBuilder2 = ElementBuilder.elementBuilder(elementListBuilder(PojoBuilder.creator)); + + //Then + assertThat(elementBuilder).isNotEqualTo(elementBuilder2); + } + + @Test + @DisplayName("Should initialize to empty the fields") + public void shouldInitFields() { + //When + ElementBuilder elementBuilder = ElementBuilder.elementBuilder(elementListBuilder(PojoBuilder.creator)); + + //Then + assertThat(elementBuilder.getFields()).isEmpty(); + } + + @Test + @DisplayName("Should add a field override") + public void should() { + //Given + Field field = new Field<>("default"); + + //When + ElementBuilder elementBuilder = ElementBuilder.elementBuilder(elementListBuilder(PojoBuilder.creator)); + elementBuilder.override(field, "override"); + + //Then + assertThat(elementBuilder.getFields()).hasSize(1); + assertThat(elementBuilder.getFields().get(field)).isPresent(); + assertThat(elementBuilder.getFields().get(field)).contains("override"); + } + + @Test + @DisplayName("Should return back the elements list builder") + public void shouldGoBackToElementsListBuilder() { + //Given + Field field = new Field<>("default"); + ElementListBuilder elementListBuilder = elementListBuilder(PojoBuilder.creator); + + //When + ElementBuilder elementBuilder = ElementBuilder.elementBuilder(elementListBuilder); + ElementListBuilder elementListBuilder1 = elementBuilder.override(field, "override").end(); + + //Then + assertThat(elementBuilder.getFields()).hasSize(1); + assertThat(elementBuilder.getFields().get(field)).isPresent(); + assertThat(elementBuilder.getFields().get(field)).contains("override"); + assertThat(elementListBuilder1).isEqualTo(elementListBuilder); + } + + + +} \ No newline at end of file diff --git a/src/test/java/uk/co/caeldev/builder4test/ElementListBuilderTest.java b/src/test/java/uk/co/caeldev/builder4test/ElementListBuilderTest.java new file mode 100644 index 0000000..3fb6f36 --- /dev/null +++ b/src/test/java/uk/co/caeldev/builder4test/ElementListBuilderTest.java @@ -0,0 +1,79 @@ +package uk.co.caeldev.builder4test; + +import org.junit.jupiter.api.Test; +import uk.co.caeldev.builder4test.impl.Pojo; +import uk.co.caeldev.builder4test.impl.PojoBuilder; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class ElementListBuilderTest { + + @Test + public void shouldBuilderDifferentBuilders() { + //When + ElementListBuilder pojoListBuilder = ElementListBuilder.elementListBuilder(PojoBuilder.creator); + ElementListBuilder pojoListBuilder2 = ElementListBuilder.elementListBuilder(PojoBuilder.creator); + + //Then + assertThat(pojoListBuilder).isNotEqualTo(pojoListBuilder2); + } + + @Test + public void shouldBuildEmptyList() { + //When + List pojos = ElementListBuilder.elementListBuilder(PojoBuilder.creator).get(); + + //Then + assertThat(pojos).hasSize(1); + } + + @Test + public void shouldBuildListWithOneElement() { + //When + List pojos = ElementListBuilder.elementListBuilder(PojoBuilder.creator) + .element() + .override(PojoBuilder.name, "test") + .end() + .get(); + //Then + assertThat(pojos).isNotEmpty(); + assertThat(pojos).hasSize(1); + + //And + Pojo pojo = pojos.get(0); + assertThat(pojo.getName()).isEqualTo("test"); + assertThat(pojo.getValue()).isEqualTo("defaultValue"); + } + + @Test + public void shouldBuildListWithTwoElements() { + //When + List pojos = ElementListBuilder.elementListBuilder(PojoBuilder.creator) + .element() + .override(PojoBuilder.name, "test1") + .override(PojoBuilder.value, "testValue1") + .end() + .element() + .override(PojoBuilder.name, "test2") + .override(PojoBuilder.value, "testValue2") + .end() + .get(); + + //Then + assertThat(pojos).isNotEmpty(); + assertThat(pojos).hasSize(2); + + //And + Pojo pojo = pojos.get(0); + assertThat(pojo.getName()).isEqualTo("test1"); + assertThat(pojo.getValue()).isEqualTo("testValue1"); + + //And + Pojo pojo1 = pojos.get(1); + assertThat(pojo1.getName()).isEqualTo("test2"); + assertThat(pojo1.getValue()).isEqualTo("testValue2"); + } + +} \ No newline at end of file diff --git a/src/test/java/uk/co/caeldev/builder4test/RandomListBuilderTest.java b/src/test/java/uk/co/caeldev/builder4test/FixedSizeListBuilderTest.java similarity index 79% rename from src/test/java/uk/co/caeldev/builder4test/RandomListBuilderTest.java rename to src/test/java/uk/co/caeldev/builder4test/FixedSizeListBuilderTest.java index 01682c6..e397080 100644 --- a/src/test/java/uk/co/caeldev/builder4test/RandomListBuilderTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/FixedSizeListBuilderTest.java @@ -9,13 +9,13 @@ import static uk.co.caeldev.builder4test.impl.PojoBuilder.*; import static uk.org.fyodor.generators.RDG.string; -class RandomListBuilderTest { +class FixedSizeListBuilderTest { @Test public void shouldReturnUniqueInstances() { //When - RandomListBuilder builder1 = RandomListBuilder.randomListBuilder(1, creator); - RandomListBuilder builder2 = RandomListBuilder.randomListBuilder(1, creator); + FixedSizeListBuilder builder1 = FixedSizeListBuilder.fixedSizeListBuilder(1, creator); + FixedSizeListBuilder builder2 = FixedSizeListBuilder.fixedSizeListBuilder(1, creator); //Then assertThat(builder1).isNotEqualTo(builder2); @@ -24,7 +24,7 @@ public void shouldReturnUniqueInstances() { @Test public void shouldBuildAListWithDefaultValuesNoRandoms() { //When - RandomListBuilder builder = RandomListBuilder.randomListBuilder(2, creator); + FixedSizeListBuilder builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator); List pojos = builder.get(); //Then @@ -38,7 +38,7 @@ public void shouldBuildAListWithDefaultValuesNoRandoms() { @Test public void shouldBuildAListWithOverrideConstantValues() { //When - RandomListBuilder builder = RandomListBuilder.randomListBuilder(2, creator) + FixedSizeListBuilder builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator) .override(name, "testName").override(value, "testValue"); List pojos = builder.get(); @@ -53,7 +53,7 @@ public void shouldBuildAListWithOverrideConstantValues() { @Test public void shouldBuildAListWithOverrideConstantAndRandomValues() { //When - RandomListBuilder builder = RandomListBuilder.randomListBuilder(2, creator) + FixedSizeListBuilder builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator) .override(name, "testName").override(value, string()); List pojos = builder.get(); @@ -69,7 +69,7 @@ public void shouldBuildAListWithOverrideConstantAndRandomValues() { @Test public void shouldBuildAListWithOverrideRandomValues() { //When - RandomListBuilder builder = RandomListBuilder.randomListBuilder(2, creator) + FixedSizeListBuilder builder = FixedSizeListBuilder.fixedSizeListBuilder(2, creator) .override(name, string()).override(value, string()); List pojos = builder.get(); diff --git a/src/test/java/uk/co/caeldev/builder4test/ListBuilderTest.java b/src/test/java/uk/co/caeldev/builder4test/ListBuilderTest.java index 61b4527..dce4f8f 100644 --- a/src/test/java/uk/co/caeldev/builder4test/ListBuilderTest.java +++ b/src/test/java/uk/co/caeldev/builder4test/ListBuilderTest.java @@ -1,79 +1,34 @@ 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; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; class ListBuilderTest { @Test - public void shouldBuilderDifferentBuilders() { + @DisplayName("Should Create two different instance of ElementListBuilder") + public void shouldCreateDistinctElementsBuilder() { //When - ListBuilder pojoListBuilder = ListBuilder.listBuilder(PojoBuilder.creator); - ListBuilder pojoListBuilder2 = ListBuilder.listBuilder(PojoBuilder.creator); + ElementListBuilder elements = ListBuilder.listBuilder(PojoBuilder.creator).elements(); + ElementListBuilder elements2 = ListBuilder.listBuilder(PojoBuilder.creator).elements(); //Then - assertThat(pojoListBuilder).isNotEqualTo(pojoListBuilder2); + assertThat(elements).isNotEqualTo(elements2); } @Test - public void shouldBuildEmptyList() { + @DisplayName("Should Create two different instance of FixedSizeListBuilder") + public void shouldCreateDistinctFixedSizeListBuilder() { //When - List pojos = ListBuilder.listBuilder(PojoBuilder.creator).get(); - - //Then - assertThat(pojos).isEmpty(); - } + FixedSizeListBuilder fixedSizeListBuilder = ListBuilder.listBuilder(PojoBuilder.creator).size(1); + FixedSizeListBuilder fixedSizeListBuilder2 = ListBuilder.listBuilder(PojoBuilder.creator).size(1); - @Test - public void shouldBuildListWithOneElement() { - //When - List pojos = ListBuilder.listBuilder(PojoBuilder.creator) - .element() - .override(PojoBuilder.name, "test") - .end() - .get(); //Then - assertThat(pojos).isNotEmpty(); - assertThat(pojos).hasSize(1); - - //And - Pojo pojo = pojos.get(0); - assertThat(pojo.getName()).isEqualTo("test"); - assertThat(pojo.getValue()).isEqualTo("defaultValue"); - } - - @Test - public void shouldBuildListWithTwoElements() { - //When - List pojos = ListBuilder.listBuilder(PojoBuilder.creator) - .element() - .override(PojoBuilder.name, "test1") - .override(PojoBuilder.value, "testValue1") - .end() - .element() - .override(PojoBuilder.name, "test2") - .override(PojoBuilder.value, "testValue2") - .end() - .get(); - - //Then - assertThat(pojos).isNotEmpty(); - assertThat(pojos).hasSize(2); - - //And - Pojo pojo = pojos.get(0); - assertThat(pojo.getName()).isEqualTo("test1"); - assertThat(pojo.getValue()).isEqualTo("testValue1"); - - //And - Pojo pojo1 = pojos.get(1); - assertThat(pojo1.getName()).isEqualTo("test2"); - assertThat(pojo1.getValue()).isEqualTo("testValue2"); + assertThat(fixedSizeListBuilder).isNotEqualTo(fixedSizeListBuilder2); } } \ No newline at end of file