/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.trino.testing.containers;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.TestcontainersConfiguration;

import java.io.Closeable;

import static com.google.common.base.Preconditions.checkState;
import static java.lang.Boolean.parseBoolean;
import static java.lang.System.getenv;
import static org.testcontainers.utility.MountableFile.forClasspathResource;

public final class TestContainers
{
    // To reuse container set TESTCONTAINERS_REUSE_ENABLE=true environment variable.
    // TESTCONTAINERS_REUSE_ENABLE is an environment variable defined in testcontainers library.
    private static final boolean TESTCONTAINERS_REUSE_ENABLE = parseBoolean(getenv("TESTCONTAINERS_REUSE_ENABLE"));

    private TestContainers() {}

    // You should not close the container directly if you want to reuse it.
    // Instead you should close closeable returned by {@link this::startOrReuse}
    public static Closeable startOrReuse(GenericContainer<?> container)
    {
        boolean reuse = TestcontainersConfiguration.getInstance().environmentSupportsReuse();
        checkState(reuse == TESTCONTAINERS_REUSE_ENABLE, "Unable to enable or disable container reuse");

        container.withReuse(TESTCONTAINERS_REUSE_ENABLE);
        container.start();
        if (reuse) {
            return () -> {};
        }
        return container::stop;
    }

    public static String getPathFromClassPathResource(String resourcePath)
    {
        return forClasspathResource(resourcePath)
                // Container fails to mount jar:file:/<host_path>!<resource_path> resources
                // This ensures that JAR resources are being copied out to tmp locations
                // and mounted from there.
                .getResolvedPath();
    }
}

@@ -594,16 +594,168 @@
 iners;%0A%0A
+import com.github.dockerjava.api.model.ExposedPort;%0Aimport com.github.dockerjava.api.model.PortBinding;%0Aimport com.google.common.collect.ImmutableList;%0A
 import o
@@ -915,50 +915,191 @@
 om.g
-oogle.common.base.Preconditions.checkState
+ithub.dockerjava.api.model.Ports.Binding.bindPort;%0Aimport static com.google.common.base.Preconditions.checkState;%0Aimport static com.google.common.collect.ImmutableList.toImmutableList
 ;%0Aim
@@ -1181,16 +1181,64 @@
 getenv;%0A
+import static java.util.Objects.requireNonNull;%0A
 import s
@@ -2690,11 +2690,914 @@
 );%0A    %7D
+%0A%0A    public static void exposeFixedPorts(GenericContainer%3C?%3E container)%0A    %7B%0A        checkState(System.getenv(%22CONTINUOUS_INTEGRATION%22) == null, %22%22 +%0A                %22Exposing fixed ports should not be used in regular test code. This could break parallel test execution. %22 +%0A                %22This method is supposed to be invoked from local development helpers only e.g. QueryRunner.main(), %22 +%0A                %22hence it should never run on CI%22);%0A%0A        ImmutableList%3CPortBinding%3E portBindings = container.getExposedPorts().stream()%0A                .map(exposedPort -%3E new PortBinding(bindPort(exposedPort), new ExposedPort(exposedPort)))%0A                .collect(toImmutableList());%0A%0A        container.withCreateContainerCmdModifier(cmd -%3E cmd%0A                .withHostConfig(requireNonNull(cmd.getHostConfig(), %22hostConfig is null%22)%0A                        .withPortBindings(portBindings)));%0A    %7D
 %0A%7D%0A

Add method for exposing port on local environment