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