name: Common Linux test logic

on:
  workflow_call:
    inputs:
      os:
        required: true
        type: string
      jdk_distro:
        required: true
        type: string
      jdk_version:
        required: true
        type: string
      wolfssl_configure:
        required: true
        type: string
      javash_cflags:
        required: false
        type: string

jobs:
  build_wolfssljni:
    runs-on: ${{ inputs.os }}
    steps:
      - uses: actions/checkout@v4

      - name: Download junit-4.13.2.jar
        run: wget --directory-prefix=$GITHUB_WORKSPACE/junit https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar
      - name: Download hamcrest-all-1.3.jar
        run: wget --directory-prefix=$GITHUB_WORKSPACE/junit https://repo1.maven.org/maven2/org/hamcrest/hamcrest-all/1.3/hamcrest-all-1.3.jar

      - name: Build native wolfSSL
        uses: wolfSSL/actions-build-autotools-project@v1
        with:
          repository: wolfSSL/wolfssl
          ref: master
          path: wolfssl
          configure: ${{ inputs.wolfssl_configure }}
          check: false
          install: true

      - name: Setup java
        uses: actions/setup-java@v4
        with:
          distribution: ${{ inputs.jdk_distro }}
          java-version: ${{ inputs.jdk_version }}

      - name: Set JUNIT_HOME
        run: |
          echo "JUNIT_HOME=$GITHUB_WORKSPACE/junit" >> "$GITHUB_ENV"
      - name: Set LD_LIBRARY_PATH
        run: |
          echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GITHUB_WORKSPACE/build-dir/lib" >> "$GITHUB_ENV"

      - name: Build JNI library
        run: CFLAGS=${{ inputs.javah_cflags }} ./java.sh $GITHUB_WORKSPACE/build-dir
      - name: Build JAR (ant)
        run: ant
      - name: Run Java tests (ant test)
        run: ant test

      - name: Show logs on failure
        if: failure() || cancelled()
        run: |
          cat build/reports/*.txt
