Adding tests for multiple contracts
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user