Добавил API проведения и отмены операций с HTTP-тестами успешных запросов и неизвестного ID
This commit is contained in:
1 parent
e8ec273693
commit
39786aaba5
2 files changed
+67
-2
No files matched your search
@@ -31,7 +31,8 @@ public class OperationController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/opening-balance")
|
@PostMapping("/opening-balance")
|
||||||
public ResponseEntity<@NonNull OperationResponse> createOpeningBalance(@RequestBody CreateOpeningBalanceRequest request) {
|
public ResponseEntity<@NonNull OperationResponse> createOpeningBalance(
|
||||||
|
@RequestBody CreateOpeningBalanceRequest request) {
|
||||||
if (request.accountId() == null) {
|
if (request.accountId() == null) {
|
||||||
throw new InvalidOperationException("Не указан ID счёта");
|
throw new InvalidOperationException("Не указан ID счёта");
|
||||||
}
|
}
|
||||||
@@ -42,6 +43,18 @@ public class OperationController {
|
|||||||
return ResponseEntity.created(location).body(response);
|
return ResponseEntity.created(location).body(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/{id}/post")
|
||||||
|
public OperationResponse post(@PathVariable("id") Long id) {
|
||||||
|
operationService.post(id);
|
||||||
|
return getOperationResponse(operationService.getById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/{id}/unpost")
|
||||||
|
public OperationResponse unpost(@PathVariable("id") Long id) {
|
||||||
|
operationService.unpost(id);
|
||||||
|
return getOperationResponse(operationService.getById(id));
|
||||||
|
}
|
||||||
|
|
||||||
private OperationResponse getOperationResponse(Operation operation) {
|
private OperationResponse getOperationResponse(Operation operation) {
|
||||||
return new OperationResponse(
|
return new OperationResponse(
|
||||||
operation.getId(),
|
operation.getId(),
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ class OperationControllerTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private Ledger ledger;
|
private Ledger ledger;
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void gettingUnknownOperationReturnsNotFound() throws Exception {
|
void gettingUnknownOperationReturnsNotFound() throws Exception {
|
||||||
// When / Then
|
// When / Then
|
||||||
@@ -160,4 +159,57 @@ class OperationControllerTest {
|
|||||||
.andExpect(jsonPath("$.detail").value("Счет не найден: 999"))
|
.andExpect(jsonPath("$.detail").value("Счет не найден: 999"))
|
||||||
.andExpect(jsonPath("$.status").value(404));
|
.andExpect(jsonPath("$.status").value(404));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void postingOperationByIdUpdatesBalanceAndReturnsPostedDocument() throws Exception {
|
||||||
|
// Given
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 23);
|
||||||
|
Account account = accountService.create("Карта");
|
||||||
|
Operation operation = operationService.createOpeningBalance(date, account, new BigDecimal(15000));
|
||||||
|
Long operationId = operation.getId();
|
||||||
|
|
||||||
|
// When / Then
|
||||||
|
mockMvc.perform(post("/api/operations/" + operationId + "/post"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.id", Long.class).value(operationId))
|
||||||
|
.andExpect(jsonPath("$.posted").value(true));
|
||||||
|
|
||||||
|
assertEquals(0, new BigDecimal(15000).compareTo(ledger.balance(account, date)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void unpostingOperationByIdUpdatesBalanceAndReturnsUnpostedDocument() throws Exception {
|
||||||
|
// Given
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 23);
|
||||||
|
Account account = accountService.create("Карта");
|
||||||
|
Operation operation = operationService.createOpeningBalance(date, account, new BigDecimal(15000));
|
||||||
|
operationService.post(operation.getId());
|
||||||
|
Long operationId = operation.getId();
|
||||||
|
|
||||||
|
// When / Then
|
||||||
|
mockMvc.perform(post("/api/operations/" + operationId + "/unpost"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.id", Long.class).value(operationId))
|
||||||
|
.andExpect(jsonPath("$.posted").value(false));
|
||||||
|
|
||||||
|
assertEquals(0, BigDecimal.ZERO.compareTo(ledger.balance(account, date)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void postingUnknownOperationReturnsNotFound() throws Exception {
|
||||||
|
// When / Then
|
||||||
|
mockMvc.perform(post("/api/operations/999/post"))
|
||||||
|
.andExpect(status().isNotFound())
|
||||||
|
.andExpect(jsonPath("$.status").value(404))
|
||||||
|
.andExpect(jsonPath("$.detail").value("Операция не найдена: 999"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void unpostingUnknownOperationReturnsNotFound() throws Exception {
|
||||||
|
// When / Then
|
||||||
|
mockMvc.perform(post("/api/operations/999/unpost"))
|
||||||
|
.andExpect(status().isNotFound())
|
||||||
|
.andExpect(jsonPath("$.status").value(404))
|
||||||
|
.andExpect(jsonPath("$.detail").value("Операция не найдена: 999"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user