带有HATEOAS PagedResources的Spring Boot的TestRestTemplate [英] Spring Boot's TestRestTemplate with HATEOAS PagedResources
问题描述
我正在尝试在Spring Boot应用程序的Integration-Test中使用TestRestTemplate来向Spring Data REST存储库发出请求.
I'm trying to use the TestRestTemplate in my Spring Boot Application's Integration-Test, to make a request to a Spring Data REST Repository.
浏览器中的响应具有以下形式:
The response in the browser has the form:
{
"links": [
{
"rel": "self",
"href": "http://localhost:8080/apiv1/data/users"
},
{
"rel": "profile",
"href": "http://localhost:8080/apiv1/data/profile/users"
},
{
"rel": "search",
"href": "http://localhost:8080/apiv1/data/users/search"
}
],
"content": [
{
"username": "admin",
"enabled": true,
"firstName": null,
"lastName": null,
"permissions": [ ],
"authorities": [
"ROLE_ADMIN"
],
"accountNonExpired": true,
"accountNonLocked": true,
"credentialsNonExpired": true,
"content": [ ],
"links": [
{
"rel": "self",
"href": "http://localhost:8080/apiv1/data/users/1"
},
{
"rel": "myUser",
"href": "http://localhost:8080/apiv1/data/users/1"
},
{
"rel": "mandant",
"href": "http://localhost:8080/apiv1/data/users/1/mandant"
}
]
},
{
"username": "dba",
"enabled": true,
"firstName": null,
"lastName": null,
"permissions": [ ],
"authorities": [
"ROLE_DBA"
],
"accountNonExpired": true,
"accountNonLocked": true,
"credentialsNonExpired": true,
"content": [ ],
"links": [
{
"rel": "self",
"href": "http://localhost:8080/apiv1/data/users/2"
},
{
"rel": "myUser",
"href": "http://localhost:8080/apiv1/data/users/2"
},
{
"rel": "mandant",
"href": "http://localhost:8080/apiv1/data/users/2/mandant"
}
]
},
{
"username": "user",
"enabled": true,
"firstName": null,
"lastName": null,
"permissions": [ ],
"authorities": [
"ROLE_USER"
],
"accountNonExpired": true,
"accountNonLocked": true,
"credentialsNonExpired": true,
"content": [ ],
"links": [
{
"rel": "self",
"href": "http://localhost:8080/apiv1/data/users/3"
},
{
"rel": "myUser",
"href": "http://localhost:8080/apiv1/data/users/3"
},
{
"rel": "mandant",
"href": "http://localhost:8080/apiv1/data/users/3/mandant"
}
]
}
],
"page": {
"size": 20,
"totalElements": 3,
"totalPages": 1,
"number": 0
}
}
这是测试:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("unittest")
public class MyUserRepositoryIntegrationTest {
private static Logger logger = LoggerFactory.getLogger(MyUserRepositoryIntegrationTest.class);
private static final int NUM_USERS = 4;
private static final String USER_URL = "/apiv1/data/users";
@Autowired
private TestRestTemplate restTemplate;
@Test
public void listUsers() {
ResponseEntity<PagedResources<MyUser>> response = restTemplate.withBasicAuth("user", "user").exchange(USER_URL,
HttpMethod.GET, null, new ParameterizedTypeReference<PagedResources<MyUser>>() {
});
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
logger.debug("Res : " + response.getBody().toString());
assertThat(response.getBody().getContent().size()).isEqualTo(NUM_USERS);
}
@TestConfiguration
public static class MyTestConfig {
@Autowired
@Qualifier("halJacksonHttpMessageConverter")
private TypeConstrainedMappingJackson2HttpMessageConverter halJacksonHttpMessageConverter;
@Bean
public RestTemplateBuilder restTemplateBuilder() {
return new RestTemplateBuilder().messageConverters(halJacksonHttpMessageConverter);
}
}
}
问题是,我没有得到内容.有趣的是,元数据(分页信息)在那里.
The Problem is, that I don't get the content. Interestingly, the Metadata (paging-info) is there.
我的TestConfig被检测到,但是我认为它没有使用'halJacksonHttpMessageConverter'(我从这里得到:
My TestConfig gets detected, but I think it's not using the 'halJacksonHttpMessageConverter' (which I got from here: https://github.com/bijukunjummen/hateoas-sample/blob/master/src/test/java/univ/HALRestTemplateIntegrationTests.java). That's why I used "messageConverters()" and not "additionalMessageConverters()" (to no avail).
这是日志:
m.m.a.RequestResponseBodyMethodProcessor : Written [PagedResource { content: [Resource { content: at.mycompany.myapp.auth.MyUser@7773211c, links: [<http://localhost:51708/apiv1/data/users/1>;rel="self", <http://localhost:51708/apiv1/data/users/1>;rel="logisUser"] }, Resource { content: at.mycompany.myapp.auth.MyUser@2c96fdee, links: [<http://localhost:51708/apiv1/data/users/2>;rel="self", <http://localhost:51708/apiv1/data/users/2>;rel="logisUser"] }, Resource { content: at.mycompany.myapp.auth.MyUser@1ddfd104, links: [<http://localhost:51708/apiv1/data/users/3>;rel="self", <http://localhost:51708/apiv1/data/users/3>;rel="logisUser"] }, Resource { content: at.mycompany.myapp.auth.MyUser@55d71419, links: [<http://localhost:51708/apiv1/data/users/4>;rel="self", <http://localhost:51708/apiv1/data/users/4>;rel="logisUser"] }], metadata: Metadata { number: 0, total pages: 1, total elements: 4, size: 20 }, links: [<http://localhost:51708/apiv1/data/users>;rel="self", <http://localhost:51708/apiv1/data/profile/users>;rel="profile", <http://localhost:51708/apiv1/data/users/search>;rel="search"] }] as "application/hal+json" using [org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$ResourceSupportHttpMessageConverter@2f58f492]
o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
o.s.web.client.RestTemplate : GET request for "http://localhost:51708/apiv1/data/users" resulted in 200 (null)
o.s.web.servlet.DispatcherServlet : Successfully completed request
o.s.web.client.RestTemplate : Reading [org.springframework.hateoas.PagedResources<at.mycompany.myapp.auth.MyUser>] as "application/hal+json;charset=UTF-8" using [org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$ResourceSupportHttpMessageConverter@10ad95cd]
o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@76e257e2
d.l.a.MyUserRepositoryIntegrationTest : Res : PagedResource { content: [], metadata: Metadata { number: 0, total pages: 1, total elements: 4, size: 20 }, links: [] }
The idea of overriding the restTemplate Bean comes from the docs: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-rest-templates-test-utility
有什么想法可以简单地进行一些REST调用并作为测试对象获得答案吗?
Any Ideas how I can simply make some REST calls and get the answer as an Object for my tests?
推荐答案
我切换到MockMvc,一切正常:
I switched to MockMvc and everything works:
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("unittest")
public class MyUserRepositoryIntegrationTest {
@Autowired WebApplicationContext context;
@Autowired FilterChainProxy filterChain;
MockMvc mvc;
@Before
public void setupTests() {
this.mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(filterChain).build();
@Test
public void listUsers() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);
headers.add(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encode(("user:user").getBytes())));
mvc.perform(get(USER_URL).headers(headers))
.andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content", hasSize(NUM_USERS)));
}
}
对于那些感兴趣的人,基于 Wellington Souza 的解决方案的替代解决方案:
For those interested, an alternative solution based on Wellington Souza's solution:
虽然jsonpath非常强大,但我还没有找到一种使用MockMvc将JSON解组为实际对象的方法.
While jsonpath is really powerful, I haven't found a way to really unmarshall the JSON into an actual Object with MockMvc.
如果查看我发布的JSON输出,您会注意到,它不是默认的Spring Data Rest HAL + JSON输出.我将属性data.rest.defaultMediaType更改为"application/json".这样,我也无法让特拉弗森工作.但是,当我停用它时,以下方法将起作用:
If you look at my posted JSON output, you'll notice, that it's not the default Spring Data Rest HAL+JSON output. I changed the property data.rest.defaultMediaType to "application/json". With that, I couldn't get Traverson to work either. But when I deactivate that, the following works:
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.client.Hop;
import org.springframework.hateoas.client.Traverson;
import org.springframework.http.HttpHeaders;
import org.springframework.security.crypto.codec.Base64;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("unittest")
public class MyUserRepositoryIntegrationTest {
private static HttpHeaders userHeaders;
private static HttpHeaders adminHeaders;
@LocalServerPort
private int port;
@BeforeClass
public static void setupTests() {
MyUserRepositoryIntegrationTest.userHeaders = new HttpHeaders();
MyUserRepositoryIntegrationTest.userHeaders.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);
MyUserRepositoryIntegrationTest.userHeaders.add(HttpHeaders.AUTHORIZATION,
"Basic " + new String(Base64.encode(("user:user").getBytes())));
MyUserRepositoryIntegrationTest.adminHeaders = new HttpHeaders();
MyUserRepositoryIntegrationTest.adminHeaders.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);
MyUserRepositoryIntegrationTest.adminHeaders.add(HttpHeaders.AUTHORIZATION,
"Basic " + new String(Base64.encode(("admin:admin").getBytes())));
}
@Test
public void listUsersSorted() throws Exception {
final ParameterizedTypeReference<PagedResources<MyUser>> resourceParameterizedTypeReference = //
new ParameterizedTypeReference<PagedResources<MyUser>>() {
};
final PagedResources<MyUser> actual = new Traverson(new URI("http://localhost:" + port + "/apiv1/data"),
MediaTypes.HAL_JSON)//
.follow(Hop.rel("myUsers").withParameter("sort", "username,asc"))//
.withHeaders(userHeaders)//
.toObject(resourceParameterizedTypeReference);
assertThat(actual.getContent()).isNotNull().isNotEmpty();
assertThat(actual.getContent()//
.stream()//
.map(user -> user.getUsername())//
.collect(Collectors.toList())//
).isSorted();
}
}
(注意:由于我是从较大的Test Class复制而来的,因此可能不包含所有导入内容.)
(Note: Might not contain all imports etc., since I copied this from a larger Test Class.)
.withParam"适用于模板化URL,即那些接受查询参数的URL.如果您尝试使用原始URL,它将失败,因为链接的字面意思是" http://[. .]/用户 {option1,option2,...}",因此格式不正确.
The ".withParam" works for templated URLs, i.e., ones that accept query parameters. If you try to follow the raw URL, it will fail, because the link is literally "http://[...]/users{option1,option2,...}" and thus not well-formed.
这篇关于带有HATEOAS PagedResources的Spring Boot的TestRestTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!