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 ******************************************************************************/
22 package org.onap.aaf.auth.dao.aaf.test;
24 import static junit.framework.Assert.assertEquals;
25 import static junit.framework.Assert.assertTrue;
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 import java.util.List;
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;
40 * Test the PermissionDAO
42 * Utilize AbsJUCass to initialize and pre-load Cass
46 public class JU_PermDAO extends AbsJUCass{
49 public void test() throws APIException, IOException {
50 PermDAO pd = new PermDAO(trans,cluster,"authz");
52 PermDAO.Data data = new PermDAO.Data();
53 data.ns = "com.test.ju_perm";
55 data.instance = "MyInstance";
56 data.action = "MyAction";
57 data.roles(true).add(data.ns + ".dev");
62 Result<Data> rpdc = pd.create(trans,data);
63 assertTrue(rpdc.isOK());
65 Result<List<PermDAO.Data>> rlpd;
68 ByteBuffer bb = data.bytify();
69 Data bdata = new PermDAO.Data();
70 bdata.reconstitute(bb);
73 // Validate Read with key fields in Data
74 if((rlpd = pd.read(trans,data)).isOK())
75 for(PermDAO.Data d : rlpd.value) {
79 // Validate readByName
80 if((rlpd = pd.readByType(trans,data.ns, data.type)).isOK())
81 for(PermDAO.Data d : rlpd.value) {
86 RoleDAO.Data role = new RoleDAO.Data();
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) {
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) {
107 Data data2 = new Data();
109 data2.type = data.type + ".2";
110 data2.instance = data.instance;
111 data2.action = data.action;
113 rpdc = pd.create(trans, data2);
114 assertTrue(rpdc.isOK());
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());
122 pd.delete(trans, data2,true);
125 } catch (IOException e) {
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);
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));
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);
156 Set<String> ss = d.roles(true);
157 assertEquals(1,ss.size());
158 assertTrue(ss.contains(data.ns+".dev"));
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);
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"));