2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
21 package org.onap.aaf.auth.oauth;
23 import static org.mockito.Mockito.only;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 import static org.mockito.MockitoAnnotations.initMocks;
28 import java.io.IOException;
30 import javax.servlet.FilterChain;
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServletRequest;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.onap.aaf.cadi.principal.BearerPrincipal;
39 public class JU_OAuth2FilterTest {
42 private HttpServletRequest request;
44 private FilterChain chain;
46 private BearerPrincipal principal;
54 public void testDoFilterWithContentType() throws IOException, ServletException {
55 when(request.getContentType()).thenReturn("application/x-www-form-urlencoded");
57 OAuth2Filter filter = new OAuth2Filter();
58 filter.doFilter(request, null, chain);
60 verify(chain, only()).doFilter(request, null);
64 public void testDoFilter() throws IOException, ServletException {
65 when(request.getContentType()).thenReturn("somethingElse");
66 when(request.getUserPrincipal()).thenReturn(principal);
67 when(request.getHeader("Authorization")).thenReturn("Bearer 1;Bearer2");
69 OAuth2Filter filter = new OAuth2Filter();
72 filter.doFilter(request, null, chain);
74 verify(chain, only()).doFilter(request, null);
75 verify(principal, only()).setBearer("1");
79 public void testDoFilterWithoutBearerPrincipal() throws IOException, ServletException {
80 when(request.getContentType()).thenReturn("somethingElse");
81 when(request.getHeader("Authorization")).thenReturn("Bearer 1;Bearer2");
83 OAuth2Filter filter = new OAuth2Filter();
84 filter.doFilter(request, null, chain);
86 verify(chain, only()).doFilter(request, null);