Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. /**
  2. * Create statistics included point, matches played, matches result, point and level for player.
  3. *
  4. * @param id Id players.
  5. * @return Statistics of player.
  6. */
  7. public GuiStatisticsDto createUserStatistic(Long id) {
  8. return new GuiStatisticsDto(statisticsDao.findById(id), userDao.findById(id));
  9. }
  10.  
  11. /**
  12. * Method show statistic for user by his id.
  13. */
  14. @RequestMapping(value = "/userStatistic/{id}", method = {RequestMethod.POST, RequestMethod.GET})
  15. public GuiStatisticsDto userStatistic(@PathVariable Long id){
  16. return statisticsService.createUserStatistic(id);
  17. }
  18.  
  19. package com.capgemini.chess.rest;
  20.  
  21. import java.util.Arrays;
  22. import java.util.List;
  23.  
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.mockito.InjectMocks;
  27. import org.mockito.Mock;
  28. import org.mockito.Mockito;
  29. import org.mockito.MockitoAnnotations;
  30. import org.springframework.http.MediaType;
  31. import org.springframework.test.web.servlet.MockMvc;
  32. import org.springframework.test.web.servlet.ResultActions;
  33. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  34. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  35.  
  36. import com.capgemini.chess.dataaccess.dao.StatisticsDao;
  37. import com.capgemini.chess.dataaccess.dao.UserDao;
  38. import com.capgemini.chess.dataaccess.dto.GuiStatisticsDto;
  39. import com.capgemini.chess.dataaccess.dto.StatisticsDto;
  40. import com.capgemini.chess.dataaccess.dto.UserDto;
  41. import com.capgemini.chess.dataaccess.enums.Level;
  42. import com.capgemini.chess.service.StatisticsService;
  43. import com.capgemini.chess.service.UserRegistrationService;
  44.  
  45. import static org.hamcrest.Matchers.hasSize;
  46. import static org.hamcrest.core.Is.is;
  47. import static org.mockito.Mockito.*;
  48. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  49. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  50.  
  51. public class RestServiceControllerTest {
  52. private MockMvc mockMvc;
  53.  
  54. @Mock
  55. private UserRegistrationService userRegistrationService;
  56.  
  57. @Mock
  58. private StatisticsService statisticsService;
  59.  
  60. @Mock
  61. private StatisticsDao statisticsDao;
  62.  
  63. @Mock
  64. private UserDao userDao;
  65.  
  66. @InjectMocks
  67. private RestServiceController restServiceController;
  68.  
  69. @Before
  70. public void setup(){
  71.  
  72. MockitoAnnotations.initMocks(this);
  73. mockMvc = MockMvcBuilders.standaloneSetup(restServiceController).build();
  74. }
  75. @Test
  76. public void shouldShowStatisticsForUser() throws Exception {
  77. //given
  78. List<StatisticsDto> statisticsForUsers = Arrays.asList(
  79. new StatisticsDto(1l, Level.NEWBIE, 20L, 2L, 1L, 1L, 0L),
  80. new StatisticsDto(2l, Level.CHUCK_NORRIS_OF_CHESS, 80000L, 500L, 495L, 1L, 4L),
  81. new StatisticsDto(3l, Level.BEGINNER, 700L, 30L, 28L, 2L, 0L));
  82. List<UserDto> users = Arrays.asList(
  83. new UserDto(1L, "monika.zaborowska@gmail.com", "12345678", "Monika", "Zaborowska", "moska14", "lubie podróże", "YOLO"),
  84. new UserDto(2L, "michal1@gmail.com", "12345678", "Michał", "Zaborowski", "minonek", "lubie tenis", "YOLO"),
  85. new UserDto(3L, "ania@gmail.com", "12345678", "Ania", "Pokrzywinska", "pokrzywasis", "lubie pikłę", "YOLO"));
  86. List<GuiStatisticsDto> usersRanking = Arrays.asList(
  87. new GuiStatisticsDto(statisticsForUsers.get(0), users.get(0)),
  88. new GuiStatisticsDto(statisticsForUsers.get(1), users.get(1)),
  89. new GuiStatisticsDto(statisticsForUsers.get(2), users.get(2)));
  90.  
  91. Mockito.when(statisticsDao.findById(1L)).thenReturn(statisticsForUsers.get(0));
  92. Mockito.when(userDao.findById(1L)).thenReturn(users.get(0));
  93.  
  94. ResultActions resultActions = mockMvc.perform(get("/userStatistic/{id}", 1));
  95.  
  96. //then
  97. resultActions.andExpect(status().isOk())
  98. .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  99. .andExpect(jsonPath("[0].id").value(usersRanking.get(0).getId().intValue()))
  100. .andExpect(jsonPath("[0].name").value(usersRanking.get(0).getName()));
  101.  
  102. verify(statisticsService, times(1)).createUserStatistic(1L);
  103. verifyNoMoreInteractions(statisticsService);
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement