From ddf92f7150dea3c7728966caaa3a0affb8277ced Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 28 Jun 2016 23:22:28 +0200 Subject: [PATCH] Added a missing test for message sending --- .../stubrunner/StubRunnerExecutorSpec.groovy | 50 +++++++++++++++++++ .../repository/contracts/contract1.groovy | 27 ++++++++++ 2 files changed, 77 insertions(+) create mode 100644 spring-cloud-contract-stub-runner/spring-cloud-contract-stub-runner/src/test/resources/repository/contracts/contract1.groovy diff --git a/spring-cloud-contract-stub-runner/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy b/spring-cloud-contract-stub-runner/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy index ad59d1374b..ad8b9bfe41 100644 --- a/spring-cloud-contract-stub-runner/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy +++ b/spring-cloud-contract-stub-runner/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy @@ -16,9 +16,14 @@ package org.springframework.cloud.contract.stubrunner +import groovy.json.JsonOutput import org.springframework.cloud.contract.stubrunner.util.StubsParser +import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage +import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging import spock.lang.Specification +import java.util.concurrent.TimeUnit + class StubRunnerExecutorSpec extends Specification { static final int MIN_PORT = 8999 @@ -71,10 +76,55 @@ class StubRunnerExecutorSpec extends Specification { executor.findStubUrl("group", "artifact") == 'http://localhost:12345'.toURL() } + def 'should ensure that triggered contracts have properly parsed message body when a message is sent'() { + given: + StubRunnerExecutor executor = new StubRunnerExecutor(portScanner, new AssertingContractVerifierMessaging()) + executor.runStubs(stubRunnerOptions, repository, stub) + when: + executor.trigger('send_order') + then: + noExceptionThrown() + } + Map stubIdsWithPortsFromString(String stubIdsToPortMapping) { return stubIdsToPortMapping.split(',').collectEntries { String entry -> return StubsParser.fromStringWithPort(entry) } } + private class AssertingContractVerifierMessaging implements ContractVerifierMessaging { + + @Override + void send(ContractVerifierMessage message, String destination) { + + } + + @Override + ContractVerifierMessage receiveMessage(String destination, long timeout, TimeUnit timeUnit) { + return null + } + + @Override + ContractVerifierMessage receiveMessage(String destination) { + return null + } + + @Override + void send(Object o, Map headers, String destination) { + + } + + @Override + ContractVerifierMessage create(Object o, Map headers) { + assert !(JsonOutput.toJson(o).contains("serverValue")) + assert headers.entrySet().every { !(it.value.toString().contains("serverValue")) } + return null + } + + @Override + ContractVerifierMessage create(Object o) { + return null + } + } + } diff --git a/spring-cloud-contract-stub-runner/spring-cloud-contract-stub-runner/src/test/resources/repository/contracts/contract1.groovy b/spring-cloud-contract-stub-runner/spring-cloud-contract-stub-runner/src/test/resources/repository/contracts/contract1.groovy new file mode 100644 index 0000000000..bd67b68b91 --- /dev/null +++ b/spring-cloud-contract-stub-runner/spring-cloud-contract-stub-runner/src/test/resources/repository/contracts/contract1.groovy @@ -0,0 +1,27 @@ +org.springframework.cloud.contract.verifier.dsl.Contract.make { + // Human readable description + description 'Sends an order message' + // Label by means of which the output message can be triggered + label 'send_order' + // input to the contract + input { + // the contract will be triggered by a method + triggeredBy('orderTrigger()') + } + // output message of the contract + outputMessage { + // destination to which the output message will be sent + sentTo('orders') + // any headers for the output message + headers { + header('contentType': 'application/json') + } + // the body of the output message + body( + orderId: value( + consumer('40058c70-891c-4176-a033-f70bad0c5f77'), + producer(regex('([0-9|a-f]*-*)*'))), + description: "This is the order description" + ) + } +} \ No newline at end of file