From 0af9111103d133cee304d69e36da868b9a40ac67 Mon Sep 17 00:00:00 2001 From: Kashey Date: Thu, 22 Oct 2026 13:15:50 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D1=8B=20=D0=B8=D0=BC=D1=91=D0=BD=20=D0=B8=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D1=87=D1=91=D1=82=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=20Location?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/model/AccountTest.java | 30 +++++++++ src/test/java/model/CategoryTest.java | 29 +++++++++ .../easymoney/api/AccountControllerTest.java | 65 +++++++++++++++---- 3 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 src/test/java/model/AccountTest.java create mode 100644 src/test/java/model/CategoryTest.java diff --git a/src/test/java/model/AccountTest.java b/src/test/java/model/AccountTest.java new file mode 100644 index 0000000..c505073 --- /dev/null +++ b/src/test/java/model/AccountTest.java @@ -0,0 +1,30 @@ +package model; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.*; + +class AccountTest { + + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {" ", "\t", "\n"}) + void creatingAccountWithInvalidNameThrowsException(String name) { + InvalidAccountNameException exception = assertThrows(InvalidAccountNameException.class, + () -> new Account(1L, name)); + assertEquals("Не указано имя счёта", exception.getMessage()); + } + + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {" ", "\t", "\n"}) + void renamingAccountWithInvalidNameThrowsExceptionAndPreservesName(String name) { + Account account = new Account(1L, "Карта"); + InvalidAccountNameException exception = assertThrows(InvalidAccountNameException.class, + () -> account.setName(name)); + assertEquals("Не указано имя счёта", exception.getMessage()); + assertEquals("Карта", account.getName()); + } +} \ No newline at end of file diff --git a/src/test/java/model/CategoryTest.java b/src/test/java/model/CategoryTest.java new file mode 100644 index 0000000..87ad3b9 --- /dev/null +++ b/src/test/java/model/CategoryTest.java @@ -0,0 +1,29 @@ +package model; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.*; + +class CategoryTest { + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {" ", "\t", "\n"}) + void creatingCategoryWithInvalidNameThrowsException(String name) { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Category(1L, name)); + assertEquals("Не указано имя категории", exception.getMessage()); + } + + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {" ", "\t", "\n"}) + void renamingCategoryWithInvalidNameThrowsExceptionAndPreservesName(String name) { + Category category = new Category(1L, "Продукты"); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> category.setName(name)); + assertEquals("Не указано имя категории", exception.getMessage()); + assertEquals("Продукты", category.getName()); + } +} \ No newline at end of file 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 a2104e4..c27aa08 100644 --- a/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java +++ b/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java @@ -2,33 +2,42 @@ package net.sytes.kashey.easymoney.api; import net.sytes.kashey.easymoney.config.AccountingConfig; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest; 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.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 tools.jackson.databind.ObjectMapper; + @WebMvcTest(AccountController.class) @Import(AccountingConfig.class) class AccountControllerTest { @Autowired private MockMvc mockMvc; + @Autowired + private ObjectMapper objectMapper; @Test void creatingAccountReturnsCreatedAccountAndLocation() throws Exception { + // Given + String body = objectMapper.writeValueAsString(new CreateAccountRequest("Карта")); + // When / Then mockMvc.perform(post("/api/accounts") .contentType(MediaType.APPLICATION_JSON) - .content(""" - { - "name":"Карта" - } - """)) + + .content(body)) .andExpect(status().isCreated()) .andExpect(header().exists("Location")) .andExpect(jsonPath("$.id").isNumber()) @@ -44,17 +53,49 @@ class AccountControllerTest { .andExpect(jsonPath("$.detail").value("Счет не найден: 999")); } - @Test - void creatingAccountWithInvalidNameReturnsBadRequestAnswer() throws Exception { + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {" ", "\t", "\n"}) + void creatingAccountWithInvalidNameReturnsBadRequestAnswer(String name) throws Exception { + String body = objectMapper.writeValueAsString(new CreateAccountRequest(name)); + mockMvc.perform(post("/api/accounts") .contentType(MediaType.APPLICATION_JSON) - .content(""" - { - "name":"" - } - """)) + .content(body)) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.status").value(400)) .andExpect(jsonPath("$.detail").value("Не указано имя счёта")); } + + @Test + void creatingAccountWithAbsentNameReturnsBadRequestAnswer() throws Exception { + mockMvc.perform(post("/api/accounts") + .contentType(MediaType.APPLICATION_JSON) + .content("{}")) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.detail").value("Не указано имя счёта")); + } + + @Test + void createdAccountCanBeRetrievedByLocation() throws Exception { + // Given + String body = objectMapper.writeValueAsString(new CreateAccountRequest("Карта")); + + MvcResult result = mockMvc.perform(post("/api/accounts") + .contentType(MediaType.APPLICATION_JSON) + .content(body)) + .andExpect(status().isCreated()) + .andReturn(); + + String location = result.getResponse().getHeader("Location"); + assertNotNull(location, "POST должен возвращать заголовок Location"); + AccountResponse createdAccount = objectMapper.readValue(result.getResponse().getContentAsString(), AccountResponse.class); + + // When / Then + mockMvc.perform(get(location)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", Long.class).value(createdAccount.id())) + .andExpect(jsonPath("$.name").value("Карта")); + } } \ No newline at end of file