Skip to content

Commit

Permalink
add option integration Test
Browse files Browse the repository at this point in the history
  • Loading branch information
seunghaen committed Dec 28, 2024
1 parent 02e3633 commit a81f497
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ public class ChoiceController {
private final OptionService optionService;
private final ChoiceService choiceService;

/**
* GET /choices/{optionId}
* 특정 옵션의 선택지 조회
*/
@GetMapping("/{optionId}")
public ResponseEntity<ApiResDTO<List<ChoiceResDTO>>> getMenuById(@PathVariable Long optionId) {
List<Choice> choices = optionService.getChoicesById(optionId);
List<ChoiceResDTO> choiceResDTOList = choices.stream().map(ChoiceResDTO::from).collect(Collectors.toList());
return ResponseEntity.ok(ApiResDTO.success(choiceResDTOList));

}

/**
* POST /choices
* 선택지생성
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,17 @@ public ResponseEntity<ApiResDTO<CommonIdResDTO>> deleteMenu(@PathVariable Long m
menuService.deleteMenu(menuId);
return ResponseEntity.ok(ApiResDTO.success(CommonIdResDTO.builder().id(menuId).build()));
}

/**
* GET /options/{menuId}
* 특정 메뉴의 옵션 조회
*/
@GetMapping("/{menuId}/options")
public ResponseEntity<ApiResDTO<List<OptionResDTO>>> getOptionsById(@PathVariable Long menuId) {
List<Option> options = menuService.getOptionsById(menuId);
List<OptionResDTO> optionResDTOS = options.stream().map(OptionResDTO::from).collect(Collectors.toList());
return ResponseEntity.ok(ApiResDTO.success(optionResDTOS));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.terte.dto.common.ApiResDTO;
import com.terte.dto.common.CommonIdResDTO;
import com.terte.dto.menu.MenuDetailResDTO;
import com.terte.dto.menu.OptionCreateReqDTO;
import com.terte.dto.menu.OptionResDTO;
import com.terte.dto.menu.OptionUpdateReqDTO;
import com.terte.dto.menu.*;
import com.terte.entity.menu.Choice;
import com.terte.entity.menu.Menu;
import com.terte.entity.menu.Option;
import com.terte.service.menu.MenuService;
Expand All @@ -26,16 +24,6 @@ public class OptionController {
private final MenuService menuService;
private final OptionService optionService;

/**
* GET /options/{menuId}
* 특정 메뉴의 옵션 조회
*/
@GetMapping("/{menuId}")
public ResponseEntity<ApiResDTO<List<OptionResDTO>>> getMenuById(@PathVariable Long menuId) {
List<Option> options = menuService.getOptionsById(menuId);
List<OptionResDTO> optionResDTOS = options.stream().map(OptionResDTO::from).collect(Collectors.toList());
return ResponseEntity.ok(ApiResDTO.success(optionResDTOS));
}

/**
* POST /options
Expand Down Expand Up @@ -77,4 +65,16 @@ public ResponseEntity<ApiResDTO<CommonIdResDTO>> updateOption(@PathVariable Long
optionService.deleteOption(optionId);
return ResponseEntity.ok(ApiResDTO.success(CommonIdResDTO.builder().id(optionId).build()));
}

/**
* GET /options/{optionId}/choices
* 특정 옵션의 선택지 조회
*/
@GetMapping("/{optionId}/choices")
public ResponseEntity<ApiResDTO<List<ChoiceResDTO>>> getMenuById(@PathVariable Long optionId) {
List<Choice> choices = optionService.getChoicesById(optionId);
List<ChoiceResDTO> choiceResDTOList = choices.stream().map(ChoiceResDTO::from).collect(Collectors.toList());
return ResponseEntity.ok(ApiResDTO.success(choiceResDTOList));

}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
package com.terte.repository.menu;

import com.terte.entity.menu.Choice;
import com.terte.entity.menu.Option;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Repository
public class OptionRepository {

public OptionRepository() {
// 초기 데이터 설정
Choice choice1 = new Choice(1L, "샷 추가", 500);
Choice choice2 = new Choice(2L, "시럽 추가", 500);
Choice choice3 = new Choice(3L, "얼음 추가", 0);
Choice choice4 = new Choice(4L, "얼음 빼기", 0);

Option option1 = new Option(1L, "음료 옵션", false, false, List.of(choice1, choice2));
Option option2 = new Option(2L, "빙수 옵션", true, true, List.of(choice3, choice4));

optionStorage.put(1L, option1);
optionStorage.put(2L, option2);
}
private final Map<Long, Option> optionStorage = new ConcurrentHashMap<>();

public Option findById(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.terte.dto.menu.CategoryResDTO;
import com.terte.dto.menu.CategoryCreateReqDTO;
import com.terte.dto.menu.CategoryUpdateReqDTO;
import org.json.JSONObject;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand Down Expand Up @@ -75,7 +76,6 @@ void testGetCategoriesByIdWithSpecificFields() throws Exception {
}
@Test
@DisplayName("카테고리가 성공적으로 생성되고 성공 후, 생성된 ID를 반환한다")
@Order(1)
void testCreateCategorySuccess() throws Exception {
CategoryCreateReqDTO categoryCreateReqDTO = new CategoryCreateReqDTO("New Category", "New Category Description");
mockMvc.perform(post("/categories")
Expand All @@ -99,9 +99,14 @@ void testCreateCategoryMissingRequiredField() throws Exception {

@Test
@DisplayName("카테고리 수정 시 성공하면 200 OK와 수정된 카테고리 ID를 반환한다")
@Order(2)
void testUpdateCategorySuccess() throws Exception {
Long targetId = 3L;
CategoryCreateReqDTO categoryCreateReqDTO = new CategoryCreateReqDTO("New Category", "New Category Description");
String res = mockMvc.perform(post("/categories")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(categoryCreateReqDTO))).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = new JSONObject(res);
Long targetId = jsonObject.getJSONObject("data").getLong("id");

CategoryUpdateReqDTO categoryUpdateReqDTO = CategoryUpdateReqDTO.builder()
.id(targetId)
.name("Updated Category")
Expand Down Expand Up @@ -130,12 +135,18 @@ void testUpdateCategoryNotFound() throws Exception {

@Test
@DisplayName("카테고리 삭제 시 성공하면 200 OK와 삭제된 카테고리 ID를 반환한다")
@Order(3)
void testDeleteCategorySuccess() throws Exception {
mockMvc.perform(delete("/categories/3")
CategoryCreateReqDTO categoryCreateReqDTO = new CategoryCreateReqDTO("New Category", "New Category Description");
String res = mockMvc.perform(post("/categories")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(categoryCreateReqDTO))).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = new JSONObject(res);
Long targetId = jsonObject.getJSONObject("data").getLong("id");
String deleteReqUrl = "/categories/" + targetId;
mockMvc.perform(delete(deleteReqUrl)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.id").value(3));
.andExpect(jsonPath("$.data.id").value(targetId));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.terte.controller.menu;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.terte.TerteMainApplication;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
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.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest(classes = TerteMainApplication.class)
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ChoiceControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;

@Autowired
ObjectMapper objectMapper;


@Test
@DisplayName("옵션ID로 선택지를 조회할 때 존재하지 않는 옵션ID를 요청하면 404를 반환한다.")
void testGetChoicesByNonExistingOptionId() {
}

@Test
@DisplayName("선택지를 생성한다.")
void testCreateChoice() {
}

@Test
@DisplayName("선택지를 수정한다.")
void testUpdateChoice() {
}

@Test
@DisplayName("존재하지 않는 선택지를 수정하려고 하면 404를 반환한다.")
void testUpdateChoiceWithNonExistingChoice() {
}

@Test
@DisplayName("선택지를 삭제한다.")
void testDeleteChoice() {
}

@Test
@DisplayName("존재하지 않는 선택지를 삭제하려고 하면 404를 반환한다.")
void testDeleteNonExistingChoice() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ void testCreateMenuMissingRequiredField() throws Exception {

@Test
@DisplayName("메뉴 수정 시 성공하면 200 OK와 수정된 메뉴 ID를 반환한다")
@Order(2)
void testUpdateMenuSuccess() throws Exception {
MenuCreateReqDTO menuCreateReqDTO = new MenuCreateReqDTO("New Menu", "New Menu Description", 1000, 101L, "image.jpg");
String res = mockMvc.perform(post("/menus")
Expand Down
Loading

0 comments on commit a81f497

Please sign in to comment.