|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 the original author or 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 | + * https://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 | + |
| 17 | +package sample; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.function.Consumer; |
| 22 | + |
| 23 | +import org.apache.http.impl.client.HttpClients; |
| 24 | +import org.apache.http.impl.client.StandardHttpRequestRetryHandler; |
| 25 | +import org.awaitility.Awaitility; |
| 26 | +import org.awaitility.core.ConditionTimeoutException; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.testcontainers.containers.GenericContainer; |
| 29 | +import org.testcontainers.images.builder.ImageFromDockerfile; |
| 30 | + |
| 31 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 32 | +import org.springframework.boot.web.client.RestTemplateBuilder; |
| 33 | +import org.springframework.http.HttpStatus; |
| 34 | +import org.springframework.http.ResponseEntity; |
| 35 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +/** |
| 40 | + * Abstract class for deployment integration tests. |
| 41 | + */ |
| 42 | +abstract class AbstractDeploymentIntegrationTests { |
| 43 | + |
| 44 | + protected static final int DEFAULT_PORT = 8080; |
| 45 | + |
| 46 | + @Test |
| 47 | + void home() { |
| 48 | + getDeployedApplication().test((rest) -> { |
| 49 | + ResponseEntity<String> response = rest.getForEntity("/", String.class); |
| 50 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 51 | + assertThat(response.getBody()).isEqualTo("Hello World"); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void health() { |
| 57 | + getDeployedApplication().test((rest) -> { |
| 58 | + ResponseEntity<String> response = rest.getForEntity("/actuator/health", String.class); |
| 59 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 60 | + assertThat(response.getBody()).isEqualTo("{\"status\":\"UP\"}"); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void conditionalOnWarShouldBeTrue() { |
| 66 | + getDeployedApplication().test((rest) -> { |
| 67 | + ResponseEntity<String> response = rest.getForEntity("/actuator/war", String.class); |
| 68 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 69 | + assertThat(response.getBody()).isEqualTo("{\"hello\":\"world\"}"); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + private DeployedApplication getDeployedApplication() { |
| 74 | + return new DeployedApplication(getContainer(), getPort()); |
| 75 | + } |
| 76 | + |
| 77 | + protected int getPort() { |
| 78 | + return DEFAULT_PORT; |
| 79 | + } |
| 80 | + |
| 81 | + abstract WarDeploymentContainer getContainer(); |
| 82 | + |
| 83 | + static final class DeployedApplication { |
| 84 | + |
| 85 | + private final WarDeploymentContainer container; |
| 86 | + |
| 87 | + private final int port; |
| 88 | + |
| 89 | + DeployedApplication(WarDeploymentContainer container, int port) { |
| 90 | + this.container = container; |
| 91 | + this.port = port; |
| 92 | + } |
| 93 | + |
| 94 | + private void test(Consumer<TestRestTemplate> consumer) { |
| 95 | + TestRestTemplate rest = new TestRestTemplate(new RestTemplateBuilder() |
| 96 | + .rootUri("http://" + this.container.getHost() + ":" + this.container.getMappedPort(this.port) |
| 97 | + + "/spring-boot") |
| 98 | + .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(HttpClients.custom() |
| 99 | + .setRetryHandler(new StandardHttpRequestRetryHandler(10, false)).build()))); |
| 100 | + try { |
| 101 | + Awaitility.await().atMost(Duration.ofMinutes(10)).until(() -> { |
| 102 | + try { |
| 103 | + consumer.accept(rest); |
| 104 | + return true; |
| 105 | + } |
| 106 | + catch (Throwable ex) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + catch (ConditionTimeoutException ex) { |
| 112 | + System.out.println(this.container.getLogs()); |
| 113 | + throw ex; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + static final class WarDeploymentContainer extends GenericContainer<WarDeploymentContainer> { |
| 120 | + |
| 121 | + WarDeploymentContainer(String baseImage, String deploymentLocation, int port) { |
| 122 | + super(new ImageFromDockerfile().withFileFromFile("spring-boot.war", findWarToDeploy()) |
| 123 | + .withDockerfileFromBuilder((builder) -> builder.from(baseImage) |
| 124 | + .add("spring-boot.war", deploymentLocation + "/spring-boot.war").build())); |
| 125 | + withExposedPorts(port).withStartupTimeout(Duration.ofMinutes(5)).withStartupAttempts(3); |
| 126 | + } |
| 127 | + |
| 128 | + private static File findWarToDeploy() { |
| 129 | + File[] candidates = new File("build/libs").listFiles(); |
| 130 | + assertThat(candidates).hasSize(1); |
| 131 | + return candidates[0]; |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments