/*
 * MIT License
 *
 * Copyright (c) 2020-present Cloudogu GmbH and Contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

plugins {
  id 'java-library'
  id 'org.scm-manager.ci'
  id 'org.scm-manager.java'
  id 'org.scm-manager.core-plugins'
  id 'org.scm-manager.integration-tests'
  id 'com.github.node-gradle.node' version '2.2.4'
}

configurations {
  itWebApp
  itPlugin
}

dependencies {
  testImplementation platform(project(':'))

  testImplementation project(':scm-core')
  testImplementation project(':scm-test')

  testImplementation project(':scm-plugins:scm-git-plugin')
  testImplementation project(path: ':scm-plugins:scm-git-plugin', configuration: 'tests')
  testImplementation project(':scm-plugins:scm-hg-plugin')
  testImplementation project(path: ':scm-plugins:scm-hg-plugin', configuration: 'tests')
  testImplementation project(':scm-plugins:scm-svn-plugin')
  testImplementation project(path: ':scm-plugins:scm-svn-plugin', configuration: 'tests')

  testImplementation 'io.rest-assured:rest-assured:4.3.0'
  testImplementation 'org.glassfish:javax.json:1.1.4'

  // dependencies for tests in sonia.scm.it.webapp
  testImplementation project(':scm-webapp')
  testImplementation libraries.jerseyClientApi
  testImplementation libraries.jerseyClientRuntime
  testImplementation libraries.jacksonJaxbAnnotations

  testImplementation libraries.junitPioneer

  itWebApp project(path: ':scm-webapp', configuration: 'webapp')
  itPlugin project(path: ':scm-plugins:scm-integration-test-plugin', configuration: 'smp')
}

scmServer {
  configuration 'itWebApp'
  openBrowser = false
  liveReload = false
  plugins = configurations.itPlugin
}

test {
  include '**/*Test.class'
  exclude '**/*ITCase.class'
}

task javaIntegrationTests(type: Test) {
  include '**/*ITCase.class'
  exclude '**/*Test.class'
  ignoreFailures = project.isCI
  outputs.upToDateWhen { !project.hasProperty('rerunIntegrationTests') }
  finalizedBy = ['stopScmServer']
  useJUnitPlatform()

  dependsOn 'test', 'startScmServer'
  mustRunAfter 'startScmServer'
}

node {
  download = true
  version = nodeVersion
  yarnVersion = yarnVersion
  nodeModulesDir = file(project.rootProject.projectDir)
}

tasks.getByName('yarn_install').configure {
  inputs.file( project.rootProject.file('yarn.lock') )
  outputs.dir( project.rootProject.file('node_modules') )
}

task e2eTests(type: YarnTask) {
  inputs.files(fileTree(project.project(":scm-ui").projectDir) {
    include 'e2e-*/**'
    include 'ui-*/**'
  })
  outputs.dir(project.project(":scm-ui").file('build/reports/e2e'))

  args = ['run', 'e2e-tests']
  ignoreExitValue = project.isCI
  dependsOn 'yarn_install', 'startScmServer'
}

task updateE2eTestTimestamps(type: TouchFiles) {
  directory = project.project(":scm-ui").file('build/reports/e2e')
  extension = "xml"
}

task integrationTest {
  dependsOn(javaIntegrationTests, e2eTests)
  description = "Runs all integration tests."
  group = "verification"
}

sonarqube {
  skipProject = true
}
