-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInstancio4CollectionsTest.java
201 lines (171 loc) · 6.74 KB
/
Instancio4CollectionsTest.java
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.example;
import org.example.generics.Item;
import org.example.generics.Pair;
import org.example.person.Address;
import org.example.person.Gender;
import org.example.person.Person;
import org.example.person.Phone;
import org.example.person.PhoneWithExtension;
import org.instancio.Instancio;
import org.instancio.Result;
import org.instancio.TypeToken;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.instancio.Select.all;
import static org.instancio.Select.allInts;
import static org.instancio.Select.allStrings;
import static org.instancio.Select.field;
import static org.instancio.Select.root;
/**
* Creating collections
*
* <ul>
* <li>ofList(), ofSet(), ofMap() - collection APIs</li>
* <li>stream() - creating collections via {@link java.util.stream.Stream}</li>
* <li>customising collections via generator specs</li>
* </ul>
*/
class Instancio4CollectionsTest {
@Test
void ofList() {
List<Person> list = Instancio.ofList(Person.class).create();
assertThat(list).isNotEmpty();
}
@Test
@DisplayName("collections can be returned as a 'Result' object containing the result and the seed")
void asResult() {
Result<List<Person>> result = Instancio.ofList(Person.class).size(3).asResult();
List<Person> list = result.get();
assertThat(list).isNotEmpty();
System.out.println("Seed to create the list: " + result.getSeed());
}
@Test
void ofSet() {
Set<Phone> set = Instancio.ofSet(Phone.class).size(5).create();
assertThat(set).hasSize(5);
}
@Test
@DisplayName("Using root() selector to specify the map type")
void ofMapWithSubtype() {
Map<String, Address> map = Instancio.ofMap(String.class, Address.class)
.size(5)
.subtype(root(), TreeMap.class)
.create();
assertThat(map).isInstanceOf(TreeMap.class).hasSize(5);
}
@Test
@DisplayName("Collections can be customised using the builder API")
void ofListWithBuilderAPI() {
List<Person> persons = Instancio.ofList(Person.class).size(10)
.set(field(Person::getFullName), "Homer Simpson")
.generate(field(Person::getAge), gen -> gen.ints().range(40, 50))
.withNullable(all(Gender.class))
.subtype(all(Phone.class), PhoneWithExtension.class)
.create();
assertThat(persons).hasSize(10).allSatisfy(person -> {
assertThat(person.getFullName()).isEqualTo("Homer Simpson");
assertThat(person.getAge()).isBetween(40, 50);
assertThat(person.getAddress().getPhoneNumbers()).allSatisfy(phone -> {
assertThat(phone).isInstanceOf(PhoneWithExtension.class);
});
});
}
//
// Stream API: alternative to the collection API
// Note: stream API returns infinite streams, therefore limit() must be called
//
@Test
void streamClass() {
final Set<Phone> phones = Instancio.stream(Phone.class)
.limit(5)
.collect(Collectors.toSet());
assertThat(phones).hasSize(5);
}
@Test
@DisplayName("Streams can be customised using the builder API")
void streamTypeToken() {
final List<Pair<Integer, Item<String>>> list = Instancio.of(new TypeToken<Pair<Integer, Item<String>>>() {})
.generate(allStrings(), gen -> gen.string().digits().length(3))
.generate(allInts(), gen -> gen.ints().range(1, 9))
.stream()
.limit(5)
.collect(Collectors.toList());
assertThat(list).hasSize(5).allSatisfy(pair -> {
assertThat(pair.getLeft()).isBetween(1, 9);
final Item<String> right = pair.getRight();
assertThat(right.getValue()).containsOnlyDigits().hasSize(3);
});
}
//
// Customising collections via generator specs
//
@Test
@DisplayName("Setting up a map with expected entries")
void mapWithExpectedEntries() {
Map<String, Integer> map = Instancio.ofMap(String.class, Integer.class)
.generate(root(), gen -> gen.map()
.minSize(5)
.with("bar", 100)
.with("baz", 200))
.create();
assertThat(map)
.hasSizeGreaterThanOrEqualTo(5)
.containsEntry("bar", 100)
.containsEntry("baz", 200);
}
@Test
@DisplayName("Setting up a map with expected keys")
void usingMapGeneratorSpec() {
Map<String, Address> map = Instancio.ofMap(String.class, Address.class)
.generate(all(Map.class), gen -> gen.map()
.subtype(TreeMap.class)
.size(5)
.withKeys("foo", "bar"))
.create();
assertThat(map).isInstanceOf(TreeMap.class)
.hasSize(5)
.containsKeys("foo", "bar");
}
@Test
@DisplayName("Using list generator spec to customise the list with expected elements")
void usingListGeneratorSpec() {
Phone expectedPhone1 = new Phone("+33", "12345678");
Phone expectedPhone2 = new Phone("+49", "345678901");
List<Phone> list = Instancio.ofList(Phone.class)
.generate(root(), gen -> gen.collection()
.minSize(100)
.nullableElements()
.with(expectedPhone1, expectedPhone2))
.create();
assertThat(list)
.hasSizeGreaterThanOrEqualTo(100)
.containsNull()
.contains(expectedPhone1, expectedPhone2);
}
//
// Using emit() to set expected values for a certain field
//
@Test
@DisplayName("Create 10 persons with expected countries")
void usingEmit() {
final int numberOfPeople = 10;
List<Person> persons = Instancio.ofList(Person.class)
.size(numberOfPeople)
.generate(field(Address::getCountry), gen -> gen.emit()
.item("Sweden", 2)
.item("Denmark", 2)
.items("Poland", "Italy", "Germany", "Spain", "Romania", "Netherlands"))
.create();
assertThat(persons)
.extracting(Person::getAddress)
.extracting(Address::getCountry)
.containsExactly("Sweden", "Sweden", "Denmark", "Denmark",
"Poland", "Italy", "Germany", "Spain", "Romania", "Netherlands");
}
}