<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright 2017-2022 Micro Focus or one of its affiliates.

    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.

-->
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.github.cafdataprocessing</groupId>
        <artifactId>worker-markup-aggregator</artifactId>
        <version>3.2.2-1171</version>
    </parent>

    <artifactId>worker-markup-container-fs</artifactId>
    <packaging>pom</packaging>

    <!-- Properties for the worker. -->
    <properties>
        <test.case.generation>false</test.case.generation>
        <test.configs>${project.basedir}/test-configs</test.configs>
        <test.data>${project.basedir}/test-data/input</test.data>
        <tests.binary.content.location>${test.data}</tests.binary.content.location>
        <maven.install.skip>true</maven.install.skip>
        <maven.deploy.skip>true</maven.deploy.skip>
    </properties>

    <!-- CAF standard worker container dependency, MarkupWorker dependency. -->
    <dependencies>
        <dependency>
            <groupId>com.github.workerframework</groupId>
            <artifactId>standard-worker-container</artifactId>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.github.cafdataprocessing</groupId>
            <artifactId>worker-markup</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.cafdataprocessing</groupId>
            <artifactId>util-email-content-segregation-script</artifactId>
            <type>tar.gz</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.github.workerframework.testing</groupId>
            <artifactId>workerframework-testing-integration</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.cafdataprocessing</groupId>
            <artifactId>worker-markup-testing</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Plugin for compiling the test source files. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Unpack the base worker configuration files -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>process-config-files</id>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.github.workerframework</groupId>
                                    <artifactId>worker-default-configs</artifactId>
                                    <outputDirectory>${project.build.directory}/config-files</outputDirectory>
                                    <excludes>META-INF/**</excludes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Process the local configuration files -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>process-config-files</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/config-files</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/config</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Plugin for running integration tests in the integration-test phase -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <systemPropertyVariables>
                        <docker.host.address>${docker.host.address}</docker.host.address>
                        <worker.adminport>${worker.adminport}</worker.adminport>
                        <rabbitmq.node.port>${rabbitmq.node.port}</rabbitmq.node.port>
                        <rabbitmq.ctrl.port>${rabbitmq.ctrl.port}</rabbitmq.ctrl.port>
                        <input.folder>${test.data}</input.folder>
                        <expected.folder>${test.data}</expected.folder>
                        <test.sourcefile.base.folder>${test.binary.content.location}</test.sourcefile.base.folder>
                        <task.template>${project.basedir}/test-templates/sourcedata-template.yml</task.template>
                        <testCaseGeneration>${test.case.generation}</testCaseGeneration>
                        <worker.testing.usehttpdatastore>true</worker.testing.usehttpdatastore>
                    </systemPropertyVariables>
                    <environmentVariables>
                        <CAF_APPNAME>test/worker-markup</CAF_APPNAME>
                        <CAF_CONFIG_PATH>${test.configs}</CAF_CONFIG_PATH>
                        <CAF_INTEGRATIONTESTS_FS_PATH>\\${docker.host.address}@${webdav.apache.port}\webdav</CAF_INTEGRATIONTESTS_FS_PATH>
                        <CAF_WORKER_HTTP_DATASTORE_URL>http://${docker.host.address}:${webdav.apache.port}/webdav</CAF_WORKER_HTTP_DATASTORE_URL>
                    </environmentVariables>
                </configuration>
            </plugin>

            <!--
                Builds the Docker image for the MarkupWorker, starts RabbitMQ and assembles the configuration files for running the
                integration tests.
            -->
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.20.1</version>
                <executions>
                    <!-- Build the container in compile phase. -->
                    <execution>
                        <id>build-docker-container</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                    <!-- Start the containers in pre-integration-test phase. -->
                    <execution>
                        <id>start</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <!-- Stop the containers in post-integration-test phase. -->
                    <execution>
                        <id>stop</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                    <!-- Push the docker image for the worker to Artifactory in deploy phase, using the address specified in <registry> -->
                    <execution>
                        <id>upload-docker-container</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <filter>worker-markup-fs</filter>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <watchInterval>500</watchInterval>
                    <logDate>default</logDate>
                    <verbose>true</verbose>
                    <autoPull>on</autoPull>
                    <images>
                        <image>
                            <alias>webdav</alias>
                            <name>${dockerHubPublic}/cloudesire/webdav</name>
                            <run>
                                <volumes>
                                    <bind>
                                        <volume>/srv/common/webdav</volume>
                                    </bind>
                                </volumes>
                                <ports>
                                    <port>${webdav.apache.port}:8080</port>
                                </ports>
                                <env>
                                    <APACHE_PORT>8080</APACHE_PORT>
                                </env>
                                <wait>
                                    <log>WebDAV Server startup complete</log>
                                    <http>
                                        <url>http://${docker.host.address}:${webdav.apache.port}</url>
                                    </http>
                                    <time>120000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                            </run>
                        </image>

                        <!-- Run the RabbitMQ image. -->
                        <image>
                            <alias>rabbitmq</alias>
                            <name>${dockerHubPublic}/library/rabbitmq:3-management</name>
                            <run>
                                <ports>
                                    <port>${rabbitmq.ctrl.port}:15672</port>
                                    <port>${rabbitmq.node.port}:5672</port>
                                </ports>
                                <wait>
                                    <log>Server startup complete</log>
                                    <http>
                                        <url>http://${docker.host.address}:${rabbitmq.ctrl.port}</url>
                                    </http>
                                    <time>120000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                            </run>
                        </image>

                        <!-- Assemble the config files from the test-configs folder to an image in /config on the host. -->
                        <image>
                            <alias>config</alias>
                            <name>${project.artifactId}-test-config:${project.version}</name>
                            <build>
                                <from>${dockerHubPublic}/library/busybox:latest</from>
                                <assembly>
                                    <exportTargetDir>true</exportTargetDir>
                                    <basedir>/config</basedir>
                                    <inline>
                                        <fileSets>
                                            <fileSet>
                                                <directory>test-configs</directory>
                                                <outputDirectory>/</outputDirectory>
                                                <includes>
                                                    <include>*</include>
                                                </includes>
                                            </fileSet>
                                        </fileSets>
                                    </inline>
                                </assembly>
                            </build>
                        </image>

                        <!-- Configuration for the worker-markup-container image -->
                        <image>
                            <alias>worker-markup-fs</alias>
                            <name>${dockerDataProcessingOrg}worker-markup${dockerProjectVersion}</name>
                            <build>
                                <from>${dockerHubPublic}/cafapi/opensuse-jeptalon:3.4.4</from>
                                <cmd>
                                    /maven/worker.sh
                                </cmd>
                                <healthCheck>
                                    <cmd>curl -f http://localhost:8081/healthcheck || exit 1</cmd>
                                </healthCheck>
                                <assembly>
                                    <mode>tar</mode>
                                    <inline>
                                        <!-- Including the executable worker.sh and worker.yaml files in the docker tar -->
                                        <fileSets>
                                            <fileSet>
                                                <lineEnding>unix</lineEnding>
                                                <outputDirectory>/</outputDirectory>
                                                <includes>
                                                    <include>worker.sh</include>
                                                </includes>
                                                <fileMode>0755</fileMode>
                                            </fileSet>
                                            <fileSet>
                                                <lineEnding>unix</lineEnding>
                                                <outputDirectory>/</outputDirectory>
                                                <includes>
                                                    <include>worker.yaml</include>
                                                </includes>
                                            </fileSet>
                                            <fileSet>
                                                <directory>${project.build.directory}/config-files</directory>
                                                <outputDirectory>config</outputDirectory>
                                            </fileSet>
                                        </fileSets>
                                        <!-- Including the dependencies into the docker tar -->
                                        <dependencySets>
                                            <dependencySet>
                                                <useProjectArtifact>false</useProjectArtifact>
                                                <scope>runtime</scope>
                                                <excludes>
                                                    <exclude>com.github.cafdataprocessing:util-email-content-segregation-script</exclude>
                                                </excludes>
                                            </dependencySet>
                                            <dependencySet>
                                                <useProjectArtifact>false</useProjectArtifact>
                                                <unpack>true</unpack>
                                                <includes>
                                                    <include>com.github.cafdataprocessing:util-email-content-segregation-script</include>
                                                </includes>
                                            </dependencySet>
                                        </dependencySets>
                                    </inline>
                                </assembly>
                            </build>
                            <run>
                                <ports>
                                    <port>${worker.adminport}:8081</port>
                                </ports>
                                <env>
                                    <CAF_APPNAME>test/worker-markup</CAF_APPNAME>
                                    <CAF_CONFIG_PATH>/config</CAF_CONFIG_PATH>
                                </env>
                                <volumes>
                                    <from>
                                        <image>config</image>
                                        <image>webdav</image>
                                    </from>
                                </volumes>
                                <links>
                                    <link>rabbitmq</link>
                                </links>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                                <wait>
                                    <http>
                                        <url>http://${docker.host.address}:${worker.adminport}</url>
                                    </http>
                                    <time>120000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                            </run>
                        </image>
                    </images>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.mycila</groupId>
                <artifactId>license-maven-plugin</artifactId>
                <configuration>
                    <excludes combine.children="append">
                        <exclude>testcases/**</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <!-- Use this profile if your virtual machine setup must use fixed ports. -->
        <profile>
            <id>use-default-fixed-ports</id>
            <properties>
                <rabbitmq.ctrl.port>15672</rabbitmq.ctrl.port>
                <rabbitmq.node.port>5672</rabbitmq.node.port>
                <worker.adminport>8081</worker.adminport>
            </properties>
        </profile>

        <profile>
            <id>nightly-test</id>
            <activation>
                <property>
                    <name>RE_BUILD_TYPE</name>
                    <value>release</value>
                </property>
            </activation>
            <properties>
                <test.data>${project.basedir}/testcases/automated</test.data>
                <!-- A property is created by the windows or linux nightly tests profiles to allow the nightly tests to pick
                up binary content files from a remote location e.g. test.binary.content.basedir=/mnt/test-sources/
                <test.binary.content.location>${test.binary.content.basedir}markup-test-data</test.binary.content.location>-->
                <failsafe.timeout>120000</failsafe.timeout>
            </properties>
        </profile>

        <!--
            Profile to optionally disable build of a container.
            You can enable this profile, and run your tests against a fixed container without rebuilding each time.
        -->
        <profile>
            <id>skip-build-docker-container</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>io.fabric8</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>build-docker-container</id>
                                <phase>compile</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <configuration>
                                    <skip>true</skip>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>
