# Copyright (c) 2023 Project CHIP Authors
#
# Licensed 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.

name: Java Tests

on:
    push:
        branches-ignore:
            - 'dependabot/**'
    pull_request:
    merge_group:
    workflow_dispatch:

concurrency:
    group: ${{ github.ref }}-${{ github.workflow }}-${{ (github.event_name ==
        'pull_request' && github.event.number) || (github.event_name ==
        'workflow_dispatch' && github.run_number) || github.sha }}
    cancel-in-progress: true

env:
    CHIP_NO_LOG_TIMESTAMPS: true
    
jobs:
    java_tests_linux:
        name: Linux

        env:
            TSAN_OPTIONS: "halt_on_error=1 suppressions=scripts/tests/chiptest/tsan-linux-suppressions.txt"

        if: github.actor != 'restyled-io[bot]'
        runs-on: ubuntu-latest

        container:
            image: ghcr.io/project-chip/chip-build-java:74
            options: --privileged --sysctl "net.ipv6.conf.all.disable_ipv6=0
                net.ipv4.conf.all.forwarding=0 net.ipv6.conf.all.forwarding=0"

        steps:
            - name: Checkout
              uses: actions/checkout@v4
            - name: Checkout submodules & Bootstrap
              uses: ./.github/actions/checkout-submodules-and-bootstrap
              with:
                platform: linux
                bootstrap-log-name: bootstrap-logs-linux-${{ matrix.build_variant }}${{ matrix.chip_tool }}
            - name: Try to ensure the directories for core dumping exist and we
                  can write them.
              run: |
                  mkdir /tmp/cores || true
                  sysctl -w kernel.core_pattern=/tmp/cores/core.%u.%p.%t || true
                  mkdir objdir-clone || true
            - name: Generate unit tests
              run: |
                  scripts/run_in_build_env.sh \
                  './scripts/build/build_examples.py \
                     --target linux-x64-tests \
                     gen \
                  '
            - name: Build unit tests
              run: scripts/run_in_build_env.sh 'ninja -C out/linux-x64-tests src:java_controller_tests'

            - name: Run unit tests
              # TODO: this direct path loading is not maintainable. Our build system should define and
              #       support test classes.
              run: |
                 $JAVA_PATH/bin/java \
                   -cp 'third_party/java_deps/artifacts/*:out/linux-x64-tests/lib/src/controller/java/*' \
                   org.junit.runner.JUnitCore       \
                   matter.tlv.TlvWriterTest           \
                   matter.tlv.TlvReadWriteTest        \
                   matter.tlv.TlvReaderTest           \
                   matter.jsontlv.JsonToTlvToJsonTest \
                   matter.onboardingpayload.ManualCodeTest \
                   matter.onboardingpayload.QRCodeTest
            - name: Build Java Matter Controller and all clusters app
              run: |
                  scripts/run_in_build_env.sh './scripts/build_python.sh --install_virtual_env out/venv'
                  scripts/run_in_python_env.sh out/venv 'pip install -r scripts/setup/requirements.build.txt'
                  scripts/run_in_python_env.sh out/venv 'pip install colorama'
                  ./scripts/run_in_build_env.sh \
                   "./scripts/build/build_examples.py \
                      --target linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test \
                      --target linux-x64-java-matter-controller \
                      --target linux-x64-lit-icd-ipv6only \
                      build \
                   "
            - name: Build Kotlin Matter Controller
              run: |
                  ./scripts/run_in_build_env.sh \
                   "./scripts/build/build_examples.py \
                      --target linux-x64-kotlin-matter-controller \
                      build \
                   "                   
            - name: Run Discover Commissionables Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "discover" \
                     --tool-args "commissionables" \
                     --factoryreset \
                  '
            - name: Run Pairing Onnetwork Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "pairing" \
                     --tool-args "onnetwork-long --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
                     --factoryreset \
                  '
            - name: Run IM Invoke Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "im" \
                     --tool-args "onnetwork-long-im-invoke --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
                     --factoryreset \
                  '                    
            - name: Run IM Extendable Invoke Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "im" \
                     --tool-args "onnetwork-long-im-extendable-invoke --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
                     --factoryreset \
                  '
            - name: Run IM Read Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "im" \
                     --tool-args "onnetwork-long-im-read --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 3000" \
                     --factoryreset \
                  '
            - name: Run IM Write Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "im" \
                     --tool-args "onnetwork-long-im-write --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
                     --factoryreset \
                  '
            - name: Run IM Subscribe Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "im" \
                     --tool-args "onnetwork-long-im-subscribe --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 3000" \
                     --factoryreset \
                  '
            - name: Run Pairing AlreadyDiscovered Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "pairing" \
                     --tool-args "already-discovered --nodeid 1 --setup-pin-code 20202021 --address ::1 --port 5540 -t 1000" \
                     --factoryreset \
                  '
            # Disabled due to failure: https://github.com/project-chip/connectedhomeip/issues/27361
            # - name: Run Pairing Address-PaseOnly Test
            #   run: |
            #      scripts/run_in_python_env.sh out/venv \
            #       './scripts/tests/run_java_test.py \
            #          --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
            #          --app-args "--discriminator 3840 --interface-id -1" \
            #          --tool-path out/linux-x64-java-matter-controller \
            #          --tool-cluster "pairing" \
            #          --tool-args "address-paseonly --nodeid 1 --setup-pin-code 20202021 --address ::1 --port 5540 -t 1000" \
            #          --factoryreset \
            #       '
            - name: Run Pairing SetupQRCode Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "pairing" \
                     --tool-args "code --nodeid 1 --setup-payload MT:-24J0AFN00KA0648G00 --discover-once 1 --use-only-onnetwork-discovery 0 -t 1000" \
                     --factoryreset \
                  '
            - name: Run Pairing ManualCode Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "pairing" \
                     --tool-args "code --nodeid 1 --setup-payload 34970112332 --discover-once 1 --use-only-onnetwork-discovery 0 -t 1000" \
                     --factoryreset \
                  '
            - name: Run Pairing ICD Onnetwork Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_java_test.py \
                     --app out/linux-x64-lit-icd-ipv6only/lit-icd-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-java-matter-controller \
                     --tool-cluster "pairing" \
                     --tool-args "onnetwork-long --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
                     --factoryreset \
                  '
            - name: Run Pairing Onnetwork Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_kotlin_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-kotlin-matter-controller \
                     --tool-cluster "pairing" \
                     --tool-args "onnetwork-long --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
                     --factoryreset \
                  '                  
            - name: Run Kotlin IM Invoke Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_kotlin_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-kotlin-matter-controller \
                     --tool-cluster "im" \
                     --tool-args "onnetwork-long-im-invoke --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
                     --factoryreset \
                  '
            - name: Run Kotlin IM Read Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_kotlin_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-kotlin-matter-controller \
                     --tool-cluster "im" \
                     --tool-args "onnetwork-long-im-read --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 3000" \
                     --factoryreset \
                  '
            - name: Run Kotlin IM Write Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_kotlin_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-kotlin-matter-controller \
                     --tool-cluster "im" \
                     --tool-args "onnetwork-long-im-write --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 1000" \
                     --factoryreset \
                  '
            - name: Run Kotlin IM Subscribe Test
              run: |
                  scripts/run_in_python_env.sh out/venv \
                  './scripts/tests/run_kotlin_test.py \
                     --app out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app \
                     --app-args "--discriminator 3840 --interface-id -1" \
                     --tool-path out/linux-x64-kotlin-matter-controller \
                     --tool-cluster "im" \
                     --tool-args "onnetwork-long-im-subscribe --nodeid 1 --setup-pin-code 20202021 --discriminator 3840 -t 3000" \
                     --factoryreset \
                  '                                                  
            - name: Uploading core files
              uses: actions/upload-artifact@v4
              if: ${{ failure() && !env.ACT }}
              with:
                  name: crash-core-linux-java-controller
                  path: /tmp/cores/
                  # Cores are big; don't hold on to them too long.
                  retention-days: 5
            - name: Uploading objdir for debugging
              uses: actions/upload-artifact@v4
              if: ${{ failure() && !env.ACT }}
              with:
                  name: crash-objdir-linux-java-controller
                  path: objdir-clone/
                  # objdirs are big; don't hold on to them too long.
                  retention-days: 5