<?xml version="1.0" encoding="UTF-8"?>
<!--
   Copyright The Narayana Authors
   SPDX short identifier: Apache-2.0
 -->
<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.jboss.narayana.lra</groupId>
    <artifactId>lra-parent</artifactId>
    <version>1.0.0.Final</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <artifactId>lra-test</artifactId>
  <packaging>pom</packaging>
  <name>LRA tests</name>
  <description>LRA tests running LRA TCKs and Narayana LRA internal testsuite</description>

  <modules>
    <module>arquillian-extension</module>
    <module>tck</module>
    <module>basic</module>
  </modules>

  <properties>

    <lra.coordinator.debug.params></lra.coordinator.debug.params>
    <!-- defined within -Ddebug.coordinator/-Pdebug.lra.coordinator -->
    <lra.coordinator.debug.port>8788</lra.coordinator.debug.port>
    <lra.coordinator.exec.plugin.phase>none</lra.coordinator.exec.plugin.phase>
    <!-- not starting coordinator by default, need -Parq -->

    <!-- currently only defines where to search for the coordinator, it does not change where the coordinator is started -->
    <lra.coordinator.host>localhost</lra.coordinator.host>
    <lra.coordinator.port>8080</lra.coordinator.port>
    <!-- the property used by NarayanaLRAClient when passed into the container as the JVM parameter -->
    <lra.coordinator.url>http://${lra.coordinator.host}:${lra.coordinator.port}/lra-coordinator</lra.coordinator.url>
    <!-- for test executions where coordinator is started as a separate JVM (e.g. for Quarkus) and the test application
             is deployed at the different JVM; these properties define where the test JVM application is started -->
    <test.application.host>localhost</test.application.host>
    <test.application.port>8180</test.application.port>
    <version.resteasy-client>${version.org.jboss.resteasy}</version.resteasy-client>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.jboss.resteasy</groupId>
      <artifactId>resteasy-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.jboss.narayana.lra</groupId>
      <artifactId>lra-client</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jboss.narayana.lra</groupId>
      <artifactId>narayana-lra</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <configuration>
          <redirectTestOutputToFile>${test.logs.to.file}</redirectTestOutputToFile>
          <systemPropertyVariables combine="merge">
            <jvm.args.debug>${jvm.args.debug}</jvm.args.debug>
            <lra.coordinator.debug.params>${lra.coordinator.debug.params}</lra.coordinator.debug.params>
            <!-- failsafe test startup logging configuration-->
            <java.util.logging.config.file>src/test/resources/logging.properties</java.util.logging.config.file>
            <!-- host and port where to find the test application -->
            <test.application.host>${test.application.host}</test.application.host>
            <test.application.port>${test.application.port}</test.application.port>
            <lra.tck.suite.host>${test.application.host}</lra.tck.suite.host>
            <!-- not necessary for test/basic but for TCK it's necessary -->
            <lra.tck.suite.port>${test.application.port}</lra.tck.suite.port>
            <lra.coordinator.url>${lra.coordinator.url}</lra.coordinator.url>
            <!-- passing to Arquillian as configured at arquillian*.xml and used for app server start-up (e.g. sets up debug) -->
            <server.jvm.args>${server.jvm.args}</server.jvm.args>
            <!-- used at AppServerCoordinatorDeploymentObserver -->
            <project.version>${project.version}</project.version>
            <!-- used in arquillian*.xml descriptor to place test run related data -->
            <project.build.directory>${project.build.directory}</project.build.directory>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>arq</id>
      <modules>
        <module>crash</module>
      </modules>
      <properties>
        <config.to.replace>standalone.xml</config.to.replace>
        <!-- Qualifier of the container hosting lra-coordinator -->
        <lra.coordinator.container.qualifier>lra-coordinator-server</lra.coordinator.container.qualifier>
        <!-- Qualifier of the lra-coordinator of the deployment -->
        <lra.coordinator.deployment.qualifier>lra-coordinator</lra.coordinator.deployment.qualifier>
        <!-- The path where the Object Store of the coordinator should be saved -->
        <lra.coordinator.object.store.path>${project.build.directory}/wfly_lra_objectstore</lra.coordinator.object.store.path>
        <!-- the AppServerCoordinatorDeploymentObserver creates the deployment named 'lra-coordinator' and deploys it to the app server,
             the last part of the path with value 'lra-coordinator' is defined by LRA coordinator JAX-RS endpoint -->
        <lra.coordinator.url>http://${lra.coordinator.host}:${lra.coordinator.port}/lra-coordinator/lra-coordinator</lra.coordinator.url>
        <lra.coordinator.war.name>lra-coordinator.war</lra.coordinator.war.name>
        <!-- The following filename is used for starting the WildFly instance that hosts the LRA coordinator; this file is a copy of the
             standalone.xml file provided in WildFly (${env.JBOSS_HOME}/standalone/configuration/standalone.xml). Please, see the plugins
             section of this profile (specifically, maven-antrun-plugin) for further info -->
        <lra.coordinator.xml.filename>lra-coordinator.xml</lra.coordinator.xml.filename>
        <!-- Qualifier of the container hosting lra-participant -->
        <lra.participant.container.qualifier>lra-participant-server</lra.participant.container.qualifier>
        <!-- Qualifier of the lra-participant of the deployment -->
        <lra.participant.deployment.qualifier>lra-participant</lra.participant.deployment.qualifier>
        <!-- The following filename is used for starting the WildFly instance that hosts the LRA participant; this file is a copy of the
             standalone.xml file provided in WildFly (${env.JBOSS_HOME}/standalone/configuration/standalone.xml). Please, see the plugins
             section of this profile (specifically, maven-antrun-plugin) for further info -->
        <lra.participant.xml.filename>lra-participant.xml</lra.participant.xml.filename>
        <server.jvm.object.store.arg>-DObjectStoreEnvironmentBean.objectStoreDir=${lra.coordinator.object.store.path}</server.jvm.object.store.arg>
        <skip.wildfly.plugin>true</skip.wildfly.plugin>
        <!-- where the WFLY test application will be started at (the coordinator is war deployed for the same app server at the same port) -->
        <test.application.port>8080</test.application.port>
        <!-- this property is only used in direct submodules, if used outside this scope it needs to be redefined -->
        <wildfly.trace.logging.cli.script>${project.basedir}/../wildfly-trace-logging.cli</wildfly.trace.logging.cli.script>
      </properties>

      <dependencies>
        <dependency>
          <groupId>org.wildfly.arquillian</groupId>
          <artifactId>wildfly-arquillian-container-managed</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.jboss.arquillian.protocol</groupId>
          <artifactId>arquillian-protocol-servlet-jakarta</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>

      <build>
        <plugins>

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <executions>
              <execution>
                <id>enforce-env-variable-jboss-home</id>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <phase>initialize</phase>
                <configuration>
                  <rules>
                    <requireProperty>
                      <property>env.JBOSS_HOME</property>
                      <message>You need to define JBOSS_HOME environmental variable!</message>
                      <regex>[^ ]*</regex>
                      <regexMessage>You must set JBOSS_HOME environmental variable containing at least one non-space character.</regexMessage>
                    </requireProperty>
                  </rules>
                  <fail>true</fail>
                </configuration>
              </execution>
            </executions>
          </plugin>

          <!-- Makes sure that the two Wildfly instances use different standalone XML configurations -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>run</goal>
                </goals>
                <phase>generate-test-resources</phase>
                <configuration>
                  <target>
                    <zip basedir="../../" destfile="../../ObjectStore-${project.artifactId}.zip" includes="ObjectStore" whenempty="skip"></zip>
                    <delete dir="../../ObjectStore" failonerror="false"></delete>
                    <!-- We overwrite the config files as the config.to.replace can be different in different modules and without overwrite being the case, then the file will not be written over -->
                    <copy file="${env.JBOSS_HOME}/standalone/configuration/${config.to.replace}" overwrite="true" tofile="${env.JBOSS_HOME}/standalone/configuration/${lra.coordinator.xml.filename}"></copy>
                    <copy file="${env.JBOSS_HOME}/standalone/configuration/${config.to.replace}" overwrite="true" tofile="${env.JBOSS_HOME}/standalone/configuration/${lra.participant.xml.filename}"></copy>
                    <replace file="${env.JBOSS_HOME}/standalone/configuration/${lra.coordinator.xml.filename}" token="org.wildfly.extension.microprofile.jwt-smallrye" value="org.wildfly.extension.microprofile.jwt-smallrye&quot;/&gt;&#xA;&lt;extension module=&quot;org.wildfly.extension.microprofile.lra-coordinator&quot;/&gt;&#xA;&lt;extension module=&quot;org.wildfly.extension.microprofile.openapi-smallrye"></replace>
                    <replace file="${env.JBOSS_HOME}/standalone/configuration/${lra.coordinator.xml.filename}" token="subsystem xmlns=&quot;urn:wildfly:microprofile-jwt-smallrye:1.0" value="subsystem xmlns=&quot;urn:wildfly:microprofile-jwt-smallrye:1.0&quot;/&gt;&#xA;&lt;subsystem xmlns=&quot;urn:wildfly:microprofile-lra-coordinator:1.0&quot;/&gt;&#xA;&lt;subsystem xmlns=&quot;urn:wildfly:microprofile-openapi-smallrye:1.0"></replace>
                    <replace file="${env.JBOSS_HOME}/standalone/configuration/${lra.coordinator.xml.filename}" token="object-store path=&quot;tx-object-store" value="object-store path=&quot;wfly_lra_objectstore"></replace>
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>

          <!--
            Wildfly is activated and configured through wildfly-maven-plugin instead
            of using an Arquillian cluster. This design choice reflects the preference
            to be consistent with the mechanism used to deploy lra-coordinator in
            Quarkus. Moreover, the Microprofile LRA TCK does not allow to use multiple
            Arquillian containers. As a consequence, lra-coordinator needs to be deployed
            separately.
          -->
          <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>${version.wildfly-maven-plugin}</version>
            <configuration>
              <skip>${skip.wildfly.plugin}</skip>
            </configuration>

            <executions>
              <!-- Starts Wildfly -->
              <execution>
                <id>start</id>
                <goals>
                  <goal>start</goal>
                </goals>
                <phase>pre-integration-test</phase>
                <configuration>
                  <!-- used by start -->
                  <jbossHome>${env.JBOSS_HOME}</jbossHome>
                  <serverConfig>${lra.coordinator.xml.filename}</serverConfig>
                  <javaOpts>${server.jvm.args} ${server.jvm.object.store.arg} ${lra.coordinator.debug.params}</javaOpts>
                  <force>true</force>
                </configuration>
              </execution>

              <!-- Undeploys and Shutdowns Wildfly -->
              <execution>
                <id>undeploy</id>
                <goals>
                  <goal>shutdown</goal>
                </goals>
                <phase>post-integration-test</phase>
              </execution>
            </executions>
          </plugin>

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
              <argLine>${server.jvm.args}</argLine>
              <skipITs>false</skipITs>
              <reportsDirectory>${project.build.directory}/failsafe-reports-arq</reportsDirectory>
              <summaryFile>${project.build.directory}/failsafe-reports-arq/failsafe-summary.xml</summaryFile>
              <systemProperties>
                <arquillian.xml>arquillian-wildfly.xml</arquillian.xml>
                <!-- qualifier used to identify the Wildfly cluster -->
                <arquillian.group.qualifier>wildfly-cluster</arquillian.group.qualifier>
                <!-- qualifier used to identify the Wildfly container where lra-coordinator will be deployed-->
                <arquillian.lra.coordinator.container.qualifier>${lra.coordinator.container.qualifier}</arquillian.lra.coordinator.container.qualifier>
                <!-- qualifier used to identify the Wildfly container where lra-participant will be deployed-->
                <arquillian.lra.participant.container.qualifier>${lra.participant.container.qualifier}</arquillian.lra.participant.container.qualifier>
                <!-- qualifier used to identify the lra-coordinator deployment-->
                <arquillian.lra.coordinator.deployment.qualifier>${lra.coordinator.deployment.qualifier}</arquillian.lra.coordinator.deployment.qualifier>
                <!-- qualifier used to identify the lra-participant deployment-->
                <arquillian.lra.participant.deployment.qualifier>${lra.participant.deployment.qualifier}</arquillian.lra.participant.deployment.qualifier>
                <!-- sharing with Arquillian the names of the XML standalone configurations for Wildfly instances -->
                <lra.coordinator.xml.filename>${lra.coordinator.xml.filename}</lra.coordinator.xml.filename>
                <lra.participant.xml.filename>${lra.participant.xml.filename}</lra.participant.xml.filename>
                <!-- used to resolve the resteasy-client version -->
                <version.resteasy-client>${version.resteasy-client}</version.resteasy-client>
                <!-- used to resolve eclipse microprofile versions -->
                <version.microprofile.lra>${version.microprofile.lra}</version.microprofile.lra>
              </systemProperties>
            </configuration>
          </plugin>

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

    <profile>
      <id>debug.lra.coordinator</id>
      <activation>
        <property>
          <name>debug.coordinator</name>
        </property>
      </activation>
      <properties>
        <lra.coordinator.debug.params>-Xrunjdwp:transport=dt_socket,address=${lra.coordinator.debug.port},server=y,suspend=y</lra.coordinator.debug.params>
      </properties>
    </profile>

    <profile>
      <id>trace.lra</id>
      <activation>
        <property>
          <name>trace.lra</name>
        </property>
      </activation>
      <properties>
        <lra.log.file.path>${project.build.directory}/failsafe-reports/lra-coordinator-trace.log</lra.log.file.path>
      </properties>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>exec</goal>
                </goals>
                <phase>pre-integration-test</phase>
                <configuration>
                  <skip>${skip.wildfly.plugin}</skip>
                  <basedir>${env.JBOSS_HOME}/bin/</basedir>
                  <executable>./jboss-cli.sh</executable>
                  <arguments>
                    <argument>--file=${wildfly.trace.logging.cli.script}</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>codeCoverage</id>
      <properties>
        <server.jvm.args>${jvm.args.other} ${jvm.args.memory} ${jvm.args.jacoco} ${jvm.args.modular}</server.jvm.args>
      </properties>
    </profile>
    <profile>
      <id>release</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.sonatype.plugins</groupId>
            <artifactId>nexus-staging-maven-plugin</artifactId>
            <inherited>false</inherited>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <inherited>false</inherited>
            <executions>
              <execution>
                <id>default-install</id>
                <phase>install</phase>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
