Adding tests for multiple contracts

This commit is contained in:
Marcin Grzejszczak
2016-12-07 15:10:45 +01:00
parent 02bd7af2cd
commit 84d361bae7
5 changed files with 96 additions and 14 deletions

View File

@@ -0,0 +1,39 @@
package com.example.fraud;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FraudStatsController {
private static final String FRAUD_SERVICE_JSON_VERSION_1 = "application/vnd.fraud.v1+json";
private final StatsProvider statsProvider;
public FraudStatsController(StatsProvider statsProvider) {
this.statsProvider = statsProvider;
}
@GetMapping(
value = "/frauds",
produces = FRAUD_SERVICE_JSON_VERSION_1)
public int countAllFrauds() {
return this.statsProvider.count(FraudType.ALL);
}
@GetMapping(
value = "/drunks",
produces = FRAUD_SERVICE_JSON_VERSION_1)
public int countAllDrunks() {
return this.statsProvider.count(FraudType.DRUNKS);
}
}
enum FraudType {
DRUNKS, ALL
}
interface StatsProvider {
int count(FraudType fraudType);
}

View File

@@ -1,15 +1,27 @@
package com.example.fraud;
import com.example.fraud.FraudDetectionController;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.Before;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
public class FraudBase {
@Before
public void setup() {
RestAssuredMockMvc.standaloneSetup(new FraudDetectionController());
RestAssuredMockMvc.standaloneSetup(new FraudDetectionController(),
new FraudStatsController(stubbedStatsProvider()));
}
private StatsProvider stubbedStatsProvider() {
return fraudType -> {
switch (fraudType) {
case DRUNKS:
return 100;
case ALL:
return 200;
}
return 0;
};
}
public void assertThatRejectionReasonIsNull(Object rejectionReason) {

View File

@@ -9,7 +9,7 @@ org.springframework.cloud.contract.spec.Contract.make {
loanAmount: 99999
])
headers { // (5)
header('Content-Type', 'application/vnd.fraud.v1+json')
contentType("application/vnd.fraud.v1+json")
}
}
response { // (6)
@@ -19,10 +19,7 @@ org.springframework.cloud.contract.spec.Contract.make {
rejectionReason: "Amount too high"
])
headers { // (9)
header('Content-Type': value(
producer(regex('application/vnd.fraud.v1.json.*')),
consumer('application/vnd.fraud.v1+json'))
)
contentType("application/vnd.fraud.v1+json")
}
}
}

View File

@@ -12,7 +12,7 @@ org.springframework.cloud.contract.spec.Contract.make {
"""
)
headers {
header('Content-Type', 'application/vnd.fraud.v1+json')
contentType("application/vnd.fraud.v1+json")
}
}
@@ -23,10 +23,7 @@ org.springframework.cloud.contract.spec.Contract.make {
rejectionReason: $(consumer(null), producer(execute('assertThatRejectionReasonIsNull($it)')))
)
headers {
header('Content-Type': value(
producer(regex('application/vnd.fraud.v1.json.*')),
consumer('application/vnd.fraud.v1+json'))
)
contentType("application/vnd.fraud.v1+json")
}
}

View File

@@ -0,0 +1,37 @@
package contracts
import org.springframework.cloud.contract.spec.Contract
[
Contract.make {
request {
name "should count all frauds"
method GET()
url '/frauds'
}
response {
status 200
body([
count: 200
])
headers {
contentType("application/vnd.fraud.v1+json")
}
}
},
Contract.make {
request {
method GET()
url '/drunks'
}
response {
status 200
body([
count: 100
])
headers {
contentType("application/vnd.fraud.v1+json")
}
}
}
]