diff --git a/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java b/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java index f595fc4..b9be7ec 100644 --- a/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java +++ b/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java @@ -125,7 +125,7 @@ class AccountControllerTest { // When / Then MvcResult result = mockMvc.perform(get("/api/accounts/{id}/balance", accountId) - .param("date", date.toString())) + .param("date", date.toString())) .andExpect(status().isOk()) .andReturn(); @@ -133,4 +133,51 @@ class AccountControllerTest { BigDecimal.class); assertEquals(0, response.compareTo(new BigDecimal(15000))); } + + @Test + void gettingAccountBalanceExcludesMovementsAfterRequestedDate() throws Exception { + // Given + LocalDate date = LocalDate.of(2026, 10, 25); + LocalDate dateAfter = LocalDate.of(2026, 10, 26); + Account account = accountService.create("Карта"); + Operation openingBalance = operationService.createOpeningBalance(date, account, new BigDecimal(15000)); + operationService.post(openingBalance.getId()); + Operation openingBalanceLater = operationService.createOpeningBalance(dateAfter, account, new BigDecimal(10000)); + operationService.post(openingBalanceLater.getId()); + + // When / Then + MvcResult result = mockMvc.perform(get("/api/accounts/{id}/balance", account.getId()) + .param("date", date.toString())) + .andExpect(status().isOk()) + .andReturn(); + + BigDecimal response = objectMapper.readValue(result.getResponse().getContentAsString(), + BigDecimal.class); + assertEquals(0, response.compareTo(new BigDecimal(15000))); + } + + @Test + void gettingAccountBalanceByUnknownAccountReturnsNotFound() throws Exception { + // When / Then + mockMvc.perform(get("/api/accounts/{id}/balance", 999L) + .param("date", LocalDate.of(2026, 10, 28).toString())) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$.status").value(404)) + .andExpect(jsonPath("$.detail").value("Счет не найден: 999")); + } + + @Test + void gettingAccountBalanceWithInvalidDateFormatReturnsBadRequest() throws Exception { + // When / Then + mockMvc.perform(get("/api/accounts/{id}/balance", 1L) + .param("date", "28-10-2026")) + .andExpect(status().isBadRequest()); + } + + @Test + void gettingAccountBalanceWithoutDateReturnsBadRequest() throws Exception { + // When / Then + mockMvc.perform(get("/api/accounts/{id}/balance", 1L)) + .andExpect(status().isBadRequest()); + } } \ No newline at end of file