Fixed object mapping

This commit is contained in:
Marcin Grzejszczak
2016-04-25 12:07:52 +02:00
parent 728278dcc7
commit f1a7975bce
3 changed files with 38 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package io.codearte.accurest
import groovy.transform.Canonical
import groovy.transform.EqualsAndHashCode
import groovy.transform.PackageScope
import groovy.util.logging.Slf4j
import io.codearte.accurest.builder.ClassBuilder
@@ -99,6 +100,7 @@ class SingleTestGenerator {
}
@Canonical
@EqualsAndHashCode
private static class ParsedDsl {
Contract contract
GroovyDsl groovyDsl
@@ -124,10 +126,10 @@ class SingleTestGenerator {
private ClassBuilder addMessagingRelatedEntries(ClassBuilder clazz) {
clazz.addField(['@Inject AccurestMessaging accurestMessaging',
'ObjectMapper accurestObjectMapper = new ObjectMapper()'
'AccurestObjectMapper accurestObjectMapper = new AccurestObjectMapper()'
])
clazz.addImport([ 'javax.inject.Inject',
'com.fasterxml.jackson.databind.ObjectMapper',
'io.codearte.accurest.messaging.AccurestObjectMapper',
'io.codearte.accurest.messaging.AccurestMessage',
'io.codearte.accurest.messaging.AccurestMessaging',
])

View File

@@ -86,7 +86,7 @@ and:
}
private String stripped(String text) {
return text.stripIndent().stripMargin().replace('\t', '').replace('\n', '')
return text.stripIndent().stripMargin().replace(' ', '').replace('\n', '')
}
def "should generate tests triggered by a message for Spock"() {
@@ -309,9 +309,9 @@ then:
'''
// given:
AccurestMessage inputMessage = accurestMessaging.create(
\t\t\t"{\\"bookName\\":\\"foo\\"}"
\t\t, headers()
\t\t\t.header("sample", "header"));
"{\\"bookName\\":\\"foo\\"}"
, headers()
.header("sample", "header"));
// when:
accurestMessaging.send(inputMessage, "jms:input");

View File

@@ -0,0 +1,30 @@
package io.codearte.accurest.messaging;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Wrapper over {@link ObjectMapper} that won't try to parse
* String but will directly return it.
*
* @author Marcin Grzejszczak
*/
public class AccurestObjectMapper {
private final ObjectMapper objectMapper;
public AccurestObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public AccurestObjectMapper() {
this.objectMapper = new ObjectMapper();
}
public String writeValueAsString(Object payload) throws JsonProcessingException {
if (payload instanceof String) {
return payload.toString();
}
return objectMapper.writeValueAsString(payload);
}
}