From acbffc6b4a22044c16efd2a3203cea4fc7dfd0f8 Mon Sep 17 00:00:00 2001 From: hyunw9 Date: Sat, 23 Sep 2023 16:53:40 +0900 Subject: [PATCH] test: testfile --- .../com/bibum_server/domain/TestUtil.java | 8 ++- .../presentation/RoomControllerTest.java | 50 ++++++++++++++--- .../presentation/VoteRestaurantTest.java | 53 +++++++++++++------ 3 files changed, 86 insertions(+), 25 deletions(-) diff --git a/src/test/java/com/bibum_server/domain/TestUtil.java b/src/test/java/com/bibum_server/domain/TestUtil.java index 1da34ef..e474da0 100644 --- a/src/test/java/com/bibum_server/domain/TestUtil.java +++ b/src/test/java/com/bibum_server/domain/TestUtil.java @@ -15,6 +15,10 @@ import java.util.stream.LongStream; public class TestUtil { + + public static final long LEAST_ID = 1L; + public static final long MAX_ID = 6L; + public static Room CreateTestRoom() { return Room.builder() .id(1L) @@ -25,7 +29,7 @@ public static Room CreateTestRoom() { .build(); } public static List CreateTestRestaurantList(Room room){ - return LongStream.range(1L, 11L) + return LongStream.range(1L, 6L) .mapToObj((i) -> Restaurant.builder() .room(room) .id(i) @@ -39,7 +43,7 @@ public static List CreateTestRestaurantList(Room room){ ).toList(); } public static List CreatereSuggestedRestaurantList(Room room){ - return LongStream.range(1L, 11L) + return LongStream.range(LEAST_ID, MAX_ID) .mapToObj((i) -> Restaurant.builder() .room(room) .id(i) diff --git a/src/test/java/com/bibum_server/domain/presentation/RoomControllerTest.java b/src/test/java/com/bibum_server/domain/presentation/RoomControllerTest.java index a5d7e42..1d7f03c 100644 --- a/src/test/java/com/bibum_server/domain/presentation/RoomControllerTest.java +++ b/src/test/java/com/bibum_server/domain/presentation/RoomControllerTest.java @@ -36,14 +36,12 @@ @WebMvcTest(TodayMenuController.class) class RoomControllerTest extends AbstractRestDocsTests { + ObjectMapper mapper = new ObjectMapper(); @Autowired private MockMvc mockMvc; - @MockBean private RoomService roomService; - ObjectMapper mapper = new ObjectMapper(); - @DisplayName("Create Room.") @Test void createRoom() throws Exception { @@ -57,7 +55,7 @@ void createRoom() throws Exception { Room room = TestUtil.CreateTestRoom(); List restaurantList = TestUtil.CreateTestRestaurantList(room); - room.addRestaurant(restaurantList); + room.addRestaurants(restaurantList); List restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList(); RoomRes mockResponse = RoomRes.builder() @@ -83,7 +81,7 @@ void ReSuggest() throws Exception { Room room = TestUtil.CreateTestRoom(); List restaurantList = TestUtil.CreatereSuggestedRestaurantList(room); List restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList(); - given(roomService.ReSuggestRestaurant(roomId)).willReturn(RoomRes.builder().id(room.getId()) + given(roomService.ReSuggestRestaurants(roomId)).willReturn(RoomRes.builder().id(room.getId()) .total(room.getTotal()) .x(room.getX()) .y(room.getY()) @@ -94,13 +92,14 @@ void ReSuggest() throws Exception { .andExpect(status().isOk()) .andDo(restDocs.document()); } + @Test void retry() throws Exception { long roomId = 1L; Room room = TestUtil.CreateTestRoom(); List restaurantList = TestUtil.CreateTestRestaurantList(room); - room.addRestaurant(restaurantList); + room.addRestaurants(restaurantList); List restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList(); RoomRes mockResponse = RoomRes.builder() @@ -118,6 +117,45 @@ void retry() throws Exception { .andDo(restDocs.document()); } + @Test + void getRoomInfo() throws Exception { + long roomId = 1L; + Room room = TestUtil.CreateTestRoom(); + List restaurantList = TestUtil.CreateTestRestaurantList(room); + List restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList(); + RoomRes mockResponse = RoomRes.builder() + .id(room.getId()) + .x(room.getX()) + .y(room.getY()) + .total(room.getTotal()) + .restaurantResList(restaurantResList) + .build(); + given(roomService.getRoomInfo(any(Long.class))).willReturn(mockResponse); + this.mockMvc.perform(RestDocumentationRequestBuilders.get("/{roomId}", roomId)) + .andExpect(status().isOk()) + .andDo(restDocs.document()); + } + @Test + void ReSuggestOneRestaurant() throws Exception{ + long roomId = 1L; + long restaurantId = 1L; + Room room = TestUtil.CreateTestRoom(); + Restaurant restaurant = Restaurant.builder() + .room(room) + .id(1L) + .title("ReSuggestedRestaurant") + .link("www.ResuggestURL.com") + .distance(123L) + .count(0L) + .category("TestCategory") + .address("testAddress") + .build(); + RestaurantRes response = RestaurantRes.fromEntity(restaurant); + given(roomService.reSuggestOneRestaurant(any(Long.class),any(Long.class))).willReturn(response); + this.mockMvc.perform(RestDocumentationRequestBuilders.post("/{roomId}/resuggest/{restaurantId}",roomId,restaurantId)) + .andExpect(status().isOk()) + .andDo(restDocs.document()); + } } diff --git a/src/test/java/com/bibum_server/domain/presentation/VoteRestaurantTest.java b/src/test/java/com/bibum_server/domain/presentation/VoteRestaurantTest.java index afb5d2c..edca72f 100644 --- a/src/test/java/com/bibum_server/domain/presentation/VoteRestaurantTest.java +++ b/src/test/java/com/bibum_server/domain/presentation/VoteRestaurantTest.java @@ -3,11 +3,14 @@ import com.bibum_server.domain.AbstractRestDocsTests; import com.bibum_server.domain.TestUtil; import com.bibum_server.domain.application.RoomService; +import com.bibum_server.domain.dto.request.VoteReq; import com.bibum_server.domain.dto.response.MostPopularRestaurantRes; import com.bibum_server.domain.dto.response.RestaurantRes; +import com.bibum_server.domain.dto.response.VoteRes; import com.bibum_server.domain.restaurant.entity.Restaurant; import com.bibum_server.domain.restaurant.repository.RestaurantRepository; import com.bibum_server.domain.room.entity.Room; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -15,10 +18,14 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; import org.springframework.test.web.servlet.MockMvc; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.LongStream; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; @@ -37,28 +44,43 @@ public class VoteRestaurantTest extends AbstractRestDocsTests { @MockBean private RestaurantRepository restaurantRepository; + ObjectMapper mapper = new ObjectMapper(); + + @DisplayName("Vote Restaurant") @Test void voteRestaurant() throws Exception{ //given long roomId = 1L; - long restaurantId = 2L; - RestaurantRes restaurantRes= RestaurantRes.builder() - .id(1L) - .title("Test Food") - .category("TestCategory") - .link("www.test.com") - .count(1L) - .distance(1L) - .rank(0L) - .roomId(1L) - .address("TestAddress") + List restaurantId = new ArrayList<>(); + restaurantId.add(1L); + restaurantId.add(2L); + VoteReq voteReq = VoteReq.builder() + .restaurantIdList(restaurantId) .build(); - given(roomService.voteRestaurant(any(),any())).willReturn(restaurantRes); + String VoteRequest = mapper.writeValueAsString(voteReq); + + List voteResList = LongStream.range(1L,3L).mapToObj(i->{ + return VoteRes.RestaurantVote.builder() + .roomId(i) + .id(i) + .rank(0L) + .address("testAddress") + .distance(999L) + .link("www.test.com") + .count(1L) + .category("testCategory") + .title("testRestaurant") + .build(); + }).toList(); + VoteRes voteRes = VoteRes.builder().restaurantVotes(voteResList).build(); + + given(roomService.voteRestaurant(any(),any())).willReturn(voteRes); - this.mockMvc.perform(RestDocumentationRequestBuilders.post("/{roomId}/vote/{restaurantId}", - roomId,restaurantId)) + this.mockMvc.perform(RestDocumentationRequestBuilders.post("/{roomId}/vote", + roomId).contentType(MediaType.APPLICATION_JSON) + .content(VoteRequest)) .andExpect(status().isOk()) .andDo(restDocs.document()); @@ -74,9 +96,6 @@ void checkBestRestaurant() throws Exception { MostPopularRestaurantRes mostPopularRestaurantRes = TestUtil.CreateTestBestRestaurantList(room,restaurantList); given(restaurantRepository.findAllByRoomId(roomId)).willReturn(restaurantList); given(roomService.checkBestRestaurant(roomId)).willReturn(mostPopularRestaurantRes); - - - //then this.mockMvc.perform(RestDocumentationRequestBuilders.get("/{roomId}/result",roomId))