Advertisement
Guest User

Untitled

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