Rebranding (#287)

This commit is contained in:
Marcin Grzejszczak
2016-06-19 23:55:09 +02:00
committed by GitHub
parent a562775980
commit 817c232d3f
747 changed files with 13363 additions and 6399 deletions

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
repositories {
mavenLocal()
jcenter()
maven {
url "http://repo.spring.io/snapshot"
}
maven {
url "http://repo.spring.io/milestone"
}
}
String verifier = "spring-cloud-contract-verifier"
dependencies {
compile project(":$verifier-root:$verifier-core")
compile "org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}"
compile "org.springframework:spring-jms:${springVersion}"
compile 'org.apache.activemq:activemq-broker:5.12.1'
compile 'org.apache.activemq:activemq-pool:5.12.1'
testCompile project(":$verifier-root:$verifier-messaging-root:$verifier-messaging-core")
testCompile "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
testCompile('org.spockframework:spock-spring:1.0-groovy-2.4') {
exclude(group: 'org.codehaus.groovy')
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.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
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.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)
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.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
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.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)
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.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)
}
}

View File

@@ -0,0 +1,158 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.samples.spring
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import com.toomuchcoding.jsonassert.JsonAssertion
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.SpringApplicationContextLoader
import org.springframework.cloud.contract.verifier.dsl.Contract
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierObjectMapper
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 ContractVerifierMessaging contractVerifierMessaging
ContractVerifierObjectMapper contractVerifierObjectMapper = new ContractVerifierObjectMapper()
def "should work for triggered based messaging"() {
given:
def dsl = Contract.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 = contractVerifierMessaging.receiveMessage('output')
response.headers.get('BOOK-NAME') == 'foo'
and:
DocumentContext parsedJson = JsonPath.parse(contractVerifierObjectMapper.writeValueAsString(response.payload))
JsonAssertion.assertThat(parsedJson).field('bookName').isEqualTo('foo')
}
def "should generate tests triggered by a message"() {
given:
def dsl = Contract.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:
ContractVerifierMessage inputMessage = contractVerifierMessaging.create(
contractVerifierObjectMapper.writeValueAsString([bookName: 'foo']),
[sample: 'header']
)
when:
contractVerifierMessaging.send(inputMessage, 'input')
then:
def response = contractVerifierMessaging.receiveMessage('output')
response.headers.get('BOOK-NAME') == 'foo'
and:
DocumentContext parsedJson = JsonPath.parse(contractVerifierObjectMapper.writeValueAsString(response.payload))
JsonAssertion.assertThat(parsedJson).field('bookName').isEqualTo('foo')
}
def "should generate tests without destination, triggered by a message"() {
given:
def dsl = Contract.make {
label 'some_label'
input {
messageFrom('delete')
messageBody([
bookName: 'foo'
])
messageHeaders {
header('sample', 'header')
}
assertThat('bookWasDeleted()')
}
}
// generated test should look like this:
//given:
ContractVerifierMessage inputMessage = contractVerifierMessaging.create(
contractVerifierObjectMapper.writeValueAsString([bookName: 'foo']),
[sample: 'header']
)
when:
contractVerifierMessaging.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()
}
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.samples.spring.config
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessageBuilder
import org.springframework.jms.core.MessageCreator
import javax.jms.JMSException
import javax.jms.Message
import javax.jms.Session
/**
* @author Marcin Grzejszczak
*/
public class ContractVerifierSpringMessageBuilder<T extends Serializable> implements ContractVerifierMessageBuilder<T, Message> {
@Override
public ContractVerifierMessage<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 ContractVerifierMessage<T, Message> create(Message message) {
return new SpringMessage<>(message);
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.samples.spring.config
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessageBuilder
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging
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 ContractVerifierSpringMessaging<T> implements ContractVerifierMessaging<T, Message> {
private static final Logger log = LoggerFactory.getLogger(ContractVerifierSpringMessaging.class);
private final JmsTemplate jmsTemplate;
private final ContractVerifierMessageBuilder builder;
@Autowired
@SuppressWarnings("unchecked")
public ContractVerifierSpringMessaging(ContractVerifierMessageBuilder builder, JmsTemplate jmsTemplate) {
this.builder = builder
this.jmsTemplate = jmsTemplate
}
@Override
@SuppressWarnings("unchecked")
public void send(T payload, Map<String, Object> headers, String destination) {
send(builder.create(payload, headers), destination);
}
@Override
public void send(ContractVerifierMessage<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 ContractVerifierMessage<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 ContractVerifierMessage<T, Message> receiveMessage(String destination) {
return receiveMessage(destination, 5, TimeUnit.SECONDS);
}
@Override
@SuppressWarnings("unchecked")
public ContractVerifierMessage<T, Message> create(T t, Map<String, Object> headers) {
return builder.create(t, headers);
}
@Override
@SuppressWarnings("unchecked")
public ContractVerifierMessage<T, Message> create(Message message) {
return builder.create(message);
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.samples.spring.config
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.jms.core.JmsTemplate
/**
* @author Marcin Grzejszczak
*/
@Configuration
class ManualContractVerifierConfiguration {
@Bean
ContractVerifierMessaging contractVerifierMessaging(JmsTemplate jmsTemplate) {
return new ContractVerifierSpringMessaging(new ContractVerifierSpringMessageBuilder(), jmsTemplate);
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.samples.spring.config
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage
import org.springframework.jms.core.MessageCreator
import javax.jms.Message
import javax.jms.ObjectMessage
/**
* @author Marcin Grzejszczak
*/
public class SpringMessage<T> implements ContractVerifierMessage<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;
}
}