@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 testUpdateRestaurantFail() { Restaurant restaurant = createExoticRestaurant(); when(restaurantRepository.exists(Mockito.anyLong())).thenReturn(false); try { MvcResult updateRestaurantResponse = mockMvc .perform( post("/restaurant/delete") .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(restaurant))) .andExpect(status().isOk()) .andReturn(); String responseContent = updateRestaurantResponse.getResponse().getContentAsString(); assertTrue(responseContent.contains(Status.FAILED.getStatusValue())); } catch (Exception e) { fail(); } }
@Test public void testDeleteRestaurantFail() { when(restaurantRepository.exists(Mockito.anyLong())).thenReturn(false); 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.FAILED.getStatusValue())); } catch (Exception e) { fail(); } }