52b2beb4960bff5962647daea1232004ee6fb40d
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / oauth / test / JU_OAuth2HttpTaf.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.cadi.oauth.test;
23
24 import static org.mockito.Mockito.when;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Matchers.anyString;
27 import static org.mockito.Matchers.any;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.LocatorException;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.oauth.OAuth2HttpTaf;
43 import org.onap.aaf.cadi.oauth.OAuth2Principal;
44 import org.onap.aaf.cadi.oauth.TokenMgr;
45 import org.onap.aaf.misc.env.APIException;
46 import org.onap.aaf.cadi.Taf.LifeForm;
47 import org.onap.aaf.cadi.client.Result;
48
49 public class JU_OAuth2HttpTaf {
50
51         private static final String authz = "Bearer John Doe";
52
53         @Mock private TokenMgr tmgrMock;
54         @Mock private HttpServletResponse respMock;
55         @Mock private HttpServletRequest reqMock;
56         @Mock private OAuth2Principal princMock;
57
58         private PropAccess access;
59
60         @Before
61         public void setup() {
62                 MockitoAnnotations.initMocks(this);
63
64                 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
65         }
66
67         @Test
68         public void test() throws APIException, CadiException, LocatorException {
69                 OAuth2HttpTaf taf = new OAuth2HttpTaf(access, tmgrMock);
70
71                 taf.validate(LifeForm.CBLF, reqMock, respMock);
72                 when(reqMock.getHeader("Authorization")).thenReturn(authz);
73
74                 doReturn(Result.ok(200, princMock)).when(tmgrMock).toPrincipal(anyString(), (byte[])any());
75                 taf.validate(LifeForm.CBLF, reqMock, respMock);
76
77                 when(reqMock.isSecure()).thenReturn(true);
78
79                 doReturn(Result.err(404, "not found")).when(tmgrMock).toPrincipal(anyString(), (byte[])any());
80                 taf.validate(LifeForm.CBLF, reqMock, respMock);
81
82                 taf.revalidate(null, null);
83         }
84
85 }