Skip to content

Commit 30d4390

Browse files
committed
feat: #115 Add test messages retrieval methods
Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com>
1 parent 8fd8ddf commit 30d4390

File tree

10 files changed

+557
-2
lines changed

10 files changed

+557
-2
lines changed

src/main/java/io/github/microcks/testcontainers/MicrocksContainer.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616
package io.github.microcks.testcontainers;
1717

18+
import io.github.microcks.testcontainers.model.RequestResponsePair;
1819
import io.github.microcks.testcontainers.model.Secret;
1920
import io.github.microcks.testcontainers.model.TestResult;
2021
import io.github.microcks.testcontainers.model.TestRequest;
22+
import io.github.microcks.testcontainers.model.UnidirectionalEvent;
2123

2224
import com.fasterxml.jackson.annotation.JsonInclude;
2325
import com.github.dockerjava.api.command.InspectContainerResponse;
@@ -44,9 +46,11 @@
4446
import java.net.HttpURLConnection;
4547
import java.net.URL;
4648
import java.net.URLDecoder;
49+
import java.net.URLEncoder;
4750
import java.nio.charset.StandardCharsets;
4851
import java.util.Arrays;
4952
import java.util.HashSet;
53+
import java.util.List;
5054
import java.util.Set;
5155
import java.util.concurrent.CompletableFuture;
5256
import java.util.concurrent.CompletionException;
@@ -455,6 +459,109 @@ public static TestResult testEndpoint(String microcksContainerHttpEndpoint, Test
455459
throw new MicrocksException("Couldn't launch on new test on Microcks. Please check Microcks container logs");
456460
}
457461

