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