UserController { @RequestMapping(value = "role/{role}", method = RequestMethod.GET) public Boolean claimContainsRole(@PathVariable final String role, final HttpServletRequest request) { final Claims claims = (Claims) request.getAttribute(JwtFilter.ATTRIBUTE_CLAIMS); if (claims == null || claims.isEmpty()) { return false; } return getRoles(claims).stream().anyMatch(map -> map.containsValue(role)); } @Autowired  UserController(final UserService userService, final AuthenticationService authenticationService); @RequestMapping(value = "login", method = RequestMethod.POST) LoginResponse login(@NonNull @RequestBody final LoginRequest login); @RequestMapping(value = "check", method = RequestMethod.GET) LoginResponse check(final HttpServletResponse response); @RequestMapping(value = "role/{role}", method = RequestMethod.GET) Boolean claimContainsRole(@PathVariable final String role,
        final HttpServletRequest request);  }
@Test public void testNullClaims() { final boolean containsRole; Mockito.when(mockHttpServletRequest.getAttribute(JwtFilter.ATTRIBUTE_CLAIMS)).thenReturn(null); containsRole = userController.claimContainsRole(ROLE, mockHttpServletRequest); assertFalse(containsRole); Mockito.verify(mockHttpServletRequest).getAttribute(JwtFilter.ATTRIBUTE_CLAIMS); }
@Test public void testEmptyClaims() { final boolean containsRole; final Claims emptyClaims = Jwts.claims(); Mockito.when(mockHttpServletRequest.getAttribute(JwtFilter.ATTRIBUTE_CLAIMS)).thenReturn(emptyClaims); containsRole = userController.claimContainsRole(ROLE, mockHttpServletRequest); assertFalse(containsRole); Mockito.verify(mockHttpServletRequest).getAttribute(JwtFilter.ATTRIBUTE_CLAIMS); }
@Test public void testClaimsWithoutRoles() { final boolean containsRole; final Claims claims = Jwts.claims().setSubject("test"); Mockito.when(mockHttpServletRequest.getAttribute(JwtFilter.ATTRIBUTE_CLAIMS)).thenReturn(claims); containsRole = userController.claimContainsRole(ROLE, mockHttpServletRequest); assertFalse(containsRole); Mockito.verify(mockHttpServletRequest).getAttribute(JwtFilter.ATTRIBUTE_CLAIMS); }
@Test public void testClaimsWithEmptyRoles() { final boolean containsRole; final Map<String, Object> map = new HashMap<>(); map.put("roles", new ArrayList<>()); final Claims claims = Jwts.claims(map); Mockito.when(mockHttpServletRequest.getAttribute(JwtFilter.ATTRIBUTE_CLAIMS)).thenReturn(claims); containsRole = userController.claimContainsRole(ROLE, mockHttpServletRequest); assertFalse(containsRole); Mockito.verify(mockHttpServletRequest).getAttribute(JwtFilter.ATTRIBUTE_CLAIMS); }
@Test public void testClaimsWithRoles() { final boolean containsRole; final List<LinkedHashMap<String, String>> roles = new ArrayList<>(); final LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>(); linkedHashMap.put(ROLE, ROLE); roles.add(linkedHashMap); final Map<String, Object> map = new HashMap<>(); map.put("roles", roles); final Claims claims = Jwts.claims(map); Mockito.when(mockHttpServletRequest.getAttribute(JwtFilter.ATTRIBUTE_CLAIMS)).thenReturn(claims); containsRole = userController.claimContainsRole(ROLE, mockHttpServletRequest); assertTrue(containsRole); Mockito.verify(mockHttpServletRequest).getAttribute(JwtFilter.ATTRIBUTE_CLAIMS); }