Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.capgemini.chess.rest;
- import java.util.Arrays;
- import java.util.List;
- import org.junit.Before;
- import org.junit.Test;
- import org.mockito.InjectMocks;
- import org.mockito.Mock;
- import org.mockito.Mockito;
- import org.mockito.MockitoAnnotations;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.MediaType;
- import org.springframework.test.web.servlet.MockMvc;
- import org.springframework.test.web.servlet.ResultActions;
- import org.springframework.test.web.servlet.setup.MockMvcBuilders;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.servlet.view.InternalResourceViewResolver;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
- import com.capgemini.chess.dataaccess.dao.StatisticsDao;
- import com.capgemini.chess.dataaccess.dao.UserDao;
- import com.capgemini.chess.dataaccess.dto.GuiStatisticsDto;
- import com.capgemini.chess.dataaccess.dto.StatisticsDto;
- import com.capgemini.chess.dataaccess.dto.UserDto;
- import com.capgemini.chess.dataaccess.enums.Level;
- import com.capgemini.chess.service.StatisticsService;
- import com.capgemini.chess.service.UserRegistrationService;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.http.*;
- import static org.hamcrest.Matchers.containsString;
- import static org.hamcrest.Matchers.hasSize;
- import static org.hamcrest.core.Is.is;
- import static org.mockito.Mockito.*;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
- public class RestServiceControllerTest {
- private MockMvc mockMvc;
- @Mock
- private UserRegistrationService userRegistrationService;
- @Mock
- private StatisticsService statisticsService;
- @Mock
- private StatisticsDao statisticsDao;
- @Mock
- private UserDao userDao;
- // @Autowired
- // private WebApplicationContext wac;
- @InjectMocks
- private RestServiceController restServiceController;
- @Before
- public void init(){
- InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- viewResolver.setPrefix("/WEB-INF/views/");
- viewResolver.setSuffix(".jsp");
- MockitoAnnotations.initMocks(this);
- //this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
- mockMvc = MockMvcBuilders.standaloneSetup(restServiceController).setViewResolvers(viewResolver).build();
- }
- @Test
- public void shouldShowStatisticsForUser() throws Exception {
- //given
- List<StatisticsDto> statisticsForUsers = Arrays.asList(
- new StatisticsDto(1l, Level.NEWBIE, 20L, 2L, 1L, 1L, 0L),
- new StatisticsDto(2l, Level.CHUCK_NORRIS_OF_CHESS, 80000L, 500L, 495L, 1L, 4L),
- new StatisticsDto(3l, Level.BEGINNER, 700L, 30L, 28L, 2L, 0L));
- List<UserDto> users = Arrays.asList(
- new UserDto(1L, "monika.zaborowska@gmail.com", "12345678", "Monika", "Zaborowska", "moska14", "lubie podróże", "YOLO"),
- new UserDto(2L, "michal1@gmail.com", "12345678", "Michał", "Zaborowski", "minonek", "lubie tenis", "YOLO"),
- new UserDto(3L, "ania@gmail.com", "12345678", "Ania", "Pokrzywinska", "pokrzywasis", "lubie pikłę", "YOLO"));
- List<GuiStatisticsDto> usersRanking = Arrays.asList(
- new GuiStatisticsDto(statisticsForUsers.get(0), users.get(0)),
- new GuiStatisticsDto(statisticsForUsers.get(1), users.get(1)),
- new GuiStatisticsDto(statisticsForUsers.get(2), users.get(2)));
- Mockito.when(statisticsDao.findById(1L)).thenReturn(statisticsForUsers.get(0));
- Mockito.when(userDao.findById(1L)).thenReturn(users.get(0));
- // ResultActions resultActions = mockMvc.perform(get("/userStatistic/{id}", 1L).accept(MediaType.APPLICATION_JSON)
- // .contentType(MediaType.APPLICATION_JSON).content("1").param("id", "1"));
- ResultActions resultActions = mockMvc.perform(get("/userStatistic/{id}", 1));
- resultActions.andExpect(status().isOk())
- .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
- .andExpect(jsonPath("[0].id").value(usersRanking.get(0).getId().intValue()))
- .andExpect(jsonPath("[0].name").value(usersRanking.get(0).getName()));
- // verify(userService, times(1)).getAll();
- // verifyNoMoreInteractions(userService);
- }
- /**
- * Method show statistic for user by his id.
- */
- @RequestMapping(value = "/userStatistic/{id}", method = {RequestMethod.POST, RequestMethod.GET})
- public GuiStatisticsDto userStatistic(@PathVariable Long id){
- return statisticsService.createUserStatistic(id);
- }
- /**
- * Create statistics included point, matches played, matches result, point and level for player.
- *
- * @param id Id players.
- * @return Statistics of player.
- */
- public GuiStatisticsDto createUserStatistic(Long id) {
- return new GuiStatisticsDto(statisticsDao.findById(id), userDao.findById(id));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement