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