/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import groovy.json.JsonOutput

plugins {
  id 'java'
  id 'org.apache.beam.module'
  id 'com.gradleup.shadow'
}

applyJavaNature(
  exportJavadoc: false,
  automaticModuleName: 'org.apache.beam.examples.sql',
)
provideIntegrationTestingDependencies()
enableJavaPerformanceTesting()

description = "Apache Beam :: Examples :: Java"
ext.summary = """Apache Beam SDK provides a simple, Java-based
interface for processing virtually any size data. This
artifact includes all Apache Beam Java SDK examples."""

apply from: "$project.rootDir/examples/java/common.gradle"

dependencies {
  implementation enforcedPlatform(library.java.google_cloud_platform_libraries_bom)
  implementation project(path: ":sdks:java:core", configuration: "shadow")
  implementation project(":sdks:java:extensions:sql")
  implementation library.java.slf4j_api
  runtimeOnly project(path: ":runners:direct-java", configuration: "shadow")
  runtimeOnly library.java.hadoop_client
  runtimeOnly library.java.bigdataoss_gcs_connector
  testImplementation project(path: ":runners:direct-java", configuration: "shadow")
  testImplementation library.java.hamcrest
  testImplementation library.java.junit
  testImplementation library.java.mockito_core
  testImplementation library.java.testcontainers_gcloud

  // Add dependencies for the PreCommit configurations
  // For each runner a project level dependency on the examples project.
  for (String runner : preCommitRunners) {
    delegate.add(runner + "PreCommit", project(path: ":examples:java", configuration: "testRuntimeMigration"))
  }

  // Add dependency if requested on command line for runner
  if (project.hasProperty("runnerDependency")) {
    runtimeOnly project(path: project.getProperty("runnerDependency"))
  }
}

/*
 * Create a ${runner}PreCommit task for each runner which runs a set
 * of integration tests for WordCount and WindowedWordCount.
 */
def preCommitRunnerClass = [
  directRunner: "org.apache.beam.runners.direct.DirectRunner",
  flinkRunner: "org.apache.beam.runners.flink.TestFlinkRunner",
  sparkRunner: "org.apache.beam.runners.spark.TestSparkRunner",
]
def gcpProject = project.findProperty('gcpProject') ?: 'apache-beam-testing'
def gcsTempRoot = project.findProperty('gcsTempRoot') ?: 'gs://temp-storage-for-end-to-end-tests/'

for (String runner : preCommitRunners) {
  tasks.create(name: runner + "PreCommit", type: Test) {
    def preCommitBeamTestPipelineOptions = [
       "--project=${gcpProject}",
       "--tempRoot=${gcsTempRoot}",
       "--runner=" + preCommitRunnerClass[runner],
    ]
    classpath = configurations."${runner}PreCommit"
    forkEvery 1
    maxParallelForks 4
    systemProperty "beamTestPipelineOptions", JsonOutput.toJson(preCommitBeamTestPipelineOptions)
  }
}

/* Define a common precommit task which depends on all the individual precommits. */
task preCommit() {
  for (String runner : preCommitRunners) {
    dependsOn runner + "PreCommit"
  }
}
