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

    Copyright 2015-2017 Hewlett Packard Enterprise Development LP.

    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">
    <parent>
        <groupId>com.github.cafdataprocessing</groupId>
        <artifactId>processing-utilities-aggregator</artifactId>
        <version>1.3.0-70</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>data-processing-initialization</artifactId>

    <properties>
        <hibernateDatabaseName>corepolicy</hibernateDatabaseName>
        <hibernatePassword>root</hibernatePassword>
        <hibernateUser>postgres</hibernateUser>

        <!-- Policy API Properties -->
        <internalHibernateConnectionstring>
            jdbc:postgresql://corepolicydb-postgres:5432/&lt;dbname&gt;
        </internalHibernateConnectionstring>
        <processingServiceHost>http://${docker.host.address}:${processingServicePort}</processingServiceHost>
        <processingServiceUrl>${processingServiceHost}/data-processing-service/v1</processingServiceUrl>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.github.cafdataprocessing</groupId>
            <artifactId>classification-service-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.cafdataprocessing</groupId>
            <artifactId>classification-service-creation-util</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.cafdataprocessing</groupId>
            <artifactId>processing-service-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.cafdataprocessing</groupId>
            <artifactId>util-boilerplate-creation</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.version}</version>
                <executions>
                    <execution>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven.failsafe.version}</version>
                <configuration>
                    <!-- use debugForkedProcess to enable debugging of tests during a run -->
                    <!--<debugForkedProcess>true</debugForkedProcess>-->
                    <environmentVariables>
                        <processingServiceUrl>${processingServiceUrl}</processingServiceUrl>
                    </environmentVariables>
                </configuration>
            </plugin>
            <!-- stand up processing api for use in integration tests -->
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>${fabric8.docker.maven.version}</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>
                </executions>
                <configuration>
                    <verbose>true</verbose>
                    <autoPull>true</autoPull>
                    <useColor>true</useColor>
                    <images>
                        <image>
                            <alias>corepolicydb-postgres</alias>
                            <name>${caf.container.databases.name}</name>
                            <run>
                                <ports>
                                    <port>${postgresDbPort}:5432</port>
                                </ports>
                                <env>
                                    <CAF_WORKFLOW_DB_NAME>${hibernateDatabaseName}</CAF_WORKFLOW_DB_NAME>
                                    <POSTGRES_PASSWORD>${hibernatePassword}</POSTGRES_PASSWORD>
                                    <POSTGRES_USER>${hibernateUser}</POSTGRES_USER>
                                </env>
                                <wait>
                                    <log>PostgreSQL init process complete</log>
                                    <time>600000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                            </run>
                        </image>
                        <!-- Policy API Container -->
                        <image>
                            <alias>policy-admin</alias>
                            <name>${caf.container.policy.admin.name}</name>
                            <run>
                                <hostname>policyadmin</hostname>
                                <ports>
                                    <!-- Expose port for tomcat -->
                                    <port>${policyApiServicePort}:8080</port>
                                </ports>
                                <env>
                                    <api.direct.repository>hibernate</api.direct.repository>
                                    <api.mode>direct</api.mode>
                                    <engine.environmentcache.verifyperiod>PT0S</engine.environmentcache.verifyperiod>
                                    <hibernate.connectionstring>${internalHibernateConnectionstring}
                                    </hibernate.connectionstring>
                                    <hibernate.databasename>${hibernateDatabaseName}</hibernate.databasename>
                                    <hibernate.password>${hibernatePassword}</hibernate.password>
                                    <hibernate.user>${hibernateUser}</hibernate.user>
                                </env>
                                <wait>
                                    <http>
                                        <!-- note health check uses external port exposed on docker VM, it may be in bridged mode, or running
                                        in its own machine, in which case, this can be the same as the container.run.tomcat.service.port -->
                                        <url>
                                            http://${docker.host.address}:${policyApiServicePort}/corepolicy/healthcheck?project_id=1
                                        </url>
                                        <method>GET</method>
                                    </http>
                                    <time>240000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                                <links>
                                    <link>corepolicydb-postgres</link>
                                </links>
                                <log>
                                    <!-- enable logs from the container, they will show with policyadmin> prefix
                                    and in the colour yellow -->
                                    <color>yellow</color>
                                    <enabled>true</enabled>
                                </log>
                            </run>
                        </image>
                        <image>
                            <alias>processing-service</alias>
                            <name>${caf.container.processing.service.name}</name>
                            <run>
                                <ports>
                                    <port>${processingServicePort}:8080</port>
                                </ports>
                                <env>
                                    <CAF_PROCESSING_SERVICE_PORT>8080</CAF_PROCESSING_SERVICE_PORT>
                                    <CAF_PROCESSING_SERVICE_POLICY_API_HOST>policy-admin</CAF_PROCESSING_SERVICE_POLICY_API_HOST>
                                    <CAF_PROCESSING_SERVICE_POLICY_API_PORT>8080</CAF_PROCESSING_SERVICE_POLICY_API_PORT>
                                </env>
                                <wait>
                                    <http>
                                        <url>${processingServiceUrl}/healthcheck</url>
                                        <method>GET</method>
                                    </http>
                                    <time>40000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                                <links>
                                    <link>policy-admin</link>
                                </links>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                            </run>
                        </image>
                    </images>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>skip-start-docker-container</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>io.fabric8</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <version>${fabric8.docker.maven.version}</version>
                        <executions>
                            <execution>
                                <id>start</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>start</goal>
                                </goals>
                                <configuration>
                                    <skip>true</skip>
                                </configuration>
                            </execution>
                            <!-- Stop the containers in post-integration-test phase. -->
                            <execution>
                                <id>stop</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>stop</goal>
                                </goals>
                                <configuration>
                                    <skip>true</skip>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>use-default-fixed-ports</id>
            <properties>
                <policyApiServicePort>9000</policyApiServicePort>
                <postgresDbPort>5432</postgresDbPort>
                <processingServicePort>15000</processingServicePort>
            </properties>
        </profile>
    </profiles>
</project>