Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for seed hash. #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ new WireMockServer(wireMockConfig().extensions(RandomExtension.class));

This will generate random first names in the `en-US` locale for every request.

You can optionally add the `seed` parameter to generate the same value for the same seed:

{% raw %}
```handlebars
{{ random 'Name.first_name' seed=1234 }}
```
%}

### Technical notes
This library brings `net.datafaker:datafaker` as transitive dependency, which may result in conflicts at building time.
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/wiremock/RandomHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import com.github.jknack.handlebars.Options;
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.HandlebarsHelper;
import java.util.Random;
import net.datafaker.Faker;

/*
* Author: Shreya Agarwal
*/
public class RandomHelper extends HandlebarsHelper<Object> {

private static final String SEED_HASH = "seed";

private final Faker faker;

public RandomHelper() {
Expand All @@ -17,13 +20,18 @@ public RandomHelper() {

@Override
public Object apply(Object context, Options options) {

// Use a seeded Faker if seed hash exists
final var seedOption = options.hash(SEED_HASH);
var myFaker = seedOption == null ? faker : new Faker(new Random(seedOption.hashCode()));

try {
// Used the `expression` method instead of `resolve` as the
// resolve method is not able to resolve nested references in the yml files. For example, in
// the resource file for en-US, `address.full_address` wasn't resolving because of the
// `#{street_address}` nested reference, but `address.postcode_by_state.AL` would work fine.
// `expression` is able to handle all cases.
return faker.expression("#{" + context + "}");
return myFaker.expression("#{" + context + "}");
} catch (RuntimeException e) {
return handleError("Unable to evaluate the expression " + context, e);
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/wiremock/RandomHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.HandlebarsHelperTestBase;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Random;
import net.datafaker.Faker;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -40,4 +41,16 @@ public void returnsRandomValue(int seed, String expression, String expected) thr
String actual = renderHelperValue(helper, expression);
assertThat(actual, is(expected));
}

@ParameterizedTest
@CsvSource(
value = {
"1, Donny",
"toto, Ethan",
})
public void rendersSeededRandomValue(Object seed, String expected) throws IOException {
final var actual = helper.apply("Name.firstName", createOptions(Map.of("seed", seed)));

assertThat(actual, is(expected));
}
}