RoleController { @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_DEVICE_MANAGER') or hasRole('ROLE_DRPROGRAM') or hasRole('ROLE_VTN') or hasRole('ROLE_XMPP')") @RequestMapping(value = "/{username}", method = RequestMethod.POST) @ResponseBody public List<String> getUserRole(@PathVariable("username") String username, HttpServletResponse response) { AbstractUser user = abstractUserDao.findOneByUsername(username); List<String> roles = new ArrayList<>(); if (user == null) { response.setStatus(HttpStatus.NOT_FOUND_404); return roles; } roles = user.getRoles(); if (user instanceof Ven) { roles.add("ROLE_VEN"); } else if (user instanceof OadrUser) { roles.add("ROLE_USER"); } else if (user instanceof OadrApp) { roles.add("ROLE_APP"); } LOGGER.info(user.toString() + " roles: " + roles.toString()); return roles; }  @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_DEVICE_MANAGER') or hasRole('ROLE_DRPROGRAM') or hasRole('ROLE_VTN') or hasRole('ROLE_XMPP')") @RequestMapping(value = "/{username}", method = RequestMethod.POST) @ResponseBody List<String> getUserRole(@PathVariable("username") String username, HttpServletResponse response);  }
@Test public void getUserRoleTest() throws Exception { this.mockMvc.perform( MockMvcRequestBuilders.post(ROLE_URL + "admin").header("Content-Type", "application/json").with(user)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.FORBIDDEN_403)); this.mockMvc.perform( MockMvcRequestBuilders.post(ROLE_URL + "admin").header("Content-Type", "application/json").with(ven)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.FORBIDDEN_403)); this.mockMvc.perform(MockMvcRequestBuilders.post(ROLE_URL + "mouaiccool") .header("Content-Type", "application/json").with(admin)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.NOT_FOUND_404)); MvcResult andReturn = this.mockMvc.perform( MockMvcRequestBuilders.post(ROLE_URL + "admin").header("Content-Type", "application/json").with(admin)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn(); assertTrue(andReturn.getResponse().getContentAsString().contains("ROLE_ADMIN")); assertTrue(andReturn.getResponse().getContentAsString().contains("ROLE_USER")); andReturn = this.mockMvc.perform( MockMvcRequestBuilders.post(ROLE_URL + "app").header("Content-Type", "application/json").with(admin)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn(); assertTrue(andReturn.getResponse().getContentAsString().contains("ROLE_APP")); assertTrue(andReturn.getResponse().getContentAsString().contains("ROLE_DEVICE_MANAGER")); andReturn = this.mockMvc.perform( MockMvcRequestBuilders.post(ROLE_URL + "ven").header("Content-Type", "application/json").with(admin)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn(); assertTrue(andReturn.getResponse().getContentAsString().contains("ROLE_VEN")); }