Skip to content

Commit 23d9850

Browse files
author
OptimistLabyrinth
committed
test: TableGroupRestController, TableGroupService 에 대한 테스트 코드 작성
1 parent 805ad80 commit 23d9850

File tree

3 files changed

+378
-1
lines changed

3 files changed

+378
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
- 비어 있는 테이블은 단체 손님 정보에 포함할 수 없다.
9898
- 하나의 주문 테이블을 동시에 두 개 이상의 단체 손님으로 지정할 수 없다.
9999
- 단체 손님으로 지정했던 주문 테이블을 별도로 분리하는 것도 가능하다.
100-
- 계산을 완료한 주문 테이블은 단체 손님에서 분리할 수 없다.
100+
- 계산을 완료한 주문 테이블만 단체 손님에서 분리할 수 없다.
101101

102102
### 테이블 그룹 생성하기 (POST /api/table-groups)
103103

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
package kitchenpos.application;
2+
3+
import kitchenpos.dao.OrderDao;
4+
import kitchenpos.dao.OrderTableDao;
5+
import kitchenpos.dao.TableGroupDao;
6+
import kitchenpos.domain.OrderTable;
7+
import kitchenpos.domain.TableGroup;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Nested;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.ExtendWith;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.Mock;
14+
import org.mockito.Mockito;
15+
import org.mockito.junit.jupiter.MockitoExtension;
16+
17+
import java.util.ArrayList;
18+
import java.util.Arrays;
19+
import java.util.List;
20+
import java.util.stream.Collectors;
21+
import java.util.stream.Stream;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.junit.jupiter.api.Assertions.*;
25+
26+
@ExtendWith(MockitoExtension.class)
27+
@DisplayName("TableGroupService 클래스 테스트")
28+
public class TableGroupServiceTest {
29+
@Mock
30+
private OrderDao orderDao;
31+
@Mock
32+
private OrderTableDao orderTableDao;
33+
@Mock
34+
private TableGroupDao tableGroupDao;
35+
@InjectMocks
36+
private TableGroupService tableGroupService;
37+
38+
@Nested
39+
@DisplayName("create 메서드 테스트")
40+
public class CreateMethod {
41+
@Nested
42+
@DisplayName("테이블 그룹 생성 성공")
43+
public class Success {
44+
@Test
45+
public void testCase() {
46+
// given
47+
final TableGroup tableGroup = setup();
48+
49+
// when
50+
final TableGroup createdTableGroup = tableGroupService.create(tableGroup);
51+
52+
// then
53+
assertAll(
54+
() -> assertThat(createdTableGroup.getId()).isPositive(),
55+
() -> assertThat(createdTableGroup.getOrderTables()).isEqualTo(tableGroup.getOrderTables())
56+
);
57+
}
58+
59+
private TableGroup setup() {
60+
final TableGroup tableGroup = new TableGroup();
61+
tableGroup.setId(1L);
62+
final List<Long> orderTableIds = Stream.of(1, 2).map(Long::new).collect(Collectors.toList());
63+
final List<OrderTable> orderTables = orderTableIds
64+
.stream()
65+
.map(id -> {
66+
final OrderTable orderTable = new OrderTable();
67+
orderTable.setId(id);
68+
orderTable.setEmpty(true);
69+
return orderTable;
70+
})
71+
.collect(Collectors.toList());
72+
tableGroup.setOrderTables(orderTables);
73+
Mockito.when(orderTableDao.findAllByIdIn(Mockito.anyList())).thenReturn(orderTables);
74+
Mockito.when(tableGroupDao.save(Mockito.any())).thenReturn(tableGroup);
75+
Mockito.when(orderTableDao.save(Mockito.any())).thenReturn(orderTables.get(0), orderTables.get(1));
76+
return tableGroup;
77+
}
78+
}
79+
80+
@Nested
81+
@DisplayName("주문 테이블이 하나도 없으면 테이블 그룹 생성 실패")
82+
public class ErrorOrderTableIsEmpty {
83+
@Test
84+
public void testCase() {
85+
// given
86+
final TableGroup tableGroup = setup();
87+
88+
// when - then
89+
assertThrows(IllegalArgumentException.class, () -> tableGroupService.create(tableGroup));
90+
}
91+
92+
private TableGroup setup() {
93+
final TableGroup tableGroup = new TableGroup();
94+
tableGroup.setOrderTables(new ArrayList<>());
95+
return tableGroup;
96+
}
97+
}
98+
99+
@Nested
100+
@DisplayName("주문 테이블이 한 개 뿐이면 테이블 그룹 생성 실패")
101+
public class ErrorOnlyOneInOrderTables {
102+
@Test
103+
public void testCase() {
104+
// given
105+
final TableGroup tableGroup = setup();
106+
107+
// when - then
108+
assertThrows(IllegalArgumentException.class, () -> tableGroupService.create(tableGroup));
109+
}
110+
111+
private TableGroup setup() {
112+
final TableGroup tableGroup = new TableGroup();
113+
final List<OrderTable> orderTables = Arrays.asList(new OrderTable());
114+
tableGroup.setOrderTables(orderTables);
115+
return tableGroup;
116+
}
117+
}
118+
119+
@Nested
120+
@DisplayName("주문 테이블 id 로 실제 주문 테이블을 하나라도 찾을 수 없으면 테이블 그룹 생성 실패")
121+
public class ErrorsOrderTablesMissingWhenTryToFindById {
122+
@Test
123+
public void testCase() {
124+
// given
125+
final TableGroup tableGroup = setup();
126+
127+
// when - then
128+
assertThrows(IllegalArgumentException.class, () -> tableGroupService.create(tableGroup));
129+
}
130+
131+
private TableGroup setup() {
132+
final TableGroup tableGroup = new TableGroup();
133+
tableGroup.setId(1L);
134+
final List<Long> orderTableIds = Stream.of(1, 2).map(Long::new).collect(Collectors.toList());
135+
final List<OrderTable> orderTables = orderTableIds
136+
.stream()
137+
.map(id -> {
138+
final OrderTable orderTable = new OrderTable();
139+
orderTable.setId(id);
140+
orderTable.setEmpty(true);
141+
return orderTable;
142+
})
143+
.collect(Collectors.toList());
144+
tableGroup.setOrderTables(orderTables);
145+
Mockito.when(orderTableDao.findAllByIdIn(Mockito.anyList())).thenReturn(orderTables.subList(0, 1));
146+
return tableGroup;
147+
}
148+
}
149+
}
150+
151+
@Nested
152+
@DisplayName("ungroup 메서드 테스트")
153+
public class UngroupMethod {
154+
@Nested
155+
@DisplayName("그룹 해제 성공")
156+
public class Success {
157+
@Test
158+
public void testCase() {
159+
// given
160+
final TableGroup tableGroup = setup();
161+
final TableGroup createdTableGroup = tableGroupService.create(tableGroup);
162+
163+
// when - then
164+
assertDoesNotThrow(() -> tableGroupService.ungroup(createdTableGroup.getId()));
165+
}
166+
167+
private TableGroup setup() {
168+
final TableGroup tableGroup = new TableGroup();
169+
tableGroup.setId(1L);
170+
final List<Long> orderTableIds = Stream.of(1, 2).map(Long::new).collect(Collectors.toList());
171+
final List<OrderTable> orderTables = orderTableIds
172+
.stream()
173+
.map(id -> {
174+
final OrderTable orderTable = new OrderTable();
175+
orderTable.setId(id);
176+
orderTable.setEmpty(true);
177+
return orderTable;
178+
})
179+
.collect(Collectors.toList());
180+
tableGroup.setOrderTables(orderTables);
181+
Mockito.when(orderTableDao.findAllByIdIn(Mockito.anyList())).thenReturn(orderTables);
182+
Mockito.when(tableGroupDao.save(Mockito.any())).thenReturn(tableGroup);
183+
Mockito.when(orderTableDao.save(Mockito.any())).thenReturn(orderTables.get(0), orderTables.get(1));
184+
Mockito.when(orderDao.existsByOrderTableIdInAndOrderStatusIn(Mockito.anyList(), Mockito.anyList()))
185+
.thenReturn(false);
186+
return tableGroup;
187+
}
188+
}
189+
190+
@Nested
191+
@DisplayName("그룹 해제 성공")
192+
public class ErrorOrderStillCookingOrMeal {
193+
@Test
194+
public void testCase() {
195+
// given
196+
final TableGroup tableGroup = setup();
197+
final TableGroup createdTableGroup = tableGroupService.create(tableGroup);
198+
199+
// when - then
200+
assertThrows(IllegalArgumentException.class,
201+
() -> tableGroupService.ungroup(createdTableGroup.getId()));
202+
}
203+
204+
private TableGroup setup() {
205+
final TableGroup tableGroup = new TableGroup();
206+
tableGroup.setId(1L);
207+
final List<Long> orderTableIds = Stream.of(1, 2).map(Long::new).collect(Collectors.toList());
208+
final List<OrderTable> orderTables = orderTableIds
209+
.stream()
210+
.map(id -> {
211+
final OrderTable orderTable = new OrderTable();
212+
orderTable.setId(id);
213+
orderTable.setEmpty(true);
214+
return orderTable;
215+
})
216+
.collect(Collectors.toList());
217+
tableGroup.setOrderTables(orderTables);
218+
Mockito.when(orderTableDao.findAllByIdIn(Mockito.anyList())).thenReturn(orderTables);
219+
Mockito.when(tableGroupDao.save(Mockito.any())).thenReturn(tableGroup);
220+
Mockito.when(orderTableDao.save(Mockito.any())).thenReturn(orderTables.get(0), orderTables.get(1));
221+
Mockito.when(orderDao.existsByOrderTableIdInAndOrderStatusIn(Mockito.anyList(), Mockito.anyList()))
222+
.thenReturn(true);
223+
return tableGroup;
224+
}
225+
}
226+
}
227+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package kitchenpos.ui;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import kitchenpos.domain.OrderTable;
5+
import kitchenpos.domain.TableGroup;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Nested;
8+
import org.junit.jupiter.api.Test;
9+
import org.mockito.Mockito;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
12+
import org.springframework.boot.test.mock.mockito.MockBean;
13+
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.mock.web.MockHttpServletResponse;
17+
import org.springframework.test.web.servlet.MockMvc;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
25+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
26+
27+
@WebMvcTest(TableGroupRestController.class)
28+
@DisplayName("TableGroupRestController 클래스 테스트")
29+
public class TableGroupRestControllerTest {
30+
@Autowired
31+
private MockMvc mockMvc;
32+
@Autowired
33+
private ObjectMapper objectMapper;
34+
@MockBean
35+
private TableGroupRestController tableGroupRestController;
36+
37+
@Nested
38+
@DisplayName("POST /api/table-groups")
39+
public class PostMethod {
40+
@Test
41+
@DisplayName("성공적으로 테이블 그룹을 생성하면 200 상태 코드를 응답받는다")
42+
public void success() throws Exception {
43+
// given
44+
final TableGroup tableGroup = setupSuccess();
45+
46+
// when
47+
MockHttpServletResponse response = mockMvc.perform(post("/api/table-groups")
48+
.contentType(MediaType.APPLICATION_JSON)
49+
.content(objectMapper.writeValueAsString(tableGroup))
50+
.accept(MediaType.APPLICATION_JSON))
51+
.andReturn().getResponse();
52+
53+
// then
54+
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
55+
56+
// then
57+
final TableGroup tableGroupResponse =
58+
objectMapper.readValue(response.getContentAsString(), TableGroup.class);
59+
assertThat(tableGroupResponse.getOrderTables()).hasSize(tableGroup.getOrderTables().size());
60+
}
61+
62+
private TableGroup setupSuccess() {
63+
final TableGroup tableGroup = new TableGroup();
64+
final List<OrderTable> orderTables = Arrays.asList(new OrderTable(), new OrderTable());
65+
tableGroup.setOrderTables(orderTables);
66+
Mockito.when(tableGroupRestController.create(Mockito.any())).thenReturn(ResponseEntity.ok(tableGroup));
67+
return tableGroup;
68+
}
69+
70+
@Test
71+
@DisplayName("테이블 그룹을 생성하는데 실패하면 400 상태 코드를 응답받는다")
72+
public void errorBadRequest() throws Exception {
73+
// given
74+
final TableGroup tableGroup = setupErrorBadRequest();
75+
76+
// when
77+
MockHttpServletResponse response = mockMvc.perform(post("/api/table-groups")
78+
.contentType(MediaType.APPLICATION_JSON)
79+
.content(objectMapper.writeValueAsString(tableGroup))
80+
.accept(MediaType.APPLICATION_JSON))
81+
.andReturn().getResponse();
82+
83+
// then
84+
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
85+
}
86+
87+
private TableGroup setupErrorBadRequest() {
88+
final TableGroup tableGroup = new TableGroup();
89+
tableGroup.setOrderTables(new ArrayList<>());
90+
Mockito.when(tableGroupRestController.create(Mockito.any())).thenReturn(ResponseEntity.badRequest().build());
91+
return tableGroup;
92+
}
93+
}
94+
95+
@Nested
96+
@DisplayName("DELETE /api/table-groups/{tableGroupId}")
97+
public class DeleteMethod {
98+
@Test
99+
@DisplayName("성공적으로 테이블 그룹을 해제하면 204 상태 코드를 응답받는다")
100+
public void success() throws Exception {
101+
// given
102+
final TableGroup tableGroup = setupSuccess();
103+
104+
// when
105+
MockHttpServletResponse response = mockMvc
106+
.perform(delete("/api/table-groups/" + tableGroup.getId())
107+
.accept(MediaType.APPLICATION_JSON))
108+
.andReturn().getResponse();
109+
110+
// then
111+
assertThat(response.getStatus()).isEqualTo(HttpStatus.NO_CONTENT.value());
112+
}
113+
114+
private TableGroup setupSuccess() {
115+
final TableGroup tableGroup = new TableGroup();
116+
tableGroup.setId(1L);
117+
final List<OrderTable> orderTables = Arrays.asList(new OrderTable(), new OrderTable());
118+
tableGroup.setOrderTables(orderTables);
119+
Mockito.when(tableGroupRestController.ungroup(Mockito.anyLong()))
120+
.thenReturn(ResponseEntity.noContent().build());
121+
return tableGroup;
122+
}
123+
124+
@Test
125+
@DisplayName("테이블 그룹을 해제하는데 실패하면 400 상태 코드를 응답받는다")
126+
public void errorBadRequest() throws Exception {
127+
// given
128+
final TableGroup tableGroup = setupErrorBadRequest();
129+
130+
// when
131+
MockHttpServletResponse response = mockMvc
132+
.perform(delete("/api/table-groups/" + tableGroup.getId())
133+
.accept(MediaType.APPLICATION_JSON))
134+
.andReturn().getResponse();
135+
136+
// then
137+
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
138+
}
139+
140+
private TableGroup setupErrorBadRequest() {
141+
final TableGroup tableGroup = new TableGroup();
142+
tableGroup.setId(1L);
143+
final List<OrderTable> orderTables = Arrays.asList(new OrderTable(), new OrderTable());
144+
tableGroup.setOrderTables(orderTables);
145+
Mockito.when(tableGroupRestController.ungroup(Mockito.anyLong()))
146+
.thenReturn(ResponseEntity.badRequest().build());
147+
return tableGroup;
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)