diff --git a/application/src/main/java/com/terte/repository/menu/ChoiceRepository.java b/application/src/main/java/com/terte/repository/menu/ChoiceRepository.java index e58da19..e90bc12 100644 --- a/application/src/main/java/com/terte/repository/menu/ChoiceRepository.java +++ b/application/src/main/java/com/terte/repository/menu/ChoiceRepository.java @@ -9,6 +9,14 @@ @Repository public class ChoiceRepository { + + public ChoiceRepository(){ + // 초기 데이터 설정 + choiceStorage.put(1L, new Choice(1L, "샷 추가", 500)); + choiceStorage.put(2L, new Choice(2L, "시럽 추가", 500)); + choiceStorage.put(3L, new Choice(3L, "얼음 추가", 0)); + choiceStorage.put(4L, new Choice(4L, "얼음 빼기", 0)); + } private final Map choiceStorage = new ConcurrentHashMap<>(); public Choice findById(Long id) { diff --git a/application/src/test/java/com/terte/controller/menu/ChoiceControllerIntegrationTest.java b/application/src/test/java/com/terte/controller/menu/ChoiceControllerIntegrationTest.java index a1d6421..9e240aa 100644 --- a/application/src/test/java/com/terte/controller/menu/ChoiceControllerIntegrationTest.java +++ b/application/src/test/java/com/terte/controller/menu/ChoiceControllerIntegrationTest.java @@ -2,6 +2,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.terte.TerteMainApplication; +import com.terte.dto.menu.ChoiceCreateReqDTO; +import com.terte.dto.menu.ChoiceUpdateReqDTO; +import org.json.JSONObject; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Test; @@ -12,7 +15,7 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -27,34 +30,74 @@ public class ChoiceControllerIntegrationTest { ObjectMapper objectMapper; - @Test - @DisplayName("옵션ID로 선택지를 조회할 때 존재하지 않는 옵션ID를 요청하면 404를 반환한다.") - void testGetChoicesByNonExistingOptionId() { - } - @Test @DisplayName("선택지를 생성한다.") - void testCreateChoice() { + void testCreateChoice() throws Exception { + ChoiceCreateReqDTO choiceCreateReqDTO = new ChoiceCreateReqDTO("샷 추가", 500); + mockMvc.perform(post("/choices") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(choiceCreateReqDTO)) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data.id").value(6L)); } @Test @DisplayName("선택지를 수정한다.") - void testUpdateChoice() { + void testUpdateChoice() throws Exception { + ChoiceCreateReqDTO choiceCreateReqDTO = new ChoiceCreateReqDTO("샷 추가", 500); + String res = mockMvc.perform(post("/choices") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(choiceCreateReqDTO)) + ).andReturn().getResponse().getContentAsString(); + JSONObject jsonObject = new JSONObject(res); + Long choiceId = jsonObject.getJSONObject("data").getLong("id"); + + ChoiceUpdateReqDTO choiceUpdateReqDTO = new ChoiceUpdateReqDTO(choiceId, null, 1000); + mockMvc.perform(put("/choices") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(choiceUpdateReqDTO)) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data.id").value(choiceId)); } @Test @DisplayName("존재하지 않는 선택지를 수정하려고 하면 404를 반환한다.") - void testUpdateChoiceWithNonExistingChoice() { + void testUpdateChoiceWithNonExistingChoice() throws Exception { + ChoiceUpdateReqDTO choiceUpdateReqDTO = new ChoiceUpdateReqDTO(100L, "샷 추가", 1000); + mockMvc.perform(put("/choices") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(choiceUpdateReqDTO)) + ) + .andExpect(status().isNotFound()); } @Test @DisplayName("선택지를 삭제한다.") - void testDeleteChoice() { + void testDeleteChoice() throws Exception { + ChoiceCreateReqDTO choiceCreateReqDTO = new ChoiceCreateReqDTO("샷 추가", 500); + String res = mockMvc.perform(post("/choices") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(choiceCreateReqDTO)) + ).andReturn().getResponse().getContentAsString(); + JSONObject jsonObject = new JSONObject(res); + Long choiceId = jsonObject.getJSONObject("data").getLong("id"); + + mockMvc.perform(delete("/choices/" + choiceId) + .contentType(MediaType.APPLICATION_JSON) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data.id").value(choiceId)); } @Test @DisplayName("존재하지 않는 선택지를 삭제하려고 하면 404를 반환한다.") - void testDeleteNonExistingChoice() { + void testDeleteNonExistingChoice() throws Exception { + mockMvc.perform(delete("/choices/100") + .contentType(MediaType.APPLICATION_JSON) + ) + .andExpect(status().isNotFound()); } } diff --git a/application/src/test/java/com/terte/controller/menu/OptionControllerIntegrationTest.java b/application/src/test/java/com/terte/controller/menu/OptionControllerIntegrationTest.java index ee58410..50206ed 100644 --- a/application/src/test/java/com/terte/controller/menu/OptionControllerIntegrationTest.java +++ b/application/src/test/java/com/terte/controller/menu/OptionControllerIntegrationTest.java @@ -157,6 +157,4 @@ void testDeleteNonExistingOption() throws Exception{ .andExpect(status().isNotFound()); } - - } diff --git a/application/src/test/java/com/terte/service/menu/ChoiceServiceTest.java b/application/src/test/java/com/terte/service/menu/ChoiceServiceTest.java new file mode 100644 index 0000000..3009cc5 --- /dev/null +++ b/application/src/test/java/com/terte/service/menu/ChoiceServiceTest.java @@ -0,0 +1,90 @@ +package com.terte.service.menu; +import com.terte.common.exception.NotFoundException; +import com.terte.entity.menu.Choice; +import com.terte.repository.menu.ChoiceRepository; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class ChoiceServiceTest { + @Mock + ChoiceRepository choiceRepository; + + @InjectMocks + ChoiceServiceImpl choiceService; + + @Test + @DisplayName("선택지 생성") + void createChoice() { + Choice choice = new Choice(null, "샷 추가", 500); + Choice createdChoice = new Choice(1L, "샷 추가", 500); + when(choiceRepository.save(choice)).thenReturn(createdChoice); + + Choice result = choiceService.createChoice(choice); + + assertNotNull(result.getId()); + assertEquals(choice.getName(), result.getName()); + assertEquals(choice.getPrice(), result.getPrice()); + + verify(choiceRepository, times(1)).save(choice); + } + + @Test + @DisplayName("존재하는 선택지 수정") + void updateChoice() { + Choice choice = new Choice(1L, "샷 추가", 500); + Choice existingChoice = new Choice(1L, "샷 추가", 500); + when(choiceRepository.findById(choice.getId())).thenReturn(existingChoice); + when(choiceRepository.save(choice)).thenReturn(choice); + + Choice result = choiceService.updateChoice(choice); + + assertEquals(choice.getId(), result.getId()); + assertEquals(choice.getName(), result.getName()); + assertEquals(choice.getPrice(), result.getPrice()); + + verify(choiceRepository, times(1)).findById(choice.getId()); + } + + @Test + @DisplayName("존재하지 않는 선택지 수정") + void updateChoiceWithNonExistingChoice() { + Choice choice = new Choice(1L, "샷 추가", 500); + when(choiceRepository.findById(choice.getId())).thenReturn(null); + + assertThrows(NotFoundException.class, () -> choiceService.updateChoice(choice)); + verify(choiceRepository, times(1)).findById(choice.getId()); + } + + @Test + @DisplayName("선택지 삭제") + void deleteChoice() { + Long choiceId = 1L; + Choice existingChoice = new Choice(choiceId, "샷 추가", 500); + when(choiceRepository + .findById(choiceId)) + .thenReturn(existingChoice); + + choiceService.deleteChoice(choiceId); + verify(choiceRepository, times(1)).deleteById(choiceId); + } + + @Test + @DisplayName("존재하지 않는 선택지 삭제") + void deleteChoiceWithNonExistingChoice() { + Long choiceId = 1L; + when(choiceRepository + .findById(choiceId)) + .thenReturn(null); + assertThrows(NotFoundException.class, () -> choiceService.deleteChoice(choiceId)); + verify(choiceRepository, never()).deleteById(choiceId); + } +} diff --git a/application/src/test/java/com/terte/service/menu/OptionServiceTest.java b/application/src/test/java/com/terte/service/menu/OptionServiceTest.java new file mode 100644 index 0000000..36657db --- /dev/null +++ b/application/src/test/java/com/terte/service/menu/OptionServiceTest.java @@ -0,0 +1,123 @@ +package com.terte.service.menu; + +import com.terte.common.exception.NotFoundException; +import com.terte.entity.menu.Choice; +import com.terte.entity.menu.Option; +import com.terte.repository.menu.OptionRepository; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class OptionServiceTest { + @Mock + OptionRepository optionRepository; + + @InjectMocks + OptionServiceImpl optionService; + + @Test + @DisplayName("옵션 생성") + void createOption() { + Option option = new Option(null, "샷 추가", true, false, null); + Option createdOption = new Option(1L, "샷 추가", true, false, null); + when(optionRepository.save(option)).thenReturn(createdOption); + + Option result = optionService.createOption(option); + + assertNotNull(result.getId()); + assertEquals(option.getName(), result.getName()); + assertEquals(option.getRequired(), result.getRequired()); + assertEquals(option.getMultipleSelection(), result.getMultipleSelection()); + + verify(optionRepository, times(1)).save(option); + + } + + @Test + @DisplayName("존재하는 옵션 수정") + void updateOption() { + Option option = new Option(1L, "샷 추가", true, false, null); + Option existingOption = new Option(1L, "샷 추가", true, false, null); + when(optionRepository.findById(option.getId())).thenReturn(existingOption); + when(optionRepository.save(option)).thenReturn(option); + + Option result = optionService.updateOption(option); + + assertEquals(option.getId(), result.getId()); + assertEquals(option.getName(), result.getName()); + assertEquals(option.getRequired(), result.getRequired()); + assertEquals(option.getMultipleSelection(), result.getMultipleSelection()); + + verify(optionRepository, times(1)).findById(option.getId()); + verify(optionRepository, times(1)).save(option); + } + + @Test + @DisplayName("존재하지 않는 옵션 수정") + void updateOptionNotFound() { + Option option = new Option(1L, "샷 추가", true, false, null); + when(optionRepository.findById(option.getId())).thenReturn(null); + + + assertThrows(NotFoundException.class, () -> optionService.updateOption(option)); + verify(optionRepository, times(1)).findById(option.getId()); + verify(optionRepository, never()).save(option); + } + + @Test + @DisplayName("옵션 삭제") + void deleteOption() { + Long optionId = 1L; + Option option = new Option(optionId, "샷 추가", true, false, null); + when(optionRepository.findById(optionId)).thenReturn(option); + + optionService.deleteOption(optionId); + + verify(optionRepository, times(1)).deleteById(optionId); + } + + @Test + @DisplayName("존재하지 않는 옵션 삭제") + void deleteOptionNotFound() { + Long optionId = 1L; + when(optionRepository.findById(optionId)). + thenReturn(null); + + assertThrows(NotFoundException.class, () -> optionService.deleteOption(optionId)); + verify(optionRepository, never()).deleteById(optionId); + } + + @Test + @DisplayName("옵션의 선택지 조회") + void getChoicesById() { + Choice choice = new Choice(1L, "샷 추가", 500); + Option option = new Option(1L, "샷 추가", true, false, List.of(choice)); + when(optionRepository.findById(option.getId())).thenReturn(option); + + List result = optionService.getChoicesById(option.getId()); + assertEquals(option.getChoices(), result); + verify(optionRepository, times(1)).findById(option.getId()); + } + + @Test + @DisplayName("존재하지 않는 옵션의 선택지 조회") + void getChoicesByIdNotFound() { + Long optionId = 1L; + when(optionRepository.findById(optionId)). + thenReturn(null); + + assertThrows(NotFoundException.class, () -> optionService.getChoicesById(optionId)); + verify(optionRepository, times(1)).findById(optionId); + + } + +}