name: Tests

on:
  - push
  - pull_request

jobs:
  test-java-8-war-generation:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: 8
          check-latest: true
          cache: "maven"

      - name: Remove test code (not java 8 compatible)
        run: rm -rf src/test

      - name: Generate war file (including apache-jsp artifact)
        run: mvn --batch-mode -f pom.jdk8.xml -D java.net.useSystemProxies=true -Dapache-jsp.scope=compile clean package

      - name: Generate war file
        run: mvn --batch-mode -f pom.jdk8.xml -D java.net.useSystemProxies=true clean package

      - name: temporarily save generated files
        uses: actions/upload-artifact@v3
        with:
          name: war-jre8
          path: target/plantuml.war
          retention-days: 1

  test-java-8-war:
    runs-on: ubuntu-latest
    needs: test-java-8-war-generation
    strategy:
      matrix:
        java-version: [ 11, 17 ]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: retrieve generated files (jre8)
        uses: actions/download-artifact@v3
        with:
          name: war-jre8
          path: artifacts

      - uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: ${{ matrix.java-version }}
          check-latest: true
          cache: "maven"

      - name: Prepare jetty-runner tests
        run: mvn --batch-mode clean package

      - name: Start jetty server over jetty-runner
        run: java -jar target/dependency/jetty-runner.jar --config src/main/config/jetty.xml --path /plantuml artifacts/plantuml.war &

      - name: Wait 5 seconds (to let jetty-runner start the jetty server)
        run: sleep 5s

      - name: Run tests against "mvn jetty:run" server
        run: mvn --batch-mode test -DskipTests=false -DargLine="-Dsystem.test.server=http://localhost:8080/plantuml"

  test-mvn-livecycle:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        java-version: [ 11, 17 ]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: ${{ matrix.java-version }}
          check-latest: true
          cache: "maven"

      - name: Lifecycle - Step 1/8 - mvn clean
        run: mvn --batch-mode clean

      - name: Lifecycle - Step 2/8 - mvn validate
        run: mvn --batch-mode validate

      - name: Lifecycle - Step 3/8 - mvn compile
        run: mvn --batch-mode compile

      - name: Lifecycle - Step 4/8 - mvn test (with skipTests=true)
        run: mvn --batch-mode test

      - name: Lifecycle - Step 5/8 - mvn package
        run: mvn --batch-mode package

      - name: Lifecycle - Step 6/8 - mvn verify
        run: mvn --batch-mode verify

      - name: Lifecycle - Step 7/8 - mvn install
        run: mvn --batch-mode install

      - name: Lifecycle - Step 8/8 - mvn site
        run: mvn --batch-mode site

  test-embedded:
    runs-on: ubuntu-latest
    needs: test-mvn-livecycle
    strategy:
      matrix:
        java-version: [ 11, 17 ]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: ${{ matrix.java-version }}
          check-latest: true
          cache: "maven"

      - name: Run tests against jetty embedded server
        run: mvn --batch-mode clean test -DskipTests=false

  test-mvn-jetty-run:
    runs-on: ubuntu-latest
    needs: test-mvn-livecycle
    strategy:
      matrix:
        java-version: [ 11, 17 ]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: ${{ matrix.java-version }}
          check-latest: true
          cache: "maven"

      - name: Prepare "mvn jetty:run" tests
        run: mvn --batch-mode clean test

      - name: Start jetty server over maven
        run: mvn --batch-mode jetty:run &

      - name: Wait 20 seconds (to let maven start the jetty server)
        run: sleep 20s

      - name: Run tests against "mvn jetty:run" server
        run: mvn --batch-mode test -DskipTests=false -DargLine="-Dsystem.test.server=http://localhost:8080/plantuml"

  test-jetty-runner:
    runs-on: ubuntu-latest
    needs: test-mvn-livecycle
    strategy:
      matrix:
        java-version: [ 11, 17 ]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: ${{ matrix.java-version }}
          check-latest: true
          cache: "maven"

      - name: Prepare jetty-runner tests
        run: mvn --batch-mode clean package

      - name: Start jetty server over jetty-runner
        run: java -jar target/dependency/jetty-runner.jar --config src/main/config/jetty.xml --path /plantuml target/plantuml.war &

      - name: Wait 5 seconds (to let jetty-runner start the jetty server)
        run: sleep 5s

      - name: Run tests against "mvn jetty:run" server
        run: mvn --batch-mode test -DskipTests=false -DargLine="-Dsystem.test.server=http://localhost:8080/plantuml"

  test-jetty:
    runs-on: ubuntu-latest
    needs: test-mvn-livecycle
    strategy:
      matrix:
        java-version: [ 11, 17 ]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: ${{ matrix.java-version }}
          check-latest: true
          cache: "maven"

      - name: Prepare external tests
        run: mvn --batch-mode clean test

      - name: Build the jetty docker stack
        run: |
          docker image build -f Dockerfile.jetty -t plantuml-server:local .
          docker run -d --hostname=test.localhost -p 8080:8080 -e BASE_URL=plantuml plantuml-server:local

      - name: Check running containers
        run: docker ps

      - name: run tests against jetty docker image
        run: mvn --batch-mode test -DskipTests=false -DargLine="-Dsystem.test.server=http://localhost:8080/plantuml"

  test-tomcat:
    runs-on: ubuntu-latest
    needs: test-mvn-livecycle
    strategy:
      matrix:
        java-version: [ 11, 17 ]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: ${{ matrix.java-version }}
          check-latest: true
          cache: "maven"

      - name: Prepare external tests
        run: mvn --batch-mode clean test

      - name: Build the tomcat docker stack
        run: |
          docker image build -f Dockerfile.tomcat -t plantuml-server:local .
          docker run -d --hostname=test.localhost -p 8080:8080 -e BASE_URL=plantuml plantuml-server:local

      - name: Check running containers
        run: docker ps

      - name: run tests against tomcat docker image
        run: mvn --batch-mode test -DskipTests=false -DargLine="-Dsystem.test.server=http://localhost:8080/plantuml"
