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====================================================
21 package org.onap.aaf.auth.oauth.service;
23 import static org.junit.Assert.assertEquals;
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;
29 import java.util.ArrayList;
30 import java.util.HashSet;
31 import java.util.List;
34 import javax.xml.ws.handler.MessageContext.Scope;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.onap.aaf.auth.common.Define;
40 import org.onap.aaf.auth.dao.cass.NsSplit;
41 import org.onap.aaf.auth.dao.cass.PermDAO;
42 import org.onap.aaf.auth.dao.hl.Question;
43 import org.onap.aaf.auth.env.AuthzEnv;
44 import org.onap.aaf.auth.env.AuthzTrans;
45 import org.onap.aaf.auth.layer.Result;
46 import org.onap.aaf.cadi.Access;
47 import org.onap.aaf.cadi.CadiException;
48 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
49 import org.onap.aaf.cadi.client.Future;
50 import org.onap.aaf.cadi.client.Rcli;
51 import org.onap.aaf.cadi.config.Config;
52 import org.onap.aaf.misc.env.APIException;
53 import org.onap.aaf.misc.env.Env;
54 import org.onap.aaf.misc.env.TimeTaken;
56 public class JU_JSONPermLoaderFactoryTest {
58 private AAFCon<?> aafcon;
60 private AuthzTrans trans;
68 private Question question;
70 private Result<NsSplit> rdns;
73 private Access access;
76 public void setup() throws CadiException {
77 access = new AuthzEnv();
80 nss = new NsSplit("APPLICATION", "APPLICATION");
84 public void testRemoteWithTimeOut() throws APIException, CadiException {
85 when(trans.start("Call AAF Service", Env.REMOTE)).thenReturn(tt);
86 when(aafcon.clientAs(Config.AAF_DEFAULT_VERSION, trans.getUserPrincipal())).thenReturn(c);
87 when(c.read("/authz/perms/user/null?scopes=APPLICATION:HANDLER",
88 "application/Perms+json;charset=utf-8;version=2.0")).thenReturn(fs);
89 when(fs.get(0)).thenReturn(true);
91 Set<String> scopes = new HashSet<String>();
92 scopes.add(Scope.APPLICATION.toString());
93 scopes.add(Scope.HANDLER.toString());
95 JSONPermLoader factory = JSONPermLoaderFactory.remote(aafcon, 0);
97 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, null, scopes);
99 assertEquals(0, loadJSONPerms.status);
101 verify(tt, only()).done();
105 public void testRemoteWith404() throws APIException, CadiException {
106 when(trans.start("Call AAF Service", Env.REMOTE)).thenReturn(tt);
107 when(aafcon.clientAs(Config.AAF_DEFAULT_VERSION, trans.getUserPrincipal())).thenReturn(c);
108 when(c.read("/authz/perms/user/null?scopes=APPLICATION:HANDLER",
109 "application/Perms+json;charset=utf-8;version=2.0")).thenReturn(fs);
110 when(fs.get(0)).thenReturn(false);
111 when(fs.code()).thenReturn(404);
113 Set<String> scopes = new HashSet<String>();
114 scopes.add(Scope.APPLICATION.toString());
115 scopes.add(Scope.HANDLER.toString());
117 JSONPermLoader factory = JSONPermLoaderFactory.remote(aafcon, 0);
119 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, null, scopes);
121 assertEquals(Result.ERR_NotFound, loadJSONPerms.status);
123 verify(tt, only()).done();
127 public void testRemote() throws APIException, CadiException {
128 when(trans.start("Call AAF Service", Env.REMOTE)).thenReturn(tt);
129 when(aafcon.clientAs(Config.AAF_DEFAULT_VERSION, trans.getUserPrincipal())).thenReturn(c);
130 when(c.read("/authz/perms/user/null?scopes=APPLICATION:HANDLER",
131 "application/Perms+json;charset=utf-8;version=2.0")).thenReturn(fs);
132 when(fs.get(0)).thenReturn(false);
134 Set<String> scopes = new HashSet<String>();
135 scopes.add(Scope.APPLICATION.toString());
136 scopes.add(Scope.HANDLER.toString());
138 JSONPermLoader factory = JSONPermLoaderFactory.remote(aafcon, 0);
140 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, null, scopes);
142 assertEquals(Result.ERR_Backend, loadJSONPerms.status);
144 verify(tt, only()).done();
148 public void testDirectWhenPdNotOk() throws APIException, CadiException {
150 Result<List<PermDAO.Data>> pd = Result.create(null, Result.ERR_Backend, "details", "vars");
152 when(question.getPermsByUser(trans, "user", false)).thenReturn(pd);
153 when(trans.start("Cached DB Perm lookup", Env.SUB)).thenReturn(tt);
155 Set<String> scopes = new HashSet<String>();
156 scopes.add(Scope.APPLICATION.toString());
157 scopes.add(Scope.HANDLER.toString());
159 JSONPermLoader factory = JSONPermLoaderFactory.direct(question);
161 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, "user", scopes);
163 assertEquals(Result.ERR_Backend, loadJSONPerms.status);
165 verify(tt, only()).done();
169 public void testDirectWhenPdOk() throws APIException, CadiException {
171 when(trans.start("Cached DB Perm lookup", Env.SUB)).thenReturn(tt);
172 when(question.deriveNsSplit(trans, "name")).thenReturn(rdns);
173 when(rdns.isOKhasData()).thenReturn(false);
175 List<PermDAO.Data> list = new ArrayList<PermDAO.Data>();
176 list.add(new PermDAO.Data(nss, "instance", "action"));
177 list.add(new PermDAO.Data(nss, "instance", "action"));
179 Result<List<PermDAO.Data>> pd = Result.create(list, Result.OK, "details", "vars");
181 when(question.getPermsByUser(trans, "user", false)).thenReturn(pd);
183 Set<String> scopes = new HashSet<String>();
184 scopes.add(Scope.APPLICATION.toString());
185 scopes.add(Scope.HANDLER.toString());
187 JSONPermLoader factory = JSONPermLoaderFactory.direct(question);
189 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, "user", scopes);
191 assertEquals(Result.OK, loadJSONPerms.status);
192 assertEquals("Success", loadJSONPerms.details);
194 "{\"perm\":[{\"ns\":\"APPLICATION\",\"type\":\"APPLICATION\",\"instance\":\"instance\",\"action\":\"action\"},{\"ns\":\"APPLICATION\",\"type\":\"APPLICATION\",\"instance\":\"instance\",\"action\":\"action\"}]}",
195 loadJSONPerms.value);
197 verify(tt, only()).done();