|
| 1 | +package com.oviva.telematik.epa4all.restservice; |
| 2 | + |
| 3 | +import static io.restassured.RestAssured.given; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 7 | +import io.restassured.RestAssured; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.StringReader; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.Optional; |
| 12 | +import java.util.Properties; |
| 13 | +import java.util.UUID; |
| 14 | +import java.util.concurrent.CountDownLatch; |
| 15 | +import java.util.concurrent.Executors; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.concurrent.atomic.AtomicReference; |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Disabled; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +// needs an actual konnektor running! |
| 24 | +@Disabled("e2e") |
| 25 | +class MainE2ETest { |
| 26 | + |
| 27 | + private static final CountDownLatch exit = new CountDownLatch(1); |
| 28 | + |
| 29 | + private static Main app; |
| 30 | + |
| 31 | + @BeforeAll |
| 32 | + static void setUp() throws Exception { |
| 33 | + app = bootApp(); |
| 34 | + } |
| 35 | + |
| 36 | + @AfterAll |
| 37 | + static void tearDown() throws Exception { |
| 38 | + exit.countDown(); |
| 39 | + exit.await(5, TimeUnit.SECONDS); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void bootsHealthy() { |
| 44 | + given().get("/health").then().statusCode(200); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void writeDocument() { |
| 49 | + |
| 50 | + final var insurantId = "X110661675"; |
| 51 | + |
| 52 | + var documentId = UUID.randomUUID(); |
| 53 | + var content = loadDocument(documentId); |
| 54 | + given() |
| 55 | + .body(new CreateDocument(content, "application/fhir+xml", insurantId)) |
| 56 | + .header("Content-Type", "application/json") |
| 57 | + .post("/documents") |
| 58 | + .then() |
| 59 | + .statusCode(200) |
| 60 | + .log() |
| 61 | + .all(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void replaceDocument() { |
| 66 | + |
| 67 | + final var insurantId = "X110661675"; |
| 68 | + |
| 69 | + var bundleId = UUID.randomUUID(); |
| 70 | + |
| 71 | + var content = loadDocument(bundleId); |
| 72 | + var writtenDocumentId = |
| 73 | + given() |
| 74 | + .body(new CreateDocument(content, "application/fhir+xml", insurantId)) |
| 75 | + .header("Content-Type", "application/json") |
| 76 | + .post("/documents") |
| 77 | + .then() |
| 78 | + .statusCode(200) |
| 79 | + .extract() |
| 80 | + .jsonPath() |
| 81 | + .get("id"); |
| 82 | + |
| 83 | + // When |
| 84 | + var newBundleId = UUID.randomUUID(); |
| 85 | + var newContent = loadDocument(newBundleId); |
| 86 | + given() |
| 87 | + .body(new CreateDocument(newContent, "application/fhir+xml", insurantId)) |
| 88 | + .header("Content-Type", "application/json") |
| 89 | + .post("/documents/" + writtenDocumentId) |
| 90 | + .then() |
| 91 | + .statusCode(200); |
| 92 | + } |
| 93 | + |
| 94 | + public record CreateDocument( |
| 95 | + @JsonProperty("content") byte[] content, |
| 96 | + @JsonProperty("content_type") String contentType, |
| 97 | + @JsonProperty("insurant_id") String insurantId) {} |
| 98 | + |
| 99 | + byte[] loadDocument(UUID id) { |
| 100 | + try (var is = MainE2ETest.class.getResourceAsStream("/fhir_document.xml.template")) { |
| 101 | + var bytes = is.readAllBytes(); |
| 102 | + var str = new String(bytes, StandardCharsets.UTF_8); |
| 103 | + return str.formatted(id).getBytes(StandardCharsets.UTF_8); |
| 104 | + } catch (IOException e) { |
| 105 | + fail(e); |
| 106 | + return null; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static Main bootApp() throws IOException { |
| 111 | + |
| 112 | + var config = new Properties(); |
| 113 | + config.load( |
| 114 | + new StringReader( |
| 115 | + """ |
| 116 | + konnektor.uri=https://10.156.145.103:443 |
| 117 | + proxy.address=127.0.0.1 |
| 118 | + port=0 |
| 119 | + environment=RU |
| 120 | + log.level=DEBUG |
| 121 | + """)); |
| 122 | + |
| 123 | + var executor = Executors.newFixedThreadPool(1); |
| 124 | + |
| 125 | + var started = new CountDownLatch(1); |
| 126 | + var appRef = new AtomicReference<Main>(); |
| 127 | + executor.execute( |
| 128 | + () -> { |
| 129 | + try (var m = new Main(k -> Optional.ofNullable(config.getProperty(k)))) { |
| 130 | + m.run(); |
| 131 | + appRef.set(m); |
| 132 | + started.countDown(); |
| 133 | + exit.await(); |
| 134 | + } catch (InterruptedException e) { |
| 135 | + Thread.currentThread().interrupt(); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + var ok = false; |
| 140 | + try { |
| 141 | + ok = started.await(5, TimeUnit.SECONDS); |
| 142 | + } catch (InterruptedException e) { |
| 143 | + Thread.currentThread().interrupt(); |
| 144 | + } |
| 145 | + if (!ok) { |
| 146 | + fail("server failed to boot within timeout"); |
| 147 | + } |
| 148 | + |
| 149 | + RestAssured.baseURI = "http://127.0.0.1:%d".formatted(appRef.get().listenerAddress().getPort()); |
| 150 | + return appRef.get(); |
| 151 | + } |
| 152 | +} |
0 commit comments