Skip to content

Commit 9baa7a8

Browse files
author
Cael
authored
[#14] Adding support for default creators as value and override (#17)
1 parent d155ad4 commit 9baa7a8

File tree

14 files changed

+285
-28
lines changed

14 files changed

+285
-28
lines changed

‎README.md‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,53 @@ Pojo pojo = Builder.build()
7474
.get();
7575
```
7676

77+
You can nullify a field by just using nullify Method from DSL.
78+
79+
```java
80+
Pojo pojo = Builder.build()
81+
.entity(PojoBuilder.creator)
82+
.override(PojoBuilder.name, "nameoverrideed")
83+
.nullify(PojoBuilder.value)
84+
.get();
85+
```
86+
87+
## Reuse amd composition of Creators
88+
89+
if you have already creators and you want to reuse them on other creator, you can achieve by:
90+
91+
- 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.
92+
93+
```java
94+
public static Field<String> name = new Field<>();
95+
public static Creator<String> nameCreator = lookUp -> lookUp.get(name, "test1");
96+
97+
public static Field<String> secondName = new Field<>();
98+
public static Creator<String> creator = lookUp -> lookUp.get(secondName, secondCreator);
99+
100+
Pojo pojo = Builder.build()
101+
.entity(creator)
102+
.override(name, "test2")
103+
.get();
104+
```
105+
106+
- The other way is by overriding a field using a creator as value:
107+
108+
```java
109+
110+
public static Field<String> secondName = new Field<>();
111+
public static Creator<String> secondCreator = lookUp -> lookUp.get(secondName, "test1");
112+
113+
Pojo pojo = Builder.build()
114+
.entity(PojoBuilder.creator)
115+
.override(PojoBuilder.name, "nameoverrideed")
116+
.override(PojoBuilder.secondName, "secondName")
117+
.override(PojoBuilder.value, secondCreator)
118+
.get();
119+
```
120+
121+
- You can also from there override a field of secondCreator but you have to override those before using the secondCreator.
122+
123+
77124
## Build a list of entities
78125

79126
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<Pojo> testSimple = Builder.build()
102149
.get();
103150
```
104151
This code will generate a List of five elements and each element will contain a random value and field.
105-
Using defaults generator provided by Fyodor is easy to generate your random values.
152+
Using defaults generator provided by Fyodor is easy to generate your random values.
153+
154+
__Note:__ that you can use creators as default values in your collections.
106155

107156
## Credits
108157
The library is highly inspired by

‎build.gradle‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ dependencies {
3232

3333
testImplementation "org.assertj:assertj-core:3.10.0"
3434

35+
testImplementation "org.mockito:mockito-junit-jupiter:2.25.0"
36+
37+
3538
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.2.0"
3639
}
3740

‎src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import java.util.Map;
55
import java.util.Optional;
66

7-
public class ElementBuilder<K> {
7+
public class ElementBuilder<K> implements OverrideField<ElementBuilder<K>> {
8+
89
private final ElementListBuilder<K> elementListBuilder;
910
private final Map<Field, Optional> fields;
1011

@@ -21,11 +22,17 @@ protected Map<Field, Optional> getFields() {
2122
return this.fields;
2223
}
2324

25+
@Override
2426
public <U> ElementBuilder<K> override(Field<U> field, U value) {
2527
this.fields.put(field, Optional.ofNullable(value));
2628
return this;
2729
}
2830

31+
@Override
32+
public <U> ElementBuilder<K> override(Field<U> field, Creator<U> creator) {
33+
return override(field, creator.build(new DefaultLookUp(fields)));
34+
}
35+
2936
public <U> ElementBuilder<K> nullify(Field<U> field) {
3037
this.fields.put(field, Optional.empty());
3138
return this;

‎src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Map;
44
import java.util.Optional;
55

6-
public class EntityBuilder<K> {
6+
public class EntityBuilder<K> implements OverrideField<EntityBuilder<K>> {
77

88
private final Creator<K> creator;
99
private final LookUp lookUp;
@@ -35,11 +35,17 @@ protected static <T> EntityBuilder<T> entityBuilder(Creator<T> Creator, LookUp l
3535
return new EntityBuilder<>(Creator, lookUp);
3636
}
3737

38+
@Override
3839
public <V> EntityBuilder<K> override(Field<V> field, V value) {
3940
lookUp.put(field, value);
4041
return this;
4142
}
4243

44+
@Override
45+
public <V> EntityBuilder<K> override(Field<V> field, Creator<V> creator) {
46+
return override(field, creator.build(lookUp));
47+
}
48+
4349
public <V> EntityBuilder<K> nullify(Field<V> field) {
4450
lookUp.put(field, null);
4551
return this;

‎src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.stream.Collectors;
1010
import java.util.stream.IntStream;
1111

12-
public class FixedSizeListBuilder<K> {
12+
public class FixedSizeListBuilder<K> implements OverrideField<FixedSizeListBuilder<K>>{
1313

1414
private final int size;
1515
private final Creator<K> creator;
@@ -33,11 +33,18 @@ public <U> FixedSizeListBuilder<K> override(Field<U> field, Generator<U> generat
3333
return this;
3434
}
3535

36+
@Override
3637
public <U> FixedSizeListBuilder<K> override(Field<U> field, U value) {
3738
values.put(field, Optional.of(value));
3839
return this;
3940
}
4041

42+
@Override
43+
public <U> FixedSizeListBuilder<K> override(Field<U> field, Creator<U> creator) {
44+
override(field, creator.build(new DefaultLookUp(values)));
45+
return this;
46+
}
47+
4148
public List<K> get() {
4249
LookUp lookUp = new RandomLookUp(values, generators);
4350
return IntStream.rangeClosed(1, size)

‎src/main/java/uk/co/caeldev/builder4test/LookUp.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ public abstract class LookUp {
88

99
public abstract <V> V get(Field<V> field, V defaultValue);
1010

11+
public <V> V get(Field<V> field, Creator<V> defaultValue) {
12+
return get(field, defaultValue.build(this));
13+
}
14+
1115
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package uk.co.caeldev.builder4test;
2+
3+
public interface OverrideField<L> {
4+
5+
<U> L override(Field<U> field, Creator<U> creator);
6+
<U> L override(Field<U> field, U value);
7+
8+
}

‎src/test/java/integration/BuilderIntegrationTest.java‎

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,45 @@ public void shouldOverrideDefaultValuesFromFieldInstantiation() {
9393
assertThat(pojo1.getValue()).isEqualTo("overridedValue");
9494
}
9595

96+
97+
@Test
98+
@DisplayName("should build a pojo successfully using another creators available")
99+
public void shouldOverrideDefaultValuesFromFieldInstantiationUsingAnotherCreator() {
100+
//When
101+
Pojo pojo1 = Builder.build()
102+
.entity(creatorWithPredefinedDefaults)
103+
.override(name2, valueCreator)
104+
.override(value2, "overridedValue")
105+
.get();
106+
107+
//Then
108+
assertThat(pojo1.getName()).isEqualTo("test1");
109+
assertThat(pojo1.getValue()).isEqualTo("overridedValue");
110+
}
111+
112+
@Test
113+
@DisplayName("should build a pojo by overriding using a creator and overriding a value of it")
114+
public void shouldOverrideDefaultValuesFromFieldInstantiationUsingAnotherCreator2() {
115+
//When
116+
Pojo pojo1 = Builder.build()
117+
.entity(creatorWithPredefinedCreatorDefaults)
118+
.override(testValue, "overridedValue1")
119+
.override(name2, valueTestCreator)
120+
.get();
121+
122+
//Then
123+
assertThat(pojo1.getName()).isEqualTo("overridedValue1");
124+
assertThat(pojo1.getValue()).isEqualTo("defaultValue");
125+
}
126+
96127
@Test
97128
@DisplayName("should build a pojo successfully setting nulls as values")
98129
public void shouldOverrideWithNulls() {
99130
//When
100131
Pojo pojo = Builder.build()
101132
.entity(creator)
102-
.override(name, null)
103-
.override(value, null)
133+
.nullify(name)
134+
.nullify(value)
104135
.get();
105136

106137
//Then
@@ -120,10 +151,10 @@ public void shouldBuildAListOfTwoEntities() {
120151
.list(creator)
121152
.elements()
122153
.element()
123-
.override(name, "testSiumple")
154+
.override(name, "testSimple")
124155
.end()
125156
.element()
126-
.override(name, "testSiumple2")
157+
.override(name, "testSimple2")
127158
.end()
128159
.get();
129160

@@ -133,12 +164,12 @@ public void shouldBuildAListOfTwoEntities() {
133164

134165
//And
135166
Pojo pojo = testSiumple.get(0);
136-
assertThat(pojo.getName()).isEqualTo("testSiumple");
167+
assertThat(pojo.getName()).isEqualTo("testSimple");
137168
assertThat(pojo.getValue()).isEqualTo("defaultValue");
138169

139170
//And
140171
Pojo pojo1 = testSiumple.get(1);
141-
assertThat(pojo1.getName()).isEqualTo("testSiumple2");
172+
assertThat(pojo1.getName()).isEqualTo("testSimple2");
142173
assertThat(pojo1.getValue()).isEqualTo("defaultValue");
143174
}
144175

@@ -164,6 +195,51 @@ public void shouldBuildAListOfTwoUsingGenerators() {
164195
assertThat(testSimple.get(0).getValue()).isNotEqualTo(testSimple.get(1).getValue());
165196
}
166197

198+
@Test
199+
@DisplayName("should build a list of two elements overriding defaults values with creators using size")
200+
public void shouldBuildAListOfTwoUsingCreatorAndSize() {
201+
//Given
202+
int size = 2;
203+
204+
//When
205+
List<Pojo> testSimple = Builder.build()
206+
.list(creator)
207+
.size(size)
208+
.override(name, valueCreator)
209+
.override(value, valueCreator)
210+
.get();
211+
212+
//Then
213+
assertThat(testSimple).isNotEmpty();
214+
assertThat(testSimple).hasSize(size);
215+
216+
assertThat(testSimple.get(0).getName()).isEqualTo(testSimple.get(1).getName());
217+
assertThat(testSimple.get(0).getValue()).isEqualTo(testSimple.get(1).getValue());
218+
assertThat(testSimple.get(0).getName()).isEqualTo("test1");
219+
assertThat(testSimple.get(0).getValue()).isEqualTo("test1");
220+
}
221+
222+
@Test
223+
@DisplayName("should build a list of one element overriding defaults values with creators")
224+
public void shouldBuildAListOfTwoUsingCreators() {
225+
//When
226+
List<Pojo> testSimple = Builder.build()
227+
.list(creator)
228+
.elements()
229+
.element()
230+
.override(name, valueCreator).end()
231+
.element()
232+
.override(value, valueCreator).end()
233+
.get();
234+
235+
//Then
236+
assertThat(testSimple).isNotEmpty();
237+
assertThat(testSimple).hasSize(2);
238+
239+
assertThat(testSimple.get(0).getName()).isEqualTo("test1");
240+
assertThat(testSimple.get(1).getValue()).isEqualTo("test1");
241+
}
242+
167243
@Test
168244
@DisplayName("should build a list of one elements using defaults values when there is no size or elements definitions")
169245
public void shouldBuildAListSizeOneWithNoSizeAndNoElementsDefinitions() {

‎src/test/java/uk/co/caeldev/builder4test/DefaultLookUpTest.java‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,6 @@ public void shouldGetNullWhenItHasBeenOverrideWithNull() {
7474
assertThat(value).isNull();
7575
}
7676

77-
@Test
78-
@DisplayName("Should retrieve null when there default is null")
79-
public void shouldGetNullWhenThereIsNoValueAndDefaultValueIsNull() {
80-
//Given
81-
Field<String> field = new Field<>();
82-
83-
//When
84-
String value = defaultLookUp.get(field, null);
85-
86-
//Then
87-
assertThat(value).isNull();
88-
}
89-
9077
@Test
9178
@DisplayName("Should retrieve default value declared by using field constructor")
9279
public void shouldGetDefaultWhenItUseFieldDefaultValue() {

‎src/test/java/uk/co/caeldev/builder4test/ElementBuilderTest.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ public void shouldBeAbleToNullifyAField() {
8282
assertThat(elementListBuilder1).isEqualTo(elementListBuilder);
8383
}
8484

85+
@Test
86+
@DisplayName("Should be able to use a creator as default value for a field")
87+
public void shouldBeAbleToUseCreatorAsDefaultValueForAField() {
88+
//Given
89+
Field<String> field = new Field<>("default");
90+
ElementListBuilder<Pojo> elementListBuilder = elementListBuilder(PojoBuilder.creator);
91+
92+
93+
//When
94+
ElementBuilder<Pojo> elementBuilder = ElementBuilder.elementBuilder(elementListBuilder);
95+
ElementListBuilder<Pojo> elementListBuilder1 = elementBuilder.override(field, PojoBuilder.valueCreator).end();
96+
97+
//Then
98+
assertThat(elementBuilder.getFields()).hasSize(1);
99+
assertThat(elementBuilder.getFields().get(field)).isPresent();
100+
assertThat(elementBuilder.getFields().get(field).get()).isEqualTo("test1");
101+
assertThat(elementListBuilder1).isEqualTo(elementListBuilder);
102+
}
103+
85104

86105

87106
}

0 commit comments

Comments
 (0)