<?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>
    <name>io.opentdf.platform:sdk</name>
    <artifactId>sdk</artifactId>
    <parent>
        <artifactId>sdk-pom</artifactId>
        <groupId>io.opentdf.platform</groupId>
        <version>0.7.9</version>
    </parent>
    <packaging>jar</packaging>
    <properties>
        <jazzer.version>0.22.1</jazzer.version>
        <jazzer.baseurl>https://github.com/CodeIntelligenceTesting/jazzer/releases/download/v${jazzer.version}</jazzer.baseurl>
        <platform.branch>protocol/go/v0.3.0</platform.branch>
    </properties>
    <dependencies>
        <!-- Logging Dependencies -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.13</version>
        </dependency>
        <!-- Security and Authentication Dependencies -->
        <dependency>
            <groupId>com.nimbusds</groupId>
            <artifactId>oauth2-oidc-sdk</artifactId>
            <version>11.10.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.hakky54</groupId>
            <artifactId>sslcontext-kickstart-for-pem</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.hakky54</groupId>
            <artifactId>sslcontext-kickstart</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.hakky54</groupId>
            <artifactId>sslcontext-kickstart-for-netty</artifactId>
        </dependency>
        <!-- Serialization and Deserialization Dependencies -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>com.vdurmont</groupId>
            <artifactId>semver4j</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.17.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.erdtman</groupId>
            <artifactId>java-json-canonicalization</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>build.buf</groupId>
            <artifactId>protovalidate</artifactId>
            <version>0.1.9</version>
            <scope>compile</scope>
        </dependency>
        <!-- Networking Dependencies -->
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
        </dependency>
        <!-- Other Dependencies -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>annotations-api</artifactId>
            <version>6.0.53</version>
            <scope>provided</scope>
        </dependency>
        <!-- Crypto Dependencies -->
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk18on</artifactId>
            <version>1.78.1</version>
        </dependency>
        <!-- Testing Dependencies -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.8.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>mockwebserver</artifactId>
            <version>5.0.0-alpha.14</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.code-intelligence</groupId>
            <artifactId>jazzer-api</artifactId>
            <version>${jazzer.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.code-intelligence</groupId>
            <artifactId>jazzer-junit</artifactId>
            <version>${jazzer.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.26.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp-tls</artifactId>
            <version>5.0.0-alpha.14</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>
        <!-- Mockito Inline Dependency for mocking final classes -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>
        <!-- gRPC Testing Utilities -->
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-testing</artifactId>
            <version>${grpc.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Plugin to create source JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.3.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Plugin to create Javadoc JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.8.0</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Plugin to generate checksum files -->
            <plugin>
                <groupId>net.nicoulaj.maven.plugins</groupId>
                <artifactId>checksum-maven-plugin</artifactId>
                <version>1.11</version>
                <executions>
                    <execution>
                        <id>create-checksums</id>
                        <phase>package</phase>
                        <goals>
                            <goal>files</goal>
                        </goals>
                        <configuration>
                            <algorithms>
                                <algorithm>MD5</algorithm>
                                <algorithm>SHA-1</algorithm>
                                <algorithm>SHA-256</algorithm>
                                <algorithm>SHA-512</algorithm>
                            </algorithms>
                            <failOnError>true</failOnError>
                            <fileSets>
                                <fileSet>
                                    <directory>${project.build.directory}</directory>
                                    <includes>
                                        <include>*.jar</include>
                                    </includes>
                                </fileSet>
                            </fileSets>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <!-- Generate Sources Execution under target/generated-sources -->
                    <execution>
                        <id>generateSources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <target>
                                <!-- Generate OpenTDF Platform Protobuf -->
                                <exec executable="buf" dir="." failOnError="true">
                                    <arg value="generate"/>
                                    <arg value="https://github.com/opentdf/platform.git#branch=${platform.branch},subdir=service"/>
                                    <arg value="-o"/>
                                    <arg value="target/generated-sources"/>
                                </exec>
                                <!-- Generate gRPC Protobuf -->
                                <exec executable="buf" dir="." failOnError="true">
                                    <arg value="generate"/>
                                    <arg value="buf.build/grpc-ecosystem/grpc-gateway"/>
                                    <arg value="-o"/>
                                    <arg value="target/generated-sources"/>
                                </exec>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Add Generate Sources Directory -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-sources</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.12</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${project.parent.basedir}/target/jacoco.exec</destFile>
                        </configuration>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${project.parent.basedir}/target/jacoco.exec</dataFile>
                            <outputDirectory>${project.parent.basedir}/target/site/jacoco/</outputDirectory>
                            <formats>
                                <format>XML</format>
                            </formats>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <!--profile to execute fuzz test -->
    <profiles>
        <profile>
            <id>fuzz</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <skipTests>true</skipTests>
            </properties>
            <build>
                <plugins>
                    <!-- Plugin to download and unpack the Jazzer agent -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>3.1.0</version>
                        <executions>
                            <execution>
                                <id>download-and-unpack-jazzer</id>
                                <phase>process-test-classes</phase>
                                <configuration>
                                    <target>
                                        <condition property="jazzer.os" value="windows">
                                            <os family="windows"/>
                                        </condition>
                                        <condition property="jazzer.os" value="macos">
                                            <os family="mac"/>
                                        </condition>
                                        <condition property="jazzer.os" value="linux">
                                            <os family="unix"/>
                                        </condition>
                                        <echo message="Detected OS: ${jazzer.os}"/>
                                        <mkdir dir="${project.build.directory}/jazzer"/>
                                        <get src="${jazzer.baseurl}/jazzer-${jazzer.os}.tar.gz" dest="${project.build.directory}/jazzer/jazzer.tar.gz"/>
                                        <untar compression="gzip" src="${project.build.directory}/jazzer/jazzer.tar.gz" dest="${project.build.directory}/jazzer"/>
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Copy dependencies to a directory for Jazzer to reference -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>3.4.0</version>
                        <executions>
                            <execution>
                                <id>copy-dependencies</id>
                                <phase>process-test-classes</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/dependency-jars</outputDirectory>
                                    <includeScope>test</includeScope>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Plugin to execute Jazzer -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>3.1.0</version>
                        <executions>
                            <execution>
                                <id>run-jazzer-fuzzing</id>
                                <phase>verify</phase>
                                <configuration>
                                    <target>
                                        <path id="project.classpath">
                                            <pathelement location="${project.build.directory}/classes"/>
                                            <pathelement location="${project.build.directory}/test-classes"/>
                                            <fileset dir="${project.build.directory}/dependency-jars">
                                                <include name="**/*.jar"/>
                                            </fileset>
                                        </path>
                                        <pathconvert property="project.classpath.string" pathsep="${path.separator}">
                                            <path refid="project.classpath"/>
                                        </pathconvert>
                                        <property environment="env"/>

                                        <chmod file="${project.build.directory}/jazzer/jazzer" perm="777"/>

                                        <exec executable="bash">
                                            <arg value="-c"/>
                                            <arg value="if [ -z &quot;${JAVA_HOME}&quot; ]; then JAVA_HOME=$(dirname $(dirname $(which java))); fi; DYLD_LIBRARY_PATH=$(find &quot;${JAVA_HOME}&quot; -type d | grep 'libexec/openjdk.jdk/Contents/Home/lib/server' 2&gt;/dev/null | head -n 1); if [ -z &quot;${DYLD_LIBRARY_PATH}&quot; ]; then DYLD_LIBRARY_PATH=&quot;${JAVA_HOME}/lib/server&quot;; fi; export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}; ${project.build.directory}/jazzer/jazzer --cp='${project.classpath.string}' --target_class='io.opentdf.platform.sdk.Fuzzing' --instrumentation_includes='io.opentdf.platform.sdk.**' ${jazzer.testDir}"/>
                                        </exec>
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
