/*
 * Copyright 2013-2020 the original author or 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
 *
 *      https://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.
 */

plugins {
	id("org.springframework.boot") apply false
	id("org.springframework.cloud.contract") apply false
}

ext {
	restAssuredVersion = '3.0.7'
	spockVersion = '1.0-groovy-2.4'

	contractVerifierStubsBaseDirectory = 'src/test/resources/stubs'
}

group = 'org.springframework.cloud.testprojects'

subprojects {
	apply plugin: 'groovy'

	sourceCompatibility = 17
	targetCompatibility = 17
	version = "1.0.0"

	repositories {
		mavenCentral()
		mavenLocal()
		maven { url "https://repo.spring.io/snapshot" }
		maven { url "https://repo.spring.io/milestone" }
	}

	dependencies {
		testImplementation "org.apache.groovy:groovy"
		testImplementation("org.spockframework:spock-core:$spockVersion") {
			exclude(group: 'org.apache.groovy')
		}
		testImplementation "junit:junit:4.12"
		testImplementation "com.github.tomakehurst:wiremock:${wiremockVersion}"
		testImplementation "com.toomuchcoding.jsonassert:jsonassert:${jsonAssertVersion}"
		testImplementation "org.assertj:assertj-core:2.4.1"
		testImplementation "org.springframework.cloud:spring-cloud-contract-verifier:${verifierVersion}"
	}
}

configure([project(':fraudDetectionService'), project(':loanApplicationService')]) {
	apply plugin: 'org.springframework.boot'
	apply plugin: 'io.spring.dependency-management'
	apply plugin: 'org.springframework.cloud.contract'
	apply plugin: 'maven-publish'

	// tag::jar_setup[]
	ext {
		contractsDir = file("mappings")
		stubsOutputDirRoot = file("${project.buildDir}/production/${project.name}-stubs/")
	}

	// Automatically added by plugin:
	// copyContracts - copies contracts to the output folder from which JAR will be created
	// verifierStubsJar - JAR with a provided stub suffix

	publishing {
		publications {
			stubs(MavenPublication) {
				artifactId "${project.name}-stubs"
				artifact verifierStubsJar
			}
		}
	}
	// end::jar_setup[]

	contracts {
		contractsMode = "REMOTE"
		// tag::target_framework[]
		testFramework = 'Spock'
		// end::target_framework[]
		testMode = 'MockMvc'
		//baseClassForTests = 'org.springframework.cloud.MvcSpec'
		// tag::base_class_mapping[]
		baseClassMappings {
			baseClassMapping('.*', 'org.springframework.cloud.MvcSpec')
		}
		// end::base_class_mapping[]
		contractsDslDir = file("${project.projectDir.absolutePath}/mappings/")
		generatedTestSourcesDir = file("${project.buildDir}/generated-test-sources/")
		stubsOutputDir = stubsOutputDirRoot
		sourceSet = "contractTest"
	}

	jar {
		version = '0.0.1'
	}

	dependencies {
		implementation("org.springframework.boot:spring-boot-starter-web") {
			exclude module: "spring-boot-starter-tomcat"
		}
		implementation("org.springframework.boot:spring-boot-starter-jetty")
		implementation("org.springframework.boot:spring-boot-starter-actuator")

		testRuntime("org.spockframework:spock-spring:$spockVersion") {
			exclude(group: 'org.apache.groovy')
		}
		testImplementation "org.mockito:mockito-core"
		testImplementation "org.springframework:spring-test"
		testImplementation "org.springframework.boot:spring-boot-test"
		testImplementation "org.springframework.cloud:spring-cloud-test-support"
		testImplementation "io.rest-assured:rest-assured:$restAssuredVersion"
		testImplementation "io.rest-assured:spring-mock-mvc:$restAssuredVersion"
	}

	task cleanup(type: Delete) {
		delete 'src/test/resources/mappings', 'src/test/resources/stubs'
	}

	clean.dependsOn('cleanup')

	test {
		testLogging {
			exceptionFormat = 'full'
		}
	}
}

configure(project(':fraudDetectionService')) {
	test.dependsOn('generateClientStubs')
}

configure(project(':loanApplicationService')) {

	task copyCollaboratorStubs(type: Copy) {
		File fraudBuildDir = project(':fraudDetectionService').buildDir
		from(new File(fraudBuildDir, "/production/${project(':fraudDetectionService').name}-stubs/"))
		into "src/test/resources/mappings"
	}

	project.tasks.named("test").get().dependsOn('copyCollaboratorStubs')
}
