|
| 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 | +} |
0 commit comments