Skip to content

Commit 0f5387e

Browse files
authored
ADD: raw body subscription triggering (#157)
* ADD: raw body POST * review fixes * CHANGE: deprecated method
1 parent 5c94a45 commit 0f5387e

File tree

4 files changed

+109
-87
lines changed

4 files changed

+109
-87
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,13 @@
241241
<dependency>
242242
<groupId>org.mock-server</groupId>
243243
<artifactId>mockserver-netty</artifactId>
244-
<version>5.3.0</version>
244+
<version>5.4.1</version>
245245
</dependency>
246246

247247
<dependency>
248248
<groupId>org.mock-server</groupId>
249249
<artifactId>mockserver-client-java</artifactId>
250-
<version>5.3.0</version>
250+
<version>5.4.1</version>
251251
</dependency>
252252

253253
<!-- Cucumber -->

src/functionaltests/java/com/ericsson/ei/notifications/ttl/TestTTLSteps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.json.JSONArray;
1717
import org.json.JSONException;
1818
import org.junit.Ignore;
19-
import org.mockserver.client.server.MockServerClient;
19+
import org.mockserver.client.MockServerClient;
2020
import org.mockserver.integration.ClientAndServer;
2121
import org.mockserver.model.Format;
2222
import org.mockserver.model.HttpResponse;

src/functionaltests/java/com/ericsson/ei/subscriptions/trigger/SubscriptionTriggerSteps.java

Lines changed: 74 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,60 @@
33
import com.dumbster.smtp.SimpleSmtpServer;
44
import com.dumbster.smtp.SmtpMessage;
55
import com.ericsson.ei.utils.FunctionalTestBase;
6-
7-
import static org.junit.Assert.assertEquals;
8-
import static org.junit.Assert.assertTrue;
9-
import static org.mockserver.model.HttpRequest.request;
10-
import static org.mockserver.model.HttpResponse.response;
11-
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
12-
13-
import java.io.File;
14-
import java.io.IOException;
15-
import java.util.ArrayList;
16-
import java.util.Arrays;
17-
import java.util.List;
18-
import java.util.concurrent.TimeUnit;
19-
6+
import com.ericsson.ei.utils.HttpRequest;
7+
import cucumber.api.java.After;
8+
import cucumber.api.java.Before;
9+
import cucumber.api.java.en.Given;
10+
import cucumber.api.java.en.Then;
11+
import cucumber.api.java.en.When;
2012
import org.apache.commons.io.FileUtils;
2113
import org.json.JSONArray;
2214
import org.json.JSONException;
2315
import org.junit.Ignore;
24-
import org.mockserver.client.server.MockServerClient;
16+
import org.mockserver.client.MockServerClient;
2517
import org.mockserver.integration.ClientAndServer;
2618
import org.mockserver.model.Format;
2719
import org.slf4j.Logger;
2820
import org.slf4j.LoggerFactory;
2921
import org.springframework.beans.factory.annotation.Autowired;
3022
import org.springframework.beans.factory.annotation.Value;
31-
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
23+
import org.springframework.boot.web.server.LocalServerPort;
3224
import org.springframework.http.HttpStatus;
33-
import org.springframework.http.MediaType;
25+
import org.springframework.http.ResponseEntity;
3426
import org.springframework.mail.javamail.JavaMailSenderImpl;
35-
import org.springframework.test.web.servlet.MockMvc;
36-
import org.springframework.test.web.servlet.MvcResult;
37-
import org.springframework.test.web.servlet.RequestBuilder;
38-
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
3927
import org.springframework.util.SocketUtils;
4028

41-
import cucumber.api.java.After;
42-
import cucumber.api.java.Before;
43-
import cucumber.api.java.en.Given;
44-
import cucumber.api.java.en.Then;
45-
import cucumber.api.java.en.When;
29+
import java.io.File;
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.Arrays;
33+
import java.util.List;
34+
import java.util.concurrent.TimeUnit;
35+
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertTrue;
38+
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
39+
import static org.mockserver.model.HttpRequest.request;
40+
import static org.mockserver.model.HttpResponse.response;
4641

4742
@Ignore
48-
@AutoConfigureMockMvc
4943
public class SubscriptionTriggerSteps extends FunctionalTestBase {
5044

45+
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionTriggerSteps.class);
46+
5147
private static final String SUBSCRIPTION_WITH_JSON_PATH = "src/functionaltests/resources/subscription_multiple.json";
5248
private static final String EIFFEL_EVENTS_JSON_PATH = "src/functionaltests/resources/eiffel_events_for_test.json";
53-
5449
private static final String REST_ENDPOINT = "/rest";
5550
private static final String REST_ENDPOINT_AUTH = "/rest/with/auth";
5651
private static final String REST_ENDPOINT_PARAMS = "/rest/with/params";
5752
private static final String REST_ENDPOINT_AUTH_PARAMS = "/rest/with/auth/params";
58-
private static final String BASE_URL = "localhost";
53+
private static final String REST_ENDPOINT_ROW_BODY = "/rest/rowBody";
5954

6055
private List<String> subscriptionNames = new ArrayList<>();
6156

57+
@LocalServerPort
58+
private int applicationPort;
59+
6260
@Value("${email.sender}")
6361
private String sender;
6462

@@ -68,20 +66,13 @@ public class SubscriptionTriggerSteps extends FunctionalTestBase {
6866
@Value("${spring.data.mongodb.database}")
6967
private String database;
7068

71-
@Autowired
72-
private MockMvc mockMvc;
73-
7469
@Autowired
7570
private JavaMailSenderImpl mailSender;
7671

77-
private MvcResult result;
78-
private MvcResult postResult;
79-
private MvcResult getResult;
8072
private SimpleSmtpServer smtpServer;
8173
private ClientAndServer restServer;
8274
private MockServerClient mockClient;
83-
84-
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionTriggerSteps.class);
75+
private ResponseEntity response;
8576

8677
@Before("@SubscriptionTriggerScenario")
8778
public void beforeScenario() throws IOException {
@@ -99,12 +90,14 @@ public void afterScenario() throws IOException {
9990

10091
@Given("^The REST API \"([^\"]*)\" is up and running$")
10192
public void the_REST_API_is_up_and_running(String endPoint) throws Exception {
102-
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(endPoint).accept(MediaType.APPLICATION_JSON);
103-
104-
result = mockMvc.perform(requestBuilder).andReturn();
105-
LOGGER.debug("Response code from mocked REST API: " + String.valueOf(result.getResponse().getStatus()));
106-
107-
assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
93+
HttpRequest getRequest = new HttpRequest(HttpRequest.HttpMethod.GET);
94+
response = getRequest.setHost(getHostName())
95+
.setPort(applicationPort)
96+
.addHeader("content-type", "application/json")
97+
.addHeader("Accept", "application/json")
98+
.setEndpoint(endPoint)
99+
.performRequest();
100+
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
108101
}
109102

110103
@Given("^Subscriptions are setup using REST API \"([^\"]*)\"$")
@@ -140,7 +133,7 @@ public void wait_for_ei_to_aggregate_objects_and_trigger_subscriptions() throws
140133
}
141134

142135
@Then("^Mail subscriptions were triggered$")
143-
public void check_mail_subscriptions_were_triggered() throws Throwable {
136+
public void check_mail_subscriptions_were_triggered() {
144137
LOGGER.debug("Verifying received emails.");
145138
List<SmtpMessage> emails = smtpServer.getReceivedEmails();
146139
assert (emails.size() > 0);
@@ -149,28 +142,26 @@ public void check_mail_subscriptions_were_triggered() throws Throwable {
149142
// assert correct sender.
150143
assertEquals(email.getHeaderValue("From"), sender);
151144
// assert given test case exist in body.
152-
assert (email.getBody().toString().contains("TC5"));
145+
assert (email.getBody().contains("TC5"));
153146
}
154147
}
155148

156149
@Then("^Rest subscriptions were triggered$")
157150
public void check_rest_subscriptions_were_triggered() throws Throwable {
158151
LOGGER.debug("Verifying REST requests.");
159152
List<String> endpointsToCheck = new ArrayList<>(
160-
Arrays.asList(REST_ENDPOINT, REST_ENDPOINT_AUTH, REST_ENDPOINT_PARAMS, REST_ENDPOINT_AUTH_PARAMS));
153+
Arrays.asList(REST_ENDPOINT, REST_ENDPOINT_AUTH, REST_ENDPOINT_PARAMS, REST_ENDPOINT_AUTH_PARAMS, REST_ENDPOINT_ROW_BODY));
161154

162155
assert (allEndpointsGotAtLeastXCalls(endpointsToCheck, 1));
163156
for (String endpoint : endpointsToCheck) {
164157
assert (requestBodyContainsStatedValues(endpoint));
165-
166158
}
167159
}
168160

169161
/**
170162
* Assemble subscription names in a list.
171163
*
172-
* @param jsonDataAsString
173-
* JSON string containing subscriptions
164+
* @param jsonDataAsString JSON string containing subscriptions
174165
* @throws Throwable
175166
*/
176167
private void readSubscriptionNames(String jsonDataAsString) throws Throwable {
@@ -183,58 +174,55 @@ private void readSubscriptionNames(String jsonDataAsString) throws Throwable {
183174
/**
184175
* POST subscriptions to endpoint.
185176
*
186-
* @param jsonDataAsString
187-
* JSON string containing subscriptions
188-
* @param endPoint
189-
* endpoint to use in POST
177+
* @param jsonDataAsString JSON string containing subscriptions
178+
* @param endPoint endpoint to use in POST
190179
* @throws Exception
191180
*/
192181
private void postSubscriptions(String jsonDataAsString, String endPoint) throws Exception {
193-
RequestBuilder requestBuilder = MockMvcRequestBuilders.post(endPoint).accept(MediaType.APPLICATION_JSON)
194-
.content(jsonDataAsString).contentType(MediaType.APPLICATION_JSON);
195-
196-
postResult = mockMvc.perform(requestBuilder).andReturn();
197-
LOGGER.debug("Response code from REST when adding subscriptions: "
198-
+ String.valueOf(postResult.getResponse().getStatus()));
199-
200-
assertEquals(HttpStatus.OK.value(), postResult.getResponse().getStatus());
182+
HttpRequest postRequest = new HttpRequest(HttpRequest.HttpMethod.POST);
183+
response = postRequest.setHost(getHostName())
184+
.setPort(applicationPort)
185+
.addHeader("content-type", "application/json")
186+
.addHeader("Accept", "application/json")
187+
.setEndpoint(endPoint)
188+
.setBody(jsonDataAsString)
189+
.performRequest();
190+
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
201191
}
202192

203193
/**
204194
* Verify that subscriptions were successfully posted.
205195
*
206-
* @param endPoint
207-
* endpoint to use in GET
196+
* @param endPoint endpoint to use in GET
208197
* @throws Exception
209198
*/
210199
private void validateSubscriptionsSuccessfullyAdded(String endPoint) throws Exception {
211-
RequestBuilder getRequest = MockMvcRequestBuilders.get(endPoint);
212-
getResult = mockMvc.perform(getRequest).andReturn();
213-
214-
LOGGER.debug("Response code from REST when getting subscriptions: "
215-
+ String.valueOf(getResult.getResponse().getStatus()));
216-
assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
217-
200+
HttpRequest getRequest = new HttpRequest(HttpRequest.HttpMethod.GET);
201+
response = getRequest.setHost(getHostName())
202+
.setPort(applicationPort)
203+
.addHeader("content-type", "application/json")
204+
.addHeader("Accept", "application/json")
205+
.setEndpoint(endPoint)
206+
.performRequest();
207+
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
218208
LOGGER.debug("Checking that response contains all subscriptions");
219209
for (String subscriptionName : subscriptionNames) {
220-
assertTrue(getResult.getResponse().getContentAsString().contains(subscriptionName));
210+
assertTrue(response.toString().contains(subscriptionName));
221211
}
222212
}
223213

224214
/**
225215
* Checks that an enpoint got at least the number of calls as expected.
226216
*
227-
* @param endpoints
228-
* List of endpoints to check.
229-
* @param expectedCalls
230-
* Integer with the least number of calls.
217+
* @param endpoints List of endpoints to check.
218+
* @param expectedCalls Integer with the least number of calls.
231219
* @return true if all endpoints had atleast the number of calls as expected.
232220
* @throws JSONException
233221
* @throws InterruptedException
234222
*/
235223
private boolean allEndpointsGotAtLeastXCalls(final List<String> endpoints, int expectedCalls)
236224
throws JSONException, InterruptedException {
237-
List<String> endpointsToCheck = new ArrayList<String>(endpoints);
225+
List<String> endpointsToCheck = new ArrayList<>(endpoints);
238226

239227
long stopTime = System.currentTimeMillis() + 30000;
240228
while (!endpointsToCheck.isEmpty() && stopTime > System.currentTimeMillis()) {
@@ -252,8 +240,7 @@ private boolean allEndpointsGotAtLeastXCalls(final List<String> endpoints, int e
252240
/**
253241
* Verify that request made to endpoint contains the correct information.
254242
*
255-
* @param endpoint
256-
* endpoint to check
243+
* @param endpoint endpoint to check
257244
* @return true if verification was successful, false otherwise
258245
* @throws JSONException
259246
*/
@@ -285,15 +272,18 @@ private void setupRestEndpoints() {
285272
int port = SocketUtils.findAvailableTcpPort();
286273
restServer = startClientAndServer(port);
287274

288-
LOGGER.debug("Setting up endpoints on host '" + BASE_URL + "' and port '" + port + "'.");
289-
mockClient = new MockServerClient(BASE_URL, port);
275+
LOGGER.debug("Setting up endpoints on host '" + getHostName() + "' and port '" + port + "'.");
276+
mockClient = new MockServerClient(getHostName(), port);
290277
mockClient.when(request().withMethod("POST").withPath(REST_ENDPOINT)).respond(response().withStatusCode(201));
291-
mockClient.when(request().withMethod("POST").withPath(REST_ENDPOINT_AUTH))
278+
mockClient.when(request().withMethod("POST").withPath(REST_ENDPOINT_AUTH)
279+
.withHeader("Authorization", "Basic TXkgbW90aGVyIGhhcyAyIGNhdHMgYW5kIHRoZXkgYXJlIGNyYXp5"))
292280
.respond(response().withStatusCode(201));
293281
mockClient.when(request().withMethod("POST").withPath(REST_ENDPOINT_PARAMS))
294282
.respond(response().withStatusCode(201));
295283
mockClient.when(request().withMethod("POST").withPath(REST_ENDPOINT_AUTH_PARAMS))
296284
.respond(response().withStatusCode(201));
285+
mockClient.when(request().withMethod("POST").withPath(REST_ENDPOINT_ROW_BODY))
286+
.respond(response().withStatusCode(201));
297287
}
298288

299289
/**
@@ -323,13 +313,13 @@ protected List<String> getEventNamesToSend() {
323313
/**
324314
* Replaces tags in the subscription JSON string with valid information.
325315
*
326-
* @param text
327-
* JSON string containing replaceable tags
316+
* @param text JSON string containing replaceable tags
328317
* @return Processed content
329318
*/
330319
private String stringReplaceText(String text) {
331320
text = text.replaceAll("\\$\\{rest\\.host\\}", "localhost");
332-
text = text.replaceAll("\\$\\{rest\\.port\\}", String.valueOf(restServer.getPort()));
321+
text = text.replaceAll("\\$\\{rest\\.port\\}", String.valueOf(restServer.getLocalPort()));
322+
text = text.replaceAll("\\$\\{rest\\.row.body\\}", REST_ENDPOINT_ROW_BODY);
333323
text = text.replaceAll("\\$\\{rest\\.endpoint\\}", REST_ENDPOINT);
334324
text = text.replaceAll("\\$\\{rest\\.endpoint\\.auth\\}", REST_ENDPOINT_AUTH);
335325
text = text.replaceAll("\\$\\{rest\\.endpoint\\.params\\}", REST_ENDPOINT_PARAMS);

src/functionaltests/resources/subscription_multiple.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,37 @@
149149
]
150150
}
151151
]
152+
},
153+
{
154+
"subscriptionName": "Subscription_Row_Body",
155+
"userName": "DEF",
156+
"repeat": false,
157+
"created": "data-time",
158+
"notificationType": "REST_POST",
159+
"notificationMeta": "http://${rest.host}:${rest.port}${rest.row.body}",
160+
"restPostBodyMediaType": "application/json",
161+
"notificationMessageKeyValues": [
162+
{
163+
"formkey": "",
164+
"formvalue": "{parameter: [{ name: 'jsonparams', value : to_string(@)}]}"
165+
}
166+
],
167+
"requirements": [
168+
{
169+
"type": "ARTIFACT_1",
170+
"conditions": [
171+
{"jmespath": "gav.groupId=='com.mycompany.myproduct'"},
172+
{"jmespath": "testCaseExecutions[?outcome.conclusion == 'SUCCESSFUL' && outcome.id=='TC5']"}
173+
]
174+
175+
},
176+
{
177+
"type": "ARTIFACT_1",
178+
"conditions": [
179+
{"jmespath": "gav.groupId=='com.mycompany.myproduct'"},
180+
{"jmespath": "testCaseExecutions[?testCaseStartedEventId == 'cb9d64b0-a6e9-4419-8b5d-a650c27c59ca']"}
181+
]
182+
}
183+
]
152184
}
153185
]

0 commit comments

Comments
 (0)