Added tests for messaging
This commit is contained in:
@@ -41,11 +41,14 @@ class StubRunnerCamelProcessor implements Processor {
|
||||
@Override
|
||||
void process(Exchange exchange) throws Exception {
|
||||
Message input = exchange.in
|
||||
input.body = BodyAsStringUtil.extractClientValueFrom(groovyDsl.outputMessage.body)
|
||||
groovyDsl.input.messageHeaders.entries.each {
|
||||
groovyDsl.input.messageHeaders?.entries?.each {
|
||||
input.removeHeader(it.name)
|
||||
}
|
||||
groovyDsl.outputMessage.headers.entries.each {
|
||||
if (!groovyDsl.outputMessage) {
|
||||
return
|
||||
}
|
||||
input.body = BodyAsStringUtil.extractStubValueFrom(groovyDsl.outputMessage.body)
|
||||
groovyDsl.outputMessage.headers?.entries?.each {
|
||||
input.setHeader(it.name, it.clientValue)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.springframework.cloud.contract.stubrunner.messaging.camel
|
||||
|
||||
import org.apache.camel.CamelContext
|
||||
import org.apache.camel.Exchange
|
||||
import org.apache.camel.builder.ExchangeBuilder
|
||||
import org.apache.camel.spring.SpringCamelContext
|
||||
import org.springframework.cloud.contract.verifier.dsl.Contract
|
||||
import spock.lang.Specification
|
||||
|
||||
class StubRunnerCamelProcessorSpec extends Specification {
|
||||
|
||||
CamelContext camelContext = new SpringCamelContext()
|
||||
Exchange message = ExchangeBuilder.anExchange(camelContext).build()
|
||||
|
||||
def noOutputMessageContract = Contract.make {
|
||||
label 'return_book_2'
|
||||
input {
|
||||
messageFrom('bookStorage')
|
||||
messageBody([
|
||||
bookId: $(consumer(regex('[0-9]+')), producer('123'))
|
||||
])
|
||||
messageHeaders {
|
||||
header('sample', 'header')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def 'should not process the message if there is no output message'() {
|
||||
given:
|
||||
StubRunnerCamelProcessor processor = new StubRunnerCamelProcessor(noOutputMessageContract)
|
||||
when:
|
||||
processor.process(message)
|
||||
then:
|
||||
noExceptionThrown()
|
||||
}
|
||||
|
||||
def dsl = Contract.make {
|
||||
label 'return_book_2'
|
||||
input {
|
||||
messageFrom('bookStorage')
|
||||
messageBody([
|
||||
bookId: $(consumer(regex('[0-9]+')), producer('123'))
|
||||
])
|
||||
messageHeaders {
|
||||
header('sample', 'header')
|
||||
}
|
||||
}
|
||||
outputMessage {
|
||||
sentTo('returnBook')
|
||||
body([
|
||||
responseId: $(producer(regex('[0-9]+')), consumer('123'))
|
||||
])
|
||||
headers {
|
||||
header('BOOK-NAME', 'foo')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def 'should process message when it has an output message section'() {
|
||||
given:
|
||||
StubRunnerCamelProcessor processor = new StubRunnerCamelProcessor(dsl)
|
||||
when:
|
||||
processor.process(message)
|
||||
then:
|
||||
message.getIn().getBody(String) == '{"responseId":"123"}'
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class StubRunnerIntegrationTransformer implements GenericTransformer<Message<?>,
|
||||
if (!groovyDsl.outputMessage) {
|
||||
return source
|
||||
}
|
||||
String payload = BodyAsStringUtil.extractClientValueFrom(groovyDsl.outputMessage.body)
|
||||
String payload = BodyAsStringUtil.extractStubValueFrom(groovyDsl.outputMessage.body)
|
||||
Map<String, Object> headers = groovyDsl.outputMessage.headers.asStubSideMap()
|
||||
return MessageBuilder.createMessage(payload, new MessageHeaders(headers))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.springframework.cloud.contract.stubrunner.messaging.integration
|
||||
|
||||
import org.springframework.cloud.contract.verifier.dsl.Contract
|
||||
import org.springframework.messaging.Message
|
||||
import org.springframework.messaging.support.MessageBuilder
|
||||
import spock.lang.Specification
|
||||
|
||||
class StubRunnerIntegrationTransformerSpec extends Specification {
|
||||
|
||||
Message message = MessageBuilder.withPayload("hello").build()
|
||||
|
||||
def noOutputMessageContract = Contract.make {
|
||||
label 'return_book_2'
|
||||
input {
|
||||
messageFrom('bookStorage')
|
||||
messageBody([
|
||||
bookId: $(consumer(regex('[0-9]+')), producer('123'))
|
||||
])
|
||||
messageHeaders {
|
||||
header('sample', 'header')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def 'should not transform the message if there is no output message'() {
|
||||
given:
|
||||
StubRunnerIntegrationTransformer transformer = new StubRunnerIntegrationTransformer(noOutputMessageContract)
|
||||
when:
|
||||
def result = transformer.transform(message)
|
||||
then:
|
||||
result.is(message)
|
||||
}
|
||||
|
||||
def dsl = Contract.make {
|
||||
label 'return_book_2'
|
||||
input {
|
||||
messageFrom('bookStorage')
|
||||
messageBody([
|
||||
bookId: $(consumer(regex('[0-9]+')), producer('123'))
|
||||
])
|
||||
messageHeaders {
|
||||
header('sample', 'header')
|
||||
}
|
||||
}
|
||||
outputMessage {
|
||||
sentTo('returnBook')
|
||||
body([
|
||||
responseId: $(producer(regex('[0-9]+')), consumer('123'))
|
||||
])
|
||||
headers {
|
||||
header('BOOK-NAME', 'foo')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def 'should convert dsl into message'() {
|
||||
given:
|
||||
StubRunnerIntegrationTransformer transformer = new StubRunnerIntegrationTransformer(dsl)
|
||||
when:
|
||||
def result = transformer.transform(message)
|
||||
then:
|
||||
result.payload == '{"responseId":"123"}'
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class StubRunnerStreamTransformer implements GenericTransformer<Message<?>, Mess
|
||||
if (!groovyDsl.outputMessage) {
|
||||
return source
|
||||
}
|
||||
String payload = BodyAsStringUtil.extractClientValueFrom(groovyDsl.outputMessage.body)
|
||||
String payload = BodyAsStringUtil.extractStubValueFrom(groovyDsl.outputMessage.body)
|
||||
Map<String, Object> headers = groovyDsl.outputMessage.headers.asStubSideMap()
|
||||
return MessageBuilder.createMessage(payload, new MessageHeaders(headers))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.springframework.cloud.contract.stubrunner.messaging.stream
|
||||
|
||||
import org.springframework.cloud.contract.verifier.dsl.Contract
|
||||
import org.springframework.messaging.Message
|
||||
import org.springframework.messaging.support.MessageBuilder
|
||||
import spock.lang.Specification
|
||||
|
||||
class StubRunnerStreamTransformerSpec extends Specification {
|
||||
|
||||
Message message = MessageBuilder.withPayload("hello").build()
|
||||
|
||||
def noOutputMessageContract = Contract.make {
|
||||
label 'return_book_2'
|
||||
input {
|
||||
messageFrom('bookStorage')
|
||||
messageBody([
|
||||
bookId: $(consumer(regex('[0-9]+')), producer('123'))
|
||||
])
|
||||
messageHeaders {
|
||||
header('sample', 'header')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def 'should not transform the message if there is no output message'() {
|
||||
given:
|
||||
StubRunnerStreamTransformer streamTransformer = new StubRunnerStreamTransformer(noOutputMessageContract)
|
||||
when:
|
||||
def result = streamTransformer.transform(message)
|
||||
then:
|
||||
result.is(message)
|
||||
}
|
||||
|
||||
def dsl = Contract.make {
|
||||
label 'return_book_2'
|
||||
input {
|
||||
messageFrom('bookStorage')
|
||||
messageBody([
|
||||
bookId: $(consumer(regex('[0-9]+')), producer('123'))
|
||||
])
|
||||
messageHeaders {
|
||||
header('sample', 'header')
|
||||
}
|
||||
}
|
||||
outputMessage {
|
||||
sentTo('returnBook')
|
||||
body([
|
||||
responseId: $(producer(regex('[0-9]+')), consumer('123'))
|
||||
])
|
||||
headers {
|
||||
header('BOOK-NAME', 'foo')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def 'should convert dsl into message'() {
|
||||
given:
|
||||
StubRunnerStreamTransformer streamTransformer = new StubRunnerStreamTransformer(dsl)
|
||||
when:
|
||||
def result = streamTransformer.transform(message)
|
||||
then:
|
||||
result.payload == '{"responseId":"123"}'
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ class BodyAsStringUtil {
|
||||
* That means that all the interpolations etc. will be resolved for the
|
||||
* server side.
|
||||
*/
|
||||
static String extractServerValueFrom(Object body) {
|
||||
static String extractTestValueFrom(Object body) {
|
||||
Object bodyValue = extractServerValueFromBody(body)
|
||||
String json = new JsonOutput().toJson(bodyValue)
|
||||
json = StringEscapeUtils.unescapeJavaScript(json)
|
||||
@@ -52,7 +52,7 @@ class BodyAsStringUtil {
|
||||
* That means that all the interpolations etc. will be resolved for the
|
||||
* client side.
|
||||
*/
|
||||
static String extractClientValueFrom(Object body) {
|
||||
static String extractStubValueFrom(Object body) {
|
||||
Object bodyValue = extractClientValueFromBody(body);
|
||||
String json = new JsonOutput().toJson(bodyValue)
|
||||
json = StringEscapeUtils.unescapeJavaScript(json)
|
||||
|
||||
Reference in New Issue
Block a user