-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAboutTests.java
More file actions
162 lines (106 loc) · 4.49 KB
/
Copy pathAboutTests.java
File metadata and controls
162 lines (106 loc) · 4.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
/*
* 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 com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.util.ResourceUtils;
/**
* About Tests.
*
* @author Lukáš Sahula
* @author Martin Myslik
* @author Thomas Turrell-Croft
*/
@DisplayName("About tests")
class AboutTests {
private final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
@Test
void whenDeserializingAboutThenResultIsInstanceOfAbout() throws IOException {
final var file = ResourceUtils.getFile("classpath:about/about.json");
// When Deserializing About
final var result = objectMapper.readValue(file, About.class);
// Then Result Is Instance Of About
assertThat(result, instanceOf(About.class));
}
@Test
void whenDeserializingAboutWithVersionThenVersionIsExpected() throws IOException {
final var file = ResourceUtils.getFile("classpath:about/about.json");
// When Deserializing About With Version
final var result = objectMapper.readValue(file, About.class);
// Then Version Is Expected
assertThat(result.getVersion(), is(Collections.singletonList("1.0")));
}
@Test
void whenDeserializingAboutWithMultipleVersionsThenMultipleVersionsAreExpected()
throws IOException {
final var file = ResourceUtils.getFile("classpath:about/about_with_multiple_versions.json");
// When Deserializing About With Multiple Versions
final var result = objectMapper.readValue(file, About.class);
// Then Multiple Versions Are Expected
assertThat(result.getVersion(), is(Arrays.asList("1.0", "1.0.1")));
}
@Test
void whenDeserializingAboutWithExtensionsThenExtensionsAreExpected() throws IOException {
final var file = ResourceUtils.getFile("classpath:about/about_with_extensions.json");
// When Deserializing About With Extensions
final var result = objectMapper.readValue(file, About.class);
// Then Extensions Are Expected
assertThat(result.getExtensions().get(URI.create("http://url")), is("www.example.com"));
}
@Test
void whenDeserializingAboutWithVersionAsNumberThenVersionIsExpected() throws IOException {
final var file = ResourceUtils.getFile("classpath:about/about_with_version_as_number.json");
// When Deserializing About With Version As Number
final var result = objectMapper.readValue(file, About.class);
// Then Version Is Expected
assertThat(result.getVersion(), is(Collections.singletonList("1.0")));
}
@Test
void whenSerializingAboutThenResultIsEqualToExpectedJson() throws IOException {
final var about = About.builder()
.version(Collections.singletonList("1.0"))
.build();
// When Serializing About
final var result = objectMapper.readTree(objectMapper.writeValueAsString(about));
// Then Result Is Equal To Expected Json
assertThat(result,
is(objectMapper.readTree(ResourceUtils.getFile("classpath:about/about.json"))));
}
@Test
void whenSerializingAboutWithExtensionsThenResultIsEqualToExpectedJson() throws IOException {
final var extensions = new LinkedHashMap<URI, Object>();
extensions.put(URI.create("http://url"), "www.example.com");
final var about = About.builder()
.version(Collections.singletonList("1.0"))
.extensions(extensions)
.build();
// When Serializing About With Extensions
final var result = objectMapper.readTree(objectMapper.writeValueAsString(about));
// Then Result Is Equal To Expected Json
assertThat(result, is(objectMapper
.readTree(ResourceUtils.getFile("classpath:about/about_with_extensions.json"))));
}
@Test
void whenCallingToStringThenResultIsExpected() {
final var extensions = new LinkedHashMap<URI, Object>();
extensions.put(URI.create("http://url"), "www.example.com");
final var about = About.builder()
.version(Collections.singletonList("1.0"))
.extensions(extensions)
.build();
// When Calling ToString
final var result = about.toString();
// Then Result Is Expected
assertThat(result, is("About(version=[1.0], extensions={http://url=www.example.com})"));
}
}