|
1 | 1 | /* |
2 | | - * Copyright 2014-2021 the original author or authors. |
| 2 | + * Copyright 2014-2024 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
26 | 26 |
|
27 | 27 | import org.junit.jupiter.api.Test; |
28 | 28 | import org.springframework.beans.factory.annotation.Autowired; |
29 | | -import org.springframework.boot.autoconfigure.SpringBootApplication; |
30 | 29 | import org.springframework.boot.test.context.SpringBootTest; |
31 | 30 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 31 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
32 | 32 | import org.springframework.boot.test.web.server.LocalServerPort; |
33 | | -import org.springframework.context.annotation.Bean; |
34 | 33 | import org.springframework.hateoas.EntityModel; |
35 | 34 | import org.springframework.hateoas.MediaTypes; |
36 | 35 | import org.springframework.hateoas.client.Traverson; |
37 | 36 | import org.springframework.hateoas.server.core.TypeReferences.CollectionModelType; |
38 | 37 | import org.springframework.hateoas.server.core.TypeReferences.EntityModelType; |
39 | 38 | import org.springframework.hateoas.server.core.TypeReferences.PagedModelType; |
40 | | -import org.springframework.http.RequestEntity; |
41 | | -import org.springframework.web.client.RestOperations; |
42 | | -import org.springframework.web.client.RestTemplate; |
| 39 | +import org.springframework.web.client.RestClient; |
43 | 40 |
|
44 | 41 | /** |
45 | 42 | * A test case to discover the search resource and execute a predefined search with it. |
46 | 43 | * |
47 | 44 | * @author Oliver Gierke |
48 | 45 | * @author Divya Srivastava |
49 | 46 | */ |
50 | | -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
51 | 47 | @Slf4j |
| 48 | +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
52 | 49 | class StarbucksClient { |
53 | 50 |
|
54 | | - @SpringBootApplication |
55 | | - static class Config { |
56 | | - |
57 | | - @Bean |
58 | | - public RestTemplate restTemplate() { |
59 | | - return new RestTemplate(); |
60 | | - } |
61 | | - } |
62 | | - |
63 | 51 | @LocalServerPort int port; |
64 | | - |
65 | | - @Autowired RestOperations restOperations; |
| 52 | + @Autowired TestRestTemplate template; |
66 | 53 |
|
67 | 54 | private static final String SERVICE_URI = "http://localhost:%s/api"; |
68 | 55 |
|
@@ -102,17 +89,23 @@ void accessServiceUsingRestTemplate() { |
102 | 89 |
|
103 | 90 | // Access root resource |
104 | 91 |
|
105 | | - var uri = URI.create(String.format(SERVICE_URI, port)); |
106 | | - var request = RequestEntity.get(uri).accept(HAL_JSON).build(); |
107 | | - var rootLinks = restOperations.exchange(request, new EntityModelType<>() {}).getBody(); |
108 | | - var links = rootLinks.getLinks(); |
| 92 | + var client = RestClient.create(template.getRestTemplate()); |
| 93 | + |
| 94 | + var links = client.get() |
| 95 | + .uri(URI.create(String.format(SERVICE_URI, port))) |
| 96 | + .accept(HAL_JSON) |
| 97 | + .retrieve() |
| 98 | + .body(new EntityModelType<>() {}) |
| 99 | + .getLinks(); |
109 | 100 |
|
110 | 101 | // Follow stores link |
111 | 102 |
|
112 | 103 | var storesLink = links.getRequiredLink("stores").expand(); |
113 | 104 |
|
114 | | - request = RequestEntity.get(URI.create(storesLink.getHref())).accept(HAL_JSON).build(); |
115 | | - var stores = restOperations.exchange(request, new CollectionModelType<Store>() {}).getBody(); |
| 105 | + var stores = client.get().uri(storesLink.toUri()) |
| 106 | + .accept(HAL_JSON) |
| 107 | + .retrieve() |
| 108 | + .body(new CollectionModelType<Store>() {}); |
116 | 109 |
|
117 | 110 | stores.getContent().forEach(store -> log.info("{} - {}", store.name, store.address)); |
118 | 111 | } |
|
0 commit comments