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