<?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>com.atlassian.bitbucket.server</groupId>
        <artifactId>bitbucket-it-parent</artifactId>
        <version>7.6.17</version>
    </parent>

    <artifactId>bitbucket-it-installer</artifactId>

    <name>Bitbucket Server - Installer Tests</name>

    <properties>
        <install.service>false</install.service>
        <installer.bitness.classifier />
        <installer.type />
        <http.port>7990</http.port>
        <service.port>8006</service.port>
        <include.tests />
        <stash.installer.version>3.11.0</stash.installer.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.atlassian.bitbucket.server</groupId>
                <artifactId>bitbucket-installer</artifactId>
                <version>${project.parent.version}</version>
                <type>exe</type>
                <classifier>x64</classifier>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.atlassian.bitbucket.server</groupId>
                <artifactId>bitbucket-installer</artifactId>
                <version>${project.parent.version}</version>
                <type>bin</type>
                <classifier>x64</classifier>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.atlassian.stash</groupId>
                <artifactId>stash-installer</artifactId>
                <version>3.11.0</version>
                <type>exe</type>
                <classifier>x64</classifier>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- Ensure we don't run until after the installer is built by declaring a dependency on the installer -->
        <dependency>
            <groupId>com.atlassian.bitbucket.server</groupId>
            <artifactId>bitbucket-installer</artifactId>
            <type>bin</type>
            <classifier>x64</classifier>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.atlassian.bitbucket.server</groupId>
            <artifactId>bitbucket-it-common</artifactId>
            <scope>test</scope>
            <exclusions>
                <!-- see the comment above javax.ws.rs-api -->
                <exclusion>
                    <groupId>com.atlassian.plugins.rest</groupId>
                    <artifactId>atlassian-rest-common</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.atlassian.bitbucket.server</groupId>
            <artifactId>bitbucket-service-impl</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.atlassian.bitbucket.server</groupId>
            <artifactId>bitbucket-page-objects</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.github.docker-java</groupId>
            <artifactId>docker-java</artifactId>
            <version>3.0.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- This dependency is needed by docker-java because the version that atlassian-rest-common brings in is too new -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>${include.tests}</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-installers</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.atlassian.bitbucket.server</groupId>
                                    <artifactId>bitbucket-installer</artifactId>
                                    <type>bin</type>
                                    <classifier>x64</classifier>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                    <destFileName>bitbucket-installer-x64.bin</destFileName>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.atlassian.bitbucket.server</groupId>
                                    <artifactId>bitbucket-installer</artifactId>
                                    <type>exe</type>
                                    <classifier>x64</classifier>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                    <destFileName>bitbucket-installer-x64.exe</destFileName>
                                </artifactItem>
                                <!-- needed for upgrade tests -->
                                <artifactItem>
                                    <groupId>com.atlassian.stash</groupId>
                                    <artifactId>stash-installer</artifactId>
                                    <version>${stash.installer.version}</version>
                                    <type>bin</type>
                                    <classifier>x64</classifier>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                    <destFileName>stash-installer-x64.bin</destFileName>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.atlassian.stash</groupId>
                                    <artifactId>stash-installer</artifactId>
                                    <version>${stash.installer.version}</version>
                                    <type>exe</type>
                                    <classifier>x64</classifier>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                    <destFileName>stash-installer-x64.exe</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>test-upgrade-linux</id>
            <properties>
                <include.tests>**/installer/upgrade/UpgradeStashToBitbucketLinuxTest.java</include.tests>
                <stash.installer.binary>${project.build.directory}/stash-installer-x64.bin</stash.installer.binary>
                <bitbucket.installer.binary>${project.build.directory}/bitbucket-installer-x64.bin</bitbucket.installer.binary>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>set-installer-permissions</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <chmod file="${project.build.directory}/bitbucket-installer*" perm="a+x" />
                                        <chmod file="${project.build.directory}/stash-installer*" perm="a+x" />
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>test-upgrade-windows</id>
            <properties>
                <include.tests>**/installer/upgrade/UpgradeStashToBitbucketWindowsTest.java</include.tests>
                <stash.installer.binary>${project.build.directory}/stash-installer-x64.exe</stash.installer.binary>
                <bitbucket.installer.binary>${project.build.directory}/bitbucket-installer-x64.exe</bitbucket.installer.binary>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>setup-installation</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <!-- The installer itself tries to create the following user, and sometimes fails
                                             due to issues with Active Directory ("The directory service is busy").
                                             So workaround it by creating the user here first. -->
                                        <exec executable="cmd">
                                            <arg value="/c" />
                                            <arg value="net user atlbitbucket /ADD /FULLNAME:&quot;Atlassian Bitbucket&quot; /y" />
                                        </exec>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>test-host-unix</id>
            <properties>
                <include.tests>**/host/*.java</include.tests>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>setup-installation</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <chmod file="${project.build.directory}/bitbucket-installer-${installer.bitness.classifier}.${installer.type}" perm="a+x" />
                                        <exec executable="${project.build.directory}/bitbucket-installer-${installer.bitness.classifier}.${installer.type}">
                                            <arg value="-q" />
                                            <arg value="-Dinstaller.pingback.host=http://localhost:3000/pingback" />
                                            <arg value="-Dinstall4j.alternativeLogfile=${project.build.directory}/installer.log" />
                                            <arg value="-Dinstall4j.logToStderr=true" />
                                            <arg value="-Dinstall4j.debug=true" />
                                            <arg value="-varfile" />
                                            <arg value="${project.build.testOutputDirectory}/installer-varfile.properties" />
                                        </exec>
                                    </tasks>
                                </configuration>
                            </execution>
                            <execution>
                                <id>stop-bitbucket</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <exec executable="bin/stop-bitbucket.sh" dir="${project.build.directory}/stash/${project.version}" />
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>test-host-windows</id>
            <properties>
                <include.tests>**/host/*.java</include.tests>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>setup-installation</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <exec executable="${project.build.directory}\bitbucket-installer-${installer.bitness.classifier}.${installer.type}">
                                            <arg value="-q" />
                                            <arg value="-Dinstaller.pingback.host=http://localhost:3000/pingback" />
                                            <arg value="-Dinstall4j.alternativeLogfile=${project.build.directory}\installer.log" />
                                            <arg value="-Dinstall4j.logToStderr=true" />
                                            <arg value="-Dinstall4j.debug=true" />
                                            <arg value="-Dplugin.bitbucket-git.path.executable=${plugin.bitbucket-git.path.executable}" />
                                            <arg value="-varfile" />
                                            <arg value="${project.build.testOutputDirectory}\installer-varfile.properties" />
                                        </exec>
                                    </tasks>
                                </configuration>
                            </execution>
                            <execution>
                                <id>stop-bitbucket</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <!-- List the running processes first, for debug purposes.-->
                                        <exec executable="tasklist">
                                            <arg value="/v" />
                                        </exec>

                                        <!-- Kill the Elasticsearch and Bitbucket processes. -->
                                        <exec executable="cmd">
                                            <arg value="/c" />
                                            <arg value="for /f &quot;tokens=2 delims=,&quot; %a in ('tasklist /v /fo:csv /nh ^| findstr /r /c:&quot;.*Elasticsearch[^,]*$&quot;') do ( taskkill /f /t /pid %a )" />
                                        </exec>
                                        <exec executable="cmd">
                                            <arg value="/c" />
                                            <arg value="for /f &quot;tokens=2 delims=,&quot; %a in ('tasklist /v /fo:csv /nh ^| findstr /r /c:&quot;.*Bitbucket[^,]*$&quot;') do ( taskkill /f /t /pid %a )" />
                                        </exec>

                                        <!-- List the running processes again. -->
                                        <exec executable="tasklist">
                                            <arg value="/v" />
                                        </exec>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
