Skip to content

Commit

Permalink
[#23] Removing Creator interface to use a java Function (#26)
Browse files Browse the repository at this point in the history
* [#23] Removing Creator interface to use a java Function

* [#23] Update README
  • Loading branch information
Cael authored Mar 20, 2019
1 parent e2ac704 commit fe14727
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 63 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Library to build your POJO entities using a very intuitive DSL.

**NOTE**: from version 0.2.x the DSL has changed a little bit to provide more support and new features. you will have to update your test in order to use the new version but hopefully the DSL should change.
**NOTE**: from version 0.2.0 the DSL has changed a little bit to provide more support and new features. you will have to update your test in order to use the new version but hopefully the DSL should change.

## Motivation
Most of the time when I am writing my tests I have the need to write clean and readable tests. One way to achieve is by having Test Builder, but normally it takes time and are difficult to mantain in time. So after looking around I came up with this library to help you to create your pojo in a different and easy way.
Expand All @@ -26,10 +26,10 @@ public class PojoBuilder {
public static Field<String> name2 = new Field<>("defaultName");
public static Field<String> value2 = new Field<>("defaultValue");

public static Creator<Pojo> creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"),
public static Function<LookUp, Pojo> creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"),
lookUp.get(value, "defaultValue"));

public static Creator<Pojo> creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2),
public static Function<LookUp, Pojo> creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2),
lookUp.get(value2));

}
Expand All @@ -38,7 +38,8 @@ There many things going on there but I will try to explain it in the best way th
There are s few concepts to keep in mind; Field, Creator and LookUp.

### Creator
The Creator implements a method build that should contains how the object is going to be build.
The Creator is just a java Function that receives as Argument a LookUp and return the object.
**NOTE:** Since version 0.2.1 the Creator interface was removed in order to use a Function.

### Field
The Field represent the value that you want to change by using the DSL in the construction of your objects.
Expand Down Expand Up @@ -94,10 +95,10 @@ If you already have creators and you want to reuse them on other creator, you ca

```java
public static Field<String> name = new Field<>();
public static Creator<String> nameCreator = lookUp -> lookUp.get(name, "test1");
public static Function<LookUp, String> nameCreator = lookUp -> lookUp.get(name, "test1");

public static Field<String> secondName = new Field<>();
public static Creator<String> creator = lookUp -> lookUp.get(secondName, secondCreator);
public static Function<LookUp, String> creator = lookUp -> lookUp.get(secondName, secondCreator);

Pojo pojo = Builder.build()
.entity(creator)
Expand All @@ -110,7 +111,7 @@ If you already have creators and you want to reuse them on other creator, you ca
```java

public static Field<String> secondName = new Field<>();
public static Creator<String> secondCreator = lookUp -> lookUp.get(secondName, "test1");
public static Function<LookUp, String> secondCreator = lookUp -> lookUp.get(secondName, "test1");

Pojo pojo = Builder.build()
.entity(PojoBuilder.creator)
Expand Down Expand Up @@ -171,7 +172,5 @@ The library is highly inspired by

