Skip to content

Commit 09ea93b

Browse files
committed
move faker
1 parent 2116a2e commit 09ea93b

3 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package it.aboutbits.springboot.testing.testdata;
2+
3+
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
4+
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
5+
import net.datafaker.Faker;
6+
import net.datafaker.service.FakeValuesService;
7+
import net.datafaker.service.FakerContext;
8+
import net.datafaker.service.RandomService;
9+
import org.jspecify.annotations.NullMarked;
10+
11+
import java.util.Locale;
12+
import java.util.Random;
13+
import java.util.function.Function;
14+
import java.util.function.LongFunction;
15+
16+
@NullMarked
17+
public class FakerExtended extends Faker {
18+
public FakerExtended() {
19+
super();
20+
}
21+
22+
public FakerExtended(Locale locale) {
23+
super(locale);
24+
}
25+
26+
public FakerExtended(Random random) {
27+
super(random);
28+
}
29+
30+
public FakerExtended(Locale locale, Random random) {
31+
super(locale, random);
32+
}
33+
34+
public FakerExtended(Locale locale, RandomService randomService) {
35+
super(locale, randomService);
36+
}
37+
38+
public FakerExtended(FakeValuesService fakeValuesService, FakerContext context) {
39+
super(fakeValuesService, context);
40+
}
41+
42+
public <T extends Enum<?>> T randomEnumValue(Class<T> enumClass) {
43+
var values = enumClass.getEnumConstants();
44+
if (values.length == 0) {
45+
throw new IllegalArgumentException("Enum class must have at least one value");
46+
}
47+
return values[this.random().nextInt(values.length)];
48+
}
49+
50+
public <T extends EntityId<Long>> T randomEntityId(LongFunction<T> constructor) {
51+
return constructor.apply(
52+
super.random().nextInt(9999999)
53+
);
54+
}
55+
56+
public <T extends EntityId<String>> T randomEntityId(Function<String, T> constructor) {
57+
return constructor.apply(
58+
super.internet().uuid()
59+
);
60+
}
61+
62+
public String unique(String value) {
63+
return value + "_" + super.random().nextInt(9999999);
64+
}
65+
66+
public RandomNumericRange numericRange() {
67+
return new RandomNumericRange(
68+
super.getFaker()
69+
);
70+
}
71+
72+
public static final class RandomNumericRange {
73+
private final Faker parent;
74+
75+
private RandomNumericRange(Faker parent) {
76+
this.parent = parent;
77+
}
78+
79+
public NumericRange random() {
80+
return random(-999999999, 999999999);
81+
}
82+
83+
public NumericRange positive() {
84+
return random(1, 999999999);
85+
}
86+
87+
public NumericRange positiveOrZero() {
88+
return random(0, 999999999);
89+
}
90+
91+
public NumericRange negative() {
92+
return random(-999999999, -1);
93+
}
94+
95+
public NumericRange negativeOrZero() {
96+
return random(-999999999, 0);
97+
}
98+
99+
public NumericRange random(double min, double max) {
100+
var lower = parent.random().nextDouble(min, max - 1);
101+
var upper = parent.random().nextDouble(lower, max);
102+
103+
return new NumericRange(
104+
ScaledBigDecimal.valueOf(lower),
105+
ScaledBigDecimal.valueOf(upper)
106+
);
107+
}
108+
109+
public record NumericRange(ScaledBigDecimal lower, ScaledBigDecimal upper) {
110+
111+
}
112+
}
113+
}

src/main/java/it/aboutbits/springboot/testing/testdata/base/FakerExtended.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import java.util.function.Function;
1414
import java.util.function.LongFunction;
1515

16+
/**
17+
* @deprecated Moved. Use {@link it.aboutbits.springboot.testing.testdata.FakerExtended} instead. This class will be removed in a future release.
18+
*/
19+
@Deprecated(forRemoval = true)
1620
@NullMarked
1721
public class FakerExtended extends Faker {
1822
public FakerExtended() {

src/main/java/it/aboutbits/springboot/testing/testdata/base/TestDataCreator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package it.aboutbits.springboot.testing.testdata.base;
22

3+
import it.aboutbits.springboot.testing.testdata.FakerExtended;
34
import org.jspecify.annotations.NullMarked;
45

56
import java.util.ArrayList;

0 commit comments

Comments
 (0)