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.cadi.oauth.test;
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;
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
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;
49 public class JU_OAuth2HttpTaf {
51 private static final String authz = "Bearer John Doe";
53 @Mock private TokenMgr tmgrMock;
54 @Mock private HttpServletResponse respMock;
55 @Mock private HttpServletRequest reqMock;
56 @Mock private OAuth2Principal princMock;
58 private PropAccess access;
62 MockitoAnnotations.initMocks(this);
64 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
68 public void test() throws APIException, CadiException, LocatorException {
69 OAuth2HttpTaf taf = new OAuth2HttpTaf(access, tmgrMock);
71 taf.validate(LifeForm.CBLF, reqMock, respMock);
72 when(reqMock.getHeader("Authorization")).thenReturn(authz);
74 doReturn(Result.ok(200, princMock)).when(tmgrMock).toPrincipal(anyString(), (byte[])any());
75 taf.validate(LifeForm.CBLF, reqMock, respMock);
77 when(reqMock.isSecure()).thenReturn(true);
79 doReturn(Result.err(404, "not found")).when(tmgrMock).toPrincipal(anyString(), (byte[])any());
80 taf.validate(LifeForm.CBLF, reqMock, respMock);
82 taf.revalidate(null, null);