@Test public void testVoteNone() { Vote correctVote = new Vote(); correctVote.setRestaurantId(Long.MIN_VALUE); Restaurant restaurant = new Restaurant(); restaurant.setRestaurantId(Long.MIN_VALUE); when(restaurantRepository.findOne(Long.MIN_VALUE)).thenReturn(restaurant); Restaurant votingRestaurant = new Restaurant(); votingRestaurant.setRestaurantId(Long.MIN_VALUE); Voting identicalVoting = new Voting(); identicalVoting.setRestaurant(votingRestaurant); when(votingRepository.findByUser("admin")).thenReturn(identicalVoting); try { MvcResult outdatedVoteRes = mockMvc .perform( post("/vote") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(correctVote))) .andExpect(status().isOk()) .andReturn(); String outdatedVoteResString = outdatedVoteRes.getResponse().getContentAsString(); assertTrue(outdatedVoteResString.contains(Status.IGNORED.getStatusValue())); } catch (Exception e) { fail(); } }
@Test public void testVoteSuccess() { Vote correctVote = new Vote(); correctVote.setRestaurantId(Long.MIN_VALUE); Restaurant restaurant = new Restaurant(); restaurant.setRestaurantId(Long.MIN_VALUE); when(restaurantRepository.findOne(Long.MIN_VALUE)).thenReturn(restaurant); when(votingRepository.findByUser("admin")).thenReturn(null); try { MvcResult successVoteRes = mockMvc .perform( post("/vote") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(correctVote))) .andExpect(status().isOk()) .andReturn(); String outdatedVoteResString = successVoteRes.getResponse().getContentAsString(); assertTrue(outdatedVoteResString.contains(Status.SUCCESS.getStatusValue())); } catch (Exception e) { fail(); } LocalTime currentTime = LocalTime.now(); if (currentTime.isBefore(RestaurantController.DEADLINE)) { Restaurant votingRestaurant = new Restaurant(); votingRestaurant.setRestaurantId(Long.MAX_VALUE); Voting identicalVoting = new Voting(); identicalVoting.setRestaurant(votingRestaurant); when(votingRepository.findByUser("admin")).thenReturn(identicalVoting); try { MvcResult actualVoteRes = mockMvc .perform( post("/vote") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(correctVote))) .andExpect(status().isOk()) .andReturn(); String outdatedVoteResString = actualVoteRes.getResponse().getContentAsString(); assertTrue(outdatedVoteResString.contains(Status.SUCCESS.getStatusValue())); } catch (Exception e) { fail(); } } }
@Test public void testVoteError() { try { Vote wrongVote = new Vote(); wrongVote.setRestaurantId(Long.MAX_VALUE); when(restaurantRepository.findOne(Long.MAX_VALUE)).thenReturn(null); MvcResult wrongIdVoteRes = mockMvc .perform( post("/vote") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(wrongVote))) .andExpect(status().isOk()) .andReturn(); String wrongIdVoteResponseString = wrongIdVoteRes.getResponse().getContentAsString(); assertTrue(wrongIdVoteResponseString.contains(Status.FAILED.getStatusValue())); assertTrue(wrongIdVoteResponseString.contains(RestaurantController.WRONG_RESTAURANT_MSG)); LocalTime currentTime = LocalTime.now(); if (currentTime.isAfter(RestaurantController.DEADLINE)) { Vote correctVote = new Vote(); correctVote.setRestaurantId(Long.MIN_VALUE); Restaurant restaurant = new Restaurant(); restaurant.setRestaurantId(Long.MIN_VALUE); when(restaurantRepository.findOne(Long.MIN_VALUE)).thenReturn(restaurant); Restaurant votingRestaurant = new Restaurant(); votingRestaurant.setRestaurantId(Long.MAX_VALUE); Voting outdatedVoting = new Voting(); outdatedVoting.setRestaurant(votingRestaurant); when(votingRepository.findByUser("admin")).thenReturn(outdatedVoting); MvcResult outdatedVoteRes = mockMvc .perform( post("/vote") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(correctVote))) .andExpect(status().isOk()) .andReturn(); String outdatedVoteResString = outdatedVoteRes.getResponse().getContentAsString(); assertTrue(outdatedVoteResString.contains(Status.FAILED.getStatusValue())); } } catch (Exception e) { fail(); } }
@Test public void testCollect() { Restaurant exoticRestaurant = createExoticRestaurant(); Restaurant traditionalRestaurant = createTraditionalRestaurant(); Voting voting1 = new Voting(); voting1.setRestaurant(traditionalRestaurant); voting1.setUser("user1"); Voting voting2 = new Voting(); voting2.setRestaurant(exoticRestaurant); voting2.setUser("user2"); when(votingRepository.findAll()).thenReturn(Arrays.asList(voting1, voting2)); try { MvcResult collectRestaurantResponse = mockMvc.perform(get("/collect")).andExpect(status().isOk()).andReturn(); String responseContent = collectRestaurantResponse.getResponse().getContentAsString(); assertTrue(responseContent.contains(Status.SUCCESS.getStatusValue())); } catch (Exception e) { fail(); } }
@Test public void testTopVotedSuccess() { Long exoticRestaurantId = new Random().nextLong(); Restaurant exoticRestaurant = createExoticRestaurant(); exoticRestaurant.setRestaurantId(exoticRestaurantId); Long traditionalRestaurantId = new Random().nextLong(); Restaurant traditionalRestaurant = createTraditionalRestaurant(); traditionalRestaurant.setRestaurantId(traditionalRestaurantId); Voting voting1 = new Voting(); voting1.setRestaurant(traditionalRestaurant); voting1.setUser("user1"); Voting voting2 = new Voting(); voting2.setRestaurant(traditionalRestaurant); voting2.setUser("user2"); traditionalRestaurant.setVotings(Arrays.asList(voting1, voting2)); Voting voting3 = new Voting(); voting3.setRestaurant(exoticRestaurant); voting3.setUser("user3"); exoticRestaurant.setVotings(Arrays.asList(voting3)); when(votingRepository.findAll()).thenReturn(Arrays.asList(voting1, voting2, voting3)); when(restaurantRepository.findOne(exoticRestaurantId)).thenReturn(exoticRestaurant); when(restaurantRepository.findOne(traditionalRestaurantId)).thenReturn(traditionalRestaurant); try { MvcResult topVotedRestaurantResponse = mockMvc.perform(get("/top")).andExpect(status().isOk()).andReturn(); String responseContent = topVotedRestaurantResponse.getResponse().getContentAsString(); assertTrue(responseContent.contains(Status.SUCCESS.getStatusValue())); assertTrue(responseContent.contains(traditionalRestaurant.getName())); assertTrue(responseContent.contains(voting1.getUser())); assertTrue(responseContent.contains(voting2.getUser())); assertFalse(responseContent.contains(exoticRestaurant.getName())); assertFalse(responseContent.contains(voting3.getUser())); } catch (Exception e) { fail(); } }