<?xml version="1.0" encoding="UTF-8"?>
<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>au.net.causal.shoelaces</groupId>
        <artifactId>shoelaces-jdbc-integration-tests</artifactId>
        <version>2.0</version>
    </parent>

    <artifactId>shoelaces-jdbc-integration-tests-container</artifactId>
    <name>Shoelaces JDBC module Integration Tests for containers</name>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>shoelaces-jdbc-integration-tests-app-derby</artifactId>
            <version>${project.version}</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>shoelaces-jdbc-integration-tests-common</artifactId>
            <version>${project.version}</version>
            <classifier>tests</classifier>
        </dependency>

        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>shoelaces-testing</artifactId>
            <version>${project.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-appconfig-resources</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/test-application-home</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/test/apphome</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                            <useDefaultDelimiters>true</useDefaultDelimiters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Sets a system property so Derby, under the SQL plugin, writes its log file to the proper directory -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>configure-derby-log-system-property</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>set-system-properties</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <property>
                                    <name>derby.stream.error.file</name>
                                    <value>${project.build.directory}/sqlplugin-derby.log</value>
                                </property>
                            </properties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- We create our own bird table here to prove that the webapp, when started up, is using this persisted database instead of in-memory one -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.derby</groupId>
                        <artifactId>derby</artifactId>
                        <version>${derby.version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <driver>org.apache.derby.jdbc.EmbeddedDriver</driver>
                    <url>jdbc:derby:${project.build.directory}/test-application-home/db;create=true</url>
                    <username>sa</username>
                </configuration>
                <executions>
                    <execution>
                        <id>make-db</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <sqlCommand>
                                create table bird(name varchar(256) not null primary key);
                                insert into bird (name) values ('Whiteman');
                                insert into bird (name) values ('Squinky');
                            </sqlCommand>
                        </configuration>
                    </execution>

                    <!-- Do this so the webapp can use the DB when it starts up -->
                    <execution>
                        <id>shutdown-db-after-creation</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <url>jdbc:derby:${project.build.directory}/test-application-home/db;shutdown=true</url>
                            <skipOnConnectionError>true</skipOnConnectionError>
                            <username>sa</username>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>reserve-tomcat-port</id>
                        <goals>
                            <goal>reserve-network-port</goal>
                        </goals>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <portNames>
                                <portName>tomcat.http.port</portName>
                                <portName>tomcat.ajp.port</portName>
                            </portNames>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <executions>
                    <execution>
                        <id>start-container</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-container</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <deployables>
                        <deployable>
                            <artifactId>shoelaces-jdbc-integration-tests-app-derby</artifactId>
                            <type>war</type>
                            <properties>
                                <context>ROOT</context>
                            </properties>
                        </deployable>
                    </deployables>

                    <configuration>
                        <properties>
                            <cargo.servlet.port>${tomcat.http.port}</cargo.servlet.port>
                            <cargo.tomcat.ajp.port>${tomcat.ajp.port}</cargo.tomcat.ajp.port>
                        </properties>
                    </configuration>
                    <container>
                        <containerId>tomcat9x</containerId>
                        <log>${project.build.directory}/cargo-tomcat.log</log>
                        <artifactInstaller>
                            <groupId>org.apache.tomcat</groupId>
                            <artifactId>tomcat</artifactId>
                            <version>${tomcat.version}</version>
                        </artifactInstaller>
                        <systemProperties>
                            <application.home.directory>${project.build.directory}/test-application-home</application.home.directory>
                        </systemProperties>
                    </container>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <configuration>
                    <systemPropertyVariables>
                        <test.server.port>${tomcat.http.port}</test.server.port>
                    </systemPropertyVariables>
                    <dependenciesToScan>
                        <dependency>${project.groupId}:shoelaces-jdbc-integration-tests-common</dependency>
                    </dependenciesToScan>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <executions>
                    <execution>
                        <id>verify-style</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
