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

    Copyright 2016 Red Hat, Inc. and/or its affiliates
    and other contributors as indicated by the @author tags.

    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>org.hawkular.services</groupId>
    <artifactId>hawkular-services</artifactId>
    <version>0.0.2.Final</version>
  </parent>

  <artifactId>hawkular-services-itest</artifactId>

  <name>Hawkular Services Integration Tests</name>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.jboss.arquillian</groupId>
        <artifactId>arquillian-bom</artifactId>
        <version>${version.org.jboss.arquillian}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>

      <dependency>
        <groupId>org.wildfly.arquillian</groupId>
        <artifactId>wildfly-arquillian-container-managed</artifactId>
        <version>${version.org.wildfly.arquillian}</version>
      </dependency>

    </dependencies>
  </dependencyManagement>

  <dependencies>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-jaxb-annotations</artifactId>
      <version>${version.com.fasterxml.jackson.core}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.squareup.okhttp</groupId>
      <artifactId>okhttp</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hawkular.alerts</groupId>
      <artifactId>hawkular-alerts-api</artifactId>
      <version>${version.org.hawkular.alerts}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.squareup.okhttp</groupId>
      <artifactId>okhttp-ws</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hawkular.commons</groupId>
      <artifactId>hawkular-bus-common</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hawkular.commons</groupId>
      <artifactId>hawkular-command-gateway-api</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hawkular.commons</groupId>
      <artifactId>hawkular-command-gateway-itest</artifactId>
      <scope>test</scope>
      <type>test-jar</type>
    </dependency>

    <dependency>
      <groupId>org.hawkular.inventory</groupId>
      <artifactId>hawkular-inventory-api</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hawkular.inventory</groupId>
      <artifactId>hawkular-inventory-json-helper</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hawkular.metrics</groupId>
      <artifactId>hawkular-metrics-model</artifactId>
      <version>${version.org.hawkular.metrics}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hawkular.services</groupId>
      <artifactId>hawkular-services-dist</artifactId>
      <type>zip</type>
      <exclusions>
        <exclusion> <!-- or otherwise logback is not picked by jboss-logging -->
          <groupId>*</groupId>
          <artifactId>*</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.jboss.arquillian.testng</groupId>
      <artifactId>arquillian-testng-container</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.jboss.logging</groupId>
      <artifactId>jboss-logging</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId> <!-- the backend for jboss-logging -->
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.wildfly.arquillian</groupId>
      <artifactId>wildfly-arquillian-container-managed</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion> <!-- or otherwise logback is not picked by jboss-logging -->
          <groupId>org.jboss.logmanager</groupId>
          <artifactId>jboss-logmanager</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <!-- because integration tests are run by maven-failsafe-plugin -->
          <skip>true</skip>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <!-- Integration tests -->
      <id>itest</id>
      <properties>
        <surefire.testng.verbose>3</surefire.testng.verbose>
        <trimStackTrace>false</trimStackTrace><!-- maven-failsafe-plugin -->
        <debug.failsafe.argLine />
        <debug.wildfly.argLine />
        <hawkular.services.itest.includes>**/*ITest.class</hawkular.services.itest.includes>

        <hawkular.services.dist.finalName>hawkular-services-dist-${project.version}</hawkular.services.dist.finalName>


        <hawkular.agent.itest.mgmt.user>itest-admin</hawkular.agent.itest.mgmt.user>
        <!-- <hawkular.agent.itest.mgmt.password> set to a random value using gmaven plugin </hawkular.agent.itest.mgmt.password> -->
        <hawkular.itest.rest.tenantId>itest-rest-tenant</hawkular.itest.rest.tenantId>
        <hawkular.itest.rest.user>itest-rest</hawkular.itest.rest.user>
        <!-- <hawkular.itest.rest.password> set to a random value using gmaven plugin </hawkular.itest.rest.password> -->
        <hawkular.itest.rest.feedId>itest-feed</hawkular.itest.rest.feedId>
        <hawkular.itest.rest.user>itest-rest</hawkular.itest.rest.user>
        <!-- <hawkular.itest.rest.password> set to a random value using gmaven plugin </hawkular.itest.rest.password> -->

        <hawkular.bind.address>127.0.0.1</hawkular.bind.address>
        <hawkular.port.offset>0</hawkular.port.offset>
        <!-- $hawkular.management.port must be equal to $hawkular.port.offset + 9990 -->
        <hawkular.management.port>9990</hawkular.management.port>

        <hawkular.agent.enabled>true</hawkular.agent.enabled>

        <hawkular.log.root>INFO</hawkular.log.root>
        <hawkular.log.console>TRACE</hawkular.log.console>
        <hawkular.log.agent>INFO</hawkular.log.agent>
        <hawkular.log.alerts>INFO</hawkular.log.alerts>
        <hawkular.log.bus>INFO</hawkular.log.bus>
        <hawkular.log.cmdgw>INFO</hawkular.log.cmdgw>
        <hawkular.log.inventory>INFO</hawkular.log.inventory>
        <hawkular.log.inventory.rest.requests>INFO</hawkular.log.inventory.rest.requests>
        <hawkular.log.metrics>INFO</hawkular.log.metrics>
        <hawkular.log.nest>INFO</hawkular.log.nest>
        <hawkular.log.datastax.driver>INFO</hawkular.log.datastax.driver>
        <hawkular.log.cassandra>INFO</hawkular.log.cassandra>

        <hawkular.log.rest.test>TRACE</hawkular.log.rest.test><!-- log level for all itests -->
        <hawkular.log.adhoc.name>dummy</hawkular.log.adhoc.name> <!-- put a logger name here that you want to debug, such as org.hawkular.services.rest.test.InventoryITest -->
        <hawkular.log.adhoc.level>TRACE</hawkular.log.adhoc.level>
      </properties>
      <build>
        <plugins>

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <id>unpack-deps</id>
                <phase>compile</phase>
                <goals>
                  <goal>unpack</goal>
                </goals>
                <configuration>
                  <artifactItems>

                    <artifactItem>
                      <groupId>org.hawkular.services</groupId>
                      <artifactId>hawkular-services-dist</artifactId>
                      <type>zip</type>
                      <outputDirectory>${project.build.directory}</outputDirectory>
                    </artifactItem>

                  </artifactItems>
                </configuration>
              </execution>
            </executions>
          </plugin>

          <!-- Generate random passwords -->
          <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <executions>
              <execution>
                <phase>compile</phase>
                <goals>
                  <goal>execute</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <scripts>
                <script><![CDATA[
                  project.properties['hawkular.agent.itest.mgmt.password'] = UUID.randomUUID().toString()
                  project.properties['hawkular.itest.rest.password'] = UUID.randomUUID().toString()
                ]]></script>
              </scripts>
            </configuration>
            <dependencies>
              <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <version>${version.org.codehaus.groovy}</version>
                <scope>runtime</scope>
              </dependency>
            </dependencies>
          </plugin>

          <!-- Add various users to the itest distro -->
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>

              <execution>
                <id>create-management-itest-user</id>
                <phase>process-classes</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <executable>java</executable>
                  <environmentVariables>
                    <JBOSS_HOME>${project.build.directory}/${hawkular.services.dist.finalName}</JBOSS_HOME>
                  </environmentVariables>
                  <arguments>
                    <argument>-jar</argument>
                    <argument>${project.build.directory}/${hawkular.services.dist.finalName}/jboss-modules.jar</argument>
                    <argument>-mp</argument>
                    <argument>${project.build.directory}/${hawkular.services.dist.finalName}/modules</argument>
                    <argument>org.jboss.as.domain-add-user</argument>
                    <argument>--user</argument>
                    <argument>${hawkular.agent.itest.mgmt.user}</argument>
                    <argument>--password</argument>
                    <argument>${hawkular.agent.itest.mgmt.password}</argument>
                  </arguments>
                </configuration>
              </execution>
              <execution>
                <id>create-rest-itest-user</id>
                <phase>process-classes</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <executable>java</executable>
                  <environmentVariables>
                    <JBOSS_HOME>${project.build.directory}/${hawkular.services.dist.finalName}</JBOSS_HOME>
                  </environmentVariables>
                  <arguments>
                    <argument>-jar</argument>
                    <argument>${project.build.directory}/${hawkular.services.dist.finalName}/jboss-modules.jar</argument>
                    <argument>-mp</argument>
                    <argument>${project.build.directory}/${hawkular.services.dist.finalName}/modules</argument>
                    <argument>org.jboss.as.domain-add-user</argument>
                    <argument>-a</argument>
                    <argument>--user</argument>
                    <argument>${hawkular.itest.rest.user}</argument>
                    <argument>--password</argument>
                    <argument>${hawkular.itest.rest.password}</argument>
                    <argument>--group</argument>
                    <argument>read-write,read-only</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>xml-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>transform-xmls</id>
                <phase>process-classes</phase> <!-- after compile where we privisioned the distro -->
                <goals>
                  <goal>transform</goal>
                </goals>
                <configuration>
                  <transformationSets>

                    <transformationSet>
                      <dir>${project.build.directory}/${hawkular.services.dist.finalName}/standalone/configuration</dir>
                      <stylesheet>${basedir}/src/test/xsl/standalone.xsl</stylesheet>
                      <includes>
                        <include>standalone.xml</include>
                      </includes>
                      <outputDir>${project.build.directory}/${hawkular.services.dist.finalName}/standalone/configuration</outputDir>
                      <fileMappers>
                        <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.RegExpFileMapper">
                          <pattern>^.*$</pattern>
                          <replacement>standalone-with-reduced-agent.xml</replacement>
                        </fileMapper>
                      </fileMappers>
                    </transformationSet>

                  </transformationSets>
                </configuration>
              </execution>
            </executions>
          </plugin>

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
              <dependenciesToScan>
                <dependency>org.hawkular.commons:hawkular-command-gateway-itest</dependency>
              </dependenciesToScan>
              <includes>
                <include>${hawkular.services.itest.includes}</include>
              </includes>
              <systemPropertyVariables>
                <jboss.home>${project.build.directory}/${hawkular.services.dist.finalName}</jboss.home>
                <jboss.options>-Xmx1024m -XX:MaxPermSize=1024m
                    -Djava.net.preferIPv4Stack=true
                    -Djboss.bind.address=${hawkular.bind.address}
                    -Djboss.socket.binding.port-offset=${hawkular.port.offset}
                    -Dhawkular.agent.enabled=${hawkular.agent.enabled}
                    -Dhawkular.rest.tenantId=${hawkular.itest.rest.tenantId}
                    -Dhawkular.rest.feedId=${hawkular.itest.rest.feedId}
                    -Dhawkular.rest.user=${hawkular.itest.rest.user}
                    -Dhawkular.rest.password=${hawkular.itest.rest.password}
                    -Dhawkular.log.root=${hawkular.log.root}
                    -Dhawkular.log.console=${hawkular.log.console}
                    -Dhawkular.log.agent=${hawkular.log.agent}
                    -Dhawkular.log.alerts=${hawkular.log.alerts}
                    -Dhawkular.log.bus=${hawkular.log.bus}
                    -Dhawkular.log.cmdgw=${hawkular.log.cmdgw}
                    -Dhawkular.log.inventory=${hawkular.log.inventory}
                    -Dorg.hawkular.inventory.rest.requests=${org.hawkular.inventory.rest.requests}
                    -Dhawkular.log.metrics=${hawkular.log.metrics}
                    -Dhawkular.log.nest=${hawkular.log.nest}
                    -Dhawkular.log.datastax.driver=${hawkular.log.datastax.driver}
                    -Dhawkular.log.cassandra=${hawkular.log.cassandra}
                    ${debug.wildfly.argLine}
                </jboss.options>
                <jboss.server.config.file.name>standalone-with-reduced-agent.xml</jboss.server.config.file.name>

                <hawkular.agent.itest.mgmt.user>${hawkular.agent.itest.mgmt.user}</hawkular.agent.itest.mgmt.user>
                <hawkular.agent.itest.mgmt.password>${hawkular.agent.itest.mgmt.password}</hawkular.agent.itest.mgmt.password>

                <hawkular.itest.rest.tenantId>${hawkular.itest.rest.tenantId}</hawkular.itest.rest.tenantId>
                <hawkular.itest.rest.feedId>${hawkular.itest.rest.feedId}</hawkular.itest.rest.feedId>
                <hawkular.itest.rest.user>${hawkular.itest.rest.user}</hawkular.itest.rest.user>
                <hawkular.itest.rest.password>${hawkular.itest.rest.password}</hawkular.itest.rest.password>

                <hawkular.bind.address>${hawkular.bind.address}</hawkular.bind.address>
                <hawkular.port.offset>${hawkular.port.offset}</hawkular.port.offset>

                <hawkular.log.rest.test>${hawkular.log.rest.test}</hawkular.log.rest.test>
                <hawkular.log.adhoc.name>${hawkular.log.adhoc.name}</hawkular.log.adhoc.name>
                <hawkular.log.adhoc.level>${hawkular.log.adhoc.level}</hawkular.log.adhoc.level>

              </systemPropertyVariables>
              <properties>
                <property>
                  <name>surefire.testng.verbose</name>
                  <value>${surefire.testng.verbose}</value>
                </property>
              </properties>
              <argLine>${debug.failsafe.argLine}</argLine>
            </configuration>
            <executions>
              <execution>
                <goals>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>

        </plugins>
      </build>
    </profile>


    <profile>
      <id>itest.debug</id>
      <activation>
        <property>
          <name>itest.debug</name>
        </property>
      </activation>
      <properties>
        <debug.failsafe.argLine>-Xdebug -Xrunjdwp:transport=dt_socket,address=5007,server=y,suspend=y</debug.failsafe.argLine>
        <!-- this can only be a single argument due to the way <javaOpt> works with wildfly maven plugin -->
        <debug.wildfly.argLine>-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n</debug.wildfly.argLine>
      </properties>
    </profile>
  </profiles>

</project>
