Skip to content

Commit

Permalink
test: testfile
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunw9 committed Sep 23, 2023
1 parent bd7e3a2 commit acbffc6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 25 deletions.
8 changes: 6 additions & 2 deletions src/test/java/com/bibum_server/domain/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -25,7 +29,7 @@ public static Room CreateTestRoom() {
.build();
}
public static List<Restaurant> CreateTestRestaurantList(Room room){
return LongStream.range(1L, 11L)
return LongStream.range(1L, 6L)
.mapToObj((i) -> Restaurant.builder()
.room(room)
.id(i)
Expand All @@ -39,7 +43,7 @@ public static List<Restaurant> CreateTestRestaurantList(Room room){
).toList();
}
public static List<Restaurant> CreatereSuggestedRestaurantList(Room room){
return LongStream.range(1L, 11L)
return LongStream.range(LEAST_ID, MAX_ID)
.mapToObj((i) -> Restaurant.builder()
.room(room)
.id(i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -57,7 +55,7 @@ void createRoom() throws Exception {
Room room = TestUtil.CreateTestRoom();

List<Restaurant> restaurantList = TestUtil.CreateTestRestaurantList(room);
room.addRestaurant(restaurantList);
room.addRestaurants(restaurantList);

List<RestaurantRes> restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList();
RoomRes mockResponse = RoomRes.builder()
Expand All @@ -83,7 +81,7 @@ void ReSuggest() throws Exception {
Room room = TestUtil.CreateTestRoom();
List<Restaurant> restaurantList = TestUtil.CreatereSuggestedRestaurantList(room);
List<RestaurantRes> 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())
Expand All @@ -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<Restaurant> restaurantList = TestUtil.CreateTestRestaurantList(room);
room.addRestaurant(restaurantList);
room.addRestaurants(restaurantList);

List<RestaurantRes> restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList();
RoomRes mockResponse = RoomRes.builder()
Expand All @@ -118,6 +117,45 @@ void retry() throws Exception {
.andDo(restDocs.document());
}

@Test
void getRoomInfo() throws Exception {
long roomId = 1L;
Room room = TestUtil.CreateTestRoom();
List<Restaurant> restaurantList = TestUtil.CreateTestRestaurantList(room);
List<RestaurantRes> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
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;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
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;
Expand All @@ -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<Long> 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<VoteRes.RestaurantVote> 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());

Expand All @@ -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))
Expand Down

0 comments on commit acbffc6

Please sign in to comment.