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