Skip to content

Commit 53186ca

Browse files
committed
#864 - Use newer APIs.
1 parent a237116 commit 53186ca

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

src/test/java/org/springframework/hateoas/MappingTestUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public static ObjectMapper defaultObjectMapper() {
5151
return mapper;
5252
}
5353

54+
public static ContextualMapper createMapper(Class<?> context) {
55+
return createMapper(context, objectMapper -> {});
56+
}
57+
5458
public static ContextualMapper createMapper(Class<?> context, Consumer<ObjectMapper> configurer) {
5559

5660
ObjectMapper mapper = defaultObjectMapper();

src/test/java/org/springframework/hateoas/server/mvc/Embedded2IntegrationTest.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
package org.springframework.hateoas.server.mvc;
1717

1818
import static org.assertj.core.api.AssertionsForInterfaceTypes.*;
19+
import static org.springframework.hateoas.MappingTestUtils.*;
1920
import static org.springframework.hateoas.MediaTypes.*;
2021
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
21-
import static org.springframework.hateoas.support.MappingUtils.*;
2222
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
2323
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2424
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
@@ -33,7 +33,6 @@
3333
import org.springframework.beans.factory.annotation.Autowired;
3434
import org.springframework.context.annotation.Bean;
3535
import org.springframework.context.annotation.Configuration;
36-
import org.springframework.core.io.ClassPathResource;
3736
import org.springframework.hateoas.Link;
3837
import org.springframework.hateoas.LinkRelation;
3938
import org.springframework.hateoas.ModelBuilder2;
@@ -68,13 +67,17 @@ public class Embedded2IntegrationTest {
6867

6968
private @Autowired WebApplicationContext context;
7069
private MockMvc mockMvc;
70+
private ContextualMapper contextualMapper;
7171

7272
@BeforeEach
7373
public void setUp() {
74+
7475
this.mockMvc = webAppContextSetup(this.context).build();
76+
this.contextualMapper = createMapper(getClass());
77+
7578
}
7679

77-
@Test
80+
@Test // #864
7881
public void embeddedSpecUsingAPIs() throws Exception {
7982

8083
String results = this.mockMvc.perform(get("/author/1").accept(HAL_JSON)) //
@@ -83,10 +86,10 @@ public void embeddedSpecUsingAPIs() throws Exception {
8386
.getResponse() //
8487
.getContentAsString();
8588

86-
assertThat(results).isEqualTo(read(new ClassPathResource("hal-embedded-author-illustrator.json", getClass())));
89+
assertThat(results).isEqualTo(contextualMapper.readFile("hal-embedded-author-illustrator.json"));
8790
}
8891

89-
@Test
92+
@Test // #864
9093
public void singleItem() throws Exception {
9194

9295
String results = this.mockMvc.perform(get("/other-author").accept(HAL_JSON)) //
@@ -95,10 +98,10 @@ public void singleItem() throws Exception {
9598
.getResponse() //
9699
.getContentAsString();
97100

98-
assertThat(results).isEqualTo(read(new ClassPathResource("hal-single-item.json", getClass())));
101+
assertThat(results).isEqualTo(contextualMapper.readFile("hal-single-item.json"));
99102
}
100103

101-
@Test
104+
@Test // #864
102105
public void collection() throws Exception {
103106

104107
String results = this.mockMvc.perform(get("/authors").accept(HAL_JSON)) //
@@ -107,9 +110,7 @@ public void collection() throws Exception {
107110
.getResponse() //
108111
.getContentAsString();
109112

110-
System.out.println(results);
111-
112-
assertThat(results).isEqualTo(read(new ClassPathResource("hal-embedded-collection.json", getClass())));
113+
assertThat(results).isEqualTo(contextualMapper.readFile("hal-embedded-collection.json"));
113114
}
114115

115116
@RestController
@@ -122,7 +123,7 @@ RepresentationModel<?> singleItem() {
122123

123124
return ModelBuilder2 //
124125
.entity(new Author("Alan Watts", "January 6, 1915", "November 16, 1973")) //
125-
.link(new Link("/people/alan-watts")) //
126+
.link(Link.of("/people/alan-watts")) //
126127
.build();
127128
}
128129

@@ -158,15 +159,15 @@ RepresentationModel<?> authorDetails(@PathVariable int id) {
158159
return ModelBuilder2 //
159160
.subModel(LinkRelation.of("author"), ModelBuilder2 //
160161
.entity(new Author("Alan Watts", "January 6, 1915", "November 16, 1973")) //
161-
.link(new Link("/people/alan-watts")) //
162+
.link(Link.of("/people/alan-watts")) //
162163
.build())
163164
.subModel(LinkRelation.of("illustrator"), ModelBuilder2 //
164165
.entity(new Author("John Smith", null, null)) //
165-
.link(new Link("/people/john-smith")) //
166+
.link(Link.of("/people/john-smith")) //
166167
.build())
167-
.link(new Link("/books/the-way-of-zen")) //
168-
.link(new Link("/people/alan-watts", LinkRelation.of("author"))) //
169-
.link(new Link("/people/john-smith", LinkRelation.of("illustrator"))) //
168+
.link(Link.of("/books/the-way-of-zen")) //
169+
.link(Link.of("/people/alan-watts", LinkRelation.of("author"))) //
170+
.link(Link.of("/people/john-smith", LinkRelation.of("illustrator"))) //
170171
.build();
171172
}
172173
}

src/test/java/org/springframework/hateoas/server/mvc/EmbeddedIntegrationTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ RepresentationModel<?> singleItem() {
122122

123123
return ModelBuilder //
124124
.entity(new Author("Alan Watts", "January 6, 1915", "November 16, 1973")) //
125-
.link(new Link("/people/alan-watts")) //
125+
.link(Link.of("/people/alan-watts")) //
126126
.build();
127127
}
128128

@@ -156,15 +156,15 @@ RepresentationModel<?> authorDetails(@PathVariable int id) {
156156
.embed(LinkRelation.of("author")) //
157157

158158
.entity(new Author("Alan Watts", "January 6, 1915", "November 16, 1973")) //
159-
.link(new Link("/people/alan-watts")) //
159+
.link(Link.of("/people/alan-watts")) //
160160

161161
.embed(LinkRelation.of("illustrator")) //
162162
.entity(new Author("John Smith", null, null)) //
163-
.link(new Link("/people/john-smith")) //
163+
.link(Link.of("/people/john-smith")) //
164164

165-
.rootLink(new Link("/books/the-way-of-zen")) //
166-
.rootLink(new Link("/people/alan-watts", LinkRelation.of("author"))) //
167-
.rootLink(new Link("/people/john-smith", LinkRelation.of("illustrator"))) //
165+
.rootLink(Link.of("/books/the-way-of-zen")) //
166+
.rootLink(Link.of("/people/alan-watts", LinkRelation.of("author"))) //
167+
.rootLink(Link.of("/people/john-smith", LinkRelation.of("illustrator"))) //
168168

169169
.build();
170170
}

src/test/java/org/springframework/hateoas/server/mvc/ZoomProtocol2WebMvcIntegrationTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.AssertionsForInterfaceTypes.*;
1919
import static org.springframework.hateoas.IanaLinkRelations.*;
20+
import static org.springframework.hateoas.MappingTestUtils.*;
2021
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
2122
import static org.springframework.hateoas.support.MappingUtils.*;
2223
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@@ -42,6 +43,8 @@
4243
import org.springframework.hateoas.EntityModel;
4344
import org.springframework.hateoas.Link;
4445
import org.springframework.hateoas.LinkRelation;
46+
import org.springframework.hateoas.MappingTestUtils;
47+
import org.springframework.hateoas.MappingTestUtils.ContextualMapper;
4548
import org.springframework.hateoas.MediaTypes;
4649
import org.springframework.hateoas.ModelBuilder2;
4750
import org.springframework.hateoas.RepresentationModel;
@@ -75,14 +78,19 @@ public class ZoomProtocol2WebMvcIntegrationTest {
7578

7679
MockMvc mockMvc;
7780

78-
private static Map<Integer, Product> PRODUCTS;
81+
private ContextualMapper contextualMapper;
7982

80-
@BeforeEach
83+
private static Map<Integer, Product> PRODUCTS;
84+
85+
86+
@BeforeEach
8187
public void setUp() {
8288

8389
this.mockMvc = webAppContextSetup(this.context).build();
8490

85-
PRODUCTS = new TreeMap<>();
91+
this.contextualMapper = createMapper(getClass());
92+
93+
PRODUCTS = new TreeMap<>();
8694

8795
PRODUCTS.put(998, new Product("someValue", true, true));
8896
PRODUCTS.put(777, new Product("someValue", true, false));
@@ -94,7 +102,7 @@ public void setUp() {
94102
PRODUCTS.put(666, new Product("someValue", false, true));
95103
}
96104

97-
@Test // #175
105+
@Test // #175 #864
98106
public void modelBuilderCanAssembleZoomProtocol() throws Exception {
99107

100108
String results = this.mockMvc.perform(get("/products").accept(MediaTypes.HAL_JSON)) //
@@ -103,7 +111,7 @@ public void modelBuilderCanAssembleZoomProtocol() throws Exception {
103111
.getResponse() //
104112
.getContentAsString();
105113

106-
assertThat(results).isEqualTo(read(new ClassPathResource("zoom-hypermedia.json", getClass())));
114+
assertThat(results).isEqualTo(contextualMapper.readFile("zoom-hypermedia.json"));
107115
}
108116

109117
@RestController
@@ -116,7 +124,7 @@ static class ProductController {
116124
public RepresentationModel<?> all() {
117125

118126
List<EntityModel<Product>> products = PRODUCTS.keySet().stream() //
119-
.map(id -> new EntityModel<>(PRODUCTS.get(id), new Link("http://localhost/products/{id}").expand(id))) //
127+
.map(id -> EntityModel.of(PRODUCTS.get(id), new Link("http://localhost/products/{id}").expand(id))) //
120128
.collect(Collectors.toList());
121129

122130
ModelBuilder2.EmbeddedModelBuilder<EntityModel<Product>> builder = new ModelBuilder2.EmbeddedModelBuilder<>();

0 commit comments

Comments
 (0)