Added AccurestObjectMapper

This commit is contained in:
Marcin Grzejszczak
2016-04-25 12:40:51 +02:00
parent f1a7975bce
commit 941a7bd30c
13 changed files with 76 additions and 71 deletions

View File

@@ -123,12 +123,14 @@ class ClassBuilder {
clazz.addEmptyLine()
}
clazz.startBlock()
fields.sort().each {
clazz.addLine(it)
}
if (!fields.empty) {
clazz.addEmptyLine()
}
clazz.endBlock()
methods.each {
clazz.addBlock(it)

View File

@@ -69,6 +69,7 @@ class JUnitMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
@Override
protected void validateResponseHeadersBlock(BlockBuilder bb) {
bb.addLine("""AccurestMessage response = accurestMessaging.receiveMessage("${outputMessage.sentTo}");""")
bb.addLine("""assertThat(response).isNotNull();""")
outputMessage.headers?.collect { Header header ->\
processHeaderElement(bb, header.name, header.serverValue)
}
@@ -114,13 +115,13 @@ class JUnitMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
protected String getInputString() {
String request = 'AccurestMessage inputMessage = accurestMessaging.create('
if (inputMessage.messageBody) {
request = "${request}\n\t\t\t\"${StringEscapeUtils.escapeJava(bodyAsString)}\"\n\t\t"
request = "${request}\n \"${StringEscapeUtils.escapeJava(bodyAsString)}\"\n "
}
if (inputMessage.messageHeaders) {
request = "${request}, headers()\n"
}
inputMessage.messageHeaders?.collect { Header header ->
request = "${request}\t\t\t${getHeaderString(header)}"
request = "${request} ${getHeaderString(header)}"
}
return "${request})"
}

View File

@@ -63,6 +63,7 @@ class SpockMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
protected void validateResponseCodeBlock(BlockBuilder bb) {
if (outputMessage) {
bb.addLine("""def response = accurestMessaging.receiveMessage('${outputMessage.sentTo}')""")
bb.addLine("""assert response != null""")
} else {
bb.addLine('noExceptionThrown()')
}
@@ -114,18 +115,18 @@ class SpockMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
protected String getInputString() {
String request = 'def inputMessage = accurestMessaging.create('
if (inputMessage.messageBody) {
request = "${request}'''${bodyAsString}'''\n\t\t"
request = "${request}'''${bodyAsString}'''\n "
}
if (inputMessage.messageHeaders) {
request = "${request},[\n"
}
def headers = []
inputMessage.messageHeaders?.collect { Header header ->
headers << "\t\t\t${getHeaderString(header)}"
headers << " ${getHeaderString(header)}"
}
request = "${request}${headers.join(',\n')}"
if (inputMessage.messageHeaders) {
request = "${request}\n\t\t]"
request = "${request}\n ]"
}
return "${request})"
}

View File

@@ -30,21 +30,23 @@ def contractDsl = GroovyDsl.make {
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
stripped(test) == stripped(
String expectedMessage =
// tag::trigger_method_test[]
'''
when:
bookReturnedTriggered()
when:
bookReturnedTriggered()
then:
def response = accurestMessaging.receiveMessage('activemq:output')
assert response != null
response.getHeader('BOOK-NAME') == 'foo'
and:
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.payload))
assertThatJson(parsedJson).field("bookName").isEqualTo("foo")
then:
def response = accurestMessaging.receiveMessage('activemq:output')
response.getHeader('BOOK-NAME') == 'foo'
and:
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.payload))
assertThatJson(parsedJson).field("bookName").isEqualTo("foo")
'''
// end::trigger_method_test[]
)
stripped(test) == stripped(expectedMessage)
}
def "should work for triggered based messaging with JUnit"() {
@@ -68,25 +70,22 @@ and:
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
stripped(test) == stripped(
String expectedMessage =
// tag::trigger_method_junit_test[]
'''
// when:
bookReturnedTriggered();
// when:
bookReturnedTriggered();
// then:
AccurestMessage response = accurestMessaging.receiveMessage("activemq:output");
assertThat(response.getHeader("BOOK-NAME")).isEqualTo("foo");
// and:
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.getPayload()));
assertThatJson(parsedJson).field("bookName").isEqualTo("foo");
// then:
AccurestMessage response = accurestMessaging.receiveMessage("activemq:output");
assertThat(response).isNotNull();
assertThat(response.getHeader("BOOK-NAME")).isEqualTo("foo");
// and:
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.getPayload()));
assertThatJson(parsedJson).field("bookName").isEqualTo("foo");
'''
// end::trigger_method_junit_test[]
)
}
private String stripped(String text) {
return text.stripIndent().stripMargin().replace(' ', '').replace('\n', '')
stripped(test) == stripped(expectedMessage)
}
def "should generate tests triggered by a message for Spock"() {
@@ -120,27 +119,28 @@ def contractDsl = GroovyDsl.make {
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
stripped(test) == stripped(
String expectedMessage =
// tag::trigger_message_spock[]
'''
"""\
given:
def inputMessage = accurestMessaging.create(
\'\'\'{"bookName":"foo"}\'\'\',
['sample': 'header']
)
def inputMessage = accurestMessaging.create(
'''{"bookName":"foo"}''',
['sample': 'header']
)
when:
accurestMessaging.send(inputMessage, 'jms:input')
accurestMessaging.send(inputMessage, 'jms:input')
then:
def response = accurestMessaging.receiveMessage('jms:output')
response.getHeader('BOOK-NAME') == 'foo'
def response = accurestMessaging.receiveMessage('jms:output')
assert response !- null
response.getHeader('BOOK-NAME') == 'foo'
and:
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.payload))
assertThatJson(parsedJson).field("bookName").isEqualTo("foo")
'''
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.payload))
assertThatJson(parsedJson).field("bookName").isEqualTo("foo")
"""
// end::trigger_message_spock[]
)
stripped(test) == stripped(expectedMessage)
}
def "should generate tests triggered by a message for JUnit"() {
@@ -172,28 +172,28 @@ and:
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
stripped(test) == stripped(
// tag::trigger_message_junit[]
String expectedMessage =
// tag::trigger_message_junit[]
'''
// given:
AccurestMessage inputMessage = accurestMessaging.create(
"{\\"bookName\\":\\"foo\\"}"
"{\\"bookName\\":\\"foo\\"}"
, headers()
.header("sample", "header"));
.header("sample", "header"));
// when:
accurestMessaging.send(inputMessage, "jms:input");
// then:
AccurestMessage response = accurestMessaging.receiveMessage("jms:output");
assertThat(response).isNotNull();
assertThat(response.getHeader("BOOK-NAME")).isEqualTo("foo");
// and:
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.getPayload()));
assertThatJson(parsedJson).field("bookName").isEqualTo("foo");
'''
// end::trigger_message_junit[]
)
// end::trigger_message_junit[]
stripped(test) == stripped(expectedMessage)
}
def "should generate tests without destination, triggered by a message"() {
@@ -318,6 +318,7 @@ then:
// then:
AccurestMessage response = accurestMessaging.receiveMessage("jms:output");
assertThat(response).isNotNull();
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.getPayload()));
assertThatJson(parsedJson).field("bookName").isEqualTo("foo");
'''
@@ -363,10 +364,15 @@ then:
then:
def response = accurestMessaging.receiveMessage('jms:output')
assert response != null
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.payload))
assertThatJson(parsedJson).field("bookName").isEqualTo("foo")
"""
stripped(test) == stripped(expectedMsg)
}
private String stripped(String text) {
return text.stripIndent().stripMargin().replace(' ', '').replace('\n', '').replace('\t', '').replaceAll("\\W", "")
}
}

View File

@@ -1,4 +1,4 @@
:messaging_version: 1.0.7
:messaging_version: 1.1.0
= Accurest

View File

@@ -93,7 +93,7 @@ accurest {
- **imports** - array with imports that should be included in generated tests (for example ['org.myorg.Matchers']). By default empty array []
- **staticImports** - array with static imports that should be included in generated tests(for example ['org.myorg.Matchers.*']). By default empty array []
- **basePackageForTests** - specifies base package for all generated tests. By default set to io.codearte.accurest.tests
- **baseClassForTests** - base class for generated tests. By default `spock.lang.Specification`
- **baseClassForTests** - base class for generated tests. By default `spock.lang.Specification` if using Spock tests.
- **ruleClassForTests** - specifies Rule which should be added to generated test classes.
- **ignoredFiles** - Ant matcher allowing defining stub files for which processing should be skipped. By default empty array []
- **contractsDslDir** - directory containing contracts written using the GroovyDSL. By default `$rootDir/src/test/accurest`
@@ -237,7 +237,7 @@ To change default configuration just add `configuration` section to plugin defin
- **testMode** - defines mode for acceptance tests. By default `MockMvc` which is based on Spring's MockMvc. It can also be changed to `JaxRsClient` or to `Explicit` for real HTTP calls.
- **basePackageForTests** - specifies base package for all generated tests. By default set to `io.codearte.accurest.tests`.
- **ruleClassForTests** - specifies Rule which should be added to generated test classes.
- **baseClassForTests** - base class for generated tests. By default `spock.lang.Specification`.
- **baseClassForTests** - base class for generated tests. By default `spock.lang.Specification` if using Spock tests.
- **contractsDir** - directory containing contracts written using the GroovyDSL. By default `/src/test/accurest`.
- **testFramework** - the target test framework to be used; currently Spock and JUnit are supported with Spock being the default framework

View File

@@ -1,12 +1,12 @@
package io.codearte.accurest.samples.camel
import com.fasterxml.jackson.databind.ObjectMapper
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import com.toomuchcoding.jsonassert.JsonAssertion
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.messaging.AccurestMessage
import io.codearte.accurest.messaging.AccurestMessaging
import io.codearte.accurest.messaging.AccurestObjectMapper
import org.apache.camel.model.ModelCamelContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.SpringApplicationContextLoader
@@ -24,7 +24,7 @@ public class CamelMessagingApplicationSpec extends Specification {
// ALL CASES
@Inject AccurestMessaging accurestMessaging
ObjectMapper accurestObjectMapper = new ObjectMapper()
AccurestObjectMapper accurestObjectMapper = new AccurestObjectMapper()
def "should work for triggered based messaging"() {
given:

View File

@@ -1,26 +1,25 @@
package io.codearte.accurest.samples.messaging
import com.fasterxml.jackson.databind.ObjectMapper
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import com.toomuchcoding.jsonassert.JsonAssertion
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.messaging.AccurestMessage
import io.codearte.accurest.messaging.AccurestMessaging
import io.codearte.accurest.messaging.AccurestObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.SpringApplicationContextLoader
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification
import javax.inject.Inject
// Context configuration would end up in base class
@ContextConfiguration(classes = [IntegrationMessagingApplication], loader = SpringApplicationContextLoader)
public class IntegrationMessagingApplicationSpec extends Specification {
// ALL CASES
@Inject AccurestMessaging accurestMessaging
ObjectMapper accurestObjectMapper = new ObjectMapper()
AccurestObjectMapper accurestObjectMapper = new AccurestObjectMapper()
def "should work for triggered based messaging"() {
given:

View File

@@ -1,12 +1,12 @@
package io.codearte.accurest.samples.spring
import com.fasterxml.jackson.databind.ObjectMapper
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import com.toomuchcoding.jsonassert.JsonAssertion
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.messaging.AccurestMessage
import io.codearte.accurest.messaging.AccurestMessaging
import io.codearte.accurest.messaging.AccurestObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.SpringApplicationContextLoader
import org.springframework.test.context.ContextConfiguration
@@ -14,7 +14,6 @@ import spock.lang.Specification
import spock.util.concurrent.PollingConditions
import javax.inject.Inject
/**
* SPIKE ON TESTS FROM NOTES IN MessagingSpec
*/
@@ -24,7 +23,7 @@ public class SpringApplicationSpec extends Specification {
// ALL CASES
@Inject AccurestMessaging accurestMessaging
ObjectMapper accurestObjectMapper = new ObjectMapper()
AccurestObjectMapper accurestObjectMapper = new AccurestObjectMapper()
def "should work for triggered based messaging"() {
given:

View File

@@ -1,19 +1,18 @@
package io.codearte.accurest.samples.messaging
import com.fasterxml.jackson.databind.ObjectMapper
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import com.toomuchcoding.jsonassert.JsonAssertion
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.messaging.AccurestMessage
import io.codearte.accurest.messaging.AccurestMessaging
import io.codearte.accurest.messaging.AccurestObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.SpringApplicationContextLoader
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification
import javax.inject.Inject
/**
* SPIKE ON TESTS FROM NOTES IN MessagingSpec
*/
@@ -23,7 +22,7 @@ public class StreamMessagingApplicationSpec extends Specification {
// ALL CASES
@Inject AccurestMessaging accurestMessaging
ObjectMapper accurestObjectMapper = new ObjectMapper()
AccurestObjectMapper accurestObjectMapper = new AccurestObjectMapper()
def "should work for triggered based messaging"() {
given:

View File

@@ -1,12 +1,12 @@
package io.codearte.accurest.stubrunner.messaging.camel
import com.fasterxml.jackson.databind.ObjectMapper
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import com.toomuchcoding.jsonassert.JsonAssertion
import com.toomuchcoding.jsonassert.JsonVerifiable
import groovy.transform.PackageScope
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.messaging.AccurestObjectMapper
import io.codearte.accurest.util.JsonPaths
import io.codearte.accurest.util.JsonToJsonPathsConverter
import org.apache.camel.Exchange
@@ -23,7 +23,7 @@ import java.util.regex.Pattern
class StubRunnerCamelPredicate implements Predicate {
private final GroovyDsl groovyDsl
private final ObjectMapper objectMapper = new ObjectMapper()
private final AccurestObjectMapper objectMapper = new AccurestObjectMapper()
StubRunnerCamelPredicate(GroovyDsl groovyDsl) {
this.groovyDsl = groovyDsl

View File

@@ -1,19 +1,18 @@
package io.codearte.accurest.stubrunner.messaging.integration
import com.fasterxml.jackson.databind.ObjectMapper
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import com.toomuchcoding.jsonassert.JsonAssertion
import com.toomuchcoding.jsonassert.JsonVerifiable
import groovy.transform.CompileStatic
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.messaging.AccurestObjectMapper
import io.codearte.accurest.util.JsonPaths
import io.codearte.accurest.util.JsonToJsonPathsConverter
import org.springframework.integration.core.MessageSelector
import org.springframework.messaging.Message
import java.util.regex.Pattern
/**
* Passes through a message that matches the one defined in the DSL
*
@@ -23,7 +22,7 @@ import java.util.regex.Pattern
class StubRunnerIntegrationMessageSelector implements MessageSelector {
private final GroovyDsl groovyDsl
private final ObjectMapper objectMapper = new ObjectMapper()
private final AccurestObjectMapper objectMapper = new AccurestObjectMapper()
StubRunnerIntegrationMessageSelector(GroovyDsl groovyDsl) {
this.groovyDsl = groovyDsl

View File

@@ -1,19 +1,18 @@
package io.codearte.accurest.stubrunner.messaging.stream
import com.fasterxml.jackson.databind.ObjectMapper
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import com.toomuchcoding.jsonassert.JsonAssertion
import com.toomuchcoding.jsonassert.JsonVerifiable
import groovy.transform.CompileStatic
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.messaging.AccurestObjectMapper
import io.codearte.accurest.util.JsonPaths
import io.codearte.accurest.util.JsonToJsonPathsConverter
import org.springframework.integration.core.MessageSelector
import org.springframework.messaging.Message
import java.util.regex.Pattern
/**
* Passes through a message that matches the one defined in the DSL
*
@@ -23,7 +22,7 @@ import java.util.regex.Pattern
class StubRunnerStreamMessageSelector implements MessageSelector {
private final GroovyDsl groovyDsl
private final ObjectMapper objectMapper = new ObjectMapper()
private final AccurestObjectMapper objectMapper = new AccurestObjectMapper()
StubRunnerStreamMessageSelector(GroovyDsl groovyDsl) {
this.groovyDsl = groovyDsl