Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-oauth / src / test / java / org / onap / aaf / auth / oauth / JU_OAuth2FilterTest.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.oauth;
23
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;
28
29 import java.io.IOException;
30
31 import javax.servlet.FilterChain;
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServletRequest;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.onap.aaf.cadi.principal.BearerPrincipal;
39
40 public class JU_OAuth2FilterTest {
41
42     @Mock
43     private HttpServletRequest request;
44     @Mock
45     private FilterChain chain;
46     @Mock
47     private BearerPrincipal principal;
48
49     @Before
50     public void setup() {
51         initMocks(this);
52     }
53
54     @Test
55     public void testDoFilterWithContentType() throws IOException, ServletException {
56         when(request.getContentType()).thenReturn("application/x-www-form-urlencoded");
57
58         OAuth2Filter filter = new OAuth2Filter();
59         filter.doFilter(request, null, chain);
60
61         verify(chain, only()).doFilter(request, null);
62     }
63
64     @Test
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");
69
70         OAuth2Filter filter = new OAuth2Filter();
71         filter.init(null);
72         filter.destroy();
73         filter.doFilter(request, null, chain);
74
75         verify(chain, only()).doFilter(request, null);
76         verify(principal, only()).setBearer("1");
77     }
78
79     @Test
80     public void testDoFilterWithoutBearerPrincipal() throws IOException, ServletException {
81         when(request.getContentType()).thenReturn("somethingElse");
82         when(request.getHeader("Authorization")).thenReturn("Bearer 1;Bearer2");
83
84         OAuth2Filter filter = new OAuth2Filter();
85         filter.doFilter(request, null, chain);
86
87         verify(chain, only()).doFilter(request, null);
88     }
89 }