Fixed contracts of different types in the same folder

fixes #253
This commit is contained in:
Marcin Grzejszczak
2016-04-28 13:10:08 +02:00
parent 99875748c0
commit 172007f258
8 changed files with 81 additions and 5 deletions

View File

@@ -66,7 +66,7 @@ class SingleTestGenerator {
boolean conditionalImportsAdded = false
contracts.each { ParsedDsl key, TestType value ->
if (!conditionalImportsAdded) {
if (value == TestType.HTTP) {
if (contracts.values().contains(TestType.HTTP)) {
if (configProperties.testMode == TestMode.JAXRSCLIENT) {
clazz.addStaticImport('javax.ws.rs.client.Entity.*')
if (configProperties.targetFramework == TestFramework.JUNIT) {
@@ -79,7 +79,7 @@ class SingleTestGenerator {
}
}
if (configProperties.targetFramework == TestFramework.JUNIT) {
if (value == TestType.HTTP && configProperties.testMode == TestMode.MOCKMVC) {
if (contracts.values().contains(TestType.HTTP) && configProperties.testMode == TestMode.MOCKMVC) {
clazz.addImport('com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification')
clazz.addImport('com.jayway.restassured.response.ResponseOptions')
}
@@ -89,7 +89,7 @@ class SingleTestGenerator {
if (configProperties.ruleClassForTests) {
clazz.addImport('org.junit.Rule').addRule(configProperties.ruleClassForTests)
}
if (value == TestType.MESSAGING) {
if (contracts.values().contains(TestType.MESSAGING)) {
addMessagingRelatedEntries(clazz)
}
conditionalImportsAdded = true

View File

@@ -38,7 +38,7 @@ abstract class RequestProcessingMethodBodyBuilder extends MethodBodyBuilder {
@Override
protected boolean hasGivenSection() {
return request.headers || request.body
return true
}
protected boolean allowedQueryParameter(QueryParameter param) {

View File

@@ -85,5 +85,49 @@ class SingleTestGeneratorSpec extends Specification {
SPOCK | ['import static javax.ws.rs.client.Entity.*;']
}
def "should work if there is messaging and rest in one folder #testFramework"() {
given:
File secondFile = tmpFolder.newFile()
secondFile.write("""
io.codearte.accurest.dsl.GroovyDsl.make {
label 'some_label'
input {
messageFrom('delete')
messageBody([
bookName: 'foo'
])
messageHeaders {
header('sample', 'header')
}
assertThat('bookWasDeleted()')
}
}
""")
and:
AccurestConfigProperties properties = new AccurestConfigProperties();
properties.targetFramework = testFramework
Contract contract = new Contract(file.toPath(), true, 1, 2)
contract.ignored >> true
contract.order >> 2
and:
Contract contract2 = new Contract(secondFile.toPath(), true, 1, 2)
contract2.ignored >> true
contract2.order >> 2
and:
SingleTestGenerator testGenerator = new SingleTestGenerator(properties)
when:
String clazz = testGenerator.buildClass([contract, contract2], "test", "test")
then:
classStrings.each { clazz.contains(it) }
clazz.contains('@Inject AccurestMessaging')
where:
testFramework | classStrings
JUNIT | jUnitClassStrings
SPOCK | spockClassStrings
}
}

View File

@@ -55,6 +55,7 @@ dependencies {
testCompile "org.spockframework:spock-spring:1.0-groovy-2.4"
testCompile "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
testCompile "ch.qos.logback:logback-classic:1.1.2"
testCompile 'com.jayway.restassured:spring-mock-mvc:2.9.0' // needed if you're going to use Spring MockMvc
}
accurest {
@@ -109,4 +110,4 @@ test {
testLogging {
exceptionFormat = 'full'
}
}
}

View File

@@ -0,0 +1,10 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
url '/foo'
method 'GET'
}
response {
status 200
body 'bar'
}
}

View File

@@ -3,11 +3,19 @@ package io.codearte.accurest.samples.book
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.ImportResource
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
@RestController
@ImportResource("classpath*:integration-context.xml")
class IntegrationMessagingApplication {
@RequestMapping("/foo")
String foo() {
return "bar"
}
static void main(String[] args) {
SpringApplication.run(IntegrationMessagingApplication.class, args)
}

View File

@@ -4,6 +4,8 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.SpringApplicationContextLoader
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc
/**
* @author Marcin Grzejszczak
*/
@@ -15,6 +17,10 @@ abstract class MessagingBaseSpec extends Specification {
@Autowired BookService bookService
@Autowired BookListener bookListener
def setup() {
RestAssuredMockMvc.standaloneSetup(new IntegrationMessagingApplication())
}
void bookReturnedTriggered() {
bookService.returnBook(new BookReturned("foo"))
}

View File

@@ -2,10 +2,12 @@ package io.codearte.accurest.samples.book;
import org.assertj.core.api.Assertions;
import org.junit.runner.RunWith;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationContextLoader;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
/**
* @author Marcin Grzejszczak
@@ -19,6 +21,11 @@ public abstract class MessagingBaseTest {
@Autowired BookService bookService;
@Autowired BookListener bookListener;
@Before
public void setup() {
RestAssuredMockMvc.standaloneSetup(new IntegrationMessagingApplication());
}
public void bookReturnedTriggered() {
bookService.returnBook(new BookReturned("foo"));
}