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

    Copyright 2015-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>

    <groupId>com.github.cafaudit</groupId>
    <artifactId>caf-audit-service-container</artifactId>
    <packaging>pom</packaging>

    <parent>
        <groupId>com.github.cafaudit</groupId>
        <artifactId>audit-service-aggregator</artifactId>
        <version>3.9.2-892</version>
    </parent>

    <properties>
        <webserviceWarName>caf-audit-service</webserviceWarName>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.github.cafaudit</groupId>
            <artifactId>caf-audit-service</artifactId>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>com.github.cafaudit</groupId>
            <artifactId>caf-audit-service-admin</artifactId>
            <type>war</type>
        </dependency>
        <!-- Auditing Implementations -->
        <dependency>
            <groupId>com.github.cafaudit</groupId>
            <artifactId>caf-audit-binding-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>jakarta.annotation</groupId>
            <artifactId>jakarta.annotation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>opensearch-java</artifactId>
        </dependency>
        <!-- End of Auditing Implementations -->
        <dependency>
            <groupId>com.github.cafaudit</groupId>
            <artifactId>caf-audit-service-ui</artifactId>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        
        <!-- Testing dependencies -->
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.cafaudit</groupId>
            <artifactId>caf-audit-binding-webservice</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.cafaudit</groupId>
            <artifactId>caf-audit-service-testing-auditlog</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.cafaudit</groupId>
            <artifactId>caf-audit-service-client</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.stefanbirkner</groupId>
            <artifactId>system-lambda</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.ws.rs</groupId>
            <artifactId>jakarta.ws.rs-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>integration-test</id>
            <activation>
                <property>
                    <name>RE_BUILD_TYPE</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>io.fabric8</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>start-testing-containers</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>start</goal>
                                </goals>
                                <configuration>
                                    <skip>false</skip>
                                    <image>audit-service,config,elasticsearch</image>
                                </configuration>
                            </execution>
                            <execution>
                                <id>stop-testing-containers</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>stop</goal>
                                </goals>
                                <configuration>
                                    <skip>false</skip>
                                    <image>audit-service,config,elasticsearch</image>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>use-default-fixed-ports</id>
            <properties>
                <webservice.adminport>25080</webservice.adminport>
                <webservice.https.adminport>25443</webservice.https.adminport>
                <webservice.healthcheckport>25081</webservice.healthcheckport>
                <elasticsearch.http.port>9200</elasticsearch.http.port>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <!-- Compile test sources. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <testCompilerArgument>-parameters</testCompilerArgument>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Run integration tests. -->
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <id>webservice-Docker</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <systemPropertyVariables>
                        <docker.host.address>${docker.host.address}</docker.host.address>
                        <webservice.adminport>${webservice.adminport}</webservice.adminport>
                        <elasticsearch.http.port>${elasticsearch.http.port}</elasticsearch.http.port>
                        <webservice.healthcheckport>${webservice.healthcheckport}</webservice.healthcheckport>
                    </systemPropertyVariables>
                    <environmentVariables>
                        <CAF_ELASTIC_PROTOCOL>http</CAF_ELASTIC_PROTOCOL>
                        <CAF_ELASTIC_HOST_VALUES>${docker.host.address}</CAF_ELASTIC_HOST_VALUES>
                        <CAF_ELASTIC_PORT_VALUE>${elasticsearch.http.port}</CAF_ELASTIC_PORT_VALUE>
                        <webserviceurl>http://${docker.host.address}:${webservice.adminport}/${webserviceWarName}/v1</webserviceurl>
                        <webserviceurlhttps>https://${docker.host.address}:${webservice.https.adminport}/${webserviceWarName}/v1</webserviceurlhttps>
                    </environmentVariables>
                </configuration>
            </plugin>

            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>build-docker-container</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                            <image>audit-service,config</image>
                        </configuration>
                    </execution>
                    <execution>
                        <id>upload-docker-container</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <filter>audit-service</filter>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <verbose>true</verbose>
                    <autoPull>on</autoPull>
                    <logDate>default</logDate>
                    <watchInterval>500</watchInterval>
                    <containerNamePattern>%a-%t</containerNamePattern>
                    <images>
                        <!--Build image for test keystore-->
                        <image>
                            <alias>keystore</alias>
                            <name>${project.artifactId}-keystore:${project.version}</name>
                            <build>
                                <from>${dockerHubPublic}/cafapi/opensuse-jre11:3</from>
                                <runCmds>
                                    <runCmd>mkdir /test-keystore</runCmd>
                                    <runCmd>keytool -genkey -noprompt -alias tomcat -dname "CN=myname, OU=myorganisational.unit, O=myorganisation, L=mycity, S=myprovince, C=GB" -keystore /test-keystore/tomcat.keystore -storepass changeit -keypass changeit -keyalg RSA</runCmd>
                                    <runCmd>keytool -importkeystore -srckeystore /test-keystore/tomcat.keystore -srcstorepass changeit -destkeystore /test-keystore/tomcat.keystore -deststoretype pkcs12</runCmd>
                                </runCmds>
                                <volumes>
                                    <volume>/test-keystore</volume>
                                </volumes>
                            </build>
                        </image>
                        <image>
                            <alias>audit-service</alias>
                            <name>${dockerCafAuditOrg}audit-service${dockerProjectVersion}</name>
                            <build>
                                <from>${dockerHubPublic}/cafapi/opensuse-tomcat-jre11:2</from>
                                <env>
                                    <CAF_ELASTIC_PROTOCOL>http</CAF_ELASTIC_PROTOCOL>
                                    <CAF_AUDIT_MODE>elasticsearch</CAF_AUDIT_MODE>
                                    <CAF_ELASTIC_HOST_VALUES>elasticsearch</CAF_ELASTIC_HOST_VALUES>
                                    <CAF_ELASTIC_PORT_VALUE>9200</CAF_ELASTIC_PORT_VALUE>
                                    <CAF_ELASTIC_NUMBER_OF_SHARDS>5</CAF_ELASTIC_NUMBER_OF_SHARDS>
                                    <CAF_ELASTIC_NUMBER_OF_REPLICAS>1</CAF_ELASTIC_NUMBER_OF_REPLICAS>
                                </env>
                                <assembly>
                                    <basedir>/usr/share/tomcat</basedir>
                                    <mode>tar</mode>
                                    <inline>
                                        <dependencySets>
                                            <dependencySet>
                                                <outputDirectory>webapps/${webserviceWarName}</outputDirectory>
                                                <includes>
                                                    <include>com.github.cafaudit:caf-audit-service</include>
                                                </includes>
                                                <unpack>true</unpack>
                                                <useProjectArtifact>false</useProjectArtifact>
                                            </dependencySet>
                                            <!-- service dependencies -->
                                            <dependencySet>
                                                <outputDirectory>webapps/${webserviceWarName}/WEB-INF/lib</outputDirectory>
                                                <excludes>
                                                    <exclude>com.github.cafaudit:caf-audit-service</exclude>
                                                    <exclude>com.github.cafaudit:caf-audit-service-ui</exclude>
                                                    <exclude>com.github.cafaudit:caf-audit-service-admin</exclude>
                                                </excludes>
                                                <useProjectArtifact>false</useProjectArtifact>
                                            </dependencySet>
                                            <!-- Including the swagger ui in the container image. -->
                                            <dependencySet>
                                                <outputDirectory>webapps/caf-audit-service-ui</outputDirectory>
                                                <includes>
                                                    <include>com.github.cafaudit:caf-audit-service-ui</include>
                                                </includes>
                                                <unpack>true</unpack>
                                                <useProjectArtifact>false</useProjectArtifact>
                                            </dependencySet>
                                            <!-- Including the healthcheck web application. -->
                                            <dependencySet>
                                                <outputDirectory>adminapps/ROOT/</outputDirectory>
                                                <includes>
                                                    <include>com.github.cafaudit:caf-audit-service-admin</include>
                                                </includes>
                                                <unpack>true</unpack>
                                                <useProjectArtifact>false</useProjectArtifact>
                                            </dependencySet>
                                            <!-- healthcheck dependencies -->
                                            <dependencySet>
                                                <outputDirectory>adminapps/ROOT/WEB-INF/lib</outputDirectory>
                                                <includes>
                                                    <include>com.github.cafaudit:caf-audit-binding-elasticsearch</include>
                                                    <include>org.opensearch.client:opensearch-java</include>
                                                </includes>
                                                <useTransitiveDependencies>true</useTransitiveDependencies>
                                                <useTransitiveFiltering>true</useTransitiveFiltering>
                                                <useProjectArtifact>false</useProjectArtifact>
                                            </dependencySet>
                                        </dependencySets>
                                    </inline>
                                </assembly>
                            </build>
                            <run>
                                <ports>
                                    <port>${webservice.adminport}:8080</port>
                                    <port>${webservice.https.adminport}:8443</port>
                                    <port>${webservice.healthcheckport}:8081</port>
                                </ports>
                                <env>
                                    <CAF_ELASTIC_PROTOCOL>http</CAF_ELASTIC_PROTOCOL>
                                    <CAF_ELASTIC_HOST_VALUES>elasticsearch</CAF_ELASTIC_HOST_VALUES>
                                    <CAF_ELASTIC_PORT_VALUE>9200</CAF_ELASTIC_PORT_VALUE>
                                    <CAF_LOG_LEVEL>INFO</CAF_LOG_LEVEL>
                                    <DOCKER_HOST>http://172.17.0.1:2375</DOCKER_HOST>
                                    <SSL_TOMCAT_CA_CERT_LOCATION>/test-keystore/tomcat.keystore</SSL_TOMCAT_CA_CERT_LOCATION>
                                </env>
                                <volumes>
                                    <from>
                                        <image>keystore</image>
                                    </from>
                                </volumes>
                                <links>
                                    <link>elasticsearch</link>
                                </links>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                                <wait>
                                    <http>
                                        <url>
                                            http://${docker.host.address}:${webservice.healthcheckport}/healthcheck
                                        </url>
                                    </http>
                                    <time>120000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                            </run>
                        </image>

                        <!-- elasticsearch image for running integration tests-->
                        <image>
                            <alias>elasticsearch</alias>
                            <name>docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2</name>
                            <run>
                                <hostname>elasticsearch</hostname>
                                <namingStrategy>alias</namingStrategy>
                                <ports>
                                    <port>${elasticsearch.http.port}:9200</port>
                                </ports>
                                <env>
                                    <discovery.type>single-node</discovery.type>
                                </env>
                                <wait>
                                    <log>Elasticsearch startup complete</log>
                                    <http>
                                        <url>http://${docker.host.address}:${elasticsearch.http.port}</url>
                                    </http>
                                    <time>300000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                            </run>
                        </image>

                    </images>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
