|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Contributors to the Eclipse Foundation. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.main.distributions.docker.embedded; |
| 18 | + |
| 19 | +import java.net.http.HttpResponse; |
| 20 | +import java.time.Duration; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.testcontainers.containers.BindMode; |
| 24 | +import org.testcontainers.containers.GenericContainer; |
| 25 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 26 | +import org.testcontainers.junit.jupiter.Container; |
| 27 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 28 | + |
| 29 | +import static org.glassfish.main.distributions.docker.testutils.HttpUtilities.getEmbeddedApplication; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | + |
| 33 | +@Testcontainers |
| 34 | +public class EmbeddedDeploymentIT { |
| 35 | + |
| 36 | + @SuppressWarnings({"rawtypes", "resource"}) |
| 37 | + @Container |
| 38 | + private final GenericContainer server = new GenericContainer<>(System.getProperty("embedded.docker.glassfish.image")) |
| 39 | + .withExposedPorts(8080) |
| 40 | + .withFileSystemBind("target/test-classes/application-test.war", "/deploy/application.war", BindMode.READ_ONLY) |
| 41 | + .waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofMinutes(2))) |
| 42 | + .withLogConsumer(o -> System.err.print("GF-Embedded: " + o.getUtf8String())); |
| 43 | + |
| 44 | + @Test |
| 45 | + void deployedApplicationIsAccessible() throws Exception { |
| 46 | + // Wait for the GLASSFISH STARTED message in logs |
| 47 | + long startTime = System.currentTimeMillis(); |
| 48 | + boolean glassfishStarted = false; |
| 49 | + while (!glassfishStarted && (System.currentTimeMillis() - startTime) < 120000) { // 2 minutes |
| 50 | + String logs = server.getLogs(); |
| 51 | + if (logs.contains("GLASSFISH STARTED")) { |
| 52 | + glassfishStarted = true; |
| 53 | + } else { |
| 54 | + Thread.sleep(1000); // Wait 1 second before checking again |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (!glassfishStarted) { |
| 59 | + System.err.println("Full logs: " + server.getLogs()); |
| 60 | + throw new RuntimeException("GlassFish did not start within 2 minutes"); |
| 61 | + } |
| 62 | + |
| 63 | + System.err.println("GlassFish started successfully, checking application deployment..."); |
| 64 | + |
| 65 | + // Verify the application is deployed by checking if it's accessible |
| 66 | + final HttpResponse<String> appResponse = getEmbeddedApplication(server, "/index.html"); |
| 67 | + System.err.println("Response status: " + appResponse.statusCode()); |
| 68 | + System.err.println("Response body: " + appResponse.body()); |
| 69 | + assertEquals(200, appResponse.statusCode(), "Application response status code"); |
| 70 | + assertTrue(appResponse.body().contains("Hello from test app"), "Application should return Hello message"); |
| 71 | + } |
| 72 | +} |
0 commit comments