-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSubStatementTests.java
More file actions
359 lines (222 loc) · 11.5 KB
/
SubStatementTests.java
File metadata and controls
359 lines (222 loc) · 11.5 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
/*
* 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.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.time.Instant;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.util.ResourceUtils;
/**
* SubStatement Tests.
*
* @author Lukáš Sahula
* @author Martin Myslik
* @author István Rátkai (Selindek)
*/
@DisplayName("SubStatement tests")
class SubStatementTests {
private final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
@Test
void whenDeserializingSubStatementThenResultIsInstanceOfSubStatement() throws IOException {
final File file = ResourceUtils.getFile("classpath:sub_statement/sub_statement.json");
// When Deserializing SubStatement
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Result Is Instance Of SubStatement
assertThat(result, instanceOf(SubStatement.class));
}
@Test
void whenDeserializingSubStatementThenTimestampIsExpected() throws IOException {
final File file = ResourceUtils.getFile("classpath:sub_statement/sub_statement.json");
// When Deserializing SubStatement
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Timestamp Is Expected
assertThat(result.getTimestamp(), is(Instant.parse("2015-11-18T11:17:00Z")));
}
@Test
void whenDeserializingSubStatementThenVerbIsInstanceOfVerb() throws IOException {
final File file = ResourceUtils.getFile("classpath:sub_statement/sub_statement.json");
// When Deserializing SubStatement
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Verb Is Instance Of Verb
assertThat(result.getVerb(), instanceOf(Verb.class));
}
@Test
void whenDeserializingSubStatementThenObjectIsInstanceOfStatementReference() throws IOException {
final File file = ResourceUtils.getFile("classpath:sub_statement/sub_statement.json");
// When Deserializing SubStatement
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Object Is Instance Of StatementReference
assertThat(result.getObject(), instanceOf(StatementReference.class));
}
@Test
void whenDeserializingSubStatementThenResultIsInstanceOfResult() throws IOException {
final File file = ResourceUtils.getFile("classpath:sub_statement/sub_statement.json");
// When Deserializing SubStatement
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Result Is Instance Of Result
assertThat(result.getResult(), instanceOf(Result.class));
}
@Test
void whenDeserializingSubStatementThenContextIsInstanceOfContext() throws IOException {
final File file = ResourceUtils.getFile("classpath:sub_statement/sub_statement.json");
// When Deserializing SubStatement
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Context Is Instance Of Context
assertThat(result.getContext(), instanceOf(Context.class));
}
@Test
void whenDeserializingSubStatementThenActorIsInstanceOfActor() throws IOException {
final File file = ResourceUtils.getFile("classpath:sub_statement/sub_statement.json");
// When Deserializing SubStatement
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Actor Is Instance Of Actor
assertThat(result.getActor(), instanceOf(Actor.class));
}
@Test
void whenDeserializingSubStatementThenAttachmentsIsInstanceOfAttachment() throws IOException {
final File file = ResourceUtils.getFile("classpath:sub_statement/sub_statement.json");
// When Deserializing SubStatement
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Attachments Is Instance Of Attachment
assertThat(result.getAttachments().get(0), instanceOf(Attachment.class));
}
@Test
void whenDeserializingSubStatementWithObjectOfTypeSubStatementThenExceptionIsThrown()
throws IOException {
final File file = ResourceUtils
.getFile("classpath:sub_statement/sub_statement_with_object_of_type_sub_statement.json");
assertThrows(Exception.class, () -> {
// When Deserializing SubStatement With Object Of Type SubStatement
objectMapper.readValue(file, SubStatement.class);
// Then Exception Is Thrown
});
}
@Test
void whenDeserializingSubStatementWithObjectOfTypeActivityThenObjectIsInstanceOfActivity()
throws IOException {
final File file = ResourceUtils
.getFile("classpath:sub_statement/sub_statement_with_object_of_type_activity.json");
// when Deserializing SubStatement With Object Of Type Activity
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Object Is Instance Of Activity
assertThat(result.getObject(), instanceOf(Activity.class));
}
@Test
void whenDeserializingSubStatementWithObjectOfTypeAgentThenObjectIsInstanceOfAgentThenObjectIsInstanceOfAgent()
throws IOException {
final File file = ResourceUtils
.getFile("classpath:sub_statement/sub_statement_with_object_of_type_agent.json");
// When Deserializing SubStatement With Object Of Type Agent
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Object Is Instance Of Agent
assertThat(result.getObject(), instanceOf(Agent.class));
}
@Test
void whenDeserializingSubStatementWithObjectOfTypeGroupThenObjectIsInstanceOfGroup()
throws IOException {
final File file = ResourceUtils
.getFile("classpath:sub_statement/sub_statement_with_object_of_type_group.json");
// When Deserializing SubStatement With Object Of Type Group
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Object Is Instance Of Group
assertThat(result.getObject(), instanceOf(Group.class));
}
@Test
void whenDeserializingSubStatementWithObjectOfTypeStatementReferenceThenObjectIsInstanceOfStatementReference()
throws IOException {
final File file = ResourceUtils.getFile(
"classpath:sub_statement/sub_statement_with_object_of_type_statement_reference.json");
// When Deserializing SubStatement With Object Of Type StatementReference
final SubStatement result = objectMapper.readValue(file, SubStatement.class);
// Then Object Is Instance Of StatementReference
assertThat(result.getObject(), instanceOf(StatementReference.class));
}
@Test
void whenSerializingSubStatementThenResultIsEqualToExpectedJson() throws IOException {
final Agent agent = Agent.builder().mbox("mailto:agent@example.com").build();
final Group group = Group.builder().name("Example Group").build();
final StatementReference statementRef = StatementReference.builder()
.id(UUID.fromString("9e13cefd-53d3-4eac-b5ed-2cf6693903bb")).build();
final Score score = Score.builder()
.max(5.0f)
.min(0.0f)
.raw(1.0f)
.scaled(1.0f)
.build();
final Result resultInstance = Result.builder()
.score(score)
.completion(true)
.success(true)
.duration("P1D")
.response("test")
.build();
final Activity activity =
Activity.builder().id(URI.create("http://www.example.co.uk/exampleactivity")).build();
final ContextActivities contextActivities = ContextActivities.builder()
.parent(Collections.singletonList(activity))
.grouping(Collections.singletonList(activity))
.category(Collections.singletonList(activity))
.other(Collections.singletonList(activity)).build();
final LinkedHashMap<URI, Object> extensions = new LinkedHashMap<>();
extensions.put(URI.create("http://url"), "www.example.com");
final Context context = Context.builder()
.registration(UUID.fromString("6d969975-8d7e-4506-ac19-877c57f2921a"))
.revision("revision")
.platform("platform")
.language(Locale.US)
.instructor(agent)
.team(group)
.statement(statementRef)
.contextActivities(contextActivities)
.extensions(extensions)
.build();
final Attachment attachment = Attachment.builder().usageType(URI.create("http://example.com"))
.fileUrl(URI.create("http://example.com"))
.addDisplay(Locale.US, "value")
.addDescription(Locale.US, "value")
.length(123)
.sha2("123")
.contentType("file")
.build();
final SubStatement subStatement = SubStatement.builder()
.timestamp(Instant.parse("2015-11-18T11:17:00Z"))
.actor(agent)
.verb(v -> v.id(URI.create("http://example.com/confirmed")).addDisplay(Locale.US,
"confirmed"))
.object(statementRef)
.result(resultInstance)
.context(context)
.attachments(Collections.singletonList(attachment))
.build();
// When Serializing SubStatement
final JsonNode result = objectMapper.readTree(objectMapper.writeValueAsString(subStatement));
// Then Result Is Equal To Expected Json
assertThat(result,
is(objectMapper.readTree(objectMapper.writeValueAsString(objectMapper.readValue(
ResourceUtils.getFile("classpath:sub_statement/sub_statement.json"),
SubStatement.class)))));
}
@Test
void whenCallingToStringThenResultIsExpected() throws IOException {
final SubStatement subStatement = objectMapper.readValue(
ResourceUtils.getFile("classpath:sub_statement/sub_statement.json"), SubStatement.class);
// When Calling ToString
final String result = subStatement.toString();
// Then Result Is Expected
assertThat(result, is(
"SubStatement(actor=Agent(super=Actor(name=null, mbox=mailto:agent@example.com, mboxSha1sum=null, openid=null, account=null)), verb=Verb(id=http://example.com/confirmed, display={en_US=confirmed}), object=StatementReference(id=9e13cefd-53d3-4eac-b5ed-2cf6693903bb), result=Result(score=Score(scaled=1.0, raw=1.0, min=0.0, max=5.0), success=true, completion=true, response=test, duration=P1D, extensions=null), context=Context(registration=6d969975-8d7e-4506-ac19-877c57f2921a, instructor=Agent(super=Actor(name=null, mbox=mailto:agent@example.com, mboxSha1sum=null, openid=null, account=null)), team=Group(super=Actor(name=Example Group, mbox=null, mboxSha1sum=null, openid=null, account=null), member=null), contextActivities=ContextActivities(parent=[Activity(id=http://www.example.co.uk/exampleactivity, definition=null)], grouping=[Activity(id=http://www.example.co.uk/exampleactivity, definition=null)], category=[Activity(id=http://www.example.co.uk/exampleactivity, definition=null)], other=[Activity(id=http://www.example.co.uk/exampleactivity, definition=null)]), revision=revision, platform=platform, language=en_US, statement=StatementReference(id=9e13cefd-53d3-4eac-b5ed-2cf6693903bb), extensions={http://url=www.example.com}), timestamp=2015-11-18T11:17:00Z, attachments=[Attachment(usageType=http://example.com, display={en_US=value}, description={en_US=value}, contentType=file, length=123, sha2=123, fileUrl=http://example.com)])"));
}
}