-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompanyEndpointIntegrationTests.java
150 lines (133 loc) · 5.48 KB
/
CompanyEndpointIntegrationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package kp.ws;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.util.AssertionErrors;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.ws.test.server.RequestCreator;
import org.springframework.ws.test.server.RequestCreators;
import org.springframework.ws.test.server.ResponseActions;
import org.springframework.ws.test.server.ResponseMatchers;
import org.springframework.ws.test.server.ResponseXPathExpectations;
import kp.Constants;
/**
* The server-side integration testing with the {@link MockWebServiceClient}.
*
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class CompanyEndpointIntegrationTests {
private static final boolean VERBOSE = false;
private MockWebServiceClient mockClient;
@Autowired
private ApplicationContext applicationContext;
private static final Resource SCHEMA_RESOURCE = new ClassPathResource(Constants.SCHEMA_XSD_PATH);
private static final ResponseXPathExpectations EXPECTED_X_PATH = ResponseMatchers.xpath("""
ns:getCompanyResponse/ns:company/
ns:departments[ns:name='N-a-m-e-01']/
ns:employees[ns:lastName='L-N-a-m-e-01']
""", Collections.singletonMap("ns", "http://kp.com/ws"));
/**
* The constructor.
*/
CompanyEndpointIntegrationTests() {
super();
}
/**
* Initializes the {@link MockWebServiceClient}.
*
*/
@BeforeEach
void initClient() {
mockClient = MockWebServiceClient.createClient(applicationContext);
}
/**
* Should get the company.
*
* @throws Exception the {@link Exception}.
*/
@Test
void shouldGetCompany() throws Exception {
// GIVEN
final Source requestPayload = new StreamSource(new ClassPathResource("getCompanyRequest.xml").getFile());
final RequestCreator requestCreator = RequestCreators.withPayload(requestPayload);
// WHEN
final ResponseActions responseActions = mockClient.sendRequest(requestCreator);
// THEN
responseActions.andExpect(ResponseMatchers.noFault());
responseActions.andExpect(EXPECTED_X_PATH.exists());
responseActions.andExpect(ResponseMatchers.validPayload(SCHEMA_RESOURCE));
responseActions.andExpect((request, response) -> checkResponsePayload("getCompanyResponse.soap", response));
}
/**
* Should get the departments.
*
* @throws Exception the {@link Exception}.
*/
@Test
void shouldGetDepartments() throws Exception {
// GIVEN
final Source requestPayload = new StreamSource(new ClassPathResource("getDepartmentsRequest.xml").getFile());
final RequestCreator requestCreator = RequestCreators.withPayload(requestPayload);
// WHEN
final ResponseActions responseActions = mockClient.sendRequest(requestCreator);
// THEN
responseActions.andExpect(ResponseMatchers.noFault());
responseActions.andExpect(ResponseMatchers.validPayload(SCHEMA_RESOURCE));
responseActions.andExpect((request, response) -> checkResponsePayload("getDepartmentsResponse.soap", response));
}
/**
* Should get the employees.
*
* @throws Exception the {@link Exception}.
*/
@Test
void shouldGetEmployees() throws Exception {
// GIVEN
final Source requestPayload = new StreamSource(new ClassPathResource("getEmployeesRequest.xml").getFile());
final RequestCreator requestCreator = RequestCreators.withPayload(requestPayload);
// WHEN
final ResponseActions responseActions = mockClient.sendRequest(requestCreator);
// THEN
responseActions.andExpect(ResponseMatchers.noFault());
responseActions.andExpect(ResponseMatchers.validPayload(SCHEMA_RESOURCE));
responseActions.andExpect((request, response) -> checkResponsePayload("getEmployeesResponse.soap", response));
}
/**
* Checks response payload.
*
* @param expectedResponseFile the name of the file with expected response
* @param actualResponse the actual response
*/
private void checkResponsePayload(String expectedResponseFile, WebServiceMessage actualResponse) {
String expectedPayload = "";
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (InputStream inputStream = new FileInputStream(new ClassPathResource(expectedResponseFile).getFile())) {
expectedPayload = new String(inputStream.readAllBytes()).replace("\r", "").replace("\n", "");
actualResponse.writeTo(outputStream);
} catch (IOException e) {
AssertionErrors.fail("checkResponsePayload(): IOException[" + e.getMessage() + "]");
}
final String actualPayload = outputStream.toString();
if (VERBOSE) {
System.err.println(actualPayload.replace("><", ">\n<"));
}
Assertions.assertEquals(expectedPayload, actualPayload);
}
}