[Make it Easy](https://github.com/npryce/make-it-easy) And [AssertJ](https://github.com/joel-costigliola/assertj-core)

For the random generation we decided to use [Fyodor](https://github.com/fyodor-org-uk/fyodor)

Make It Ease lib provides a Hamcrest style DSL but I am more fun of using a builder kind of DSL like AssertJ that offers straight away the option that I can use.
I want to say thank you to all the collaborator of MakeItEasy project.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Wed Mar 20 19:41:36 GMT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
3 changes: 2 additions & 1 deletion src/main/java/uk/co/caeldev/builder4test/ApplyField.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package uk.co.caeldev.builder4test;

import java.util.function.Function;
import java.util.function.Supplier;

public interface ApplyField<L> {

<U> L applyCreator(Field<U> field, Creator<U> creator);
<U> L applyCreator(Field<U> field, Function<LookUp, U> creator);
<U> L applySupplier(Field<U> field, Supplier<U> supplier);
<U> L applyValue(Field<U> field, U value);

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/uk/co/caeldev/builder4test/Builder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.co.caeldev.builder4test;

import java.util.function.Function;

public class Builder {

private Builder() {
Expand All @@ -9,11 +11,11 @@ public static Builder build() {
return new Builder();
}

public <K> EntityBuilder<K> entity(Creator<K> creator) {
public <K> EntityBuilder<K> entity(Function<LookUp, K> creator) {
return EntityBuilder.entityBuilder(creator);
}

public <K> ListBuilder<K> list(Creator<K> creator) {
public <K> ListBuilder<K> list(Function<LookUp, K> creator) {
return ListBuilder.listBuilder(creator);
}

Expand Down
7 changes: 0 additions & 7 deletions src/main/java/uk/co/caeldev/builder4test/Creator.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public class ElementBuilder<K> implements ApplyField<ElementBuilder<K>> {
Expand Down Expand Up @@ -39,8 +40,8 @@ public <U> ElementBuilder<K> applyValue(Field<U> field, U value) {
}

@Override
public <U> ElementBuilder<K> applyCreator(Field<U> field, Creator<U> creator) {
return applySupplier(field, () -> creator.build(new DefaultLookUp(fields)));
public <U> ElementBuilder<K> applyCreator(Field<U> field, Function<LookUp, U> creator) {
return applySupplier(field, () -> creator.apply(new DefaultLookUp(fields)));
}

public <U> ElementBuilder<K> nullify(Field<U> field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ElementListBuilder<K> {

private final Creator<K> creator;
private final Function<LookUp, K> creator;
private final List<Map<Field, Resolver>> elements;

private ElementListBuilder(Creator<K> creator) {
private ElementListBuilder(Function<LookUp, K> creator) {
this.creator = creator;
this.elements = new ArrayList<>();
}

public static <K> ElementListBuilder<K> elementListBuilder(Creator<K> creator) {
public static <K> ElementListBuilder<K> elementListBuilder(Function<LookUp, K> creator) {
return new ElementListBuilder<>(creator);
}

Expand Down
21 changes: 11 additions & 10 deletions src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,38 @@
import uk.co.caeldev.builder4test.resolvers.ValueResolver;

import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

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

private final Creator<K> creator;
private final Function<LookUp, K> creator;
private final LookUp lookUp;

private EntityBuilder(Creator<K> creator) {
private EntityBuilder(Function<LookUp, K> creator) {
this.creator = creator;
this.lookUp = new DefaultLookUp();
}

private EntityBuilder(Creator<K> creator, Map<Field, Resolver> fields) {
private EntityBuilder(Function<LookUp, K> creator, Map<Field, Resolver> fields) {
this.creator = creator;
this.lookUp = new DefaultLookUp(fields);
}

private EntityBuilder(Creator<K> creator, LookUp lookUp) {
private EntityBuilder(Function<LookUp, K> creator, LookUp lookUp) {
this.creator = creator;
this.lookUp = lookUp;
}

protected static <T> EntityBuilder<T> entityBuilder(Creator<T> creator, Map<Field, Resolver> fields) {
protected static <T> EntityBuilder<T> entityBuilder(Function<LookUp, T> creator, Map<Field, Resolver> fields) {
return new EntityBuilder<>(creator, fields);
}

protected static <T> EntityBuilder<T> entityBuilder(Creator<T> creator) {
protected static <T> EntityBuilder<T> entityBuilder(Function<LookUp, T> creator) {
return new EntityBuilder<>(creator);
}

protected static <T> EntityBuilder<T> entityBuilder(Creator<T> creator, LookUp lookUp) {
protected static <T> EntityBuilder<T> entityBuilder(Function<LookUp, T> creator, LookUp lookUp) {
return new EntityBuilder<>(creator, lookUp);
}

Expand All @@ -52,8 +53,8 @@ public <U> EntityBuilder<K> applyValue(Field<U> field, U value) {
}

@Override
public <V> EntityBuilder<K> applyCreator(Field<V> field, Creator<V> creator) {
return applySupplier(field, () -> creator.build(lookUp));
public <V> EntityBuilder<K> applyCreator(Field<V> field, Function<LookUp, V> creator) {
return applySupplier(field, () -> creator.apply(lookUp));
}

public <V> EntityBuilder<K> nullify(Field<V> field) {
Expand All @@ -62,6 +63,6 @@ public <V> EntityBuilder<K> nullify(Field<V> field) {
}

public K get() {
return creator.build(lookUp);
return creator.apply(lookUp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
public class FixedSizeListBuilder<K> implements ApplyField<FixedSizeListBuilder<K>> {

private final int size;
private final Creator<K> creator;
private final Function<LookUp, K> creator;
private final Map<Field, Resolver> values;

private FixedSizeListBuilder(int size, Creator<K> creator) {
private FixedSizeListBuilder(int size, Function<LookUp, K> creator) {
this.size = size;
this.creator = creator;
values = new HashMap<>();

}

protected static <U> FixedSizeListBuilder<U> fixedSizeListBuilder(int size, Creator<U> creator) {
protected static <U> FixedSizeListBuilder<U> fixedSizeListBuilder(int size, Function<LookUp, U> creator) {
return new FixedSizeListBuilder<>(size, creator);
}

Expand All @@ -43,8 +43,8 @@ public <U> FixedSizeListBuilder<K> applyValue(Field<U> field, U value) {
}

@Override
public <U> FixedSizeListBuilder<K> applyCreator(Field<U> field, Creator<U> creator) {
applySupplier(field, () -> creator.build(new DefaultLookUp(values)));
public <U> FixedSizeListBuilder<K> applyCreator(Field<U> field, Function<LookUp, U> creator) {
applySupplier(field, () -> creator.apply(new DefaultLookUp(values)));
return this;
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/uk/co/caeldev/builder4test/ListBuilder.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package uk.co.caeldev.builder4test;

import java.util.function.Function;

public class ListBuilder<K> {

private final Creator<K> creator;
private final Function<LookUp, K> creator;

private ListBuilder(Creator<K> creator) {
private ListBuilder(Function<LookUp, K> creator) {
this.creator = creator;
}

protected static <K> ListBuilder<K> listBuilder(Creator<K> creator) {
protected static <K> ListBuilder<K> listBuilder(Function<LookUp, K> creator) {
return new ListBuilder<>(creator);
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/uk/co/caeldev/builder4test/LookUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import uk.co.caeldev.builder4test.resolvers.Resolver;

import java.util.function.Function;

public abstract class LookUp {

protected abstract <V, U> void put(Field<V> field, Resolver<V, U> value);
Expand All @@ -10,8 +12,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));
public <V> V get(Field<V> field, Function<LookUp, V> defaultValue) {
return get(field, defaultValue.apply(this));
}

}
11 changes: 1 addition & 10 deletions src/test/java/integration/BuilderIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.creator;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.creatorWithPredefinedCreatorDefaults;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.creatorWithPredefinedDefaults;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.name;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.name2;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.testValue;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.value;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.value2;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.valueCreator;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.valueTestCreator;
import static uk.co.caeldev.builder4test.impl.PojoBuilder.*;
import static uk.org.fyodor.generators.RDG.string;

public class BuilderIntegrationTest {
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/uk/co/caeldev/builder4test/LookUpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import org.mockito.junit.jupiter.MockitoExtension;
import uk.co.caeldev.builder4test.resolvers.Resolver;

import java.util.function.Function;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class LookUpTest {

@Mock
private Creator<String> creator;
private Function<LookUp, String> creator;

@Test
@DisplayName("Should Create two different instance of ElementListBuilder")
Expand All @@ -26,7 +28,7 @@ public void shouldUseCreatorAsValue() {
testLookUp.get(new Field<String>(), creator);

//Then
verify(creator).build(any(LookUp.class));
verify(creator).apply(any(LookUp.class));
}

class TestLookUp extends LookUp {
Expand Down
14 changes: 8 additions & 6 deletions src/test/java/uk/co/caeldev/builder4test/impl/PojoBuilder.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package uk.co.caeldev.builder4test.impl;

import uk.co.caeldev.builder4test.Creator;
import uk.co.caeldev.builder4test.Field;
import uk.co.caeldev.builder4test.LookUp;

import java.util.function.Function;

public class PojoBuilder {

public static Field<String> name = new Field<>();
public static Field<String> value = new Field<>();
public static Creator<Pojo> creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"),
public static Function<LookUp, Pojo> creator = lookUp -> new Pojo(lookUp.get(name, "defaultName"),
lookUp.get(value, "defaultValue"));


public static Field<String> name2 = new Field<>("defaultName");
public static Field<String> value2 = new Field<>("defaultValue");
public static Creator<Pojo> creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2),
public static Function<LookUp, Pojo> creatorWithPredefinedDefaults = lookUp -> new Pojo(lookUp.get(name2),
lookUp.get(value2));

public static Creator<String> valueCreator = lookUp -> "test1";
public static Function<LookUp, String> valueCreator = lookUp -> "test1";

public static Field<String> testValue = new Field<>();
public static Creator<String> valueTestCreator = lookUp -> lookUp.get(testValue, "test1");
public static Function<LookUp, String> valueTestCreator = lookUp -> lookUp.get(testValue, "test1");

public static Creator<Pojo> creatorWithPredefinedCreatorDefaults = lookUp -> new Pojo(lookUp.get(name2, valueTestCreator),
public static Function<LookUp, Pojo> creatorWithPredefinedCreatorDefaults = lookUp -> new Pojo(lookUp.get(name2, valueTestCreator),
lookUp.get(value2));


Expand Down

0 comments on commit fe14727

Please sign in to comment.