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====================================================
22 package org.onap.aaf.auth.oauth;
24 import static org.mockito.Mockito.only;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 import static org.mockito.MockitoAnnotations.initMocks;
29 import java.io.IOException;
31 import javax.servlet.FilterChain;
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServletRequest;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.onap.aaf.cadi.principal.BearerPrincipal;
40 public class JU_OAuth2FilterTest {
43 private HttpServletRequest request;
45 private FilterChain chain;
47 private BearerPrincipal principal;
55 public void testDoFilterWithContentType() throws IOException, ServletException {
56 when(request.getContentType()).thenReturn("application/x-www-form-urlencoded");
58 OAuth2Filter filter = new OAuth2Filter();
59 filter.doFilter(request, null, chain);
61 verify(chain, only()).doFilter(request, null);
65 public void testDoFilter() throws IOException, ServletException {
66 when(request.getContentType()).thenReturn("somethingElse");
67 when(request.getUserPrincipal()).thenReturn(principal);
68 when(request.getHeader("Authorization")).thenReturn("Bearer 1;Bearer2");
70 OAuth2Filter filter = new OAuth2Filter();
73 filter.doFilter(request, null, chain);
75 verify(chain, only()).doFilter(request, null);
76 verify(principal, only()).setBearer("1");
80 public void testDoFilterWithoutBearerPrincipal() throws IOException, ServletException {
81 when(request.getContentType()).thenReturn("somethingElse");
82 when(request.getHeader("Authorization")).thenReturn("Bearer 1;Bearer2");
84 OAuth2Filter filter = new OAuth2Filter();
85 filter.doFilter(request, null, chain);
87 verify(chain, only()).doFilter(request, null);