-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVerbTests.java
More file actions
344 lines (223 loc) · 8.49 KB
/
VerbTests.java
File metadata and controls
344 lines (223 loc) · 8.49 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright 2016-2023 Berry Cloud Ltd. All rights reserved.
*/
package dev.learning.xapi.model;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Locale;
import java.util.Set;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.util.ResourceUtils;
/**
* Verb Tests.
*
* @author Lukáš Sahula
* @author Thomas Turrell-Croft
*/
@DisplayName("Verb tests")
class VerbTests {
private final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
/*
* Deserialization tests.
*/
@Test
void whenDeserializingVerbThenResultIsInstanceOfVerb() throws IOException {
final File file = ResourceUtils.getFile("classpath:verb/verb.json");
// When Deserializing Verb
final Verb result = objectMapper.readValue(file, Verb.class);
// Then Result Is Instance Of Verb
assertThat(result, instanceOf(Verb.class));
}
@Test
void whenDeserializingVerbThenIdIsExpected() throws IOException {
final File file = ResourceUtils.getFile("classpath:verb/verb.json");
// When Deserializing Verb
final Verb result = objectMapper.readValue(file, Verb.class);
// Then Id Is Expected
assertThat(result.getId(), is(URI.create("http://adlnet.gov/expapi/verbs/answered")));
}
/*
* Serialization tests.
*/
@Test
void whenSerializingVerbThenResultIsEqualToExpectedJson() throws IOException {
final Verb verb = Verb.builder()
.id(URI.create("http://adlnet.gov/expapi/verbs/answered"))
.addDisplay(Locale.US, "answered")
.build();
// When Serializing Verb
final JsonNode result = objectMapper.readTree(objectMapper.writeValueAsString(verb));
// Then Result Is Equal To Expected Json
assertThat(result,
is(objectMapper.readTree(ResourceUtils.getFile("classpath:verb/verb.json"))));
}
@Test
void whenSerializingVerbWithoutDisplayThenResultIsEqualToExpectedJson() throws IOException {
final Verb verb = Verb.builder()
.id(URI.create("http://adlnet.gov/expapi/verbs/answered"))
.build();
// When Serializing Verb Without Display
final JsonNode result = objectMapper.readTree(objectMapper.writeValueAsString(verb));
// Then Result Is Equal To Expected Json
assertThat(result, is(
objectMapper.readTree(ResourceUtils.getFile("classpath:verb/verb_without_display.json"))));
}
/*
* toString Test.
*/
@Test
void whenCallingToStringThenResultIsExpected() throws IOException {
final Verb verb =
objectMapper.readValue(ResourceUtils.getFile("classpath:verb/verb.json"), Verb.class);
// When Calling ToString
final String result = verb.toString();
// Then Result Is Expected
assertThat(result,
is("Verb(id=http://adlnet.gov/expapi/verbs/answered, display={en_US=answered})"));
}
/*
* Constructor tests.
*/
@Test
void givenVerbIsConstructedWithIdAndNameWhenGettingDisplayForLocaleUNDThenResultIsExpected() {
// Given Verb Is Constructed With Id And Name
final Verb verb = new Verb("http://adlnet.gov/expapi/verbs/answered", "answered");
// When Getting Display For Locale UND
final var result = verb.getDisplay().get(Locale.forLanguageTag("UND"));
// Then Result Is Expected
assertThat(result, is("answered"));
}
@Test
void givenVerbIsConstructedWithIdAndNameWhenGettingIdThenIdIsExpected() {
// Given Verb Is Constructed With Id And Name
final Verb verb = new Verb("http://adlnet.gov/expapi/verbs/answered", "answered");
// When Getting Id
final var result = verb.getId();
// Then Id Is Expected
assertThat(result, is(URI.create("http://adlnet.gov/expapi/verbs/answered")));
}
@Test
void givenVerbIsConstructedWithIdWhenGettingIdThenIdIsExpected() {
// Given Verb Is Constructed With Id
final Verb verb = new Verb("http://adlnet.gov/expapi/verbs/answered");
// When Getting Id
final var result = verb.getId();
// Then Id Is Expected
assertThat(result, is(URI.create("http://adlnet.gov/expapi/verbs/answered")));
}
/*
* Builder Tests
*/
@Test
void whenBuildingVerbWithTwoDisplayValuesThenDisplayLanguageMapHasTwoEntries() {
// When Building Verb With Two Display Values
final Verb verb = Verb.builder()
.id("http://adlnet.gov/expapi/verbs/answered")
.addDisplay(Locale.US, "answered")
.addDisplay(Locale.GERMAN, "antwortete")
.build();
// Then Display Language Map Has Two Entries
assertThat(verb.getDisplay(), aMapWithSize(2));
}
/*
* Equality tests.
*/
@Test
void givenVerbWithoutDisplayPropertyWhenTestingEqualityWithVerbWithDisplayPropertyThenVerbsAreEqual() {
// Given Verb Without Display Property
final Verb verb = Verb.builder()
.id(URI.create("http://adlnet.gov/expapi/verbs/answered"))
.build();
// When Testing Equality With Verb With Display Property
final boolean result = verb.equals(Verb.builder()
.id("http://adlnet.gov/expapi/verbs/answered")
.addDisplay(Locale.US, "answered")
.build());
// Then Verbs Are Equal
assertThat(result, is(true));
}
@Test
void givenVerbWithGermanDisplayPropertyWhenTestingEqualityWithVerbWithEnglishDisplayPropertyThenVerbsAreEqual() {
// Given Verb With German Display Property
final Verb verb = Verb.builder()
.id("http://adlnet.gov/expapi/verbs/answered")
.addDisplay(Locale.GERMAN, "antwortete")
.build();
// When Testing Equality With Verb With English Display Property
final boolean result = verb.equals(Verb.builder()
.id("http://adlnet.gov/expapi/verbs/answered")
.addDisplay(Locale.US, "answered")
.build());
// Then Verbs Are Equal
assertThat(result, is(true));
}
@Test
void givenVerbWithIdWhenTestingEqualityWithVerbWithDifferentIdThenVerbsAreNotEqual() {
// Given Verb With Id
final Verb verb = Verb.builder()
.id("http://adlnet.gov/expapi/verbs/answered")
.build();
// When Testing Equality With Verb With Different Id
final boolean result = verb.equals(Verb.builder()
.id("http://adlnet.gov/expapi/verbs/interacted")
.build());
// Then Verbs Are Not Equal
assertThat(result, not(true));
}
/*
* Method tests
*/
@Test
void givenVerbHasIdVoidedWhenCallingIsVoidedThenResultIsTrue() {
// Given Verb Has Id Voided
final Verb verb = (Verb.builder()
.id("http://adlnet.gov/expapi/verbs/voided")
.build());
// When Calling isVoided
final boolean result = verb.isVoided();
// Then Result Is True
assertThat(result, is(true));
}
@Test
void givenVerbDoesNotHaveIdVoidedWhenCallingIsVoidedThenResultIsFalse() {
// Given Verb Does Not Have Id Voided
final Verb verb = (Verb.builder()
.id("http://adlnet.gov/expapi/verbs/answered")
.build());
// When Calling isVoided
final boolean result = verb.isVoided();
// Then Result Is False
assertThat(result, is(false));
}
@Test
void whenValidatingVerbWithAllRequiredPropertiesThenConstraintViolationsSizeIsZero() {
final Verb verb = Verb.builder().id("http://adlnet.gov/expapi/verbs/answered")
.addDisplay(Locale.US, "answered").build();
// When Validating Interaction Component With All Required Properties
final Set<ConstraintViolation<Verb>> constraintViolations = validator.validate(verb);
// Then ConstraintViolations Size Is Zero
assertThat(constraintViolations, hasSize(0));
}
@Test
void whenValidatingVerbWithoutIdThenConstraintViolationsSizeIsOne() {
final Verb verb = Verb.builder().addDisplay(Locale.US, "answered").build();
// When Validating Interaction Component Without Id
final Set<ConstraintViolation<Verb>> constraintViolations = validator.validate(verb);
// Then ConstraintViolations Size Is One
assertThat(constraintViolations, hasSize(1));
}
}