Skip to content

Commit

Permalink
[#14] Adding support for default creators as value and override (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cael authored Mar 16, 2019
1 parent d155ad4 commit 9baa7a8
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 28 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> name = new Field<>();
public static Creator<String> nameCreator = lookUp -> lookUp.get(name, "test1");

public static Field<String> secondName = new Field<>();
public static Creator<String> 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<String> secondName = new Field<>();
public static Creator<String> 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.
Expand Down Expand Up @@ -102,7 +149,9 @@ List<Pojo> 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
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import java.util.Map;
import java.util.Optional;

public class ElementBuilder<K> {
public class ElementBuilder<K> implements OverrideField<ElementBuilder<K>> {

private final ElementListBuilder<K> elementListBuilder;
private final Map<Field, Optional> fields;

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

@Override
public <U> ElementBuilder<K> override(Field<U> field, U value) {
this.fields.put(field, Optional.ofNullable(value));
return this;
}

@Override
public <U> ElementBuilder<K> override(Field<U> field, Creator<U> creator) {
return override(field, creator.build(new DefaultLookUp(fields)));
}

public <U> ElementBuilder<K> nullify(Field<U> field) {
this.fields.put(field, Optional.empty());
return this;
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Map;
import java.util.Optional;

public class EntityBuilder<K> {
public class EntityBuilder<K> implements OverrideField<EntityBuilder<K>> {

private final Creator<K> creator;
private final LookUp lookUp;
Expand Down Expand Up @@ -35,11 +35,17 @@ protected static <T> EntityBuilder<T> entityBuilder(Creator<T> Creator, LookUp l
return new EntityBuilder<>(Creator, lookUp);
}

@Override
public <V> EntityBuilder<K> override(Field<V> field, V value) {
lookUp.put(field, value);
return this;
}

@Override
public <V> EntityBuilder<K> override(Field<V> field, Creator<V> creator) {
return override(field, creator.build(lookUp));
}

public <V> EntityBuilder<K> nullify(Field<V> field) {
lookUp.put(field, null);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class FixedSizeListBuilder<K> {
public class FixedSizeListBuilder<K> implements OverrideField<FixedSizeListBuilder<K>>{

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

@Override
public <U> FixedSizeListBuilder<K> override(Field<U> field, U value) {
values.put(field, Optional.of(value));
return this;
}

@Override
public <U> FixedSizeListBuilder<K> override(Field<U> field, Creator<U> creator) {
override(field, creator.build(new DefaultLookUp(values)));
return this;
}

public List<K> get() {
LookUp lookUp = new RandomLookUp(values, generators);
return IntStream.rangeClosed(1, size)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/uk/co/caeldev/builder4test/LookUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public abstract class LookUp {

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

public <V> V get(Field<V> field, Creator<V> defaultValue) {
return get(field, defaultValue.build(this));
}

}
8 changes: 8 additions & 0 deletions src/main/java/uk/co/caeldev/builder4test/OverrideField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.co.caeldev.builder4test;

public interface OverrideField<L> {

<U> L override(Field<U> field, Creator<U> creator);
<U> L override(Field<U> field, U value);

}
88 changes: 82 additions & 6 deletions src/test/java/integration/BuilderIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

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

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


//When
ElementBuilder<Pojo> elementBuilder = ElementBuilder.elementBuilder(elementListBuilder);
ElementListBuilder<Pojo> 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);
}



}
15 changes: 14 additions & 1 deletion src/test/java/uk/co/caeldev/builder4test/EntityBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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

0 comments on commit 9baa7a8

Please sign in to comment.