4ddd421aad8e4e61564345a615c7a83339aac9c4
[aaf/authz.git] / auth / auth-cmd / src / test / java / org / onap / aaf / auth / cmd / test / role / JU_List.java
1 /*
2  * ============LICENSE_START==========================================
3  * ===================================================================
4  * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
5  * ===================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END============================================
18  */
19
20 package org.onap.aaf.auth.cmd.test.role;
21
22 import static org.junit.Assert.*;
23
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.onap.aaf.auth.cmd.AAFcli;
30 import org.onap.aaf.auth.cmd.role.List;
31 import org.onap.aaf.auth.cmd.role.Role;
32 import org.onap.aaf.auth.cmd.test.JU_AAFCli;
33 import org.onap.aaf.auth.cmd.Cmd;
34 import org.onap.aaf.auth.cmd.Param;
35 import org.onap.aaf.auth.env.AuthzEnv;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.cadi.Access;
38 import org.onap.aaf.cadi.CadiException;
39 import org.onap.aaf.cadi.Locator;
40 import org.onap.aaf.cadi.LocatorException;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.SecuritySetter;
43 import org.onap.aaf.cadi.client.Future;
44 import org.onap.aaf.cadi.client.Rcli;
45 import org.onap.aaf.cadi.config.SecurityInfoC;
46 import org.onap.aaf.cadi.http.HMangr;
47 import org.onap.aaf.misc.env.APIException;
48
49 import aaf.v2_0.Perms;
50 import aaf.v2_0.Pkey;
51 import aaf.v2_0.Roles;
52 import aaf.v2_0.UserRoles;
53
54 import static org.mockito.Mockito.*;
55
56 import java.io.IOException;
57 import java.io.Writer;
58 import java.lang.reflect.InvocationTargetException;
59 import java.lang.reflect.Method;
60 import java.net.HttpURLConnection;
61 import java.net.URI;
62 import java.security.GeneralSecurityException;
63 import java.security.Principal;
64 import java.util.ArrayList;
65
66 import org.junit.Test;
67
68 public class JU_List {
69     
70     AAFcli cli;
71     Role role;
72     List list;
73     PropAccess prop;
74     AuthzEnv aEnv;
75     Writer wtr;
76     Locator<URI> loc;
77     HMangr hman;    
78     AAFcli aafcli;
79
80     private class ListRolesStub extends List {
81
82         public ListRolesStub(Role parent) {
83             super(parent);
84             // TODO Auto-generated constructor stub
85         }
86     }
87     
88     private class RolesStub extends Roles {
89         public void addRole(aaf.v2_0.Role role) {
90             if (this.role == null) {
91                 this.role = new ArrayList<>();
92             }
93             this.role.add(role);
94         }
95     }
96     
97     private class RoleStub extends aaf.v2_0.Role {
98         
99         public void addPerms(Pkey perms) {
100             if (this.perms == null) {
101                 this.perms = new ArrayList<>();
102             }
103             this.perms.add(perms); 
104         }
105     }
106     
107     @Before
108     public void setUp() throws APIException, LocatorException, GeneralSecurityException, IOException, CadiException{
109         prop = new PropAccess();
110         aEnv = new AuthzEnv();
111         wtr = mock(Writer.class);
112         loc = mock(Locator.class);
113         SecuritySetter<HttpURLConnection> secSet = mock(SecuritySetter.class);
114         hman = new HMangr(aEnv, loc);    
115         aafcli = new AAFcli(prop, aEnv, wtr, hman, null, secSet);
116         role = new Role(aafcli);
117         list = new List(role);
118     }
119     
120     @Test
121     public void testRoles() throws APIException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
122         Role role = new Role(aafcli);
123         ListRolesStub listStub = new ListRolesStub(role);
124         Future future = mock(Future.class);
125         Rcli rcli = mock(Rcli.class);
126         
127         Class c = listStub.getClass();
128         Class[] cArg = new Class[3];
129         cArg[0] = Future.class;
130         cArg[1] = Rcli.class;
131         cArg[2] = String.class;//Steps to test a protected method
132         //Method listMethod = c.getDeclaredMethod("list", cArg);
133         //listMethod.setAccessible(true);
134         //listMethod.invoke(listStub, future, rcli, "test");
135         
136     }
137     
138     @Test
139     public void testReport() throws Exception {
140         UserRoles urs = new UserRoles();
141         Perms perms = new Perms();
142         RolesStub roles = new RolesStub();
143         list.report(roles, perms , urs , "test");
144         AAFcli cli = JU_AAFCli.getAAfCli();
145         RoleStub role = new RoleStub();
146         roles.addRole(role);
147         Pkey pkey = new Pkey();
148         pkey.setInstance("test");
149         pkey.setAction("test");
150         pkey.setInstance("test");
151         pkey.setType("test");
152         role.addPerms(pkey);
153         list.report(roles, perms , null , "test");
154         list.report(roles, perms , urs , "test");
155         
156         aafcli.eval("DETAILS @[ 123");
157         role.setName("test");
158
159         list.report(roles, perms , urs , "test");
160     }
161
162 }