From e5efa2207dfb489e7149957716802519ea444477 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 3 Dec 2024 09:46:02 +1100 Subject: [PATCH] chore: Fix compiler and build warnings --- provider/maven/build.gradle | 65 ++++++++++--------- .../com/dius/pact/provider/maven/Consumer.kt | 1 + .../pact/provider/maven/PactProviderMojo.kt | 2 +- .../spring/SpringMessagePactRunner.kt | 4 +- .../provider/spring/SpringRestPactRunner.kt | 4 +- .../spring/WebFluxProviderVerifier.kt | 3 +- .../spring6/Spring6EnvironmentResolver.kt | 12 +++- .../spring6/Spring6MockMvcTestTarget.kt | 15 +++-- 8 files changed, 62 insertions(+), 44 deletions(-) diff --git a/provider/maven/build.gradle b/provider/maven/build.gradle index 133f4ebe49..d60dea07e5 100644 --- a/provider/maven/build.gradle +++ b/provider/maven/build.gradle @@ -23,52 +23,57 @@ dependencies { } import org.apache.tools.ant.taskdefs.condition.Os -def isWindows() { + +static def isWindows() { Os.isFamily(Os.FAMILY_WINDOWS) } -task generatePom(type: GenerateMavenPom, dependsOn: [":provider:publishToMavenLocal", - ':core:model:publishToMavenLocal', - ':core:matchers:publishToMavenLocal', - ':core:pactbroker:publishToMavenLocal', - ':core:support:publishToMavenLocal']) { - destination = file("${buildDir}/poms/pom.xml") - pom = publishMavenPublicationPublicationToMavenLocal.publication.pom - pom.packaging = 'maven-plugin' - pom.withXml { - def buildNode = asNode().appendNode('build') - buildNode.appendNode('directory', buildDir) - buildNode.appendNode('outputDirectory', "$buildDir/classes/kotlin/main") - //add and configure the maven-plugin-plugin so that we can use the shortened 'pact' prefix - //https://maven.apache.org/guides/introduction/introduction-to-plugin-prefix-mapping.html - def pluginNode = buildNode.appendNode('plugins').appendNode('plugin') - pluginNode.appendNode('artifactId', 'maven-plugin-plugin') - pluginNode.appendNode('version', '3.5') - pluginNode.appendNode('configuration').appendNode('goalPrefix', 'pact') - } +tasks.register('generatePom', GenerateMavenPom) { + dependsOn ":provider:publishToMavenLocal", + ':core:model:publishToMavenLocal', + ':core:matchers:publishToMavenLocal', + ':core:pactbroker:publishToMavenLocal', + ':core:support:publishToMavenLocal' + destination = file("${buildDir}/poms/pom.xml") + pom = publishMavenPublicationPublicationToMavenLocal.publication.pom + pom.packaging = 'maven-plugin' + pom.withXml { + def buildNode = asNode().appendNode('build') + buildNode.appendNode('directory', buildDir) + buildNode.appendNode('outputDirectory', "$buildDir/classes/kotlin/main") + //add and configure the maven-plugin-plugin so that we can use the shortened 'pact' prefix + //https://maven.apache.org/guides/introduction/introduction-to-plugin-prefix-mapping.html + def pluginNode = buildNode.appendNode('plugins').appendNode('plugin') + pluginNode.appendNode('artifactId', 'maven-plugin-plugin') + pluginNode.appendNode('version', '3.5') + pluginNode.appendNode('configuration').appendNode('goalPrefix', 'pact') + } } if (System.env.CI != 'true') { - task pluginDescriptor(type: Exec, dependsOn: generatePom) { + tasks.register('pluginDescriptor', Exec) { + dependsOn generatePom + + def pomFile = layout.buildDirectory.file("poms/pom.xml").get().asFile.path if (isWindows()) { try { // check if mvn.bat exists def proc = new ProcessBuilder('mvn.bat', '-v') proc.start().waitFor() - commandLine 'mvn.bat', '-f', "${buildDir}/poms/pom.xml", '-e', '-B', 'org.apache.maven.plugins:maven-plugin-plugin:3.6.1:descriptor' - } catch(Exception e) { - commandLine 'mvn.cmd', '-f', "${buildDir}/poms/pom.xml", '-e', '-B', 'org.apache.maven.plugins:maven-plugin-plugin:3.6.1:descriptor' + commandLine 'mvn.bat', '-f', pomFile, '-e', '-B', 'org.apache.maven.plugins:maven-plugin-plugin:3.6.1:descriptor' + } catch (Exception e) { + commandLine 'mvn.cmd', '-f', pomFile, '-e', '-B', 'org.apache.maven.plugins:maven-plugin-plugin:3.6.1:descriptor' } } else { - commandLine 'sh', '-c', "mvn -f ${buildDir}/poms/pom.xml -e -B org.apache.maven.plugins:maven-plugin-plugin:3.6.1:descriptor" + commandLine 'sh', '-c', "mvn -f ${pomFile} -e -B org.apache.maven.plugins:maven-plugin-plugin:3.6.1:descriptor" } - doLast { - final dir = project.compileKotlin.destinationDirectory.dir('META-INF/maven').get() - final pluginDescriptor = dir.file('plugin.xml').getAsFile() - assert pluginDescriptor.exists(), "[$pluginDescriptor.canonicalPath] was not created" - } + doLast { + final dir = project.compileKotlin.destinationDirectory.dir('META-INF/maven').get() + final pluginDescriptor = dir.file('plugin.xml').getAsFile() + assert pluginDescriptor.exists(), "[$pluginDescriptor.canonicalPath] was not created" + } } pluginDescriptor.shouldRunAfter project.jar diff --git a/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/Consumer.kt b/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/Consumer.kt index 7920f0e902..cbcac5893b 100644 --- a/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/Consumer.kt +++ b/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/Consumer.kt @@ -15,6 +15,7 @@ class Consumer( override var packagesToScan: List = emptyList(), override var verificationType: PactVerification? = null, override var pactSource: Any? = null, + @Deprecated("replaced with auth") override var pactFileAuthentication: List = emptyList() ) : ConsumerInfo(name, stateChange, stateChangeUsesBody, packagesToScan, verificationType, pactSource, pactFileAuthentication) { diff --git a/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/PactProviderMojo.kt b/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/PactProviderMojo.kt index d56b2fa601..bdb142f53e 100644 --- a/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/PactProviderMojo.kt +++ b/provider/maven/src/main/kotlin/au/com/dius/pact/provider/maven/PactProviderMojo.kt @@ -152,7 +152,7 @@ open class PactProviderMojo : PactBaseMojo() { verifier.displayFailures(failures) val nonPending = failures.filterNot { it.pending } if (nonPending.isNotEmpty()) { - throw MojoFailureException("There were ${nonPending.sumBy { it.failures.size }} non-pending pact failures") + throw MojoFailureException("There were ${nonPending.sumOf { it.failures.size }} non-pending pact failures") } } } finally { diff --git a/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/SpringMessagePactRunner.kt b/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/SpringMessagePactRunner.kt index 5124048010..f919f00234 100644 --- a/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/SpringMessagePactRunner.kt +++ b/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/SpringMessagePactRunner.kt @@ -26,12 +26,12 @@ open class SpringMessagePactRunner(clazz: Class<*>) : MessagePactRunner(clazz) { override fun withBeforeClasses(statement: Statement?): Statement { val withBeforeClasses = super.withBeforeClasses(statement) - return RunBeforeTestClassCallbacks(withBeforeClasses, testContextManager) + return RunBeforeTestClassCallbacks(withBeforeClasses, testContextManager!!) } override fun withAfterClasses(statement: Statement?): Statement { val withAfterClasses = super.withAfterClasses(statement) - return RunAfterTestClassCallbacks(withAfterClasses, testContextManager) + return RunAfterTestClassCallbacks(withAfterClasses, testContextManager!!) } private fun initTestContextManager(clazz: Class<*>): TestContextManager { diff --git a/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/SpringRestPactRunner.kt b/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/SpringRestPactRunner.kt index 2d3ebc6f1c..029918dffe 100644 --- a/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/SpringRestPactRunner.kt +++ b/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/SpringRestPactRunner.kt @@ -27,12 +27,12 @@ open class SpringRestPactRunner(clazz: Class<*>) : RestPactRunner(clazz) { override fun withBeforeClasses(statement: Statement?): Statement { val withBeforeClasses = super.withBeforeClasses(statement) - return RunBeforeTestClassCallbacks(withBeforeClasses, testContextManager) + return RunBeforeTestClassCallbacks(withBeforeClasses, testContextManager!!) } override fun withAfterClasses(statement: Statement?): Statement { val withAfterClasses = super.withAfterClasses(statement) - return RunAfterTestClassCallbacks(withAfterClasses, testContextManager) + return RunAfterTestClassCallbacks(withAfterClasses, testContextManager!!) } private fun initTestContextManager(clazz: Class<*>): TestContextManager { diff --git a/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/WebFluxProviderVerifier.kt b/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/WebFluxProviderVerifier.kt index 0dada1b590..8af216c487 100644 --- a/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/WebFluxProviderVerifier.kt +++ b/provider/spring/src/main/kotlin/au/com/dius/pact/provider/spring/WebFluxProviderVerifier.kt @@ -50,8 +50,9 @@ class WebFluxProviderVerifier : ProviderVerifier() { val expectedResponse = interaction.response val actualResponse = handleResponse(clientResponse) + // TODO: Add plugin config here verifyRequestResponsePact(expectedResponse, actualResponse, interactionMessage, failures, - interaction.interactionId.orEmpty(), false) + interaction.interactionId.orEmpty(), false, emptyMap()) } catch (e: Exception) { logger.error(e) { "Request to provider method failed" } diff --git a/provider/spring6/src/main/kotlin/au/com/dius/pact/provider/spring/spring6/Spring6EnvironmentResolver.kt b/provider/spring6/src/main/kotlin/au/com/dius/pact/provider/spring/spring6/Spring6EnvironmentResolver.kt index 7e23919a97..c8a9e45095 100644 --- a/provider/spring6/src/main/kotlin/au/com/dius/pact/provider/spring/spring6/Spring6EnvironmentResolver.kt +++ b/provider/spring6/src/main/kotlin/au/com/dius/pact/provider/spring/spring6/Spring6EnvironmentResolver.kt @@ -7,11 +7,19 @@ import org.springframework.core.env.Environment class Spring6EnvironmentResolver(private val environment: Environment) : ValueResolver { override fun resolveValue(property: String?): String? { val tuple = SystemPropertyResolver.PropertyValueTuple(property).invoke() - return environment.getProperty(tuple.propertyName, tuple.defaultValue) + return if (tuple.propertyName != null) { + environment.getProperty(tuple.propertyName!!, tuple.defaultValue.orEmpty()) + } else { + null + } } override fun resolveValue(property: String?, default: String?): String? { - return environment.getProperty(property, default) + return if (property != null) { + environment.getProperty(property, default.orEmpty()) + } else { + null + } } override fun propertyDefined(property: String) = environment.containsProperty(property) diff --git a/provider/spring6/src/main/kotlin/au/com/dius/pact/provider/spring/spring6/Spring6MockMvcTestTarget.kt b/provider/spring6/src/main/kotlin/au/com/dius/pact/provider/spring/spring6/Spring6MockMvcTestTarget.kt index 76b642e684..d7e814c2c7 100644 --- a/provider/spring6/src/main/kotlin/au/com/dius/pact/provider/spring/spring6/Spring6MockMvcTestTarget.kt +++ b/provider/spring6/src/main/kotlin/au/com/dius/pact/provider/spring/spring6/Spring6MockMvcTestTarget.kt @@ -13,7 +13,7 @@ import au.com.dius.pact.provider.ProviderInfo import au.com.dius.pact.provider.ProviderResponse import au.com.dius.pact.provider.junit5.TestTarget import jakarta.servlet.http.Cookie -import io.github.oshai.kotlinlogging.KLogging +import io.github.oshai.kotlinlogging.KotlinLogging import org.apache.commons.lang3.StringUtils import org.hamcrest.core.IsAnything import org.springframework.http.HttpHeaders @@ -39,6 +39,8 @@ import javax.mail.internet.ContentDisposition import javax.mail.internet.MimeMultipart import javax.mail.util.ByteArrayDataSource +private val logger = KotlinLogging.logger {} + /** * Test target for tests using Spring MockMvc. */ @@ -85,7 +87,7 @@ class Spring6MockMvcTestTarget @JvmOverloads constructor( val requestBuilder = MockMvcRequestBuilders.get("/") if (!servletPath.isNullOrEmpty()) { - requestBuilder.servletPath(servletPath) + requestBuilder.servletPath(servletPath!!) } return MockMvcBuilders.standaloneSetup(*controllers.toTypedArray()) @@ -120,7 +122,7 @@ class Spring6MockMvcTestTarget @JvmOverloads constructor( } else { MockMvcRequestBuilders.request(HttpMethod.valueOf(request.method), requestUriString(request)) .headers(mapHeaders(request, true)) - .content(body.value) + .content(body.value!!) } } else { MockMvcRequestBuilders.request(HttpMethod.valueOf(request.method), requestUriString(request)) @@ -196,7 +198,10 @@ class Spring6MockMvcTestTarget @JvmOverloads constructor( val headers = mutableMapOf>() httpResponse.headerNames.forEach { headerName -> - headers[headerName] = listOf(httpResponse.getHeader(headerName)) + val header = httpResponse.getHeader(headerName) + if (header != null) { + headers[headerName] = listOf(header) + } } val contentType = if (httpResponse.contentType.isNullOrEmpty()) { @@ -218,6 +223,4 @@ class Spring6MockMvcTestTarget @JvmOverloads constructor( } override fun supportsInteraction(interaction: Interaction) = interaction is SynchronousRequestResponse - - companion object : KLogging() }