Remove spaces from dockerbuild script
[aaf/authz.git] / auth / auth-cass / src / test / java / com / att / dao / aaf / test / JU_PermDAO.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 com.att.dao.aaf.test;
23
24 import static junit.framework.Assert.assertEquals;
25 import static junit.framework.Assert.assertTrue;
26
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 import java.util.List;
30 import java.util.Set;
31
32 import org.junit.Test;
33 import org.onap.aaf.auth.dao.CassAccess;
34 import org.onap.aaf.auth.dao.cass.PermDAO;
35 import org.onap.aaf.auth.dao.cass.RoleDAO;
36 import org.onap.aaf.auth.dao.cass.PermDAO.Data;
37 import org.onap.aaf.auth.layer.Result;
38 import org.onap.aaf.misc.env.APIException;
39
40 /**
41  * Test the PermissionDAO
42  * 
43  * Utilize AbsJUCass to initialize and pre-load Cass
44  * 
45  * @author Jonathan
46  *
47  */
48 public class JU_PermDAO extends AbsJUCass{
49
50         @Test
51         public void test() throws APIException, IOException {
52                 PermDAO pd = new PermDAO(trans,cluster,CassAccess.KEYSPACE);
53                 try {
54                         PermDAO.Data data = new PermDAO.Data();
55                         data.ns = "com.test.ju_perm";
56                         data.type = "MyType";
57                         data.instance = "MyInstance";
58                         data.action = "MyAction";
59                         data.roles(true).add(data.ns + ".dev");
60                         
61
62
63                         // CREATE
64                         Result<Data> rpdc = pd.create(trans,data);
65                         assertTrue(rpdc.isOK());
66
67                         Result<List<PermDAO.Data>> rlpd;
68                         try {
69 //                      Bytification
70                         ByteBuffer bb = data.bytify();
71                         Data bdata = new PermDAO.Data();
72                         bdata.reconstitute(bb);
73                         compare(data, bdata);
74
75                                 // Validate Read with key fields in Data
76                                 if((rlpd = pd.read(trans,data)).isOK())
77                                   for(PermDAO.Data d : rlpd.value) {
78                                         checkData1(data,d);
79                                 }
80                                 
81                                 // Validate readByName
82                                 if((rlpd = pd.readByType(trans,data.ns, data.type)).isOK())
83                                   for(PermDAO.Data d : rlpd.value) {
84                                         checkData1(data,d);
85                                 }
86                                 
87                                 // Add Role
88                                 RoleDAO.Data role = new RoleDAO.Data();
89                                 role.ns = data.ns;
90                                 role.name = "test";
91                                 
92                                 Result<Void> rvpd = pd.addRole(trans, data, role.fullName());
93                                 assertTrue(rvpd.isOK());
94                                 // Validate Read with key fields in Data
95                                 if((rlpd = pd.read(trans,data)).isOK())
96                                   for(PermDAO.Data d : rlpd.value) {
97                                         checkData2(data,d);
98                                   }
99                                 
100                                 // Remove Role
101                                 rvpd = pd.delRole(trans, data, role.fullName());
102                                 assertTrue(rvpd.isOK());
103                                 if((rlpd = pd.read(trans,data)).isOK())
104                                         for(PermDAO.Data d : rlpd.value) {
105                                                 checkData1(data,d);
106                                         }
107                                 
108                                 // Add Child
109                                 Data data2 = new Data();
110                                 data2.ns = data.ns;
111                                 data2.type = data.type + ".2";
112                                 data2.instance = data.instance;
113                                 data2.action = data.action;
114                                 
115                                 rpdc = pd.create(trans, data2);
116                                 assertTrue(rpdc.isOK());
117                                 try {
118                                         rlpd = pd.readChildren(trans, data.ns,data.type);
119                                         assertTrue(rlpd.isOKhasData());
120                                         assertEquals(rlpd.value.size(),1);
121                                         assertEquals(rlpd.value.get(0).fullType(),data2.fullType());
122                                 } finally {
123                                         // Delete Child
124                                         pd.delete(trans, data2,true);
125
126                                 }
127                         } catch (IOException e) {
128                                 e.printStackTrace();
129                         } finally {
130                                 // DELETE
131                                 Result<Void> rpdd = pd.delete(trans,data,true);
132                                 assertTrue(rpdd.isOK());
133                                 rlpd = pd.read(trans, data);
134                                 assertTrue(rlpd.isOK() && rlpd.isEmpty());
135                                 assertEquals(rlpd.value.size(),0);
136                         }
137                 } finally {
138                         pd.close(trans);
139                 }
140         }
141
142         private void compare(Data a, Data b) {
143                 assertEquals(a.ns,b.ns);
144                 assertEquals(a.type,b.type);
145                 assertEquals(a.instance,b.instance);
146                 assertEquals(a.action,b.action);
147                 assertEquals(a.roles(false).size(),b.roles(false).size());
148                 for(String s: a.roles(false)) {
149                         assertTrue(b.roles(false).contains(s));
150                 }
151         }
152         private void checkData1(Data data, Data d) {
153                 assertEquals(data.ns,d.ns);
154                 assertEquals(data.type,d.type);
155                 assertEquals(data.instance,d.instance);
156                 assertEquals(data.action,d.action);
157                 
158                 Set<String> ss = d.roles(true);
159                 assertEquals(1,ss.size());
160                 assertTrue(ss.contains(data.ns+".dev"));
161         }
162         
163         private void checkData2(Data data, Data d) {
164                 assertEquals(data.ns,d.ns);
165                 assertEquals(data.type,d.type);
166                 assertEquals(data.instance,d.instance);
167                 assertEquals(data.action,d.action);
168                 
169                 Set<String> ss = d.roles(true);
170                 assertEquals(2,ss.size());
171                 assertTrue(ss.contains(data.ns+".dev"));
172                 assertTrue(ss.contains(data.ns+".test"));
173         }
174
175
176 }