Добавил API получения остатка счёта на дату и HTTP-тест

This commit is contained in:
Kashey committed 2026-10-25 22:59:34 +03:00
1 parent 39786aaba5
commit d6a548c7b7
3 files changed
+57 -5

No files matched your search

@@ -2,33 +2,46 @@ package net.sytes.kashey.easymoney.api;
import model.Account;
import org.jspecify.annotations.NonNull;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import service.AccountService;
import service.OperationService;
import java.math.BigDecimal;
import java.net.URI;
import java.time.LocalDate;
@RestController
@RequestMapping("/api/accounts")
public class AccountController {
private final AccountService service;
private final AccountService accountService;
private final OperationService operationService;
public AccountController(AccountService service) {
this.service = service;
public AccountController(AccountService service, OperationService operationService) {
this.accountService = service;
this.operationService = operationService;
}
@GetMapping("/{id}")
public AccountResponse getById(@PathVariable("id") Long id) {
Account account = service.getById(id);
Account account = accountService.getById(id);
return new AccountResponse(account.getId(), account.getName());
}
@PostMapping
public ResponseEntity<@NonNull AccountResponse> saveAccount(@RequestBody CreateAccountRequest request) {
Account account = service.create(request.name());
Account account = accountService.create(request.name());
AccountResponse response = new AccountResponse(account.getId(), account.getName());
URI location = URI.create("/api/accounts/" + account.getId());
return ResponseEntity.created(location).body(response);
}
@GetMapping("/{id}/balance")
public BigDecimal getBalance(@PathVariable("id") Long id,
@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
Account account = accountService.getById(id);
return operationService.balance(account, date);
}
}
@@ -58,6 +58,10 @@ public class OperationService {
return operation;
}
public synchronized BigDecimal balance(Account account, LocalDate date) {
return ledger.balance(account, date);
}
private Operation requiredOperation(Long operationId) {
Operation operation = journal.getOperationById(operationId);
@@ -1,5 +1,7 @@
package net.sytes.kashey.easymoney.api;
import model.Account;
import model.Operation;
import net.sytes.kashey.easymoney.config.AccountingConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -11,14 +13,21 @@ import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.springframework.test.web.servlet.MvcResult;
import service.AccountService;
import service.Ledger;
import service.OperationService;
import tools.jackson.databind.ObjectMapper;
import java.math.BigDecimal;
import java.time.LocalDate;
@WebMvcTest(AccountController.class)
@Import(AccountingConfig.class)
class AccountControllerTest {
@@ -27,6 +36,12 @@ class AccountControllerTest {
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private AccountService accountService;
@Autowired
private OperationService operationService;
@Autowired
private Ledger ledger;
@Test
void creatingAccountReturnsCreatedAccountAndLocation() throws Exception {
@@ -98,4 +113,24 @@ class AccountControllerTest {
.andExpect(jsonPath("$.id", Long.class).value(createdAccount.id()))
.andExpect(jsonPath("$.name").value("Карта"));
}
@Test
void gettingAccountBalanceReturnsBalanceOnRequestedDate() throws Exception {
// Given
LocalDate date = LocalDate.of(2026, 10, 25);
Account account = accountService.create("Карта");
Operation operation = operationService.createOpeningBalance(date, account, new BigDecimal(15000));
operationService.post(operation.getId());
Long accountId = account.getId();
// When / Then
MvcResult result = mockMvc.perform(get("/api/accounts/{id}/balance", accountId)
.param("date", date.toString()))
.andExpect(status().isOk())
.andReturn();
BigDecimal response = objectMapper.readValue(result.getResponse().getContentAsString(),
BigDecimal.class);
assertEquals(0, response.compareTo(new BigDecimal(15000)));
}
}