Skip to content

Commit

Permalink
test: TableGroupRestController, TableGroupService 에 대한 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
OptimistLabyrinth committed Dec 12, 2022
1 parent 805ad80 commit 90f8335
Show file tree
Hide file tree
Showing 2 changed files with 377 additions and 0 deletions.
227 changes: 227 additions & 0 deletions src/test/java/kitchenpos/application/TableGroupServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package kitchenpos.application;

import kitchenpos.dao.OrderDao;
import kitchenpos.dao.OrderTableDao;
import kitchenpos.dao.TableGroupDao;
import kitchenpos.domain.OrderTable;
import kitchenpos.domain.TableGroup;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
@DisplayName("TableGroupService 클래스 테스트")
public class TableGroupServiceTest {
@Mock
private OrderDao orderDao;
@Mock
private OrderTableDao orderTableDao;
@Mock
private TableGroupDao tableGroupDao;
@InjectMocks
private TableGroupService tableGroupService;

@Nested
@DisplayName("create 메서드 테스트")
public class CreateMethod {
@Nested
@DisplayName("테이블 그룹 생성 성공")
public class Success {
@Test
public void testCase() {
// given
final TableGroup tableGroup = setup();

// when
final TableGroup createdTableGroup = tableGroupService.create(tableGroup);

// then
assertAll(
() -> assertThat(createdTableGroup.getId()).isPositive(),
() -> assertThat(createdTableGroup.getOrderTables()).isEqualTo(tableGroup.getOrderTables())
);
}

private TableGroup setup() {
final TableGroup tableGroup = new TableGroup();
tableGroup.setId(1L);
final List<Long> orderTableIds = Stream.of(1, 2).map(Long::new).collect(Collectors.toList());
final List<OrderTable> orderTables = orderTableIds
.stream()
.map(id -> {
final OrderTable orderTable = new OrderTable();
orderTable.setId(id);
orderTable.setEmpty(true);
return orderTable;
})
.collect(Collectors.toList());
tableGroup.setOrderTables(orderTables);
Mockito.when(orderTableDao.findAllByIdIn(Mockito.anyList())).thenReturn(orderTables);
Mockito.when(tableGroupDao.save(Mockito.any())).thenReturn(tableGroup);
Mockito.when(orderTableDao.save(Mockito.any())).thenReturn(orderTables.get(0), orderTables.get(1));
return tableGroup;
}
}

@Nested
@DisplayName("주문 테이블이 하나도 없으면 테이블 그룹 생성 실패")
public class ErrorOrderTableIsEmpty {
@Test
public void testCase() {
// given
final TableGroup tableGroup = setup();

// when - then
assertThrows(IllegalArgumentException.class, () -> tableGroupService.create(tableGroup));
}

private TableGroup setup() {
final TableGroup tableGroup = new TableGroup();
tableGroup.setOrderTables(new ArrayList<>());
return tableGroup;
}
}

@Nested
@DisplayName("주문 테이블이 한 개 뿐이면 테이블 그룹 생성 실패")
public class ErrorOnlyOneInOrderTables {
@Test
public void testCase() {
// given
final TableGroup tableGroup = setup();

// when - then
assertThrows(IllegalArgumentException.class, () -> tableGroupService.create(tableGroup));
}

private TableGroup setup() {
final TableGroup tableGroup = new TableGroup();
final List<OrderTable> orderTables = Arrays.asList(new OrderTable());
tableGroup.setOrderTables(orderTables);
return tableGroup;
}
}

@Nested
@DisplayName("주문 테이블 id 로 실제 주문 테이블을 하나라도 찾을 수 없으면 테이블 그룹 생성 실패")
public class ErrorsOrderTablesMissingWhenTryToFindById {
@Test
public void testCase() {
// given
final TableGroup tableGroup = setup();

// when - then
assertThrows(IllegalArgumentException.class, () -> tableGroupService.create(tableGroup));
}

private TableGroup setup() {
final TableGroup tableGroup = new TableGroup();
tableGroup.setId(1L);
final List<Long> orderTableIds = Stream.of(1, 2).map(Long::new).collect(Collectors.toList());
final List<OrderTable> orderTables = orderTableIds
.stream()
.map(id -> {
final OrderTable orderTable = new OrderTable();
orderTable.setId(id);
orderTable.setEmpty(true);
return orderTable;
})
.collect(Collectors.toList());
tableGroup.setOrderTables(orderTables);
Mockito.when(orderTableDao.findAllByIdIn(Mockito.anyList())).thenReturn(orderTables.subList(0, 1));
return tableGroup;
}
}
}

@Nested
@DisplayName("ungroup 메서드 테스트")
public class UngroupMethod {
@Nested
@DisplayName("그룹 해제 성공")
public class Success {
@Test
public void testCase() {
// given
final TableGroup tableGroup = setup();
final TableGroup createdTableGroup = tableGroupService.create(tableGroup);

// when - then
assertDoesNotThrow(() -> tableGroupService.ungroup(createdTableGroup.getId()));
}

private TableGroup setup() {
final TableGroup tableGroup = new TableGroup();
tableGroup.setId(1L);
final List<Long> orderTableIds = Stream.of(1, 2).map(Long::new).collect(Collectors.toList());
final List<OrderTable> orderTables = orderTableIds
.stream()
.map(id -> {
final OrderTable orderTable = new OrderTable();
orderTable.setId(id);
orderTable.setEmpty(true);
return orderTable;
})
.collect(Collectors.toList());
tableGroup.setOrderTables(orderTables);
Mockito.when(orderTableDao.findAllByIdIn(Mockito.anyList())).thenReturn(orderTables);
Mockito.when(tableGroupDao.save(Mockito.any())).thenReturn(tableGroup);
Mockito.when(orderTableDao.save(Mockito.any())).thenReturn(orderTables.get(0), orderTables.get(1));
Mockito.when(orderDao.existsByOrderTableIdInAndOrderStatusIn(Mockito.anyList(), Mockito.anyList()))
.thenReturn(false);
return tableGroup;
}
}

@Nested
@DisplayName("그룹 해제 성공")
public class ErrorOrderStillCookingOrMeal {
@Test
public void testCase() {
// given
final TableGroup tableGroup = setup();
final TableGroup createdTableGroup = tableGroupService.create(tableGroup);

// when - then
assertThrows(IllegalArgumentException.class,
() -> tableGroupService.ungroup(createdTableGroup.getId()));
}

private TableGroup setup() {
final TableGroup tableGroup = new TableGroup();
tableGroup.setId(1L);
final List<Long> orderTableIds = Stream.of(1, 2).map(Long::new).collect(Collectors.toList());
final List<OrderTable> orderTables = orderTableIds
.stream()
.map(id -> {
final OrderTable orderTable = new OrderTable();
orderTable.setId(id);
orderTable.setEmpty(true);
return orderTable;
})
.collect(Collectors.toList());
tableGroup.setOrderTables(orderTables);
Mockito.when(orderTableDao.findAllByIdIn(Mockito.anyList())).thenReturn(orderTables);
Mockito.when(tableGroupDao.save(Mockito.any())).thenReturn(tableGroup);
Mockito.when(orderTableDao.save(Mockito.any())).thenReturn(orderTables.get(0), orderTables.get(1));
Mockito.when(orderDao.existsByOrderTableIdInAndOrderStatusIn(Mockito.anyList(), Mockito.anyList()))
.thenReturn(true);
return tableGroup;
}
}
}
}
150 changes: 150 additions & 0 deletions src/test/java/kitchenpos/ui/TableGroupRestControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package kitchenpos.ui;