462+
463+
/**
464+
* Retrieve messages exchanged during a test on an endpoint (for further investigation or checks).
465+
* @param testResult The TestResult to get messages for
466+
* @param operationName The name of the operation to get messages corresponding to test case
467+
* @return The list of RequestResponsePair representing messages exchanged during test case
468+
* @throws IOException If connection to Microcks container failed (no route to host, low-level network stuffs)
469+
* @throws MicrocksException If Microcks fails retrieving the messages
470+
*/
471+
public List<RequestResponsePair> getMessagesForTestCase(TestResult testResult, String operationName) throws IOException, MicrocksException {
472+
return getMessagesForTestCase(getHttpEndpoint(), testResult, operationName);
473+
}
474+
475+
/**
476+
* Retrieve messages exchanged during a test on an endpoint. This may be a fallback to non-static {@code getMessagesForTestCase(TestResult testResult, String operationName)}
477+
* method if you don't have direct access to the MicrocksContainer instance you want to get messages from.
478+
* @param microcksContainerHttpEndpoint The Http endpoint where to reach running MicrocksContainer
479+
* @param testResult The TestResult to get messages for
480+
* @param operationName The name of the operation to get messages corresponding to test case
481+
* @return The list of RequestResponsePair representing messages exchanged during test case
482+
* @throws IOException If connection to Microcks container failed (no route to host, low-level network stuffs)
483+
* @throws MicrocksException If Microcks fails retrieving the messages
484+
*/
485+
public static List<RequestResponsePair> getMessagesForTestCase(String microcksContainerHttpEndpoint, TestResult testResult, String operationName)
486+
throws IOException, MicrocksException {
487+
488+
// Build the testCase unique identifier.
489+
String operation = operationName.replace('/', '!');
490+
String testCaseId = testResult.getId() + "-" + testResult.getTestNumber() + "-" + URLEncoder.encode(operation);
491+
492+
URL url = new URL(microcksContainerHttpEndpoint + "/api/tests/" + testResult.getId() + "/messages/" + testCaseId);
493+
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
494+
httpConn.setRequestMethod("GET");
495+
httpConn.setRequestProperty(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.getMediaType());
496+
httpConn.setDoOutput(false);
497+
498+
if (httpConn.getResponseCode() == 200) {
499+
// Read response content and disconnect Http connection.
500+
StringBuilder responseContent = getResponseContent(httpConn);
501+
httpConn.disconnect();
502+
503+
// Unmarshal and return result.
504+
return getMapper().readValue(responseContent.toString(),
505+
mapper.getTypeFactory().constructCollectionType(List.class, RequestResponsePair.class));
506+
}
507+
if (log.isErrorEnabled()) {
508+
log.error("Couldn't retrieve messages status {} ", httpConn.getResponseCode());
509+
}
510+
httpConn.disconnect();
511+
throw new MicrocksException("Couldn't retrieve messages on test on Microcks. Please check Microcks container logs");
512+
}
513+
514+
/**
515+
* Retrieve event messages received during a test on an endpoint (for further investigation or checks).
516+
* @param testResult The TestResult to get event messages for
517+
* @param operationName The name of the operation to get event messages corresponding to test case
518+
* @return The list of UnidirectionalEvent representing events received during test case
519+
* @throws IOException If connection to Microcks container failed (no route to host, low-level network stuffs)
520+
* @throws MicrocksException If Microcks fails retrieving the messages
521+
*/
522+
public List<UnidirectionalEvent> getEventMessagesForTestCase(TestResult testResult, String operationName) throws IOException, MicrocksException {
523+
return getEventMessagesForTestCase(getHttpEndpoint(), testResult, operationName);
524+
}
525+
526+
/**
527+
* Retrieve event messages received during a test on an endpoint. This may be a fallback to non-static {@code getEventMessagesForTestCase(TestResult testResult, String operationName)}
528+
* method if you don't have direct access to the MicrocksContainer instance you want to get messages from.
529+
* @param microcksContainerHttpEndpoint The Http endpoint where to reach running MicrocksContainer
530+
* @param testResult The TestResult to get event messages for
531+
* @param operationName The name of the operation to get event messages corresponding to test case
532+
* @return The list of UnidirectionalEvent representing events received during test case
533+
* @throws IOException If connection to Microcks container failed (no route to host, low-level network stuffs)
534+
* @throws MicrocksException If Microcks fails retrieving the event messages
535+
*/
536+
public static List<UnidirectionalEvent> getEventMessagesForTestCase(String microcksContainerHttpEndpoint, TestResult testResult, String operationName)
537+
throws IOException, MicrocksException {
538+
539+
// Build the testCase unique identifier.
540+
String operation = operationName.replace('/', '!');
541+
String testCaseId = testResult.getId() + "-" + testResult.getTestNumber() + "-" + URLEncoder.encode(operation);
542+
543+
URL url = new URL(microcksContainerHttpEndpoint + "/api/tests/" + testResult.getId() + "/events/" + testCaseId);
544+
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
545+
httpConn.setRequestMethod("GET");
546+
httpConn.setRequestProperty(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.getMediaType());
547+
httpConn.setDoOutput(false);
548+
549+
if (httpConn.getResponseCode() == 200) {
550+
// Read response content and disconnect Http connection.
551+
StringBuilder responseContent = getResponseContent(httpConn);
552+
httpConn.disconnect();
553+
554+
// Unmarshal and return result.
555+
return getMapper().readValue(responseContent.toString(),
556+
mapper.getTypeFactory().constructCollectionType(List.class, UnidirectionalEvent.class));
557+
}
558+
if (log.isErrorEnabled()) {
559+
log.error("Couldn't retrieve messages status {} ", httpConn.getResponseCode());
560+
}
561+
httpConn.disconnect();
562+
throw new MicrocksException("Couldn't retrieve messages on test on Microcks. Please check Microcks container logs");
563+
}
564+
458565
/**
459566
* Download a remote artifact as a primary or main one within the Microcks container.
460567
* @param remoteArtifactUrl The URL to remote artifact (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
@@ -600,6 +707,17 @@ private static ObjectMapper getMapper() {
600707
return mapper;
601708
}
602709

710+
private static StringBuilder getResponseContent(HttpURLConnection httpConn) throws IOException {
711+
StringBuilder responseContent = new StringBuilder();
712+
try (BufferedReader br = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), StandardCharsets.UTF_8))) {
713+
String responseLine;
714+
while ((responseLine = br.readLine()) != null) {
715+
responseContent.append(responseLine.trim());
716+
}
717+
}
718+
return responseContent;
719+
}
720+
603721
private static HttpURLConnection uploadFileToMicrocks(URL microcksApiURL, File file, String contentType) throws IOException {
604722
// Creates a unique boundary based on time stamp
605723
String boundary = "===" + System.currentTimeMillis() + "===";
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.microcks.testcontainers.model;
17+
18+
/**
19+
* A simple event message published or sent in an asynchronous exchange.
20+
* @author laurent
21+
*/
22+
public class EventMessage extends Message {
23+
24+
private String id;
25+
private String mediaType;
26+
private String dispatchCriteria;
27+
28+
public String getId() {
29+
return id;
30+
}
31+
32+
public void setId(String id) {
33+
this.id = id;
34+
}
35+
36+
public String getMediaType() {
37+
return mediaType;
38+
}
39+
40+
public void setMediaType(String mediaType) {
41+
this.mediaType = mediaType;
42+
}
43+
44+
public String getDispatchCriteria() {
45+
return dispatchCriteria;
46+
}
47+
48+
public void setDispatchCriteria(String dispatchCriteria) {
49+
this.dispatchCriteria = dispatchCriteria;
50+
}
51+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.microcks.testcontainers.model;
17+
18+
import java.util.HashSet;
19+
import java.util.Set;
20+
21+
/**
22+
* Base class holding common attributes for Request, Response and EventMessage domain objects.
23+
* @author laurent
24+
*/
25+
public abstract class Message {
26+
27+
private String name;
28+
private String content;
29+
private String operationId;
30+
private String testCaseId;
31+
private String sourceArtifact;
32+
33+
private Set<Header> headers;
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public String getContent() {
44+
return content;
45+
}
46+
47+
public void setContent(String content) {
48+
this.content = content;
49+
}
50+
51+
public String getOperationId() {
52+
return operationId;
53+
}
54+
55+
public void setOperationId(String operationId) {
56+
this.operationId = operationId;
57+
}
58+
59+
public String getTestCaseId() {
60+
return testCaseId;
61+
}
62+
63+
public void setTestCaseId(String testCaseId) {
64+
this.testCaseId = testCaseId;
65+
}
66+
67+
public String getSourceArtifact() {
68+
return sourceArtifact;
69+
}
70+
71+
public void setSourceArtifact(String sourceArtifact) {
72+
this.sourceArtifact = sourceArtifact;
73+
}
74+
75+
public Set<Header> getHeaders() {
76+
return headers;
77+
}
78+
79+
public void setHeaders(Set<Header> headers) {
80+
this.headers = headers;
81+
}
82+
83+
public void addHeader(Header header) {
84+
if (this.headers == null) {
85+
this.headers = new HashSet<>();
86+
}
87+
headers.add(header);
88+
}
89+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.microcks.testcontainers.model;
17+
18+
/**
19+
* Companion objects for Request representing query parameter.
20+
* @author laurent
21+
*/
22+
public class Parameter {
23+
24+
private String name;
25+
private String value;
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public String getValue() {
36+
return value;
37+
}
38+
39+
public void setValue(String value) {
40+
this.value = value;
41+
}
42+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.microcks.testcontainers.model;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
/**
22+
* Domain object representing a Microservice operation / action invocation request.
23+
* @author laurent
24+
*/
25+
public class Request extends Message {
26+
27+
private String id;
28+
private String responseId;
29+
30+
private List<Parameter> queryParameters;
31+
32+
public String getId() {
33+
return id;
34+
}
35+
36+
public void setId(String id) {
37+
this.id = id;
38+
}
39+
40+
public String getResponseId() {
41+
return responseId;
42+
}
43+
44+
public void setResponseId(String responseId) {
45+
this.responseId = responseId;
46+
}
47+
48+
public List<Parameter> getQueryParameters() {
49+
return queryParameters;
50+
}
51+
52+
public void setQueryParameters(List<Parameter> queryParameters) {
53+
this.queryParameters = queryParameters;
54+
}
55+
56+
public void addQueryParameter(Parameter parameter) {
57+
if (this.queryParameters == null) {
58+
this.queryParameters = new ArrayList<>();
59+
}
60+
queryParameters.add(parameter);
61+
}
62+
}

0 commit comments

Comments
 (0)