Skip to content

Commit 3a468b7

Browse files
committed
#970 - Create TCK for media types.
1 parent f9a0c0f commit 3a468b7

10 files changed

+384
-1
lines changed

pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
<java-module-name>spring.hateoas</java-module-name>
7979
<jsonpath.version>2.2.0</jsonpath.version>
8080
<jsr305.version>3.0.2</jsr305.version>
81+
<junit.version>4.12</junit.version>
82+
<junit-jupiter.version>5.3.2</junit-jupiter.version>
8183
<minidevjson.version>2.2.1</minidevjson.version>
8284
<reactor-bom.version>Californium-SR4</reactor-bom.version>
8385
<slf4j.version>1.7.25</slf4j.version>
@@ -550,7 +552,14 @@
550552
<dependency>
551553
<groupId>junit</groupId>
552554
<artifactId>junit</artifactId>
553-
<version>4.12</version>
555+
<version>${junit.version}</version>
556+
<scope>test</scope>
557+
</dependency>
558+
559+
<dependency>
560+
<groupId>org.junit.jupiter</groupId>
561+
<artifactId>junit-jupiter-api</artifactId>
562+
<version>${junit-jupiter.version}</version>
554563
<scope>test</scope>
555564
</dependency>
556565

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.mediatype;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.hateoas.support.MappingUtils.*;
20+
21+
import java.io.ByteArrayInputStream;
22+
import java.io.IOException;
23+
import java.util.Optional;
24+
25+
import org.junit.jupiter.api.Test;
26+
import org.springframework.core.io.ClassPathResource;
27+
import org.springframework.hateoas.Link;
28+
import org.springframework.hateoas.LinkRelation;
29+
import org.springframework.hateoas.Links;
30+
import org.springframework.hateoas.client.LinkDiscoverer;
31+
32+
/**
33+
* Standard collection of {@link LinkDiscoverer} tests.
34+
*
35+
* TODO: Merge with {@link org.springframework.hateoas.client.LinkDiscovererUnitTest}.
36+
*
37+
* @author Greg Turnquist
38+
*/
39+
public interface LinkDiscovererStandardUnitTest {
40+
41+
String RELATION = "http://www.foo.com/bar";
42+
43+
LinkRelation LINK_RELATION = LinkRelation.of(RELATION);
44+
45+
/**
46+
* Media type provider must provider an instance of their {@link LinkDiscoverer}.
47+
*/
48+
LinkDiscoverer getLinkDiscoverer();
49+
50+
/**
51+
* Media type provider must provide a JSON-based collection of links.
52+
*/
53+
default String getInputString() {
54+
55+
try {
56+
return read(new ClassPathResource("standard-link-discoverer-unit-test.json", getClass()));
57+
} catch (IOException e) {
58+
throw new RuntimeException(e);
59+
}
60+
}
61+
62+
@Test
63+
default void findLinkWithRelByString() {
64+
65+
Optional<Link> optionalLink = getLinkDiscoverer().findLinkWithRel(RELATION, getInputString());
66+
67+
assertThat(optionalLink).isNotEmpty();
68+
assertLink(optionalLink.orElseThrow(RuntimeException::new));
69+
}
70+
71+
@Test
72+
default void findLinkWithRelByLinkRelation() {
73+
74+
Optional<Link> optionalLink = getLinkDiscoverer().findLinkWithRel(LINK_RELATION, getInputString());
75+
76+
assertThat(optionalLink).isNotEmpty();
77+
assertLink(optionalLink.orElseThrow(RuntimeException::new));
78+
}
79+
80+
@Test
81+
default void findRequiredLinkWithRelByLinkRelation() {
82+
83+
Link link = getLinkDiscoverer().findRequiredLinkWithRel(LINK_RELATION, getInputString());
84+
85+
assertLink(link);
86+
}
87+
88+
@Test
89+
default void findLinkWithRelByLinkRelationAndInputStream() {
90+
91+
Optional<Link> optionalLink = getLinkDiscoverer().findLinkWithRel(LINK_RELATION,
92+
new ByteArrayInputStream(getInputString().getBytes()));
93+
94+
assertThat(optionalLink).isNotEmpty();
95+
assertLink(optionalLink.orElseThrow(RuntimeException::new));
96+
}
97+
98+
@Test
99+
default void findLinkWithRelByStringAndInputStream() {
100+
101+
Optional<Link> optionalLink = getLinkDiscoverer().findLinkWithRel(RELATION,
102+
new ByteArrayInputStream(getInputString().getBytes()));
103+
104+
assertThat(optionalLink).isNotEmpty();
105+
assertLink(optionalLink.orElseThrow(RuntimeException::new));
106+
}
107+
108+
@Test
109+
default void findRequiredLinkWithRelByLinkRelationAndInputStream() {
110+
111+
Link link = getLinkDiscoverer().findRequiredLinkWithRel(LINK_RELATION,
112+
new ByteArrayInputStream(getInputString().getBytes()));
113+
114+
assertLink(link);
115+
}
116+
117+
@Test
118+
default void findLinksWithRelByString() {
119+
120+
Links links = getLinkDiscoverer().findLinksWithRel(RELATION, getInputString());
121+
122+
assertLinks(links);
123+
}
124+
125+
@Test
126+
default void findLinksWithRelByLinkRelation() {
127+
128+
Links links = getLinkDiscoverer().findLinksWithRel(LINK_RELATION, getInputString());
129+
130+
assertLinks(links);
131+
}
132+
133+
@Test
134+
default void findLinksWithRelByStringAndInputStream() {
135+
136+
Links links = getLinkDiscoverer().findLinksWithRel(RELATION, new ByteArrayInputStream(getInputString().getBytes()));
137+
138+
assertLinks(links);
139+
}
140+
141+
@Test
142+
default void findLinksWithRelByLinkRelationAndInputStream() {
143+
144+
Links links = getLinkDiscoverer().findLinksWithRel(LINK_RELATION, new ByteArrayInputStream(getInputString().getBytes()));
145+
146+
assertLinks(links);
147+
}
148+
149+
/**
150+
* Verify the details of a standard set of {@link Link}s.
151+
*
152+
* @param link
153+
*/
154+
static void assertLink(Link link) {
155+
156+
assertThat(link.getRel()).isEqualTo(LINK_RELATION);
157+
assertThat(link.getHref()).isEqualTo("https://www.foo.com/bar/whatever");
158+
}
159+
160+
/**
161+
* Verify the details of a standard combined set of {@link Links}.
162+
*
163+
* @param links
164+
*/
165+
static void assertLinks(Links links) {
166+
167+
assertThat(links).hasSize(1);
168+
assertThat(links).extracting(Link::getRel).containsExactly(LINK_RELATION);
169+
assertThat(links).extracting(Link::getHref).containsExactly("https://www.foo.com/bar/whatever");
170+
}
171+
172+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.mediatype.collectionjson;
17+
18+
import org.springframework.hateoas.client.LinkDiscoverer;
19+
import org.springframework.hateoas.mediatype.LinkDiscovererStandardUnitTest;
20+
21+
/**
22+
* @author Greg Turnquist
23+
*/
24+
public class CollectionJsonLinkDiscovererStandardUnitTest implements LinkDiscovererStandardUnitTest {
25+
26+
static final LinkDiscoverer discoverer = new CollectionJsonLinkDiscoverer();
27+
28+
@Override
29+
public LinkDiscoverer getLinkDiscoverer() {
30+
return discoverer;
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.mediatype.hal;
17+
18+
import org.springframework.hateoas.client.LinkDiscoverer;
19+
import org.springframework.hateoas.mediatype.LinkDiscovererStandardUnitTest;
20+
21+
/**
22+
* @author Greg Turnquist
23+
*/
24+
public class HalLinkDiscovererStandardUnitTest implements LinkDiscovererStandardUnitTest {
25+
26+
static final LinkDiscoverer discoverer = new HalLinkDiscoverer();
27+
28+
@Override
29+
public LinkDiscoverer getLinkDiscoverer() {
30+
return discoverer;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.mediatype.hal.forms;
17+
18+
import org.springframework.hateoas.client.LinkDiscoverer;
19+
import org.springframework.hateoas.mediatype.LinkDiscovererStandardUnitTest;
20+
21+
/**
22+
* @author Greg Turnquist
23+
*/
24+
public class HalFormsLinkDiscovererStandardUnitTest implements LinkDiscovererStandardUnitTest {
25+
26+
static final LinkDiscoverer discoverer = new HalFormsLinkDiscoverer();
27+
28+
@Override
29+
public LinkDiscoverer getLinkDiscoverer() {
30+
return discoverer;
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.mediatype.uber;
17+
18+
import org.springframework.hateoas.client.LinkDiscoverer;
19+
import org.springframework.hateoas.mediatype.LinkDiscovererStandardUnitTest;
20+
21+
/**
22+
* @author Greg Turnquist
23+
*/
24+
public class UberLinkDiscovererStandardUnitTest implements LinkDiscovererStandardUnitTest {
25+
26+
static final LinkDiscoverer discoverer = new UberLinkDiscoverer();
27+
28+
@Override
29+
public LinkDiscoverer getLinkDiscoverer() {
30+
return discoverer;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"collection" :
3+
{
4+
"version" : "1.0",
5+
"href" : "http://example.com",
6+
"links" : [
7+
{
8+
"rel": "http://www.foo.com/bar",
9+
"href": "https://www.foo.com/bar/whatever"
10+
}
11+
]
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"_links": {
3+
"self": {
4+
"href": "selfHref"
5+
},
6+
"relation": [
7+
{
8+
"href": "firstHref"
9+
},
10+
{
11+
"href": "secondHref"
12+
}
13+
],
14+
"http://www.foo.com/bar": {
15+
"href": "https://www.foo.com/bar/whatever"
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"_links": {
3+
"self": {
4+
"href": "selfHref"
5+
},
6+
"relation": [
7+
{
8+
"href": "firstHref"
9+
},
10+
{
11+
"href": "secondHref"
12+
}
13+
],
14+
"http://www.foo.com/bar": {
15+
"href": "https://www.foo.com/bar/whatever"
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)