parameters:
  ServiceDirectory: ''
  TestResourceDirectories:
  Artifacts: []
  AdditionalModules: []
  EnvVars: {}
  MaxParallel: 0
  PreSteps: []
  PostSteps: []
  TimeoutInMinutes: 60
  TestMode: 'LIVE'
  TestResultsFiles: ''
  CloudConfig: {}
  ArmTemplateParameters: '@{}'
  Location: ''
  Matrix: ''
  DependsOn: ''
  UsePlatformContainer: false
  DisableAzureResourceCreation: false
  BuildParallelization: '2C'
  TestGoals: $(TestGoals)
  TestOptions: $(TestOptions)
  TestParallelization: '1'
  TestVersionSupport: $(TestVersionSupport)

jobs:
  - job:
    dependsOn: ${{ parameters.DependsOn }}
    condition: and(succeededOrFailed(), ne(${{ parameters.Matrix }}, '{}'))
    strategy:
      maxParallel: ${{ parameters.MaxParallel }}
      matrix: $[ ${{ parameters.Matrix }} ]

    variables:
      - template: ../variables/globals.yml
      - name: ArmTemplateParameters
        value: '@{}'

    timeoutInMinutes: ${{ parameters.TimeoutInMinutes }}

    pool:
      name: $(Pool)
      vmImage: $(OSVmImage)

    ${{ if eq(parameters.UsePlatformContainer, 'true') }}:
      # Add a default so the job doesn't fail when the matrix is empty
      container: $[ variables['Container'] ]

    steps:
      - template: /eng/pipelines/templates/steps/initialize-test-environment.yml
        parameters:
          Artifacts: ${{ parameters.Artifacts }}
          AdditionalModules: ${{ parameters.AdditionalModules }}
          ServiceDirectory: ${{ parameters.ServiceDirectory }}
          SDKType: ${{ parameters.SDKType }}

      - template: ../steps/install-reporting-tools.yml
        parameters:
          JdkVersion: $(JavaTestVersion)

      - template: /eng/pipelines/templates/steps/install-graalvm-jdk.yml
        parameters:
          GraalVMJdkVersion: '22.3.0'

      - ${{ if ne(parameters.DisableAzureResourceCreation, 'true') }}:
          - template: /eng/common/TestResources/build-test-resource-config.yml
            parameters:
              SubscriptionConfiguration: ${{ parameters.CloudConfig.SubscriptionConfiguration }}
              SubscriptionConfigurations: ${{ parameters.CloudConfig.SubscriptionConfigurations }}

          - ${{ if parameters.TestResourceDirectories }}:
              - ${{ each directory in parameters.TestResourceDirectories }}:
                  - template: /eng/common/TestResources/deploy-test-resources.yml
                    parameters:
                      ${{ if or(parameters.Location, parameters.CloudConfig.Location) }}:
                        Location: ${{ coalesce(parameters.Location, parameters.CloudConfig.Location) }}
                      ServiceDirectory: '${{ directory }}'
                      SubscriptionConfiguration: $(SubscriptionConfiguration)
                      ArmTemplateParameters: $(ArmTemplateParameters)
          - ${{ if not(parameters.TestResourceDirectories) }}:
              - template: /eng/common/TestResources/deploy-test-resources.yml
                parameters:
                  ${{ if or(parameters.Location, parameters.CloudConfig.Location) }}:
                    Location: ${{ coalesce(parameters.Location, parameters.CloudConfig.Location) }}
                  ServiceDirectory: '${{ parameters.ServiceDirectory }}'
                  SubscriptionConfiguration: $(SubscriptionConfiguration)
                  ArmTemplateParameters: $(ArmTemplateParameters)

      - ${{ parameters.PreSteps }}

      - template: /eng/pipelines/templates/steps/build-and-test-native.yml
        parameters:
          SDKType: ${{ parameters.SDKType }}
          ServiceDirectory: ${{ parameters.ServiceDirectory }}
          ParallelTestPlayback: 'false'
          BuildParallelization: ${{ parameters.BuildParallelization }}
          IsLiveTest: true
          TestGoals: ${{ parameters.TestGoals }}
          TestOptions: ${{ parameters.TestOptions }}
          TestParallelization: ${{ parameters.TestParallelization }}
          TestVersionSupport: ${{ parameters.TestVersionSupport }}
          TestEnvVars:
            AZURE_TEST_MODE: ${{ parameters.TestMode }}
            AZURE_VERSION_OVERRIDE_TESTS: ${{ parameters.TestVersionSupport }}
            ${{ each var in parameters.EnvVars }}:
              ${{ var.key }}: ${{ var.value }}

      - ${{ parameters.PostSteps }}

      - ${{ if ne(parameters.DisableAzureResourceCreation, 'true') }}:
          - ${{ if parameters.TestResourceDirectories }}:
              - ${{ each directory in parameters.TestResourceDirectories }}:
                  - template: /eng/common/TestResources/remove-test-resources.yml
                    parameters:
                      ServiceDirectory: '${{ directory }}'
                      SubscriptionConfiguration: $(SubscriptionConfiguration)
          - ${{ if not(parameters.TestResourceDirectories) }}:
              - template: /eng/common/TestResources/remove-test-resources.yml
                parameters:
                  ServiceDirectory: '${{ parameters.ServiceDirectory }}'
                  SubscriptionConfiguration: $(SubscriptionConfiguration)
