Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...ucer/src/main/java/dev/pater/springcloudcontractproducer/AccountNumberRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package dev.pater.springcloudcontractproducer; | ||
|
||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/accountNumber") | ||
public class AccountNumberRestController { | ||
|
||
@GetMapping | ||
public ResponseEntity<AccountNumber> get() { | ||
|
||
return ResponseEntity.ok().body(new AccountNumber(123L)); | ||
} | ||
|
||
@PutMapping(produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity put(@RequestBody AccountNumber accountNumber) { | ||
|
||
if (accountNumber.getAccountNumber() < 10) | ||
return ResponseEntity.badRequest().body(new ErrorMessage("ACCOUNT_NUMBER_INVALID", "Account number too short")); | ||
|
||
// logic | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
class ErrorMessage { | ||
private String errorCode; | ||
|
||
private String message; | ||
|
||
public String getErrorCode() { | ||
return errorCode; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
ErrorMessage(String errorCode, String message) { | ||
this.errorCode = errorCode; | ||
this.message = message; | ||
} | ||
|
||
|
||
} | ||
|
||
class AccountNumber { | ||
AccountNumber(long accountNumber) { | ||
this.accountNumber = accountNumber; | ||
} | ||
|
||
public long getAccountNumber() { | ||
return accountNumber; | ||
} | ||
|
||
public AccountNumber() { | ||
} | ||
|
||
public void setAccountNumber(long accountNumber) { | ||
this.accountNumber = accountNumber; | ||
} | ||
|
||
private long accountNumber; | ||
} |
21 changes: 21 additions & 0 deletions
21
...-contract-producer/src/test/java/dev/pater/springcloudcontractproducer/BaseTestClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dev.pater.springcloudcontractproducer; | ||
|
||
import io.restassured.module.mockmvc.RestAssuredMockMvc; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public abstract class BaseTestClass { | ||
|
||
@InjectMocks | ||
AccountNumberRestController accountNumberRestController; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
RestAssuredMockMvc.standaloneSetup(this.accountNumberRestController); | ||
|
||
} | ||
|
||
} |
13 changes: 0 additions & 13 deletions
13
...va/dev/pater/springcloudcontractproducer/SpringCloudContractProducerApplicationTests.java
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
...roducer/src/test/resources/contracts/accountNumber/ErrorCheckAccountNumberContract.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package contracts.accountNumber | ||
|
||
import org.springframework.cloud.contract.spec.Contract; | ||
|
||
Contract.make { | ||
description(""" | ||
User want to update his account number and passes invalid number | ||
``` | ||
given: | ||
any client and too short account number | ||
when: | ||
he want to update his account number | ||
then: | ||
we'll send him error message | ||
``` | ||
""") | ||
request { | ||
method 'PUT' | ||
url '/accountNumber' | ||
body( | ||
accountNumber: $(regex('[0-9]{1}')) | ||
) | ||
headers { | ||
contentType(applicationJson()) | ||
} | ||
} | ||
response { | ||
status BAD_REQUEST() | ||
body([ | ||
errorCode: "ACCOUNT_NUMBER_INVALID", | ||
message: anyNonEmptyString() | ||
]) | ||
headers { | ||
contentType(applicationJson()) | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...tract-producer/src/test/resources/contracts/accountNumber/GetAccountNumberContract.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package contracts.accountNumber | ||
|
||
import org.springframework.cloud.contract.spec.Contract; | ||
|
||
Contract.make { | ||
description(""" | ||
User asks for his account number | ||
``` | ||
given: | ||
any client | ||
when: | ||
he asks for account number | ||
then: | ||
we'll send him back his account number | ||
``` | ||
""") | ||
request { | ||
method 'GET' | ||
url '/accountNumber' | ||
headers { | ||
contentType(applicationJson()) | ||
} | ||
} | ||
response { | ||
status 200 | ||
body( | ||
accountNumber: anyNumber() | ||
) | ||
headers { | ||
contentType(applicationJson()) | ||
} | ||
} | ||
} |