import com.fasterxml.jackson.databind.ObjectMapper;
import kitchenpos.domain.OrderTable;
import kitchenpos.domain.TableGroup;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

@WebMvcTest(TableGroupRestController.class)
@DisplayName("TableGroupRestController 클래스 테스트")
public class TableGroupRestControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private TableGroupRestController tableGroupRestController;

@Nested
@DisplayName("POST /api/table-groups")
public class PostMethod {
@Test
@DisplayName("성공적으로 테이블 그룹을 생성하면 200 상태 코드를 응답받는다")
public void success() throws Exception {
// given
final TableGroup tableGroup = setupSuccess();

// when
MockHttpServletResponse response = mockMvc.perform(post("/api/table-groups")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(tableGroup))
.accept(MediaType.APPLICATION_JSON))
.andReturn().getResponse();

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());

// then
final TableGroup tableGroupResponse =
objectMapper.readValue(response.getContentAsString(), TableGroup.class);
assertThat(tableGroupResponse.getOrderTables()).hasSize(tableGroup.getOrderTables().size());
}

private TableGroup setupSuccess() {
final TableGroup tableGroup = new TableGroup();
final List<OrderTable> orderTables = Arrays.asList(new OrderTable(), new OrderTable());
tableGroup.setOrderTables(orderTables);
Mockito.when(tableGroupRestController.create(Mockito.any())).thenReturn(ResponseEntity.ok(tableGroup));
return tableGroup;
}

