1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 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 ******************************************************************************/
23 package org.onap.aaf.auth.dao.hl;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.MockitoAnnotations.initMocks;
28 import java.io.IOException;
29 import java.lang.reflect.Field;
30 import java.lang.reflect.Modifier;
31 import java.text.DateFormat;
32 import java.text.ParseException;
33 import java.text.SimpleDateFormat;
34 import java.util.ArrayList;
35 import java.util.GregorianCalendar;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Properties;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.onap.aaf.auth.common.Define;
46 import org.onap.aaf.auth.dao.cached.CachedCredDAO;
47 import org.onap.aaf.auth.dao.cached.CachedNSDAO;
48 import org.onap.aaf.auth.dao.cached.CachedPermDAO;
49 import org.onap.aaf.auth.dao.cached.CachedRoleDAO;
50 import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO;
51 import org.onap.aaf.auth.dao.cass.CredDAO;
52 import org.onap.aaf.auth.dao.cass.Namespace;
53 import org.onap.aaf.auth.dao.cass.NsDAO;
54 import org.onap.aaf.auth.dao.cass.PermDAO;
55 import org.onap.aaf.auth.dao.cass.RoleDAO;
56 import org.onap.aaf.auth.dao.cass.Status;
57 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
58 import org.onap.aaf.auth.dao.hl.Question.Access;
59 import org.onap.aaf.auth.env.AuthzTrans;
60 import org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE;
61 import org.onap.aaf.auth.layer.Result;
62 import org.onap.aaf.auth.org.Organization;
63 import org.onap.aaf.auth.org.Organization.Expiration;
64 import org.onap.aaf.auth.org.Organization.Identity;
65 import org.onap.aaf.auth.org.OrganizationException;
66 import org.onap.aaf.cadi.CadiException;
67 import org.onap.aaf.cadi.PropAccess;
68 import org.onap.aaf.cadi.config.Config;
69 import org.onap.aaf.misc.env.APIException;
70 import org.onap.aaf.misc.env.LogTarget;
72 import io.netty.util.internal.SystemPropertyUtil;
74 public class JU_Function {
85 public void setUp() throws APIException, IOException {
90 public void testCreateNs() {
91 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
92 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
93 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
94 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
97 } catch (CadiException e) {
98 // TODO Auto-generated catch block
101 Namespace namespace = Mockito.mock(Namespace.class);
102 namespace.name = "test.test";
103 List<String> owner = new ArrayList<String>();
104 namespace.owner = owner;
106 Organization org = Mockito.mock(Organization.class);
107 Mockito.doReturn(org).when(trans).org();
109 NsDAO.Data data = new NsDAO.Data();
111 Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]);
112 Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test");
114 CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
115 Result<Void> retVal = new Result<Void>(null,1,"test",new String[0]);
116 Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
117 setQuestion(ques, nsDaoObj);
119 Function funcObj = new Function(trans, ques);
120 Result<Void> result = funcObj.createNS(trans, namespace, true);
121 assertTrue(3 == result.status);
125 public void testCreateNsReadSuccess() {
126 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
127 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
128 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
129 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
132 } catch (CadiException e) {
133 // TODO Auto-generated catch block
136 Namespace namespace = Mockito.mock(Namespace.class);
137 namespace.name = "test.test";
138 List<String> owner = new ArrayList<String>();
140 namespace.owner = owner;
141 List<String> admin = new ArrayList<String>();
143 namespace.admin= admin;
145 Organization org = Mockito.mock(Organization.class);
146 Mockito.doReturn(org).when(trans).org();
148 NsDAO.Data data = new NsDAO.Data();
150 Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]);
151 Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test");
153 CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
154 Result<Void> retVal = new Result<Void>(null,1,"test",new String[0]);
155 Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
156 setQuestion(ques, nsDaoObj);
158 Function funcObj = new Function(trans, ques);
159 Result<Void> result = funcObj.createNS(trans, namespace, true);
160 assertTrue(3 == result.status);
164 public void testCreateNsFromApprovaFalse() {
165 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
166 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
167 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
168 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
171 } catch (CadiException e) {
172 // TODO Auto-generated catch block
175 Namespace namespace = Mockito.mock(Namespace.class);
176 namespace.name = "test.test";
177 List<String> owner = new ArrayList<String>();
178 namespace.owner = owner;
180 Organization org = Mockito.mock(Organization.class);
181 Mockito.doReturn(org).when(trans).org();
183 NsDAO.Data data = new NsDAO.Data();
185 Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]);
186 Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test");
187 Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(data,1,"test",new String[0]);
188 Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal1.value, Access.write);
190 Function funcObj = new Function(trans, ques);
191 Result<Void> result = funcObj.createNS(trans, namespace, false);
192 assertTrue(1 == result.status);
194 Mockito.doReturn(retVal2).when(ques).deriveNs(trans, "test");
195 funcObj = new Function(trans, ques);
196 result = funcObj.createNS(trans, namespace, false);
197 assertTrue(1 == result.status);
201 public void testCreateNsownerLoop() {
202 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
203 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
204 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
205 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
208 } catch (CadiException e) {
209 // TODO Auto-generated catch block
212 Namespace namespace = Mockito.mock(Namespace.class);
213 namespace.name = "test.test";
214 List<String> owner = new ArrayList<String>();
216 namespace.owner = owner;
218 Organization org = Mockito.mock(Organization.class);
219 Mockito.doReturn(org).when(trans).org();
220 Mockito.doReturn(org).when(trans).org();
222 Function funcObj = new Function(trans, ques);
223 Result<Void> result = funcObj.createNS(trans, namespace, true);
224 assertTrue(result.status == Status.ERR_Policy);
225 assertTrue(result.details.contains("is not a valid user at"));
227 Identity iden=Mockito.mock(Identity.class);
229 Mockito.doReturn(iden).when(org).getIdentity(trans, "test");
230 Mockito.doReturn("test").when(iden).mayOwn();
231 } catch (OrganizationException e) {
232 // TODO Auto-generated catch block
235 result = funcObj.createNS(trans, namespace, true);
236 assertTrue(result.status == Status.ERR_Policy);
237 assertTrue(result.details.contains("is an invalid Identity"));
239 Mockito.doReturn(true).when(iden).isFound();
240 result = funcObj.createNS(trans, namespace, true);
241 assertTrue(result.status == Status.ERR_Policy);
242 assertTrue(result.details.contains("cannot be the owner of the namespace "));
244 Mockito.doReturn(true).when(org).isTestEnv();
246 Mockito.doReturn("test").when(org).validate(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
247 result = funcObj.createNS(trans, namespace, true);
248 assertTrue(result.status == Status.ERR_Policy);
249 assertTrue(result.details.contains("cannot be the owner of the namespace "));
250 } catch (OrganizationException e) {
251 // TODO Auto-generated catch block
257 public void testCreateNsownerLoopException() {
258 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
259 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
260 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
261 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
264 } catch (CadiException e) {
265 // TODO Auto-generated catch block
268 Namespace namespace = Mockito.mock(Namespace.class);
269 namespace.name = "test";
270 List<String> owner = new ArrayList<String>();
272 namespace.owner = owner;
274 Organization org = Mockito.mock(Organization.class);
275 Mockito.doReturn(org).when(trans).org();
277 Function funcObj = new Function(trans, ques);
279 Identity iden=Mockito.mock(Identity.class);
281 Mockito.doThrow(new OrganizationException()).when(org).getIdentity(trans, "test");
282 Mockito.doReturn("test").when(iden).mayOwn();
283 } catch (OrganizationException e) {
284 // TODO Auto-generated catch block
288 NsDAO.Data data = new NsDAO.Data();
290 Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,1,"test",new String[0]);
291 Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test");
293 Result<Void> result = funcObj.createNS(trans, namespace, true);
294 assertTrue(result.status == Status.ERR_Security);
295 assertTrue(result.details.contains("may not create Root Namespaces"));
297 Mockito.doReturn(true).when(ques).isGranted(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
298 retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]);
299 Mockito.doReturn(retVal1).when(ques).deriveNs(trans, null);
301 CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
302 Result<Void> retVal = new Result<Void>(null,1,"test",new String[0]);
303 Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
304 setQuestion(ques, nsDaoObj);
306 result = funcObj.createNS(trans, namespace, true);
307 assertTrue(24 == result.status);
311 public void setQuestion(Question ques, CachedNSDAO userRoleDaoObj) {
314 nsDaoField = Question.class.getDeclaredField("nsDAO");
316 nsDaoField.setAccessible(true);
317 // remove final modifier from field
318 Field modifiersField = Field.class.getDeclaredField("modifiers");
319 modifiersField.setAccessible(true);
320 modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
322 nsDaoField.set(ques, userRoleDaoObj);
323 } catch (NoSuchFieldException | SecurityException e) {
324 // TODO Auto-generated catch block
326 } catch (IllegalArgumentException e) {
327 // TODO Auto-generated catch block
329 } catch (IllegalAccessException e) {
330 // TODO Auto-generated catch block
335 public void setQuestionCredDao(Question ques, CachedCredDAO credDaoObj) {
338 nsDaoField = Question.class.getDeclaredField("credDAO");
340 nsDaoField.setAccessible(true);
341 // remove final modifier from field
342 Field modifiersField = Field.class.getDeclaredField("modifiers");
343 modifiersField.setAccessible(true);
344 modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
346 nsDaoField.set(ques, credDaoObj);
347 } catch (NoSuchFieldException | SecurityException e) {
348 // TODO Auto-generated catch block
350 } catch (IllegalArgumentException e) {
351 // TODO Auto-generated catch block
353 } catch (IllegalAccessException e) {
354 // TODO Auto-generated catch block
359 public void setQuestionUserRoleDao(Question ques, CachedUserRoleDAO credDaoObj) {
362 nsDaoField = Question.class.getDeclaredField("userRoleDAO");
364 nsDaoField.setAccessible(true);
365 // remove final modifier from field
366 Field modifiersField = Field.class.getDeclaredField("modifiers");
367 modifiersField.setAccessible(true);
368 modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
370 nsDaoField.set(ques, credDaoObj);
371 } catch (NoSuchFieldException | SecurityException e) {
372 // TODO Auto-generated catch block
374 } catch (IllegalArgumentException e) {
375 // TODO Auto-generated catch block
377 } catch (IllegalAccessException e) {
378 // TODO Auto-generated catch block
382 public void setQuestionCachedRoleDao(Question ques, CachedRoleDAO credDaoObj) {
385 nsDaoField = Question.class.getDeclaredField("roleDAO");
387 nsDaoField.setAccessible(true);
388 // remove final modifier from field
389 Field modifiersField = Field.class.getDeclaredField("modifiers");
390 modifiersField.setAccessible(true);
391 modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
393 nsDaoField.set(ques, credDaoObj);
394 } catch (NoSuchFieldException | SecurityException e) {
395 // TODO Auto-generated catch block
397 } catch (IllegalArgumentException e) {
398 // TODO Auto-generated catch block
400 } catch (IllegalAccessException e) {
401 // TODO Auto-generated catch block
406 public void setQuestionCachedPermDao(Question ques, CachedPermDAO credDaoObj) {
409 nsDaoField = Question.class.getDeclaredField("permDAO");
411 nsDaoField.setAccessible(true);
412 // remove final modifier from field
413 Field modifiersField = Field.class.getDeclaredField("modifiers");
414 modifiersField.setAccessible(true);
415 modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
417 nsDaoField.set(ques, credDaoObj);
418 } catch (NoSuchFieldException | SecurityException e) {
419 // TODO Auto-generated catch block
421 } catch (IllegalArgumentException e) {
422 // TODO Auto-generated catch block
424 } catch (IllegalAccessException e) {
425 // TODO Auto-generated catch block
430 public void testCreateNsAdminLoop() {
431 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
432 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
433 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
434 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
437 } catch (CadiException e) {
438 // TODO Auto-generated catch block
441 Namespace namespace = Mockito.mock(Namespace.class);
442 namespace.name = "test.test";
443 List<String> owner = new ArrayList<String>();
445 namespace.owner = owner;
446 namespace.admin = owner;
448 Organization org = Mockito.mock(Organization.class);
449 Mockito.doReturn(org).when(trans).org();
451 NsDAO.Data data = new NsDAO.Data();
453 Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]);
454 Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test");
456 CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
457 CachedCredDAO credDAO = Mockito.mock(CachedCredDAO.class);
458 Result<Void> retVal = new Result<Void>(null,1,"test",new String[0]);
459 Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
460 Mockito.doReturn(retVal).when(nsDaoObj).create(Mockito.any(), Mockito.any());
461 List<CredDAO.Data> dataObj = new ArrayList<>();
462 CredDAO.Data indData = new CredDAO.Data();
464 indData.notes = "test";
465 DateFormat sdf = new SimpleDateFormat("yyyy/mm/dd");
467 indData.expires = sdf.parse("2090/01/01");
468 } catch (ParseException e1) {
469 // TODO Auto-generated catch block
470 e1.printStackTrace();
472 dataObj.add(indData);
473 Result<List<CredDAO.Data>> retVal2 = new Result<List<CredDAO.Data>>(dataObj,0,"test",new String[0]);
474 Mockito.doReturn(retVal2).when(credDAO).readID(Mockito.any(), Mockito.anyString());
475 setQuestion(ques, nsDaoObj);
476 setQuestionCredDao(ques, credDAO);
478 Identity iden=Mockito.mock(Identity.class);
480 Mockito.doReturn(iden).when(org).getIdentity(trans, "test");
481 Mockito.doReturn("test").when(iden).mayOwn();
482 Mockito.doReturn(true).when(org).isTestEnv();
483 } catch (OrganizationException e) {
484 // TODO Auto-generated catch block
488 Function funcObj = new Function(trans, ques);
489 Result<Void> result = funcObj.createNS(trans, namespace, true);
490 assertTrue(result.status == 1);
495 public void testCreateNsAdminLoopCreateSucReadChildrenFailure() {
496 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
497 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
498 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
499 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
502 } catch (CadiException e) {
503 // TODO Auto-generated catch block
506 Namespace namespace = Mockito.mock(Namespace.class);
507 namespace.name = "test.test";
508 List<String> owner = new ArrayList<String>();
510 namespace.owner = owner;
511 namespace.admin = owner;
513 Organization org = Mockito.mock(Organization.class);
514 Mockito.doReturn(org).when(trans).org();
516 NsDAO.Data data = new NsDAO.Data();
518 Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]);
519 Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test");
521 CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
522 CachedCredDAO credDAO = Mockito.mock(CachedCredDAO.class);
523 CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class);
524 CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class);
525 CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class);
526 Result<Void> retVal = new Result<Void>(null,0,"test",new String[0]);
527 Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
528 Mockito.doReturn(retVal).when(nsDaoObj).create(Mockito.any(), Mockito.any());
529 List<CredDAO.Data> dataObj = new ArrayList<>();
530 CredDAO.Data indData = new CredDAO.Data();
532 indData.notes = "test";
533 DateFormat sdf = new SimpleDateFormat("yyyy/mm/dd");
535 indData.expires = sdf.parse("2090/01/01");
536 } catch (ParseException e1) {
537 // TODO Auto-generated catch block
538 e1.printStackTrace();
540 dataObj.add(indData);
541 List<RoleDAO.Data> dataObj1 = new ArrayList<>();
542 RoleDAO.Data indData1 = new RoleDAO.Data();
543 indData1.ns = "test";
544 indData1.name = "test";
545 Set<String> permsSet = new HashSet<>();
546 permsSet.add("test|test");
547 indData1.perms = permsSet;
548 dataObj1.add(indData1);
550 List<UserRoleDAO.Data> dataObj4 = new ArrayList<>();
551 UserRoleDAO.Data indData4 = new UserRoleDAO.Data();
552 indData4.ns = "test";
553 indData4.rname = "test";
554 dataObj4.add(indData4);
556 List<PermDAO.Data> dataObj5 = new ArrayList<>();
557 PermDAO.Data indData5 = new PermDAO.Data();
558 indData5.ns = "test";
559 indData5.type = "test";
560 dataObj5.add(indData5);
562 Result<List<CredDAO.Data>> retVal2 = new Result<List<CredDAO.Data>>(dataObj,0,"test",new String[0]);
563 Result<List<CredDAO.Data>> retVal6 = new Result<List<CredDAO.Data>>(dataObj,1,"test",new String[0]);
564 Result<List<RoleDAO.Data>> retVal3 = new Result<List<RoleDAO.Data>>(dataObj1,0,"test",new String[0]);
565 Result<List<UserRoleDAO.Data>> retVal4 = new Result<List<UserRoleDAO.Data>>(dataObj4,0,"test",new String[0]);
566 Result<List<PermDAO.Data>> retVal5 = new Result<List<PermDAO.Data>>(dataObj5,0,"test",new String[0]);
567 Mockito.doReturn(retVal2).when(credDAO).readID(Mockito.any(), Mockito.anyString());
568 Mockito.doReturn(retVal4).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString());
569 Mockito.doReturn(retVal2).when(userRoleDAO).create(Mockito.any(), Mockito.any());
570 Mockito.doReturn(retVal6).when(cachedRoleDAO).create(Mockito.any(), Mockito.any());
571 Mockito.doReturn(retVal6).when(cachedRoleDAO).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean());
572 Mockito.doReturn(retVal2).when(cachedPermDAO).create(Mockito.any(), Mockito.any());
573 Mockito.doReturn(retVal5).when(cachedPermDAO).readChildren(trans, "test", "test");
574 Mockito.doReturn(retVal5).when(cachedPermDAO).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean());
575 Mockito.doReturn(retVal3).when(cachedRoleDAO).readChildren(trans, "test", "test");
576 setQuestion(ques, nsDaoObj);
577 setQuestionCredDao(ques, credDAO);
578 setQuestionUserRoleDao(ques, userRoleDAO);
579 setQuestionCachedRoleDao(ques, cachedRoleDAO);
580 setQuestionCachedPermDao(ques, cachedPermDAO);
582 Identity iden=Mockito.mock(Identity.class);
584 Mockito.doReturn(iden).when(org).getIdentity(trans, "test");
585 Mockito.doReturn("test").when(iden).mayOwn();
586 Mockito.doReturn(true).when(org).isTestEnv();
587 Mockito.doReturn(new GregorianCalendar(2010, 01, 01)).when(org).expiration(null, Expiration.UserInRole);
588 } catch (OrganizationException e) {
589 // TODO Auto-generated catch block
593 Function funcObj = new Function(trans, ques);
594 Result<Void> result = funcObj.createNS(trans, namespace, true);
595 assertTrue(result.status == Status.ERR_ActionNotCompleted);
600 public void testCreateNsAdminLoopCreateSuc() {
601 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
602 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
603 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
604 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
607 } catch (CadiException e) {
608 // TODO Auto-generated catch block
611 Namespace namespace = Mockito.mock(Namespace.class);
612 namespace.name = "test.test";
613 List<String> owner = new ArrayList<String>();
615 namespace.owner = owner;
616 namespace.admin = owner;
618 Organization org = Mockito.mock(Organization.class);
619 Mockito.doReturn(org).when(trans).org();
621 NsDAO.Data data = new NsDAO.Data();
623 Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]);
624 Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test");
626 CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
627 CachedCredDAO credDAO = Mockito.mock(CachedCredDAO.class);
628 CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class);
629 CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class);
630 CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class);
631 Result<Void> retVal = new Result<Void>(null,0,"test",new String[0]);
632 Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
633 Mockito.doReturn(retVal).when(nsDaoObj).create(Mockito.any(), Mockito.any());
634 List<CredDAO.Data> dataObj = new ArrayList<>();
635 CredDAO.Data indData = new CredDAO.Data();
637 indData.notes = "test";
638 DateFormat sdf = new SimpleDateFormat("yyyy/mm/dd");
640 indData.expires = sdf.parse("2090/01/01");
641 } catch (ParseException e1) {
642 // TODO Auto-generated catch block
643 e1.printStackTrace();
645 dataObj.add(indData);
646 List<RoleDAO.Data> dataObj1 = new ArrayList<>();
647 RoleDAO.Data indData1 = new RoleDAO.Data();
648 indData1.ns = "test";
649 indData1.name = "test";
650 Set<String> permsSet = new HashSet<>();
651 permsSet.add("test|test|test|test");
652 indData1.perms = permsSet;
653 dataObj1.add(indData1);
655 List<UserRoleDAO.Data> dataObj4 = new ArrayList<>();
656 UserRoleDAO.Data indData4 = new UserRoleDAO.Data();
657 indData4.ns = "test";
658 indData4.rname = "test";
659 dataObj4.add(indData4);
661 List<PermDAO.Data> dataObj5 = new ArrayList<>();
662 PermDAO.Data indData5 = new PermDAO.Data();
663 indData5.ns = "test";
664 indData5.type = "test";
665 Set<String> rolesSet = new HashSet<>();
666 rolesSet.add("test|test|test|test");
667 indData5.roles = rolesSet;
668 dataObj5.add(indData5);
670 Result<List<CredDAO.Data>> retVal2 = new Result<List<CredDAO.Data>>(dataObj,0,"test",new String[0]);
671 Result<List<RoleDAO.Data>> retVal3 = new Result<List<RoleDAO.Data>>(dataObj1,0,"test",new String[0]);
672 Result<List<UserRoleDAO.Data>> retVal4 = new Result<List<UserRoleDAO.Data>>(dataObj4,0,"test",new String[0]);
673 Result<List<PermDAO.Data>> retVal5 = new Result<List<PermDAO.Data>>(dataObj5,0,"test",new String[0]);
674 Mockito.doReturn(retVal2).when(credDAO).readID(Mockito.any(), Mockito.anyString());
675 Mockito.doReturn(retVal4).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString());
676 Mockito.doReturn(retVal2).when(userRoleDAO).create(Mockito.any(), Mockito.any());
677 Mockito.doReturn(retVal2).when(cachedRoleDAO).create(Mockito.any(), Mockito.any());
678 Mockito.doReturn(retVal2).when(cachedRoleDAO).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean());
679 Mockito.doReturn(retVal2).when(cachedPermDAO).create(Mockito.any(), Mockito.any());
680 Mockito.doReturn(retVal5).when(cachedPermDAO).readChildren(trans, "test", "test");
681 Mockito.doReturn(retVal5).when(cachedPermDAO).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean());
682 Mockito.doReturn(retVal3).when(cachedRoleDAO).readChildren(trans, "test", "test");
683 setQuestion(ques, nsDaoObj);
684 setQuestionCredDao(ques, credDAO);
685 setQuestionUserRoleDao(ques, userRoleDAO);
686 setQuestionCachedRoleDao(ques, cachedRoleDAO);
687 setQuestionCachedPermDao(ques, cachedPermDAO);
689 Identity iden=Mockito.mock(Identity.class);
691 Mockito.doReturn(iden).when(org).getIdentity(trans, "test");
692 Mockito.doReturn("test").when(iden).mayOwn();
693 Mockito.doReturn(true).when(org).isTestEnv();
694 Mockito.doReturn(new GregorianCalendar(2010, 01, 01)).when(org).expiration(null, Expiration.UserInRole);
695 } catch (OrganizationException e) {
696 // TODO Auto-generated catch block
700 Function funcObj = new Function(trans, ques);
701 Result<Void> result = funcObj.createNS(trans, namespace, true);
702 assertTrue(result.status == 0);
707 public void test4DeleteNs() {
708 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
709 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
710 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
711 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
714 } catch (CadiException e) {
715 // TODO Auto-generated catch block
718 CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
719 Result<Void> retVal = new Result<Void>(null,1,"test",new String[0]);
720 Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
721 setQuestion(ques, nsDaoObj);
723 Function funcObj = new Function(trans, ques);
724 Result<Void> result = funcObj.deleteNS(trans, "test");
726 assertTrue(result.status == Status.ERR_NsNotFound);
729 public void test4DeleteCanMOveFail() {
730 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
731 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
732 Mockito.doReturn(true).when(trans).requested(REQD_TYPE.move);
733 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
734 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
737 } catch (CadiException e) {
738 // TODO Auto-generated catch block
741 CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
742 List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>();
743 NsDAO.Data dataObj = new NsDAO.Data();
746 Result<List<NsDAO.Data>> retVal = new Result<List<NsDAO.Data>>(dataAl,0,"test",new String[0]);
747 Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
748 setQuestion(ques, nsDaoObj);
750 Mockito.doReturn(false).when(ques).canMove(Mockito.any());
752 Function funcObj = new Function(trans, ques);
753 Result<Void> result = funcObj.deleteNS(trans, "test");
754 assertTrue(result.status == Status.ERR_Denied);
758 public void test4DeleteNsReadSuc() {
759 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
760 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
761 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
762 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
765 } catch (CadiException e) {
766 // TODO Auto-generated catch block
769 CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
770 List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>();
771 NsDAO.Data dataObj = new NsDAO.Data();
774 Result<List<NsDAO.Data>> retVal = new Result<List<NsDAO.Data>>(dataAl,0,"test",new String[0]);
775 Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
776 setQuestion(ques, nsDaoObj);
778 Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,1,"test",new String[0]);
779 Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write);
781 Function funcObj = new Function(trans, ques);
782 Result<Void> result = funcObj.deleteNS(trans, "test");
783 assertTrue(result.status == 1);
787 // public void test4DeleteNsMayUserSuc() {
788 // Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
789 // Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
790 // Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
791 // Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
793 // Define.set(access);
794 // } catch (CadiException e) {
795 // // TODO Auto-generated catch block
796 // e.printStackTrace();
798 // CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
799 // List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>();
800 // NsDAO.Data dataObj = new NsDAO.Data();
802 // dataAl.add(dataObj);
803 // Result<List<NsDAO.Data>> retVal = new Result<List<NsDAO.Data>>(dataAl,0,"test",new String[0]);
804 // Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
805 // setQuestion(ques, nsDaoObj);
807 // Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]);
808 // Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write);
810 // Function funcObj = new Function(trans, ques);
811 // Result<Void> result = funcObj.deleteNS(trans, "test");
812 // assertTrue(result.status == 1);
814 // Mockito.doReturn(true).when(ques).isGranted(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
816 // CachedCredDAO credDAO = Mockito.mock(CachedCredDAO.class);
817 // Mockito.doReturn(retVal2).when(credDAO).readNS(Mockito.any(), Mockito.anyString());
818 // setQuestionCredDao(ques, credDAO);
820 // CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class);
821 // Result<List<PermDAO.Data>> retVal5 = new Result<List<PermDAO.Data>>(null,0,"test",new String[0]);
822 // Mockito.doReturn(retVal5).when(cachedPermDAO).readNS(trans, "test");
823 // setQuestionCachedPermDao(ques, cachedPermDAO);
825 // CachedUserRoleDAO cachedUserRoleDAO = Mockito.mock(CachedUserRoleDAO.class);
826 // List<UserRoleDAO.Data> dataObj4 = new ArrayList<>();
827 // UserRoleDAO.Data indData4 = new UserRoleDAO.Data();
828 // indData4.ns = "test";
829 // indData4.rname = "test";
830 // dataObj4.add(indData4);
831 // Result<List<UserRoleDAO.Data>> retVal4 = new Result<List<UserRoleDAO.Data>>(dataObj4,0,"test",new String[0]);
832 // Mockito.doReturn(retVal4).when(cachedUserRoleDAO).readByRole(trans, "test");
833 // setQuestionUserRoleDao(ques, cachedUserRoleDAO);
835 // CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class);
836 // List<RoleDAO.Data> dataObj1 = new ArrayList<>();
837 // RoleDAO.Data indData1 = new RoleDAO.Data();
838 // indData1.ns = "test";
839 // indData1.name = "test";
840 // Set<String> permsSet = new HashSet<>();
841 // permsSet.add("test|test");
842 // indData1.perms = permsSet;
843 // dataObj1.add(indData1);
844 // Result<List<RoleDAO.Data>> retVal3 = new Result<List<RoleDAO.Data>>(dataObj1,0,"test",new String[0]);
845 // Mockito.doReturn(retVal3).when(cachedRoleDAO).readNS(trans, "test");
846 // setQuestionCachedRoleDao(ques, cachedRoleDAO);
848 // funcObj = new Function(trans, ques);
849 // result = funcObj.deleteNS(trans, "test");
850 // assertTrue(result.status == Status.ERR_DependencyExists);
852 // Mockito.doReturn(true).when(trans).requested(REQD_TYPE.force);
853 // funcObj = new Function(trans, ques);
854 // result = funcObj.deleteNS(trans, "test");
855 // assertTrue(result.status == 2);
858 // public void test4DeleteNsMayUserSuc() {
859 // Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
860 // Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
861 // Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
862 // Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
864 // Define.set(access);
865 // } catch (CadiException e) {
866 // // TODO Auto-generated catch block
867 // e.printStackTrace();
869 // CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class);
870 // List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>();
871 // NsDAO.Data dataObj = new NsDAO.Data();
873 // dataAl.add(dataObj);
874 // Result<List<NsDAO.Data>> retVal = new Result<List<NsDAO.Data>>(dataAl,0,"test",new String[0]);
875 // Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString());
876 // setQuestion(ques, nsDaoObj);
878 // Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]);
879 // Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write);
881 // Function funcObj = new Function(trans, ques);
882 // Result<Void> result = funcObj.deleteNS(trans, "test");
883 // assertTrue(result.status == 1);