<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to you 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.apache.logging.log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>${revision}</version>
    <relativePath>../log4j-parent</relativePath>
  </parent>

  <artifactId>log4j-layout-template-json-test</artifactId>

  <name>Apache Log4j JSON Template Layout tests</name>

  <properties>
    <bnd.baseline.skip>true</bnd.baseline.skip>
    <log4j.docgen.skip>true</log4j.docgen.skip>
    <maven.deploy.skip>true</maven.deploy.skip>
    <sign.skip>true</sign.skip>

    <!--
      ~ OSGi and JPMS options
      -->
    <bnd-module-name>org.apache.logging.log4j.layout.template.json.test</bnd-module-name>
    <Fragment-Host>org.apache.logging.log4j.core</Fragment-Host>

    <!-- `elastic.version` is used for two purposes:

         1. `co.elastic.clients:elasticsearch-java` version
         2. The Docker image version of the ELK-stack

         As of 2024-09-16, these all (Maven artifacts and Elastic products) get released with the same version. -->
    <elastic.version>9.2.0</elastic.version>

  </properties>

  <dependencies>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-layout-template-json</artifactId>
    </dependency>

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

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-1.2-api</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core-test</artifactId>
      <scope>test</scope>
    </dependency>

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

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

    <dependency>
      <groupId>co.elastic.clients</groupId>
      <artifactId>elasticsearch-java</artifactId>
      <version>${elastic.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.elasticsearch.client</groupId>
      <artifactId>elasticsearch-rest-client</artifactId>
      <version>${elastic.version}</version>
      <scope>test</scope>
    </dependency>

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

    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-yaml</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.google.code.java-allocation-instrumenter</groupId>
      <artifactId>java-allocation-instrumenter</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>co.elastic.logging</groupId>
      <artifactId>log4j2-ecs-layout</artifactId>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <!-- Enforcing a non-UTF-8 encoding to check that the layout
               indeed handles everything in UTF-8 without implicitly
               relying on the system defaults. -->
          <argLine>-Dfile.encoding=US-ASCII</argLine>
          <systemPropertyVariables>
            <!-- Enable JUnit 5 parallel execution. -->
            <junit.jupiter.execution.parallel.enabled>true</junit.jupiter.execution.parallel.enabled>
            <junit.jupiter.execution.parallel.mode.default>concurrent</junit.jupiter.execution.parallel.mode.default>
          </systemPropertyVariables>
        </configuration>
      </plugin>

    </plugins>
  </build>

  <profiles>
    <profile>

      <id>docker</id>

      <!--
        ~ Only the `ubuntu` CI runners have access to Docker
        -->
      <activation>
        <os>
          <family>linux</family>
        </os>
        <property>
          <name>env.CI</name>
          <value>true</value>
        </property>
      </activation>

      <properties>

        <!-- Suppress `docker-maven-plugin` output -->
        <docker.showLogs>false</docker.showLogs>
        <docker.verbose>false</docker.verbose>

        <!-- `docker run elasticsearch:<TAG>` exists with code 137 due to insufficient memory.
             We limit the used JVM memory to avoid this OOM failure. -->
        <elastic.javaOpts>-Xms750m -Xmx750m</elastic.javaOpts>

      </properties>

      <build>
        <plugins>

          <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <configuration>
              <startParallel>true</startParallel>
              <autoCreateCustomNetworks>true</autoCreateCustomNetworks>
              <images>
                <image>
                  <alias>elasticsearch</alias>
                  <name>elasticsearch:${elastic.version}</name>
                  <run>
                    <env>
                      <discovery.type>single-node</discovery.type>
                      <xpack.security.enabled>false</xpack.security.enabled>
                      <ES_JAVA_OPTS>${elastic.javaOpts}</ES_JAVA_OPTS>
                    </env>
                    <ports>
                      <!-- Binds an ephemeral port to the `elasticsearch.port` Maven property. -->
                      <port>localhost:elasticsearch.port:9200</port>
                    </ports>
                    <network>
                      <mode>custom</mode>
                      <name>log4j-layout-template-json-network</name>
                      <alias>elasticsearch</alias>
                    </network>
                    <log>
                      <prefix>[ES]</prefix>
                      <color>cyan</color>
                    </log>
                    <wait>
                      <tcp>
                        <ports>
                          <port>9200</port>
                        </ports>
                      </tcp>
                      <time>60000</time>
                    </wait>
                  </run>
                </image>
                <image>
                  <alias>logstash</alias>
                  <name>logstash:${elastic.version}</name>
                  <run>
                    <dependsOn>
                      <container>elasticsearch</container>
                    </dependsOn>
                    <network>
                      <mode>custom</mode>
                      <name>log4j-layout-template-json-network</name>
                      <alias>logstash</alias>
                    </network>
                    <env>
                      <LS_JAVA_OPTS>${elastic.javaOpts}</LS_JAVA_OPTS>
                    </env>
                    <ports>
                      <!-- Binds an ephemeral port to the `logstash.gelf.port` Maven property. -->
                      <port>localhost:logstash.gelf.port:12222</port>
                      <!-- Binds an ephemeral port to the `logstash.tcp.port` Maven property. -->
                      <port>localhost:logstash.tcp.port:12345</port>
                    </ports>
                    <log>
                      <prefix>[LS]</prefix>
                      <color>green</color>
                    </log>
                    <entrypoint>
                      <exec>
                        <arg>logstash</arg>
                        <arg>--pipeline.batch.size</arg>
                        <arg>1</arg>
                        <arg>-e</arg>
                        <arg><![CDATA[

                          input {

                            # Documentation: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-gelf.html
                            gelf {
                              host => "logstash"
                              use_tcp => true
                              use_udp => false
                              port => 12222
                              type => "gelf"
                            }

                            # Documentation: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-tcp.html
                            tcp {
                              port => 12345
                              codec => json
                              type => "tcp"
                            }

                          }

                          filter {
                            if [type] == "gelf" {
                              # These are GELF/Syslog logging levels as defined in RFC 3164.
                              # Map the integer level to its human-readable format.
                              # Documentation: https://www.elastic.co/guide/en/logstash/current/plugins-filters-translate.html
                              translate {
                                source => "[level]"
                                target => "[levelName]"
                                dictionary => {
                                  "0" => "EMERG"
                                  "1" => "ALERT"
                                  "2" => "CRITICAL"
                                  "3" => "ERROR"
                                  "4" => "WARN"
                                  "5" => "NOTICE"
                                  "6" => "INFO"
                                  "7" => "DEBUG"
                                }
                              }
                            }
                          }

                          # Documentation: https://www.elastic.co/guide/en/logstash/current/plugins-filters-elasticsearch.html
                          output {
                            # (Un)comment for debugging purposes
                            # stdout { codec => rubydebug }
                            elasticsearch {
                              hosts => ["http://elasticsearch:9200"]
                              index => "log4j"
                            }
                          }

                        ]]></arg>
                      </exec>
                    </entrypoint>
                    <wait>
                      <tcp>
                        <host>localhost</host>
                        <ports>
                          <port>12222</port>
                          <port>12345</port>
                        </ports>
                      </tcp>
                      <time>60000</time>
                    </wait>
                  </run>
                </image>
              </images>
            </configuration>
            <executions>
              <execution>
                <id>start</id>
                <goals>
                  <goal>start</goal>
                </goals>
                <phase>pre-integration-test</phase>
              </execution>
              <execution>
                <id>stop</id>
                <goals>
                  <goal>stop</goal>
                </goals>
                <phase>post-integration-test</phase>
              </execution>
            </executions>
          </plugin>

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
              <includes>
                <include>**/*IT.java</include>
              </includes>
              <systemPropertyVariables>
                <log4j.elasticsearch.port>${elasticsearch.port}</log4j.elasticsearch.port>
                <log4j.logstash.gelf.port>${logstash.gelf.port}</log4j.logstash.gelf.port>
                <log4j.logstash.tcp.port>${logstash.tcp.port}</log4j.logstash.tcp.port>
              </systemPropertyVariables>
            </configuration>
            <executions>
              <execution>
                <goals>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>

        </plugins>
      </build>

    </profile>
  </profiles>

</project>
