|
15 | 15 | import org.springframework.http.*;
|
16 | 16 | import org.springframework.test.context.junit4.SpringRunner;
|
17 | 17 |
|
| 18 | +import java.io.File; |
18 | 19 | import java.io.IOException;
|
19 |
| -import java.net.URL; |
| 20 | +import java.net.URISyntaxException; |
20 | 21 | import java.nio.file.Files;
|
21 | 22 | import java.nio.file.Paths;
|
22 | 23 | import java.util.Map;
|
|
29 | 30 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
30 | 31 | public class GenericApiControllerExternalIntegrationTest {
|
31 | 32 |
|
32 |
| - @Autowired |
33 |
| - private TestRestTemplate restTemplate; |
34 |
| - |
35 |
| - @Value("${file.base}") |
36 |
| - private String fileBase; |
37 |
| - @Value("${file.extension}") |
38 |
| - private String fileExtension; |
39 |
| - |
40 |
| - private URL resource; |
41 |
| - |
42 |
| - @Before |
43 |
| - public void init() { |
44 |
| - this.resource = getClass().getClassLoader().getResource(fileBase); |
45 |
| - } |
46 |
| - |
47 |
| - @Test |
48 |
| - public void shouldFileExistsInTest() { |
49 |
| - assertNotNull(resource); |
50 |
| - assertNotNull(resource.getFile()); |
51 |
| - } |
52 |
| - |
53 |
| - private String getJson(String fileNameExpected) throws IOException { |
54 |
| - return new String(Files.readAllBytes(Paths.get(fileNameExpected))); |
55 |
| - } |
56 |
| - |
57 |
| - @Test(timeout = 10000) |
58 |
| - public void shouldResolvePostWithExternalMock() throws IOException, JSONException { |
59 |
| - shouldResolveWithExternalMock(HttpMethod.POST); |
60 |
| - } |
61 |
| - |
62 |
| - @Test(timeout = 5000) |
63 |
| - public void shouldResolvePathWithExternalMock() throws IOException, JSONException { |
64 |
| - shouldResolveWithExternalMock(HttpMethod.PATCH); |
65 |
| - } |
66 |
| - |
67 |
| - private void shouldResolveWithExternalMock(final HttpMethod httpMethod) throws IOException, JSONException { |
68 |
| - final ImmutableMap<String, String> headers = ImmutableMap.<String, String>builder() |
69 |
| - .put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build(); |
70 |
| - |
71 |
| - shouldResolveWithExternalMock(httpMethod, Optional.of(headers)); |
72 |
| - } |
73 |
| - |
74 |
| - private void shouldResolveWithExternalMock(final HttpMethod httpMethod, final Optional<Map<String, String>> headers) |
75 |
| - throws IOException, JSONException { |
76 |
| - // given |
77 |
| - final String url = "/v2/57fbd6280f0000ed154fd470"; |
78 |
| - |
79 |
| - final HttpStatus httpStatus = HttpStatus.OK; |
80 |
| - final String fileName = resource.getFile().concat("/").concat(httpMethod.name().toLowerCase()).concat(url) |
81 |
| - .concat("/1").concat(fileExtension); |
82 |
| - |
83 |
| - final EndpointDto endpointDto = new Gson().fromJson(getJson(fileName), EndpointDto.class); |
84 |
| - final String requestJson = new Gson().toJson(endpointDto.getRequest().getBody()); |
85 |
| - final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody()); |
86 |
| - |
87 |
| - final HttpHeaders httpHeaders = headers.filter(mapHeaders -> !mapHeaders.isEmpty()).map(map -> { |
88 |
| - final HttpHeaders result = new HttpHeaders(); |
89 |
| - result.setContentType(MediaType.APPLICATION_JSON); |
90 |
| - return result; |
91 |
| - }).orElse(null); |
92 |
| - |
93 |
| - final HttpEntity<String> httpEntity = new HttpEntity<>(requestJson, httpHeaders); |
94 |
| - |
95 |
| - // when |
96 |
| - final ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, httpEntity, String.class); |
97 |
| - |
98 |
| - // then |
99 |
| - assertEquals(httpStatus, response.getStatusCode()); |
100 |
| - JSONAssert.assertEquals(responseJson, response.getBody(), false); |
101 |
| - assertNotNull(response.getHeaders().get("Access-Control-Allow-Origin")); |
102 |
| - } |
| 33 | + @Autowired |
| 34 | + private TestRestTemplate restTemplate; |
| 35 | + |
| 36 | + @Value("${file.base}") |
| 37 | + private String fileBase; |
| 38 | + @Value("${file.extension}") |
| 39 | + private String fileExtension; |
| 40 | + |
| 41 | + private File resource; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void init() throws URISyntaxException { |
| 45 | + this.resource = Paths.get(getClass().getClassLoader().getResource(fileBase).toURI()).toFile(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void shouldFileExistsInTest() { |
| 50 | + assertNotNull(resource); |
| 51 | + } |
| 52 | + |
| 53 | + private String getJson(String fileNameExpected) throws IOException { |
| 54 | + return new String(Files.readAllBytes(Paths.get(fileNameExpected))); |
| 55 | + } |
| 56 | + |
| 57 | + @Test(timeout = 10000) |
| 58 | + public void shouldResolvePostWithExternalMock() throws IOException, JSONException { |
| 59 | + shouldResolveWithExternalMock(HttpMethod.POST); |
| 60 | + } |
| 61 | + |
| 62 | + @Test(timeout = 5000) |
| 63 | + public void shouldResolvePathWithExternalMock() throws IOException, JSONException { |
| 64 | + shouldResolveWithExternalMock(HttpMethod.PATCH); |
| 65 | + } |
| 66 | + |
| 67 | + private void shouldResolveWithExternalMock(final HttpMethod httpMethod) throws IOException, JSONException { |
| 68 | + final ImmutableMap<String, String> headers = ImmutableMap.<String, String>builder() |
| 69 | + .put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build(); |
| 70 | + |
| 71 | + shouldResolveWithExternalMock(httpMethod, Optional.of(headers)); |
| 72 | + } |
| 73 | + |
| 74 | + private void shouldResolveWithExternalMock(final HttpMethod httpMethod, final Optional<Map<String, String>> headers) |
| 75 | + throws IOException, JSONException { |
| 76 | + // given |
| 77 | + final String url = "/v2/57fbd6280f0000ed154fd470"; |
| 78 | + |
| 79 | + final HttpStatus httpStatus = HttpStatus.OK; |
| 80 | + final String fileName = |
| 81 | + Paths.get(resource.getAbsolutePath(), httpMethod.name().toLowerCase(), url, "1" + fileExtension) |
| 82 | + .toAbsolutePath().toString(); |
| 83 | + |
| 84 | + final EndpointDto endpointDto = new Gson().fromJson(getJson(fileName), EndpointDto.class); |
| 85 | + final String requestJson = new Gson().toJson(endpointDto.getRequest().getBody()); |
| 86 | + final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody()); |
| 87 | + |
| 88 | + final HttpHeaders httpHeaders = headers.filter(mapHeaders -> !mapHeaders.isEmpty()).map(map -> { |
| 89 | + final HttpHeaders result = new HttpHeaders(); |
| 90 | + result.setContentType(MediaType.APPLICATION_JSON); |
| 91 | + return result; |
| 92 | + }).orElse(null); |
| 93 | + |
| 94 | + final HttpEntity<String> httpEntity = new HttpEntity<>(requestJson, httpHeaders); |
| 95 | + |
| 96 | + // when |
| 97 | + final ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, httpEntity, String.class); |
| 98 | + |
| 99 | + // then |
| 100 | + assertEquals(httpStatus, response.getStatusCode()); |
| 101 | + JSONAssert.assertEquals(responseJson, response.getBody(), false); |
| 102 | + assertNotNull(response.getHeaders().get("Access-Control-Allow-Origin")); |
| 103 | + } |
103 | 104 |
|
104 | 105 | }
|
0 commit comments