|
15 | 15 | */ |
16 | 16 | package io.github.microcks.testcontainers; |
17 | 17 |
|
| 18 | +import io.github.microcks.testcontainers.model.RequestResponsePair; |
18 | 19 | import io.github.microcks.testcontainers.model.Secret; |
19 | 20 | import io.github.microcks.testcontainers.model.TestResult; |
20 | 21 | import io.github.microcks.testcontainers.model.TestRequest; |
| 22 | +import io.github.microcks.testcontainers.model.UnidirectionalEvent; |
21 | 23 |
|
22 | 24 | import com.fasterxml.jackson.annotation.JsonInclude; |
23 | 25 | import com.github.dockerjava.api.command.InspectContainerResponse; |
|
44 | 46 | import java.net.HttpURLConnection; |
45 | 47 | import java.net.URL; |
46 | 48 | import java.net.URLDecoder; |
| 49 | +import java.net.URLEncoder; |
47 | 50 | import java.nio.charset.StandardCharsets; |
48 | 51 | import java.util.Arrays; |
49 | 52 | import java.util.HashSet; |
| 53 | +import java.util.List; |
50 | 54 | import java.util.Set; |
51 | 55 | import java.util.concurrent.CompletableFuture; |
52 | 56 | import java.util.concurrent.CompletionException; |
@@ -455,6 +459,109 @@ public static TestResult testEndpoint(String microcksContainerHttpEndpoint, Test |
455 | 459 | throw new MicrocksException("Couldn't launch on new test on Microcks. Please check Microcks container logs"); |
456 | 460 | } |
457 | 461 |
|
| 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 | + |
458 | 565 | /** |
459 | 566 | * Download a remote artifact as a primary or main one within the Microcks container. |
460 | 567 | * @param remoteArtifactUrl The URL to remote artifact (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...) |
@@ -600,6 +707,17 @@ private static ObjectMapper getMapper() { |
600 | 707 | return mapper; |
601 | 708 | } |
602 | 709 |
|
| 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 | + |
603 | 721 | private static HttpURLConnection uploadFileToMicrocks(URL microcksApiURL, File file, String contentType) throws IOException { |
604 | 722 | // Creates a unique boundary based on time stamp |
605 | 723 | String boundary = "===" + System.currentTimeMillis() + "==="; |
|
0 commit comments