1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.dao.aaf.test;
\r
25 import static junit.framework.Assert.assertEquals;
\r
26 import static junit.framework.Assert.assertTrue;
\r
28 import java.io.IOException;
\r
29 import java.nio.ByteBuffer;
\r
30 import java.util.List;
\r
31 import java.util.Set;
\r
33 import org.junit.Test;
\r
34 import org.onap.aaf.authz.layer.Result;
\r
35 import org.onap.aaf.dao.aaf.cass.PermDAO;
\r
36 import org.onap.aaf.dao.aaf.cass.RoleDAO;
\r
37 import org.onap.aaf.dao.aaf.cass.PermDAO.Data;
\r
39 import org.onap.aaf.inno.env.APIException;
\r
42 * Test the PermissionDAO
\r
44 * Utilize AbsJUCass to initialize and pre-load Cass
\r
48 public class JU_PermDAO extends AbsJUCass{
\r
51 public void test() throws APIException, IOException {
\r
52 PermDAO pd = new PermDAO(trans,cluster,"authz");
\r
54 PermDAO.Data data = new PermDAO.Data();
\r
55 data.ns = "com.test.ju_perm";
\r
56 data.type = "MyType";
\r
57 data.instance = "MyInstance";
\r
58 data.action = "MyAction";
\r
59 data.roles(true).add(data.ns + ".dev");
\r
64 Result<Data> rpdc = pd.create(trans,data);
\r
65 assertTrue(rpdc.isOK());
\r
67 Result<List<PermDAO.Data>> rlpd;
\r
70 ByteBuffer bb = data.bytify();
\r
71 Data bdata = new PermDAO.Data();
\r
72 bdata.reconstitute(bb);
\r
73 compare(data, bdata);
\r
75 // Validate Read with key fields in Data
\r
76 if((rlpd = pd.read(trans,data)).isOK())
\r
77 for(PermDAO.Data d : rlpd.value) {
\r
81 // Validate readByName
\r
82 if((rlpd = pd.readByType(trans,data.ns, data.type)).isOK())
\r
83 for(PermDAO.Data d : rlpd.value) {
\r
88 RoleDAO.Data role = new RoleDAO.Data();
\r
92 Result<Void> rvpd = pd.addRole(trans, data, role.fullName());
\r
93 assertTrue(rvpd.isOK());
\r
94 // Validate Read with key fields in Data
\r
95 if((rlpd = pd.read(trans,data)).isOK())
\r
96 for(PermDAO.Data d : rlpd.value) {
\r
101 rvpd = pd.delRole(trans, data, role.fullName());
\r
102 assertTrue(rvpd.isOK());
\r
103 if((rlpd = pd.read(trans,data)).isOK())
\r
104 for(PermDAO.Data d : rlpd.value) {
\r
105 checkData1(data,d);
\r
109 Data data2 = new Data();
\r
110 data2.ns = data.ns;
\r
111 data2.type = data.type + ".2";
\r
112 data2.instance = data.instance;
\r
113 data2.action = data.action;
\r
115 rpdc = pd.create(trans, data2);
\r
116 assertTrue(rpdc.isOK());
\r
118 rlpd = pd.readChildren(trans, data.ns,data.type);
\r
119 assertTrue(rlpd.isOKhasData());
\r
120 assertEquals(rlpd.value.size(),1);
\r
121 assertEquals(rlpd.value.get(0).fullType(),data2.fullType());
\r
124 pd.delete(trans, data2,true);
\r
127 } catch (IOException e) {
\r
128 e.printStackTrace();
\r
131 Result<Void> rpdd = pd.delete(trans,data,true);
\r
132 assertTrue(rpdd.isOK());
\r
133 rlpd = pd.read(trans, data);
\r
134 assertTrue(rlpd.isOK() && rlpd.isEmpty());
\r
135 assertEquals(rlpd.value.size(),0);
\r
142 private void compare(Data a, Data b) {
\r
143 assertEquals(a.ns,b.ns);
\r
144 assertEquals(a.type,b.type);
\r
145 assertEquals(a.instance,b.instance);
\r
146 assertEquals(a.action,b.action);
\r
147 assertEquals(a.roles(false).size(),b.roles(false).size());
\r
148 for(String s: a.roles(false)) {
\r
149 assertTrue(b.roles(false).contains(s));
\r
152 private void checkData1(Data data, Data d) {
\r
153 assertEquals(data.ns,d.ns);
\r
154 assertEquals(data.type,d.type);
\r
155 assertEquals(data.instance,d.instance);
\r
156 assertEquals(data.action,d.action);
\r
158 Set<String> ss = d.roles(true);
\r
159 assertEquals(1,ss.size());
\r
160 assertTrue(ss.contains(data.ns+".dev"));
\r
163 private void checkData2(Data data, Data d) {
\r
164 assertEquals(data.ns,d.ns);
\r
165 assertEquals(data.type,d.type);
\r
166 assertEquals(data.instance,d.instance);
\r
167 assertEquals(data.action,d.action);
\r
169 Set<String> ss = d.roles(true);
\r
170 assertEquals(2,ss.size());
\r
171 assertTrue(ss.contains(data.ns+".dev"));
\r
172 assertTrue(ss.contains(data.ns+".test"));
\r