Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. @Configuration
  2. @EnableWebSecurity
  3. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  4.  
  5. public final static String ROLE_ADMIN = "ADMIN";
  6. public final static String ROLE_USER = "USER";
  7.  
  8. /**
  9. * Determines the resource access for different account types
  10. */
  11. @Override
  12. protected void configure(HttpSecurity http) throws Exception {
  13.  
  14. http
  15. .authorizeRequests()
  16. .antMatchers("/user/create").permitAll()
  17. .antMatchers("/admin/**").hasRole(ROLE_ADMIN)
  18. .anyRequest().authenticated()
  19. .and()
  20. .csrf().disable()
  21. .httpBasic();
  22. }
  23.  
  24. @Override
  25. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  26. auth.userDetailsService(inMemoryUserDetailsManager());
  27. }
  28.  
  29. /**
  30. * Initially fills Spring Security with default accounts
  31. */
  32. @Bean
  33. public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
  34.  
  35. final Properties users = new Properties();
  36. users.put("user","pass,ROLE_USER,enabled"); //login = user, password = pass
  37. users.put("admin","pass,ROLE_ADMIN,enabled"); //login = admin, password = pass
  38. return new InMemoryUserDetailsManager(users);
  39. }
  40. }
  41.  
  42. @RestController
  43. public class MovieController {
  44.  
  45. @Autowired @Qualifier("MovieService")//not relevant
  46. private MovieService ms;
  47.  
  48. @Autowired @Qualifier("CastService")//not relevant
  49. private CastService cs;
  50.  
  51. @RequestMapping(value = "admin/movies", method = GET)
  52. public List<Movie> selectAllMovies(){
  53.  
  54. return ms.selectAll();
  55. }
  56.  
  57. //the rest of the code..
  58. }
  59.  
  60. @RunWith(SpringRunner.class)
  61. @SpringBootTest
  62. @Transactional
  63. public class MovieControllerTest {
  64.  
  65. @Mock
  66. private MovieService movieService;
  67.  
  68. @Mock
  69. private CastService castService;
  70.  
  71. @Mock
  72. private ActorService actorService;
  73.  
  74. @Mock
  75. private UserService userService;
  76.  
  77. @InjectMocks
  78. private MovieController movieController;
  79.  
  80. private MockMvc mvc;
  81.  
  82. @Before
  83. public void setUp(){
  84.  
  85. MockitoAnnotations.initMocks(this);
  86.  
  87. mvc = MockMvcBuilders.standaloneSetup(movieController).build();
  88. }
  89.  
  90. @Test
  91. @WithMockUser(roles = "ADMIN", username = "admin", password = "pass")
  92. @WithUserDetails("admin")
  93. public void testGetAllMovies() throws Exception {
  94.  
  95. List<Movie> movieList = Arrays.asList(
  96. new Movie("title1", "desc1", MovieType.newest, 10f, true),
  97. new Movie("title2", "desc2", MovieType.newest, 10f, true),
  98. new Movie("title3", "desc3", MovieType.newest, 10f, true));
  99.  
  100. when(movieService.selectAll()).thenReturn(movieList);
  101.  
  102. String uri = "admin/movies";
  103.  
  104. MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri)
  105. .accept(MediaType.APPLICATION_JSON)).andReturn();
  106.  
  107. String content = result.getResponse().getContentAsString();
  108. int status = result.getResponse().getStatus();
  109.  
  110. verify(movieService, times(1)).selectAll();
  111.  
  112. Assert.assertEquals("failure - expected HTTP status 200", 200, status);
  113. Assert.assertTrue("failure - expected HTTP response body to have a value", content.trim().length() > 0);
  114. }
  115.  
  116. //the rest of the code..
  117. }
  118.  
  119. Wanted but not invoked:
  120. movieService.selectAll();
  121. -> at com.myproject.Controller.MovieControllerTest.testGetAllMovies(MovieControllerTest.java:87)
  122. Actually, there were zero interactions with this mock.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement