I am trying to write a two Spring Cloud contracts using Groovy script. A field in the request body of the second contract (contract2) shall be the value of the 'cookie' field present in the response of the first contract (contract1). I am not able to extract the field from the response.
I tried to extract the 'cookie' from the response using String cookie = getBody().properties.get('data').properties.get('cookie') but it triggered compilation error. What can I try next?
package contracts
import org.springframework.cloud.contract.spec.Contract
def contract1 = Contract.make {
request {
method 'POST'
url '/contract1'
headers {
contentType('application/json')
}
body(["url" : "test",
"name": "test",
"dnsServer": "8.8.8.8",
"vlan" : "52"])
}
response {
status 200
body("data" : ["cookie": anyNonBlankString()]])
headers {
contentType(applicationJson())
}
}
}
def contract2 = Contract.make {
request {
method 'POST'
url '/contract2'
headers {
contentType('application/json')
}
body(["model" : "test model",
"cookie" : <shall be the cookie generated by the response of contract1>
])
}
response {
status 200
}
}
[contract1, contract2]
Edit 1
I added String cookie = getResponse().properties.get("data").properties.get("cookie") right after the 'response' block in 'contract1'. Then I did "mvn clean spring-cloud-contract:generateTests" from terminal. I got the below compilation error:
[ERROR] Exception occurred while trying to evaluate the contract at path [C:\neilb\pagination\workspace\src\test\resources\contracts\ve_lifecycle.groovy]
java.lang.NullPointerException: Cannot get property 'properties' on null object
at org.codehaus.groovy.runtime.NullObject.getProperty (NullObject.java:60)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty (InvokerHelper.java:190)
at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty (NullCallSite.java:46)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty (AbstractCallSite.java:298)
at contracts.ve_lifecycle$_run_closure1.doCall (ve_lifecycle.groovy:31)
at contracts.ve_lifecycle$_run_closure1.doCall (ve_lifecycle.groovy)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:567)
at org.codehaus.groovy.reflection.CachedMethod.invoke (CachedMethod.java:101)
at groovy.lang.MetaMethod.doMethodInvoke (MetaMethod.java:323)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod (ClosureMetaClass.java:263)
at groovy.lang.MetaClassImpl.invokeMethod (MetaClassImpl.java:1041)
at groovy.lang.Closure.call (Closure.java:405)
at groovy.lang.Closure.call (Closure.java:399)
at org.springframework.cloud.contract.spec.Contract.make (Contract.java:180)
at org.springframework.cloud.contract.spec.Contract$make.call (Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall (CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call (AbstractCallSite.java:115)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call (AbstractCallSite.java:127)
at contracts.ve_lifecycle.run (ve_lifecycle.groovy:5)
at groovy.lang.GroovyShell.evaluate (GroovyShell.java:443)
at groovy.lang.GroovyShell.evaluate (GroovyShell.java:490)
at org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter.toObject (ContractVerifierDslConverter.groovy:141)
at org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter.convertAsCollection (ContractVerifierDslConverter.groovy:99)
at org.springframework.cloud.contract.verifier.file.ContractFileScanner.appendRecursively (ContractFileScanner.groovy:249)
at org.springframework.cloud.contract.verifier.file.ContractFileScanner.findContractsRecursively (ContractFileScanner.groovy:221)
at org.springframework.cloud.contract.verifier.TestGenerator.generateTestClasses (TestGenerator.groovy:125)
at org.springframework.cloud.contract.verifier.TestGenerator.generate (TestGenerator.groovy:104)
at org.springframework.cloud.contract.maven.verifier.GenerateTestsMojo.execute (GenerateTestsMojo.java:301)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:567)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)