|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.time.Duration;
|
21 |
| -import java.util.Arrays; |
22 |
| -import java.util.List; |
23 | 21 | import java.util.function.Consumer;
|
24 | 22 |
|
25 | 23 | import org.apache.http.impl.client.HttpClients;
|
26 | 24 | import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
|
27 | 25 | import org.awaitility.Awaitility;
|
28 | 26 | import org.awaitility.core.ConditionTimeoutException;
|
29 |
| -import org.junit.jupiter.params.ParameterizedTest; |
30 |
| -import org.junit.jupiter.params.provider.MethodSource; |
| 27 | +import org.junit.jupiter.api.Test; |
31 | 28 | import org.testcontainers.containers.GenericContainer;
|
32 | 29 | import org.testcontainers.images.builder.ImageFromDockerfile;
|
33 | 30 |
|
34 | 31 | import org.springframework.boot.test.web.client.TestRestTemplate;
|
35 |
| -import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable; |
36 | 32 | import org.springframework.boot.web.client.RestTemplateBuilder;
|
37 | 33 | import org.springframework.http.HttpStatus;
|
38 | 34 | import org.springframework.http.ResponseEntity;
|
|
41 | 37 | import static org.assertj.core.api.Assertions.assertThat;
|
42 | 38 |
|
43 | 39 | /**
|
44 |
| - * Deployment integration tests. |
| 40 | + * Abstract class for deployment integration tests. |
45 | 41 | */
|
46 |
| -@DisabledIfDockerUnavailable |
47 |
| -class AbstractDeploymentIntegrationTests { |
| 42 | +abstract class AbstractDeploymentIntegrationTests { |
48 | 43 |
|
49 |
| - @ParameterizedTest |
50 |
| - @MethodSource("deployedApplications") |
51 |
| - void home(DeployedApplication app) { |
52 |
| - app.test((rest) -> { |
| 44 | + protected static final int DEFAULT_PORT = 8080; |
| 45 | + |
| 46 | + @Test |
| 47 | + void home() { |
| 48 | + getDeployedApplication().test((rest) -> { |
53 | 49 | ResponseEntity<String> response = rest.getForEntity("/", String.class);
|
54 | 50 | assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
55 | 51 | assertThat(response.getBody()).isEqualTo("Hello World");
|
56 | 52 | });
|
57 | 53 | }
|
58 | 54 |
|
59 |
| - @ParameterizedTest |
60 |
| - @MethodSource("deployedApplications") |
61 |
| - void health(DeployedApplication application) { |
62 |
| - application.test((rest) -> { |
| 55 | + @Test |
| 56 | + void health() { |
| 57 | + getDeployedApplication().test((rest) -> { |
63 | 58 | ResponseEntity<String> response = rest.getForEntity("/actuator/health", String.class);
|
64 | 59 | assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
65 | 60 | assertThat(response.getBody()).isEqualTo("{\"status\":\"UP\"}");
|
66 | 61 | });
|
67 | 62 | }
|
68 | 63 |
|
69 |
| - @ParameterizedTest |
70 |
| - @MethodSource("deployedApplications") |
71 |
| - void conditionalOnWarShouldBeTrue(DeployedApplication application) throws Exception { |
72 |
| - application.test((rest) -> { |
| 64 | + @Test |
| 65 | + void conditionalOnWarShouldBeTrue() { |
| 66 | + getDeployedApplication().test((rest) -> { |
73 | 67 | ResponseEntity<String> response = rest.getForEntity("/actuator/war", String.class);
|
74 | 68 | assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
75 | 69 | assertThat(response.getBody()).isEqualTo("{\"hello\":\"world\"}");
|
76 | 70 | });
|
77 | 71 | }
|
78 | 72 |
|
79 |
| - static List<DeployedApplication> deployedApplications() { |
80 |
| - return Arrays.asList( |
81 |
| - new DeployedApplication("openliberty/open-liberty:20.0.0.9-kernel-java8-openj9-ubi", "/config/dropins", |
82 |
| - 9080), |
83 |
| - new DeployedApplication("tomcat:9.0.37-jdk8-openjdk", "/usr/local/tomcat/webapps", 8080), |
84 |
| - new DeployedApplication("tomee:8-jre-8.0.2-webprofile", "/usr/local/tomee/webapps", 8080), |
85 |
| - new DeployedApplication("jboss/wildfly:20.0.1.Final", "/opt/jboss/wildfly/standalone/deployments", |
86 |
| - 8080)); |
| 73 | + private DeployedApplication getDeployedApplication() { |
| 74 | + return new DeployedApplication(getContainer(), getPort()); |
| 75 | + } |
| 76 | + |
| 77 | + protected int getPort() { |
| 78 | + return DEFAULT_PORT; |
87 | 79 | }
|
88 | 80 |
|
89 |
| - public static final class DeployedApplication { |
| 81 | + abstract WarDeploymentContainer getContainer(); |
90 | 82 |
|
91 |
| - private final String baseImage; |
| 83 | + static final class DeployedApplication { |
92 | 84 |
|
93 |
| - private final String deploymentLocation; |
| 85 | + private final WarDeploymentContainer container; |
94 | 86 |
|
95 | 87 | private final int port;
|
96 | 88 |
|
97 |
| - private DeployedApplication(String baseImage, String deploymentLocation, int port) { |
98 |
| - this.baseImage = baseImage; |
99 |
| - this.deploymentLocation = deploymentLocation; |
| 89 | + DeployedApplication(WarDeploymentContainer container, int port) { |
| 90 | + this.container = container; |
100 | 91 | this.port = port;
|
101 | 92 | }
|
102 | 93 |
|
103 | 94 | private void test(Consumer<TestRestTemplate> consumer) {
|
104 |
| - try (WarDeploymentContainer container = new WarDeploymentContainer(this.baseImage, this.deploymentLocation, |
105 |
| - this.port)) { |
106 |
| - container.start(); |
107 |
| - TestRestTemplate rest = new TestRestTemplate(new RestTemplateBuilder() |
108 |
| - .rootUri("http://" + container.getHost() + ":" + container.getMappedPort(this.port) |
109 |
| - + "/spring-boot") |
110 |
| - .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(HttpClients.custom() |
111 |
| - .setRetryHandler(new StandardHttpRequestRetryHandler(10, false)).build()))); |
112 |
| - try { |
113 |
| - Awaitility.await().atMost(Duration.ofMinutes(10)).until(() -> { |
114 |
| - try { |
115 |
| - consumer.accept(rest); |
116 |
| - return true; |
117 |
| - } |
118 |
| - catch (Throwable ex) { |
119 |
| - return false; |
120 |
| - } |
121 |
| - }); |
122 |
| - } |
123 |
| - catch (ConditionTimeoutException ex) { |
124 |
| - System.out.println(container.getLogs()); |
125 |
| - throw ex; |
126 |
| - } |
| 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; |
127 | 114 | }
|
128 |
| - } |
129 |
| - |
130 |
| - @Override |
131 |
| - public String toString() { |
132 |
| - return this.baseImage; |
133 | 115 | }
|
134 | 116 |
|
135 | 117 | }
|
136 | 118 |
|
137 |
| - private static final class WarDeploymentContainer extends GenericContainer<WarDeploymentContainer> { |
| 119 | + static final class WarDeploymentContainer extends GenericContainer<WarDeploymentContainer> { |
138 | 120 |
|
139 |
| - private WarDeploymentContainer(String baseImage, String deploymentLocation, int port) { |
| 121 | + WarDeploymentContainer(String baseImage, String deploymentLocation, int port) { |
140 | 122 | super(new ImageFromDockerfile().withFileFromFile("spring-boot.war", findWarToDeploy())
|
141 | 123 | .withDockerfileFromBuilder((builder) -> builder.from(baseImage)
|
142 | 124 | .add("spring-boot.war", deploymentLocation + "/spring-boot.war").build()));
|
|
0 commit comments