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.service;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Mockito.only;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.MockitoAnnotations.initMocks;
30 import java.util.ArrayList;
31 import java.util.HashSet;
32 import java.util.List;
35 import javax.xml.ws.handler.MessageContext.Scope;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.onap.aaf.auth.common.Define;
41 import org.onap.aaf.auth.dao.cass.NsSplit;
42 import org.onap.aaf.auth.dao.cass.PermDAO;
43 import org.onap.aaf.auth.dao.hl.Question;
44 import org.onap.aaf.auth.env.AuthzEnv;
45 import org.onap.aaf.auth.env.AuthzTrans;
46 import org.onap.aaf.auth.layer.Result;
47 import org.onap.aaf.cadi.Access;
48 import org.onap.aaf.cadi.CadiException;
49 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
50 import org.onap.aaf.cadi.client.Future;
51 import org.onap.aaf.cadi.client.Rcli;
52 import org.onap.aaf.cadi.config.Config;
53 import org.onap.aaf.misc.env.APIException;
54 import org.onap.aaf.misc.env.Env;
55 import org.onap.aaf.misc.env.TimeTaken;
57 public class JU_JSONPermLoaderFactoryTest {
59 private AAFCon<?> aafcon;
61 private AuthzTrans trans;
69 private Question question;
71 private Result<NsSplit> rdns;
74 private Access access;
77 public void setup() throws CadiException {
78 access = new AuthzEnv();
81 nss = new NsSplit("APPLICATION", "APPLICATION");
85 public void testRemoteWithTimeOut() throws APIException, CadiException {
86 when(trans.start("Call AAF Service", Env.REMOTE)).thenReturn(tt);
87 when(aafcon.clientAs(Config.AAF_DEFAULT_API_VERSION, trans.getUserPrincipal())).thenReturn(c);
88 when(c.read("/authz/perms/user/null?scopes=APPLICATION:HANDLER",
89 "application/Perms+json;charset=utf-8;version=2.0")).thenReturn(fs);
90 when(fs.get(0)).thenReturn(true);
92 Set<String> scopes = new HashSet<String>();
93 scopes.add(Scope.APPLICATION.toString());
94 scopes.add(Scope.HANDLER.toString());
96 JSONPermLoader factory = JSONPermLoaderFactory.remote(aafcon, 0);
98 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, null, scopes);
100 assertEquals(0, loadJSONPerms.status);
102 verify(tt, only()).done();
106 public void testRemoteWith404() throws APIException, CadiException {
107 when(trans.start("Call AAF Service", Env.REMOTE)).thenReturn(tt);
108 when(aafcon.clientAs(Config.AAF_DEFAULT_API_VERSION, trans.getUserPrincipal())).thenReturn(c);
109 when(c.read("/authz/perms/user/null?scopes=APPLICATION:HANDLER",
110 "application/Perms+json;charset=utf-8;version=2.0")).thenReturn(fs);
111 when(fs.get(0)).thenReturn(false);
112 when(fs.code()).thenReturn(404);
114 Set<String> scopes = new HashSet<String>();
115 scopes.add(Scope.APPLICATION.toString());
116 scopes.add(Scope.HANDLER.toString());
118 JSONPermLoader factory = JSONPermLoaderFactory.remote(aafcon, 0);
120 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, null, scopes);
122 assertEquals(Result.ERR_NotFound, loadJSONPerms.status);
124 verify(tt, only()).done();
128 public void testRemote() throws APIException, CadiException {
129 when(trans.start("Call AAF Service", Env.REMOTE)).thenReturn(tt);
130 when(aafcon.clientAs(Config.AAF_DEFAULT_API_VERSION, trans.getUserPrincipal())).thenReturn(c);
131 when(c.read("/authz/perms/user/null?scopes=APPLICATION:HANDLER",
132 "application/Perms+json;charset=utf-8;version=2.0")).thenReturn(fs);
133 when(fs.get(0)).thenReturn(false);
135 Set<String> scopes = new HashSet<String>();
136 scopes.add(Scope.APPLICATION.toString());
137 scopes.add(Scope.HANDLER.toString());
139 JSONPermLoader factory = JSONPermLoaderFactory.remote(aafcon, 0);
141 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, null, scopes);
143 assertEquals(Result.ERR_Backend, loadJSONPerms.status);
145 verify(tt, only()).done();
149 public void testDirectWhenPdNotOk() throws APIException, CadiException {
151 Result<List<PermDAO.Data>> pd = Result.create(null, Result.ERR_Backend, "details", "vars");
153 when(question.getPermsByUser(trans, "user", false)).thenReturn(pd);
154 when(trans.start("Cached DB Perm lookup", Env.SUB)).thenReturn(tt);
156 Set<String> scopes = new HashSet<String>();
157 scopes.add(Scope.APPLICATION.toString());
158 scopes.add(Scope.HANDLER.toString());
160 JSONPermLoader factory = JSONPermLoaderFactory.direct(question);
162 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, "user", scopes);
164 assertEquals(Result.ERR_Backend, loadJSONPerms.status);
166 verify(tt, only()).done();
170 public void testDirectWhenPdOk() throws APIException, CadiException {
172 when(trans.start("Cached DB Perm lookup", Env.SUB)).thenReturn(tt);
173 when(question.deriveNsSplit(trans, "name")).thenReturn(rdns);
174 when(rdns.isOKhasData()).thenReturn(false);
176 List<PermDAO.Data> list = new ArrayList<PermDAO.Data>();
177 list.add(new PermDAO.Data(nss, "instance", "action"));
178 list.add(new PermDAO.Data(nss, "instance", "action"));
180 Result<List<PermDAO.Data>> pd = Result.create(list, Result.OK, "details", "vars");
182 when(question.getPermsByUser(trans, "user", false)).thenReturn(pd);
184 Set<String> scopes = new HashSet<String>();
185 scopes.add(Scope.APPLICATION.toString());
186 scopes.add(Scope.HANDLER.toString());
188 JSONPermLoader factory = JSONPermLoaderFactory.direct(question);
190 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, "user", scopes);
192 assertEquals(Result.OK, loadJSONPerms.status);
193 assertEquals("Success", loadJSONPerms.details);
195 "{\"perm\":[{\"ns\":\"APPLICATION\",\"type\":\"APPLICATION\",\"instance\":\"instance\",\"action\":\"action\"},{\"ns\":\"APPLICATION\",\"type\":\"APPLICATION\",\"instance\":\"instance\",\"action\":\"action\"}]}",
196 loadJSONPerms.value);
198 verify(tt, only()).done();