@@ -0,0 +1,14 @@
|
||||
package io.codearte.accurest.samples.spring
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator
|
||||
import groovy.transform.CompileStatic
|
||||
|
||||
@CompileStatic
|
||||
class BookDeleted implements Serializable {
|
||||
final String bookName
|
||||
|
||||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
|
||||
BookDeleted(String bookName) {
|
||||
this.bookName = bookName
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package io.codearte.accurest.samples.spring
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.util.logging.Slf4j
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.jms.annotation.JmsListener
|
||||
import org.springframework.jms.core.JmsTemplate
|
||||
import org.springframework.jms.core.MessageCreator
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
import javax.jms.JMSException
|
||||
import javax.jms.Message
|
||||
import javax.jms.Session
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
@CompileStatic
|
||||
@Slf4j
|
||||
@Service
|
||||
class BookListener {
|
||||
|
||||
@Autowired JmsTemplate jmsTemplate
|
||||
ObjectMapper objectMapper = new ObjectMapper()
|
||||
|
||||
/**
|
||||
Scenario for "should generate tests triggered by a message":
|
||||
client side: if sends a message to input.messageFrom then message will be sent to output.messageFrom
|
||||
server side: will send a message to input, verify the message contents and await upon receiving message on the output messageFrom
|
||||
*/
|
||||
@JmsListener(destination = "input")
|
||||
void returnBook(String messageAsString) {
|
||||
BookReturned bookReturned = objectMapper.readerFor(BookReturned).readValue(messageAsString) as BookReturned
|
||||
log.info("Returning book [$bookReturned]")
|
||||
MessageCreator messageCreator = new MessageCreator() {
|
||||
@Override
|
||||
public Message createMessage(Session session) throws JMSException {
|
||||
Message message = session.createObjectMessage(bookReturned);
|
||||
message.setStringProperty('BOOK-NAME', bookReturned.bookName)
|
||||
return message
|
||||
}
|
||||
};
|
||||
jmsTemplate.send('output', messageCreator)
|
||||
}
|
||||
|
||||
/**
|
||||
Scenario for "should generate tests triggered by a message":
|
||||
client side: if sends a message to input.messageFrom then message will be sent to output.messageFrom
|
||||
server side: will send a message to input, verify the message contents and await upon receiving message on the output messageFrom
|
||||
*/
|
||||
@JmsListener(destination = "delete")
|
||||
void bookDeleted(String bookDeletedAsString) {
|
||||
BookDeleted bookDeleted = objectMapper.readerFor(BookDeleted).readValue(bookDeletedAsString) as BookDeleted
|
||||
log.info("Deleting book [$bookDeleted]")
|
||||
bookSuccessfulyDeleted.set(true)
|
||||
}
|
||||
|
||||
AtomicBoolean bookSuccessfulyDeleted = new AtomicBoolean(false)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.codearte.accurest.samples.spring
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator
|
||||
import groovy.transform.CompileStatic
|
||||
|
||||
@CompileStatic
|
||||
class BookReturned implements Serializable {
|
||||
String bookName
|
||||
|
||||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
|
||||
BookReturned(String bookName) {
|
||||
this.bookName = bookName
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.codearte.accurest.samples.spring
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.util.logging.Slf4j
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.jms.core.JmsTemplate
|
||||
import org.springframework.jms.core.MessageCreator
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
import javax.jms.JMSException
|
||||
import javax.jms.Message
|
||||
import javax.jms.Session
|
||||
|
||||
@Service
|
||||
@CompileStatic
|
||||
@Slf4j
|
||||
class BookService {
|
||||
|
||||
@Autowired JmsTemplate jmsTemplate
|
||||
|
||||
/**
|
||||
Scenario for "should generate tests triggered by a method":
|
||||
client side: must have a possibility to "trigger" sending of a message to the given messageFrom
|
||||
server side: will run the method and await upon receiving message on the output messageFrom
|
||||
|
||||
Method triggers sending a message to a source
|
||||
*/
|
||||
void returnBook(BookReturned bookReturned ) {
|
||||
log.info("Returning book [$bookReturned]")
|
||||
MessageCreator messageCreator = new MessageCreator() {
|
||||
@Override
|
||||
public Message createMessage(Session session) throws JMSException {
|
||||
Message message = session.createObjectMessage(bookReturned);
|
||||
message.setStringProperty('BOOK-NAME', bookReturned.bookName)
|
||||
return message
|
||||
}
|
||||
};
|
||||
jmsTemplate.send('output', messageCreator)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.codearte.accurest.samples.spring
|
||||
|
||||
import org.springframework.boot.SpringApplication
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
import org.springframework.jms.annotation.EnableJms
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableJms
|
||||
class SpringMessagingApplication {
|
||||
|
||||
static void main(String[] args) {
|
||||
SpringApplication.run(SpringMessagingApplication.class, args)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
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 org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.SpringApplicationContextLoader
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
import spock.lang.Specification
|
||||
import spock.util.concurrent.PollingConditions
|
||||
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* SPIKE ON TESTS FROM NOTES IN MessagingSpec
|
||||
*/
|
||||
// Context configuration would end up in base class
|
||||
@ContextConfiguration(classes = [SpringMessagingApplication], loader = SpringApplicationContextLoader)
|
||||
public class SpringApplicationSpec extends Specification {
|
||||
|
||||
// ALL CASES
|
||||
@Inject AccurestMessaging accurestMessaging
|
||||
ObjectMapper accurestObjectMapper = new ObjectMapper()
|
||||
|
||||
def "should work for triggered based messaging"() {
|
||||
given:
|
||||
def dsl = GroovyDsl.make {
|
||||
label 'some_label'
|
||||
input {
|
||||
triggeredBy('bookReturnedTriggered()')
|
||||
}
|
||||
outputMessage {
|
||||
sentTo('output')
|
||||
body('''{ "bookName" : "foo" }''')
|
||||
headers {
|
||||
header('BOOK-NAME', 'foo')
|
||||
}
|
||||
}
|
||||
}
|
||||
// generated test should look like this:
|
||||
when:
|
||||
bookReturnedTriggered()
|
||||
then:
|
||||
def response = accurestMessaging.receiveMessage('output')
|
||||
response.headers.get('BOOK-NAME') == 'foo'
|
||||
and:
|
||||
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.payload))
|
||||
JsonAssertion.assertThat(parsedJson).field('bookName').isEqualTo('foo')
|
||||
}
|
||||
|
||||
def "should generate tests triggered by a message"() {
|
||||
given:
|
||||
def dsl = GroovyDsl.make {
|
||||
label 'some_label'
|
||||
input {
|
||||
messageFrom('input')
|
||||
messageBody([
|
||||
bookName: 'foo'
|
||||
])
|
||||
messageHeaders {
|
||||
header('sample', 'header')
|
||||
}
|
||||
}
|
||||
outputMessage {
|
||||
sentTo('output')
|
||||
body([
|
||||
bookName: 'foo'
|
||||
])
|
||||
headers {
|
||||
header('BOOK-NAME', 'foo')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// generated test should look like this:
|
||||
|
||||
//given:
|
||||
AccurestMessage inputMessage = accurestMessaging.create(
|
||||
accurestObjectMapper.writeValueAsString([bookName: 'foo']),
|
||||
[sample: 'header']
|
||||
)
|
||||
when:
|
||||
accurestMessaging.send(inputMessage, 'input')
|
||||
then:
|
||||
def response = accurestMessaging.receiveMessage('output')
|
||||
response.headers.get('BOOK-NAME') == 'foo'
|
||||
and:
|
||||
DocumentContext parsedJson = JsonPath.parse(accurestObjectMapper.writeValueAsString(response.payload))
|
||||
JsonAssertion.assertThat(parsedJson).field('bookName').isEqualTo('foo')
|
||||
}
|
||||
|
||||
def "should generate tests without destination, triggered by a message"() {
|
||||
given:
|
||||
def dsl = GroovyDsl.make {
|
||||
label 'some_label'
|
||||
input {
|
||||
messageFrom('delete')
|
||||
messageBody([
|
||||
bookName: 'foo'
|
||||
])
|
||||
messageHeaders {
|
||||
header('sample', 'header')
|
||||
}
|
||||
assertThat('bookWasDeleted()')
|
||||
}
|
||||
}
|
||||
|
||||
// generated test should look like this:
|
||||
|
||||
//given:
|
||||
AccurestMessage inputMessage = accurestMessaging.create(
|
||||
accurestObjectMapper.writeValueAsString([bookName: 'foo']),
|
||||
[sample: 'header']
|
||||
)
|
||||
when:
|
||||
accurestMessaging.send(inputMessage, 'delete')
|
||||
then:
|
||||
noExceptionThrown()
|
||||
bookWasDeleted()
|
||||
}
|
||||
|
||||
// BASE CLASS WOULD HAVE THIS:
|
||||
|
||||
@Autowired BookService bookService
|
||||
@Autowired BookListener bookListener
|
||||
|
||||
void bookReturnedTriggered() {
|
||||
bookService.returnBook(new BookReturned("foo"))
|
||||
}
|
||||
|
||||
PollingConditions pollingConditions = new PollingConditions()
|
||||
|
||||
void bookWasDeleted() {
|
||||
pollingConditions.eventually {
|
||||
assert bookListener.bookSuccessfulyDeleted.get()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package io.codearte.accurest.samples.spring.config
|
||||
|
||||
import io.codearte.accurest.messaging.AccurestMessage
|
||||
import io.codearte.accurest.messaging.AccurestMessageBuilder
|
||||
import org.springframework.jms.core.MessageCreator
|
||||
|
||||
import javax.jms.JMSException
|
||||
import javax.jms.Message
|
||||
import javax.jms.Session
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class AccurestSpringMessageBuilder<T extends Serializable> implements AccurestMessageBuilder<T, Message> {
|
||||
|
||||
@Override
|
||||
public AccurestMessage<T, Message> create(final T payload, final Map<String, Object> headers) {
|
||||
MessageCreator messageCreator = new MessageCreator() {
|
||||
@Override
|
||||
public Message createMessage(Session session) throws JMSException {
|
||||
Message message = session.createObjectMessage(payload);
|
||||
headers.entrySet().each { Map.Entry<String, Object> entry ->
|
||||
message.setObjectProperty(entry.key, entry.value)
|
||||
}
|
||||
return message
|
||||
}
|
||||
};
|
||||
|
||||
return new SpringMessage<>(messageCreator)
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccurestMessage<T, Message> create(Message message) {
|
||||
return new SpringMessage<>(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package io.codearte.accurest.samples.spring.config
|
||||
|
||||
import io.codearte.accurest.messaging.AccurestMessage
|
||||
import io.codearte.accurest.messaging.AccurestMessageBuilder
|
||||
import io.codearte.accurest.messaging.AccurestMessaging
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.jms.core.JmsTemplate
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
import javax.jms.Message
|
||||
import java.util.concurrent.TimeUnit
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@Component
|
||||
public class AccurestSpringMessaging<T> implements AccurestMessaging<T, Message> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AccurestSpringMessaging.class);
|
||||
|
||||
private final JmsTemplate jmsTemplate;
|
||||
private final AccurestMessageBuilder builder;
|
||||
|
||||
@Autowired
|
||||
@SuppressWarnings("unchecked")
|
||||
public AccurestSpringMessaging(AccurestMessageBuilder builder, JmsTemplate jmsTemplate) {
|
||||
this.builder = builder
|
||||
this.jmsTemplate = jmsTemplate
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(AccurestMessage<T, Message> message, String destination) {
|
||||
try {
|
||||
jmsTemplate.send(destination, ((SpringMessage) message).messageCreator)
|
||||
} catch (Exception e) {
|
||||
log.error("Exception occurred while trying to send a message [" + message + "] " +
|
||||
"to a channel with name [" + destination + "]", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public AccurestMessage<T, Message> receiveMessage(String destination, long timeout, TimeUnit timeUnit) {
|
||||
try {
|
||||
return builder.create(jmsTemplate.receive(destination));
|
||||
} catch (Exception e) {
|
||||
log.error("Exception occurred while trying to read a message from " +
|
||||
" a channel with name [" + destination + "]", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccurestMessage<T, Message> receiveMessage(String destination) {
|
||||
return receiveMessage(destination, 5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public AccurestMessage<T, Message> create(T t, Map<String, Object> headers) {
|
||||
return builder.create(t, headers);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public AccurestMessage<T, Message> create(Message message) {
|
||||
return builder.create(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.codearte.accurest.samples.spring.config
|
||||
|
||||
import io.codearte.accurest.messaging.AccurestMessaging
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.jms.core.JmsTemplate
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@Configuration
|
||||
class ManualAccurestConfiguration {
|
||||
|
||||
@Bean
|
||||
AccurestMessaging accurestMessaging(JmsTemplate jmsTemplate) {
|
||||
return new AccurestSpringMessaging(new AccurestSpringMessageBuilder(), jmsTemplate);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.codearte.accurest.samples.spring.config
|
||||
|
||||
import io.codearte.accurest.messaging.AccurestMessage
|
||||
import org.springframework.jms.core.MessageCreator
|
||||
|
||||
import javax.jms.Message
|
||||
import javax.jms.ObjectMessage
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class SpringMessage<T> implements AccurestMessage<T, Message> {
|
||||
|
||||
private final ObjectMessage messageDelegate;
|
||||
final MessageCreator messageCreator;
|
||||
|
||||
public SpringMessage(Message messageDelegate) {
|
||||
this.messageDelegate = (ObjectMessage) messageDelegate;
|
||||
this.messageCreator = null
|
||||
}
|
||||
|
||||
public SpringMessage(MessageCreator messageCreator) {
|
||||
this.messageDelegate = null;
|
||||
this.messageCreator = messageCreator
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getPayload() {
|
||||
return messageDelegate.getObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getHeaders() {
|
||||
Map<String, Object> headers = [:]
|
||||
def enumeration = messageDelegate.propertyNames
|
||||
while(enumeration.hasMoreElements()) {
|
||||
String propertyName = enumeration.nextElement()
|
||||
headers << [(propertyName) : messageDelegate.getObjectProperty(propertyName)]
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
@Override
|
||||
Object getHeader(String key) {
|
||||
return getHeaders().get(key)
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message convert() {
|
||||
return messageDelegate;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user