PAPAuthenticationFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter) throws IOException, ServletException { if (request instanceof HttpServletRequest) { HttpServletRequest httpServletRequest = (HttpServletRequest) request; String authCredentials = null; String url = httpServletRequest.getRequestURI(); logger.info("Request URI: " + url); authCredentials = httpServletRequest.getHeader(AUTHENTICATION_HEADER); AuthenticationService authenticationService = new AuthenticationService(); boolean authenticationStatus = authenticationService.authenticate(authCredentials); if (authenticationStatus) { if ("/pap/".equals(url)) { logger.info("Request comes from Traditional Admin Console or PolicyEngineAPI"); request.getRequestDispatcher("/pap/pap/").forward(request, response); } else if (url.startsWith("/pap/onap/") && response instanceof HttpServletResponse) { HttpServletResponse alteredResponse = ((HttpServletResponse) response); addCorsHeader(alteredResponse); logger.info("Request comes from Onap Portal"); filter.doFilter(request, response); } } else { if (response instanceof HttpServletResponse) { HttpServletResponse httpServletResponse = (HttpServletResponse) response; httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } } } }  @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain filter); @Override void destroy(); @Override void init(FilterConfig arg0); static final String AUTHENTICATION_HEADER; }
@Test public void testDoFilter() { Mockito.when(request.getRequestURI()).thenReturn("/pap/"); Mockito.when(request.getHeader("Authorization")).thenReturn("Basic dGVzdHBhcDphbHBoYTEyMw=="); callDoFilter(); Mockito.when(request.getRequestURI()).thenReturn("/pap/onap/"); callDoFilter(); }
@Test public void testOnPassingInvalidParamters() { Mockito.when(request.getRequestURI()).thenReturn("/pap/"); Mockito.when(request.getHeader("Authorization")).thenReturn("Basic dGVzdHBhcDphbHBoYTE789=="); try { papFilter.doFilter(request, response, filter); assertEquals(0, response.getStatus()); } catch (Exception e) { fail(); } }