<?xml version="1.0" encoding="UTF-8"?>
<!--
  #%L
  Wildfly Camel Testsuite
  %%
  Copyright (C) 2013 - 2014 RedHat
  %%
  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.
  #L%
  -->


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.wildfly.camel</groupId>
        <artifactId>wildfly-camel-itests-standalone</artifactId>
        <version>10.2.0</version>
    </parent>

    <name>Wildfly Camel :: Testsuite :: Standalone :: Docker</name>
    <artifactId>wildfly-camel-itests-standalone-docker</artifactId>

    <!-- Properties -->
    <properties>
        <server.config>standalone-full.xml</server.config>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.arquillian.cube</groupId>
            <artifactId>arquillian-cube-docker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.arquillian.cube</groupId>
            <artifactId>arquillian-cube-requirement</artifactId>
        </dependency>

        <dependency>
            <groupId>org.wildfly.camel</groupId>
            <artifactId>wildfly-camel-feature-pack</artifactId>
            <version>${project.version}</version>
            <type>pom</type>
            <!-- Avoid clashes with Arquillian Cube Jersey JAX-RS provider -->
            <exclusions>
                <exclusion>
                    <groupId>org.jboss.resteasy</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-consul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-stomp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>org.awaitility</groupId>
            <artifactId>awaitility</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jboss.galleon</groupId>
                <artifactId>galleon-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>
                        import java.net.InetAddress
                        import java.nio.file.Files
                        import java.nio.file.Paths
                        import java.util.regex.Matcher

                        def skip = (System.getProperty("skipTests") != null || System.getProperty("skipDockerPull") != null)
                        if (!skip) {
                            // Pull before tests run inside of the container due to: https://issues.jboss.org/browse/ARQ-688
                            boolean dockerAvailable = System.getenv("DOCKER_HOST") != null || Files.exists(Paths.get("/var/run/docker.sock"))
                            if (dockerAvailable) {
                                def cubeSpec = new File(project.basedir.path + "/src/test/resources/arquillian.xml").text
                                Matcher matcher = cubeSpec =~ /.*image: (.*)/
                                while (matcher.find()) {
                                    def image = matcher.group(1)

                                    // Replace maven properties in image names
                                    def props = (image =~ /.*\{(.*?)\}.*/)
                                    if (props.matches()) {
                                        def propName = props[0][1]
                                        def propValue = properties.get(propName)
                                        image = image.replace("\${${propName}}", propValue)
                                    }

                                    println "[DOCKER] Pulling Docker image: ${image}"
                                    "docker pull ${image}".execute().waitForProcessOutput(System.out, System.err)
                                }
                            }
                        }

                        // Enables docker.host maven property to be used in arquillian.xml
                        String dockerHost = System.getenv("DOCKER_HOST")
                        if (dockerHost == null) {
                            project.properties["docker.host"] = InetAddress.getLocalHost().getHostName()
                        } else {
                            URI uri = new URI(dockerHost)
                            project.properties["docker.host"] = uri.getHost()
                        }
                    </source>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <systemPropertyVariables>
                        <org.jboss.remoting-jmx.timeout>300</org.jboss.remoting-jmx.timeout>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
