@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 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(); } }
@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 testUpdateRestaurantSuccess() { Restaurant restaurant = createExoticRestaurant(); when(restaurantRepository.exists(Mockito.anyLong())).thenReturn(true); try { MvcResult updateRestaurantResponse = mockMvc .perform( post("/restaurant/update") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(restaurant))) .andExpect(status().isOk()) .andReturn(); String responseContent = updateRestaurantResponse.getResponse().getContentAsString(); assertTrue(responseContent.contains(Status.SUCCESS.getStatusValue())); } catch (Exception e) { fail(); } }
@Test public void testDeleteRestaurantSuccess() { when(restaurantRepository.exists(Long.MAX_VALUE)).thenReturn(true); Vote vote = new Vote(); vote.setRestaurantId(Long.MAX_VALUE); try { MvcResult deleteRestaurantResponse = mockMvc .perform( post("/restaurant/delete") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(vote))) .andExpect(status().isOk()) .andReturn(); String responseContent = deleteRestaurantResponse.getResponse().getContentAsString(); assertTrue(responseContent.contains(Status.SUCCESS.getStatusValue())); } catch (Exception e) { fail(); } }