-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathActivityDefinitionTests.java
More file actions
555 lines (368 loc) · 19.2 KB
/
ActivityDefinitionTests.java
File metadata and controls
555 lines (368 loc) · 19.2 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* Copyright 2016-2023 Berry Cloud Ltd. All rights reserved.
*/
package dev.learning.xapi.model;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.springframework.integration.test.matcher.MapContentMatchers.hasAllEntries;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.learning.xapi.model.validation.constraints.HasScheme;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.util.ResourceUtils;
/**
* Activity Definition Tests.
*
* @author Lukáš Sahula
* @author Martin Myslik
* @author Thomas Turrell-Croft
*/
@DisplayName("ActivityDefinition tests")
class ActivityDefinitionTests {
private final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
@Test
void whenDeserializingActivityDefinitionThenResultIsInstanceOfActivityDefinition()
throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Result Is Instance Of Activity Definition
assertThat(result, instanceOf(ActivityDefinition.class));
}
@Test
void whenDeserializingActivityDefinitionThenMoreInfoIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then MoreInfo Is Expected
assertThat(result.getMoreInfo(), is(URI.create("http://example.com/more")));
}
@Test
void whenDeserializingActivityDefinitionThenInteractionTypeIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then InteractionType Is Expected
assertThat(result.getInteractionType(), is(InteractionType.TRUE_FALSE));
}
@Test
void whenDeserializingActivityDefinitionThenCorrectResponsesPatternIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then CorrectResponsesPattern Is Expected
assertThat(result.getCorrectResponsesPattern(), is(Collections.singletonList(
"{case_matters=false}{lang=en}To store and provide access to learning experiences.")));
}
@Test
void whenDeserializingActivityDefinitionThenExtensionsAreExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Extensions Are Expected
assertThat(result.getExtensions().get(URI.create("http://url")), is("www.example.com"));
}
@Test
void whenDeserializingActivityDefinitionThenNameIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Name Is Expected
assertThat(result.getName().get(Locale.US),
is("Does the xAPI include the concept of statements?"));
}
@Test
void whenDeserializingActivityDefinitionThenDescriptionIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Description Is Expected
assertThat(result.getDescription().get(Locale.US), is("pong[.]1:[,]dg[.]:10[,]lunch[.]"));
}
@Test
void whenDeserializingActivityDefinitionThenTypeIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Type Is Expected
assertThat(result.getType(),
is(URI.create("http://adlnet.gov/expapi/activities/cmi.interaction")));
}
@Test
void whenDeserializingActivityDefinitionWithoutTypeWhenDeserializedThenTypeIsNull()
throws Exception {
final var file = ResourceUtils
.getFile("classpath:activity_definition/activity_definition_without_type.json");
// When Deserializing Activity Definition Without Type
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Type Is Null
assertThat(result.getType(), nullValue());
}
@Test
void whenDeserializingActivityDefinitionWithChoicesThenChoicesIDIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition With Choices
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Choices ID Is Expected
assertThat(result.getChoices().get(0).getId(), is("1"));
}
@Test
void whenDeserializingActivityDefinitionWithChoicesThenChoicesDescriptionIsExpected()
throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition With Choices
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Choices Description Is Expected
assertThat(result.getChoices().get(0).getDescription().get(Locale.US),
is("Does the xAPI include the concept of statements?"));
}
@Test
void whenDeserializingActivityDefinitionWithScaledThenScaleIDIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing ActivityDefinition With Scaled
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Scale ID Is Expected
assertThat(result.getScale().get(0).getId(), is("1"));
}
@Test
void whenDeserializingActivityDefinitionWithScaledThenScaleDescriptionIsExpected()
throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing Activity Definition With Scaled
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Scale Description Is Expected
assertThat(result.getScale().get(0).getDescription().get(Locale.US),
is("Does the xAPI include the concept of statements?"));
}
@Test
void whenDeserializingActivityDefinitionWithSourceThenSourceIDIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing Activity Definition With Source
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Source ID Is Expected
assertThat(result.getSource().get(0).getId(), is("1"));
}
@Test
void whenDeserializingActivityDefinitionWithSourceThenSourceDescriptionIsExpected()
throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing Activity Definition With Source
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Source Description Is Expected
assertThat(result.getSource().get(0).getDescription().get(Locale.US),
is("Does the xAPI include the concept of statements?"));
}
@Test
void whenDeserializingActivityDefinitionWithTargetThenTargetIDIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing Activity Definition With Target
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Target ID Is Expected
assertThat(result.getTarget().get(0).getId(), is("1"));
}
@Test
void whenDeserializingActivityDefinitionWithTargetThenTargetDescriptionIsExpected()
throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing Activity Definition With Target
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Target Description Is Expected
assertThat(result.getTarget().get(0).getDescription().get(Locale.US),
is("Does the xAPI include the concept of statements?"));
}
@Test
void whenDeserializingActivityDefinitionWithStepsThenStepsIDIsExpected() throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing Activity Definition With Steps
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Steps ID Is Expected
assertThat(result.getSteps().get(0).getId(), is("1"));
}
@Test
void whenDeserializingActivityDefinitionWithStepsThenStepsDescriptionIsExpected()
throws Exception {
final var file =
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json");
// When Deserializing Activity Definition With Steps
final var result = objectMapper.readValue(file, ActivityDefinition.class);
// Then Steps Description Is Expected
assertThat(result.getSteps().get(0).getDescription().get(Locale.US),
is("Does the xAPI include the concept of statements?"));
}
@Test
void whenSerializingActivityDefinitionOfInteractionTypeTrueFalseThenResultIsEqualToExpectedJson()
throws IOException {
final var activityDefinition = ActivityDefinition.builder()
.addName(Locale.US, "True false question")
.addDescription(Locale.US, "Does the xAPI include the concept of statements?")
.interactionType(InteractionType.TRUE_FALSE)
.correctResponsesPattern(Collections.singletonList("true"))
.type(URI.create("http://adlnet.gov/expapi/activities/cmi.interaction"))
.build();
// When Serializing Activity Definition Of InteractionType True False
final var result = objectMapper.readTree(objectMapper.writeValueAsString(activityDefinition));
// Then Result Is Equal To Expected Json
assertThat(result, is(objectMapper
.readTree(ResourceUtils.getFile("classpath:activity_definition/true_false.json"))));
}
@Test
void whenSerializingActivityDefinitionOfInteractionTypeChoiceThenResultIsEqualToExpectedJson()
throws IOException {
final var activityDefinition = ActivityDefinition.builder()
.addName(Locale.US, "Choice")
.addDescription(Locale.US, "Which of these prototypes are available at the beta site?")
.interactionType(InteractionType.CHOICE)
.correctResponsesPattern(Collections.singletonList("golf[,]tetris"))
.addChoice(c -> c.id("golf").addDescription(Locale.US, "Golf Example"))
.addChoice(c -> c.id("facebook").addDescription(Locale.US, "Facebook App"))
.addChoice(c -> c.id("tetris").addDescription(Locale.US, "Tetris Example"))
.addChoice(c -> c.id("scrabble").addDescription(Locale.US, "Scrabble Example"))
.type(URI.create("http://adlnet.gov/expapi/activities/cmi.interaction"))
.build();
// When Serializing Activity Definition Of InteractionType Choice
final var result = objectMapper.readTree(objectMapper.writeValueAsString(activityDefinition));
// Then Result Is Equal To Expected Json
assertThat(result, is(
objectMapper.readTree(ResourceUtils.getFile("classpath:activity_definition/choice.json"))));
}
@Test
void whenCallingToStringThenResultIsExpected() throws Exception {
final var activityDefinition = objectMapper.readValue(
ResourceUtils.getFile("classpath:activity_definition/activity_definition.json"),
ActivityDefinition.class);
// When Calling ToString
final var result = activityDefinition.toString();
// Then Result Is Expected
assertThat(result, is(
"ActivityDefinition(name={en_US=Does the xAPI include the concept of statements?}, description={en_US=pong[.]1:[,]dg[.]:10[,]lunch[.]}, type=http://adlnet.gov/expapi/activities/cmi.interaction, moreInfo=http://example.com/more, interactionType=TRUE_FALSE, correctResponsesPattern=[{case_matters=false}{lang=en}To store and provide access to learning experiences.], choices=[InteractionComponent(id=1, description={en_US=Does the xAPI include the concept of statements?})], scale=[InteractionComponent(id=1, description={en_US=Does the xAPI include the concept of statements?})], source=[InteractionComponent(id=1, description={en_US=Does the xAPI include the concept of statements?})], target=[InteractionComponent(id=1, description={en_US=Does the xAPI include the concept of statements?})], steps=[InteractionComponent(id=1, description={en_US=Does the xAPI include the concept of statements?})], extensions={http://url=www.example.com})"));
}
@Test
void whenBuildingActivityDefinitionWithTwoNameValuesThenNameLanguageMapHasTwoEntries() {
// When Building ActivityDefinition With Two Name Values
final var activityDefinition = ActivityDefinition.builder()
.addName(Locale.US, "True false question")
.addName(Locale.GERMAN, "Richtig / Falsch-Frage")
.addDescription(Locale.US, "Does the xAPI include the concept of statements?")
.interactionType(InteractionType.TRUE_FALSE)
.correctResponsesPattern(Collections.singletonList("true"))
.type(URI.create("http://adlnet.gov/expapi/activities/cmi.interaction"))
.build();
// Then Name Language Map Has Two Entries
assertThat(activityDefinition.getName(), aMapWithSize(2));
}
@Test
void whenBuildingActivityDefinitionWithTwoDescriptionValuesThenDescriptionLanguageMapHasTwoEntries() {
// When Building ActivityDefinition With Two Description Values
final var activityDefinition = ActivityDefinition.builder()
.addName(Locale.US, "True false question")
.addDescription(Locale.US, "Does the xAPI include the concept of statements?")
.addDescription(Locale.GERMAN, "Enthält die xAPI das Konzept von Anweisungen?")
.interactionType(InteractionType.TRUE_FALSE)
.correctResponsesPattern(Collections.singletonList("true"))
.type(URI.create("http://adlnet.gov/expapi/activities/cmi.interaction"))
.build();
// Then Description Language Map Has Two Entries
assertThat(activityDefinition.getDescription(), aMapWithSize(2));
}
@Test
void whenMergingActivityDefinitionsWithNamesThenMergedNameIsExpected() throws IOException {
final var activityDefinition1 =
ActivityDefinition.builder().addName(Locale.UK, "Colour").build();
final var x =
objectMapper.valueToTree(ActivityDefinition.builder().addName(Locale.US, "Color").build());
final var expected = new LanguageMap();
expected.put(Locale.UK, "Colour");
expected.put(Locale.US, "Color");
// When Merging ActivityDefinitions With Names
final var merged =
(ActivityDefinition) objectMapper.readerForUpdating(activityDefinition1).readValue(x);
// Then Merged Name Is Expected
assertThat(merged.getName(), hasAllEntries(expected));
}
@Test
void whenMergingActivityDefinitionsWithDescriptionsThenMergedDescriptionIsExpected()
throws IOException {
final var activityDefinition1 =
ActivityDefinition.builder().addDescription(Locale.UK, "flavour").build();
final var x = objectMapper
.valueToTree(ActivityDefinition.builder().addDescription(Locale.US, "flavor").build());
final var expected = new LanguageMap();
expected.put(Locale.UK, "flavour");
expected.put(Locale.US, "flavor");
// When Merging ActivityDefinitions With Descriptions
final var merged =
(ActivityDefinition) objectMapper.readerForUpdating(activityDefinition1).readValue(x);
// Then Merged Description Is Expected
assertThat(merged.getDescription(), hasAllEntries(expected));
}
@Test
void whenMergingActivityDefinitionsWithExtensionsThenMergedExtensionsAreExpected()
throws IOException {
final Map<@HasScheme URI, Object> extensions1 = new HashMap<>();
extensions1.put(URI.create("https://example.com/extensions/1"), "1");
final var activityDefinition1 = ActivityDefinition.builder().addName(Locale.UK, "Colour")
.addDescription(Locale.UK, "flavour").extensions(extensions1).build();
final Map<@HasScheme URI, Object> extensions2 = new HashMap<>();
extensions2.put(URI.create("https://example.com/extensions/2"), "2");
final var x = objectMapper.valueToTree(ActivityDefinition.builder().addName(Locale.US, "Color")
.addDescription(Locale.US, "flavor").extensions(extensions2).build());
final Map<@HasScheme URI, Object> expected = new HashMap<>();
expected.put(URI.create("https://example.com/extensions/1"), "1");
expected.put(URI.create("https://example.com/extensions/2"), "2");
// When Merging ActivityDefinitions With Extensions
final var merged =
(ActivityDefinition) objectMapper.readerForUpdating(activityDefinition1).readValue(x);
// Then Merged Extensions Are Expected
assertThat(merged.getExtensions(), hasAllEntries(expected));
}
@Test
void whenMergingActivityDefinitionsWithNestedExtensionsThenMergedExtensionsAreExpected()
throws IOException {
final Map<@HasScheme URI, Object> extensions1 = new HashMap<>();
extensions1.put(URI.create("https://example.com/extensions/map"),
new HashMap<>(Collections.singletonMap("a", "y")));
final var activityDefinition1 = ActivityDefinition.builder().extensions(extensions1).build();
final Map<@HasScheme URI, Object> extensions2 = new HashMap<>();
extensions2.put(URI.create("https://example.com/extensions/map"),
new HashMap<>(Collections.singletonMap("b", "z")));
final var x =
objectMapper.valueToTree(ActivityDefinition.builder().extensions(extensions2).build());
final Map<String, String> expected = new HashMap<>();
expected.put("a", "y");
expected.put("b", "z");
// When Merging ActivityDefinitions With Nested Extensions
final var merged =
(ActivityDefinition) objectMapper.readerForUpdating(activityDefinition1).readValue(x);
@SuppressWarnings("unchecked")
final var po = (Map<String, String>) merged.getExtensions()
.get(URI.create("https://example.com/extensions/map"));
// Then Merged Extensions Are Expected
assertThat(po, hasAllEntries(expected));
}
}