1a13580fffa9b0729ccd9eb823b45a90b353eee8
[aaf/authz.git] / auth / auth-oauth / src / test / java / org / onap / aaf / auth / oauth / service / JU_JSONPermLoaderFactoryTest.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.service;
22
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;
28
29 import java.util.ArrayList;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Set;
33
34 import javax.xml.ws.handler.MessageContext.Scope;
35
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;
55
56 public class JU_JSONPermLoaderFactoryTest {
57         @Mock
58         private AAFCon<?> aafcon;
59         @Mock
60         private AuthzTrans trans;
61         @Mock
62         private TimeTaken tt;
63         @Mock
64         Rcli c;
65         @Mock
66         private Future fs;
67         @Mock
68         private Question question;
69         @Mock
70         private Result<NsSplit> rdns;
71         private NsSplit nss;
72
73         private Access access;
74
75         @Before
76         public void setup() throws CadiException {
77                 access = new AuthzEnv();
78                 Define.set(access);
79                 initMocks(this);
80                 nss = new NsSplit("APPLICATION", "APPLICATION");
81         }
82
83         @Test
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);
90
91                 Set<String> scopes = new HashSet<String>();
92                 scopes.add(Scope.APPLICATION.toString());
93                 scopes.add(Scope.HANDLER.toString());
94
95                 JSONPermLoader factory = JSONPermLoaderFactory.remote(aafcon, 0);
96
97                 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, null, scopes);
98
99                 assertEquals(0, loadJSONPerms.status);
100
101                 verify(tt, only()).done();
102         }
103
104         @Test
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);
112
113                 Set<String> scopes = new HashSet<String>();
114                 scopes.add(Scope.APPLICATION.toString());
115                 scopes.add(Scope.HANDLER.toString());
116
117                 JSONPermLoader factory = JSONPermLoaderFactory.remote(aafcon, 0);
118
119                 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, null, scopes);
120
121                 assertEquals(Result.ERR_NotFound, loadJSONPerms.status);
122
123                 verify(tt, only()).done();
124         }
125
126         @Test
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);
133
134                 Set<String> scopes = new HashSet<String>();
135                 scopes.add(Scope.APPLICATION.toString());
136                 scopes.add(Scope.HANDLER.toString());
137
138                 JSONPermLoader factory = JSONPermLoaderFactory.remote(aafcon, 0);
139
140                 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, null, scopes);
141
142                 assertEquals(Result.ERR_Backend, loadJSONPerms.status);
143
144                 verify(tt, only()).done();
145         }
146
147         @Test
148         public void testDirectWhenPdNotOk() throws APIException, CadiException {
149
150                 Result<List<PermDAO.Data>> pd = Result.create(null, Result.ERR_Backend, "details", "vars");
151
152                 when(question.getPermsByUser(trans, "user", false)).thenReturn(pd);
153                 when(trans.start("Cached DB Perm lookup", Env.SUB)).thenReturn(tt);
154
155                 Set<String> scopes = new HashSet<String>();
156                 scopes.add(Scope.APPLICATION.toString());
157                 scopes.add(Scope.HANDLER.toString());
158
159                 JSONPermLoader factory = JSONPermLoaderFactory.direct(question);
160
161                 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, "user", scopes);
162
163                 assertEquals(Result.ERR_Backend, loadJSONPerms.status);
164
165                 verify(tt, only()).done();
166         }
167
168         @Test
169         public void testDirectWhenPdOk() throws APIException, CadiException {
170
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);
174
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"));
178
179                 Result<List<PermDAO.Data>> pd = Result.create(list, Result.OK, "details", "vars");
180
181                 when(question.getPermsByUser(trans, "user", false)).thenReturn(pd);
182
183                 Set<String> scopes = new HashSet<String>();
184                 scopes.add(Scope.APPLICATION.toString());
185                 scopes.add(Scope.HANDLER.toString());
186
187                 JSONPermLoader factory = JSONPermLoaderFactory.direct(question);
188
189                 Result<String> loadJSONPerms = factory.loadJSONPerms(trans, "user", scopes);
190
191                 assertEquals(Result.OK, loadJSONPerms.status);
192                 assertEquals("Success", loadJSONPerms.details);
193                 assertEquals(
194                                 "{\"perm\":[{\"ns\":\"APPLICATION\",\"type\":\"APPLICATION\",\"instance\":\"instance\",\"action\":\"action\"},{\"ns\":\"APPLICATION\",\"type\":\"APPLICATION\",\"instance\":\"instance\",\"action\":\"action\"}]}",
195                                 loadJSONPerms.value);
196
197                 verify(tt, only()).done();
198         }
199
200 }