+ * Whenever you make changes to the application, + * you have to rerun {@code mvn clean package}. + *
+ *+ * To debug simple-webapp which is deployed to tomcat, + * add a break point in {@link #beforeClass()} and evaluate tomcatContainer.getMappedPort(8000). + * Then create a remote debug configuration in IntelliJ using this port and start debugging. + * TODO use {@link org.testcontainers.containers.SocatContainer} to always have the same debugging port + *
+ */ +public abstract class AbstractTomcatIntegrationTest { + + private static final Logger logger = LoggerFactory.getLogger(ServletIntegrationTest.class); + + private static final String pathToWar = "../simple-webapp/target/ROOT.war"; + protected static GenericContainer tomcatContainer = new GenericContainer<>( + new ImageFromDockerfile() + .withDockerfileFromBuilder(builder -> builder + .from("tomcat:8") + .env("JPDA_ADDRESS", "8000") + .env("JPDA_TRANSPORT", "dt_socket") + .run("rm -rf /usr/local/tomcat/webapps/*") + .expose(8080, 8000) + .entryPoint("catalina.sh", "jpda", "run"))) + .withNetwork(Network.SHARED) + .withEnv("ELASTIC_APM_SERVER_URL", "http://apm-server:1080") + .withEnv("ELASTIC_APM_SERVICE_NAME", "servlet-test-app") + .withEnv("ELASTIC_APM_IGNORE_URLS", "/status*,/favicon.ico") + .withEnv("ELASTIC_APM_REPORT_SYNC", "true") + .withLogConsumer(new StandardOutLogConsumer().withPrefix("tomcat")) + .withFileSystemBind(pathToWar, "/usr/local/tomcat/webapps/ROOT.war") + .withExposedPorts(8080, 8000); + protected static MockServerContainer mockServerContainer = new MockServerContainer() + .withNetworkAliases("apm-server") + .withNetwork(Network.SHARED); + protected static OkHttpClient httpClient; + private static JsonSchema schema; + + static { + final File warFile = new File(pathToWar); + logger.info("Check file {}", warFile.getAbsolutePath()); + assertThat(warFile).exists(); + assertThat(warFile).isFile(); + assertThat(warFile.length()).isGreaterThan(0); + Stream.of(tomcatContainer, mockServerContainer).parallel().forEach(GenericContainer::start); + } + + @BeforeClass + public static void beforeClass() { + mockServerContainer.getClient().when(request("/v1/transactions")).respond(HttpResponse.response().withStatusCode(200)); + mockServerContainer.getClient().when(request("/v1/errors")).respond(HttpResponse.response().withStatusCode(200)); + schema = JsonSchemaFactory.getInstance().getSchema( + AbstractTomcatIntegrationTest.class.getResourceAsStream("/schema/transactions/payload.json")); + final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(logger::info); + loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); + httpClient = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build(); + } + + protected List