@Test public void vote() throws Exception { ObjectMapper mapper = new ObjectMapper(); Restaurant restaurantB = mapper.readValue(new StringReader(testRestaurantJson1), Restaurant.class); restaurantRepository.saveAndFlush(restaurantB); Restaurant restaurantA = mapper.readValue(new StringReader(testRestaurantJson2), Restaurant.class); restaurantRepository.saveAndFlush(restaurantA); doVote(restaurantB.getRestaurantName(), "qqq"); doVote(restaurantB.getRestaurantName(), "www"); doVote(restaurantB.getRestaurantName(), "ddd"); doVote(restaurantA.getRestaurantName(), "kkk"); doVote(restaurantA.getRestaurantName(), "lll"); List<Vote> votes = votesRepository.findAll(); // check all votes are here assertEquals(MAX_TOTAL_VOTES, votes.size()); Map<Long, Long> res = votes.stream().collect(Collectors.groupingBy(Vote::getRestaurantId, Collectors.counting())); // check all votes counter correctly assertEquals(3l, res.get(restaurantB.getId()).longValue()); assertEquals(2l, res.get(restaurantA.getId()).longValue()); }
@Test public void voteChangeAndTimeout() throws Exception { // do 5 votes vote(); // change mind, vote for another restaurant doVote(RESTAURANT_A_NAME, "qqq"); Restaurant restA = restaurantRepository.findByRestaurantName(RESTAURANT_A_NAME); Restaurant restB = restaurantRepository.findByRestaurantName(RESTAURANT_B_NAME); checkResults(restA, restB); // time-up ((MockTimeService) timeService).setTime(15, 00); mockMvc .perform( put("/vote/vote-by-name/" + RESTAURANT_B_NAME) .contentType(MediaType.APPLICATION_JSON) .with(httpBasic("zzz", ""))) .andExpect(status().isNotFound()); // check nothing was changed checkResults(restA, restB); }
@Before public void setup() throws Exception { this.mockMvc = webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); restaurantRepository.deleteAll(); ((MockTimeService) timeService).setTime(10, 00); }