-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStatementResultTest.java
More file actions
136 lines (89 loc) · 4.08 KB
/
StatementResultTest.java
File metadata and controls
136 lines (89 loc) · 4.08 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
/*
* 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.arrayWithSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
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.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.util.ResourceUtils;
/**
* StatementResult Tests.
*
* @author Lukáš Sahula
* @author Martin Myslik
* @author Thomas Turrell-Croft
*/
@DisplayName("StatementResult tests")
class StatementResultTest {
final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
@Test
void whenDeserializingStatementResultThenResultIsInstanceOfStatementResult() throws Exception {
final File file = ResourceUtils.getFile("classpath:statement_result/statement_result.json");
// When Deserializing StatementResult
final StatementResult result = objectMapper.readValue(file, StatementResult.class);
// Then Result Is Instance Of StatementResult
assertThat(result, instanceOf(StatementResult.class));
}
@Test
void whenDeserializingStatementResultThenStatementsIsInstanceOfStatement() throws Exception {
final File file = ResourceUtils.getFile("classpath:statement_result/statement_result.json");
// When Deserializing StatementResult
final StatementResult result = objectMapper.readValue(file, StatementResult.class);
// Then Statements Is Instance Of Statement
assertThat(result.getStatements()[0], instanceOf(Statement.class));
}
@Test
void whenDeserializingStatementResultThenMoreIsExpected() throws Exception {
final File file = ResourceUtils.getFile("classpath:statement_result/statement_result.json");
// When Deserializing StatementResult
final StatementResult result = objectMapper.readValue(file, StatementResult.class);
// Then More Is Expected
assertThat(result.getMore(), is(URI.create("123")));
}
@Test
void whenSerializingStatementResultThenResultIsEqualToExpectedJson() throws IOException {
final StatementResult statementResult = StatementResult.builder()
.addStatement(s -> s.id(UUID.fromString("fd41c918-b88b-4b20-a0a5-a4c32391aaa0")))
.more(URI.create("123"))
.build();
// When Serializing StatementResult
final JsonNode result = objectMapper.readTree(objectMapper.writeValueAsString(statementResult));
// Then Result Is Equal To Expected Json
assertThat(result, is(objectMapper.readTree(
"{\"statements\":[{\"id\":\"fd41c918-b88b-4b20-a0a5-a4c32391aaa0\"}],\"more\":\"123\"}")));
}
@Test
void whenCallingToStringThenResultIsExpected() throws IOException {
final StatementResult statementResult = StatementResult.builder()
.addStatement(s -> s
.id(UUID.fromString("fd41c918-b88b-4b20-a0a5-a4c32391aaa0"))
.version("1.0.3"))
.more(URI.create("123"))
.build();
// When Calling To String
final String result = statementResult.toString();
// Then Result Is Expected
assertThat(result, is(
"StatementResult(statements=[Statement(id=fd41c918-b88b-4b20-a0a5-a4c32391aaa0, actor=null, verb=null, object=null, result=null, context=null, timestamp=null, stored=null, authority=null, version=1.0.3, attachments=null)], more=123)"));
}
@Test
void whenBuildingStatementResultWithTwoStatementsThenStatmentsIsArrayWithSizeTwo() {
// When Building StatementResult With Two Statements
final StatementResult statementResult = StatementResult.builder()
.addStatement(s -> s.id(UUID.fromString("fd41c918-b88b-4b20-a0a5-a4c32391aaa0")))
.addStatement(s -> s.id(UUID.fromString("0b05db78-5554-4cde-8673-56ca15580d1b")))
.more(URI.create("123"))
.build();
// Then Statments Is Array With Size Two
assertThat(statementResult.getStatements(), arrayWithSize(2));
}
}