Simplify samples used in documentation (#268)
This commit is contained in:
committed by
Marcin Grzejszczak
parent
3c8b712aa2
commit
7c8a8b4d33
@@ -5,6 +5,7 @@ import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
@@ -20,9 +21,6 @@ import com.example.loan.model.Response;
|
||||
@Service
|
||||
public class LoanApplicationService {
|
||||
|
||||
private static final String FRAUD_SERVICE_JSON_VERSION_1 =
|
||||
"application/vnd.fraud.v1+json";
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
private int port = 6565;
|
||||
@@ -45,7 +43,7 @@ public class LoanApplicationService {
|
||||
private FraudServiceResponse sendRequestToFraudDetectionService(
|
||||
FraudServiceRequest request) {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, FRAUD_SERVICE_JSON_VERSION_1);
|
||||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
|
||||
// tag::client_call_server[]
|
||||
ResponseEntity<FraudServiceResponse> response =
|
||||
@@ -70,7 +68,7 @@ public class LoanApplicationService {
|
||||
|
||||
public int countAllFrauds() {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, FRAUD_SERVICE_JSON_VERSION_1);
|
||||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
ResponseEntity<Response> response =
|
||||
restTemplate.exchange("http://localhost:" + port + "/frauds", HttpMethod.GET,
|
||||
new HttpEntity<>(httpHeaders),
|
||||
@@ -80,7 +78,7 @@ public class LoanApplicationService {
|
||||
|
||||
public int countDrunks() {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, FRAUD_SERVICE_JSON_VERSION_1);
|
||||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
ResponseEntity<Response> response =
|
||||
restTemplate.exchange("http://localhost:" + port + "/drunks", HttpMethod.GET,
|
||||
new HttpEntity<>(httpHeaders),
|
||||
|
||||
@@ -15,17 +15,12 @@ import static org.springframework.web.bind.annotation.RequestMethod.PUT;
|
||||
@RestController
|
||||
public class FraudDetectionController {
|
||||
|
||||
private static final String FRAUD_SERVICE_JSON_VERSION_1 = "application/vnd.fraud.v1+json";
|
||||
private static final String NO_REASON = null;
|
||||
private static final String AMOUNT_TOO_HIGH = "Amount too high";
|
||||
private static final BigDecimal MAX_AMOUNT = new BigDecimal("5000");
|
||||
|
||||
// tag::server_api[]
|
||||
@RequestMapping(
|
||||
value = "/fraudcheck",
|
||||
method = PUT,
|
||||
consumes = FRAUD_SERVICE_JSON_VERSION_1,
|
||||
produces = FRAUD_SERVICE_JSON_VERSION_1)
|
||||
@RequestMapping(value = "/fraudcheck", method = PUT)
|
||||
public FraudCheckResult fraudCheck(@RequestBody FraudCheck fraudCheck) {
|
||||
// end::server_api[]
|
||||
// tag::new_impl[]
|
||||
|
||||
@@ -10,17 +10,13 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
class FraudNameController {
|
||||
|
||||
private static final String FRAUD_SERVICE_JSON_VERSION_1 = "application/vnd.fraud.v1+json";
|
||||
|
||||
private final FraudVerifier fraudVerifier;
|
||||
|
||||
FraudNameController(FraudVerifier fraudVerifier) {
|
||||
this.fraudVerifier = fraudVerifier;
|
||||
}
|
||||
|
||||
@PutMapping(
|
||||
value = "/frauds/name",
|
||||
produces = FRAUD_SERVICE_JSON_VERSION_1)
|
||||
@PutMapping(value = "/frauds/name")
|
||||
public NameResponse checkByName(@RequestBody NameRequest request) {
|
||||
boolean fraud = this.fraudVerifier.isFraudByName(request.getName());
|
||||
if (fraud) {
|
||||
|
||||
@@ -6,24 +6,18 @@ 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)
|
||||
@GetMapping(value = "/frauds")
|
||||
public Response countAllFrauds() {
|
||||
return new Response(this.statsProvider.count(FraudType.ALL));
|
||||
}
|
||||
|
||||
@GetMapping(
|
||||
value = "/drunks",
|
||||
produces = FRAUD_SERVICE_JSON_VERSION_1)
|
||||
@GetMapping(value = "/drunks")
|
||||
public Response countAllDrunks() {
|
||||
return new Response(this.statsProvider.count(FraudType.DRUNKS));
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ org.springframework.cloud.contract.spec.Contract.make {
|
||||
loanAmount: 99999
|
||||
])
|
||||
headers { // (5)
|
||||
contentType('application/vnd.fraud.v1+json')
|
||||
contentType('application/json')
|
||||
}
|
||||
}
|
||||
response { // (6)
|
||||
@@ -19,7 +19,7 @@ org.springframework.cloud.contract.spec.Contract.make {
|
||||
rejectionReason: "Amount too high"
|
||||
])
|
||||
headers { // (9)
|
||||
contentType('application/vnd.fraud.v1+json')
|
||||
contentType('application/json')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,12 +42,12 @@ From the Consumer perspective, when shooting a request in the integration test:
|
||||
(4) - with the JSON body that
|
||||
* has a field `clientId` that matches a regular expression `[0-9]{10}`
|
||||
* has a field `loanAmount` that is equal to `99999`
|
||||
(5) - with header `Content-Type` equal to `application/vnd.fraud.v1+json`
|
||||
(5) - with header `Content-Type` equal to `application/json`
|
||||
(6) - then the response will be sent with
|
||||
(7) - status equal `200`
|
||||
(8) - and JSON body equal to
|
||||
{ "fraudCheckStatus": "FRAUD", "rejectionReason": "Amount too high" }
|
||||
(9) - with header `Content-Type` equal to `application/vnd.fraud.v1+json`
|
||||
(9) - with header `Content-Type` equal to `application/json`
|
||||
|
||||
From the Producer perspective, in the autogenerated producer-side test:
|
||||
|
||||
@@ -57,10 +57,10 @@ From the Producer perspective, in the autogenerated producer-side test:
|
||||
(4) - with the JSON body that
|
||||
* has a field `clientId` that will have a generated value that matches a regular expression `[0-9]{10}`
|
||||
* has a field `loanAmount` that is equal to `99999`
|
||||
(5) - with header `Content-Type` equal to `application/vnd.fraud.v1+json`
|
||||
(5) - with header `Content-Type` equal to `application/json`
|
||||
(6) - then the test will assert if the response has been sent with
|
||||
(7) - status equal `200`
|
||||
(8) - and JSON body equal to
|
||||
{ "fraudCheckStatus": "FRAUD", "rejectionReason": "Amount too high" }
|
||||
(9) - with header `Content-Type` matching `application/vnd.fraud.v1+json.*`
|
||||
(9) - with header `Content-Type` matching `application/json.*`
|
||||
*/
|
||||
@@ -12,7 +12,7 @@ org.springframework.cloud.contract.spec.Contract.make {
|
||||
"""
|
||||
)
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
contentType("application/json")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,7 +23,7 @@ org.springframework.cloud.contract.spec.Contract.make {
|
||||
rejectionReason: $(consumer(null), producer(execute('assertThatRejectionReasonIsNull($it)')))
|
||||
)
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
contentType("application/json")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import org.springframework.cloud.contract.spec.Contract
|
||||
count: 200
|
||||
])
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
contentType("application/json")
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -30,7 +30,7 @@ import org.springframework.cloud.contract.spec.Contract
|
||||
count: 100
|
||||
])
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
contentType("application/json")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ org.springframework.cloud.contract.spec.Contract.make {
|
||||
name: "fraud"
|
||||
])
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
contentType("application/json")
|
||||
}
|
||||
}
|
||||
response {
|
||||
|
||||
@@ -8,7 +8,7 @@ org.springframework.cloud.contract.spec.Contract.make {
|
||||
name: $(anyAlphaUnicode())
|
||||
])
|
||||
headers {
|
||||
contentType("application/vnd.fraud.v1+json")
|
||||
contentType("application/json")
|
||||
}
|
||||
}
|
||||
response {
|
||||
|
||||
Reference in New Issue
Block a user