@Test
@DisplayName("테이블 그룹을 생성하는데 실패하면 400 상태 코드를 응답받는다")
public void errorBadRequest() throws Exception {
// given
final TableGroup tableGroup = setupErrorBadRequest();

// when
MockHttpServletResponse response = mockMvc.perform(post("/api/table-groups")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(tableGroup))
.accept(MediaType.APPLICATION_JSON))
.andReturn().getResponse();

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
}

private TableGroup setupErrorBadRequest() {
final TableGroup tableGroup = new TableGroup();
tableGroup.setOrderTables(new ArrayList<>());
Mockito.when(tableGroupRestController.create(Mockito.any())).thenReturn(ResponseEntity.badRequest().build());
return tableGroup;
}
}

@Nested
@DisplayName("DELETE /api/table-groups/{tableGroupId}")
public class DeleteMethod {
@Test
@DisplayName("성공적으로 테이블 그룹을 해제하면 204 상태 코드를 응답받는다")
public void success() throws Exception {
// given
final TableGroup tableGroup = setupSuccess();

// when
MockHttpServletResponse response = mockMvc
.perform(delete("/api/table-groups/" + tableGroup.getId())
.accept(MediaType.APPLICATION_JSON))
.andReturn().getResponse();

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.NO_CONTENT.value());
}

private TableGroup setupSuccess() {
final TableGroup tableGroup = new TableGroup();
tableGroup.setId(1L);
final List<OrderTable> orderTables = Arrays.asList(new OrderTable(), new OrderTable());
tableGroup.setOrderTables(orderTables);
Mockito.when(tableGroupRestController.ungroup(Mockito.anyLong()))
.thenReturn(ResponseEntity.noContent().build());
return tableGroup;
}

@Test
@DisplayName("테이블 그룹을 해제하는데 실패하면 400 상태 코드를 응답받는다")
public void errorBadRequest() throws Exception {
// given
final TableGroup tableGroup = setupErrorBadRequest();

// when
MockHttpServletResponse response = mockMvc
.perform(delete("/api/table-groups/" + tableGroup.getId())
.accept(MediaType.APPLICATION_JSON))
.andReturn().getResponse();

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
}

private TableGroup setupErrorBadRequest() {
final TableGroup tableGroup = new TableGroup();
tableGroup.setId(1L);
final List<OrderTable> orderTables = Arrays.asList(new OrderTable(), new OrderTable());
tableGroup.setOrderTables(orderTables);
Mockito.when(tableGroupRestController.ungroup(Mockito.anyLong()))
.thenReturn(ResponseEntity.badRequest().build());
return tableGroup;
}
}
}

0 comments on commit 90f8335

Please sign in to comment.