AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / aaf / test / JU_Bytification.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 org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 import java.util.Date;
30
31 import org.junit.Test;
32 import org.onap.aaf.auth.dao.cass.CredDAO;
33 import org.onap.aaf.auth.dao.cass.NsDAO;
34 import org.onap.aaf.auth.dao.cass.NsType;
35 import org.onap.aaf.auth.dao.cass.PermDAO;
36 import org.onap.aaf.auth.dao.cass.RoleDAO;
37 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
38
39 public class JU_Bytification {
40
41         @Test
42         public void testNS() throws IOException {
43                 
44                 // Normal
45                 NsDAO.Data ns = new NsDAO.Data();
46                 ns.name = "org.osaaf.<pass>";
47                 ns.type = NsType.APP.type;
48
49                 ByteBuffer bb = ns.bytify();
50                 
51                 NsDAO.Data nsr = new NsDAO.Data();
52                 nsr.reconstitute(bb);
53                 check(ns,nsr);
54                 
55                 // Empty admin
56 //              ns.admin(true).clear();
57                 bb = ns.bytify();
58                 nsr = new NsDAO.Data();
59                 nsr.reconstitute(bb);
60                 check(ns,nsr);
61                 
62                 // Empty responsible
63 //              ns.responsible(true).clear();
64                 bb = ns.bytify();
65                 nsr = new NsDAO.Data();
66                 nsr.reconstitute(bb);
67                 check(ns,nsr);
68
69                 bb = ns.bytify();
70                 nsr = new NsDAO.Data();
71                 nsr.reconstitute(bb);
72                 check(ns,nsr);
73         }
74         
75         private void check(NsDAO.Data a, NsDAO.Data b) {
76                 assertEquals(a.name,b.name);
77                 assertEquals(a.type,b.type);
78 //              assertEquals(a.admin.size(),b.admin.size());
79                 
80 //              for(String s: a.admin) {
81 //                      assertTrue(b.admin.contains(s));
82 //              }
83 //              
84 //              assertEquals(a.responsible.size(),b.responsible.size());
85 //              for(String s: a.responsible) {
86 //                      assertTrue(b.responsible.contains(s));
87 //              }
88         }
89
90         @Test
91         public void testRole() throws IOException {
92                 RoleDAO.Data rd1 = new RoleDAO.Data();
93                 rd1.ns = "org.osaaf.<pass>";
94                 rd1.name = "my.role";
95                 rd1.perms(true).add("org.osaaf.<pass>.my.Perm|myInstance|myAction");
96                 rd1.perms(true).add("org.osaaf.<pass>.my.Perm|myInstance|myAction2");
97
98                 // Normal
99                 ByteBuffer bb = rd1.bytify();
100                 RoleDAO.Data rd2 = new RoleDAO.Data();
101                 rd2.reconstitute(bb);
102                 check(rd1,rd2);
103                 
104                 // Overshoot Buffer
105                 StringBuilder sb = new StringBuilder(300);
106                 sb.append("role|instance|veryLongAction...");
107                 for(int i=0;i<280;++i) {
108                         sb.append('a');
109                 }
110                 rd1.perms(true).add(sb.toString());
111                 bb = rd1.bytify();
112                 rd2 = new RoleDAO.Data();
113                 rd2.reconstitute(bb);
114                 check(rd1,rd2);
115                 
116                 // No Perms
117                 rd1.perms.clear();
118                 
119                 bb = rd1.bytify();
120                 rd2 = new RoleDAO.Data();
121                 rd2.reconstitute(bb);
122                 check(rd1,rd2);
123                 
124                 // 1000 Perms
125                 for(int i=0;i<1000;++i) {
126                         rd1.perms(true).add("com|inst|action"+ i);
127                 }
128
129                 bb = rd1.bytify();
130                 rd2 = new RoleDAO.Data();
131                 rd2.reconstitute(bb);
132                 check(rd1,rd2);
133
134         }
135         
136         private void check(RoleDAO.Data a, RoleDAO.Data b) {
137                 assertEquals(a.ns,b.ns);
138                 assertEquals(a.name,b.name);
139                 
140                 assertEquals(a.perms.size(),b.perms.size());
141                 for(String s: a.perms) {
142                         assertTrue(b.perms.contains(s));
143                 }
144         }
145
146         @Test
147         public void testPerm() throws IOException {
148                 PermDAO.Data pd1 = new PermDAO.Data();
149                 pd1.ns = "org.osaaf.<pass>";
150                 pd1.type = "my.perm";
151                 pd1.instance = "instance";
152                 pd1.action = "read";
153                 pd1.roles(true).add("org.osaaf.<pass>.my.Role");
154                 pd1.roles(true).add("org.osaaf.<pass>.my.Role2");
155
156                 // Normal
157                 ByteBuffer bb = pd1.bytify();
158                 PermDAO.Data rd2 = new PermDAO.Data();
159                 rd2.reconstitute(bb);
160                 check(pd1,rd2);
161                 
162                 // No Perms
163                 pd1.roles.clear();
164                 
165                 bb = pd1.bytify();
166                 rd2 = new PermDAO.Data();
167                 rd2.reconstitute(bb);
168                 check(pd1,rd2);
169                 
170                 // 1000 Perms
171                 for(int i=0;i<1000;++i) {
172                         pd1.roles(true).add("org.osaaf.<pass>.my.Role"+ i);
173                 }
174
175                 bb = pd1.bytify();
176                 rd2 = new PermDAO.Data();
177                 rd2.reconstitute(bb);
178                 check(pd1,rd2);
179
180         }
181         
182         private void check(PermDAO.Data a, PermDAO.Data b) {
183                 assertEquals(a.ns,b.ns);
184                 assertEquals(a.type,b.type);
185                 assertEquals(a.instance,b.instance);
186                 assertEquals(a.action,b.action);
187                 
188                 assertEquals(a.roles.size(),b.roles.size());
189                 for(String s: a.roles) {
190                         assertTrue(b.roles.contains(s));
191                 }
192         }
193
194         @Test
195         public void testUserRole() throws IOException {
196                 UserRoleDAO.Data urd1 = new UserRoleDAO.Data();
197                 urd1.user = "myname@abc.att.com";
198                 urd1.role("org.osaaf.<pass>","my.role");
199                 urd1.expires = new Date();
200
201                 // Normal
202                 ByteBuffer bb = urd1.bytify();
203                 UserRoleDAO.Data urd2 = new UserRoleDAO.Data();
204                 urd2.reconstitute(bb);
205                 check(urd1,urd2);
206                 
207                 // A null
208                 urd1.expires = null; 
209                 urd1.role = null;
210                 
211                 bb = urd1.bytify();
212                 urd2 = new UserRoleDAO.Data();
213                 urd2.reconstitute(bb);
214                 check(urd1,urd2);
215         }
216
217         private void check(UserRoleDAO.Data a, UserRoleDAO.Data b) {
218                 assertEquals(a.user,b.user);
219                 assertEquals(a.role,b.role);
220                 assertEquals(a.expires,b.expires);
221         }
222
223         
224         @Test
225         public void testCred() throws IOException {
226                 CredDAO.Data cd = new CredDAO.Data();
227                 cd.id = "m55555@abc.att.com";
228                 cd.ns = "org.osaaf.abc";
229                 cd.type = 2;
230                 cd.cred = ByteBuffer.wrap(new byte[]{1,34,5,3,25,0,2,5,3,4});
231                 cd.expires = new Date();
232
233                 // Normal
234                 ByteBuffer bb = cd.bytify();
235                 CredDAO.Data cd2 = new CredDAO.Data();
236                 cd2.reconstitute(bb);
237                 check(cd,cd2);
238                 
239                 // nulls
240                 cd.expires = null;
241                 cd.cred = null;
242                 
243                 bb = cd.bytify();
244                 cd2 = new CredDAO.Data();
245                 cd2.reconstitute(bb);
246                 check(cd,cd2);
247
248         }
249
250         private void check(CredDAO.Data a, CredDAO.Data b) {
251                 assertEquals(a.id,b.id);
252                 assertEquals(a.ns,b.ns);
253                 assertEquals(a.type,b.type);
254                 if(a.cred==null) {
255                         assertEquals(a.cred,b.cred); 
256                 } else {
257                         int l = a.cred.limit();
258                         assertEquals(l,b.cred.limit());
259                         for (int i=0;i<l;++i) {
260                                 assertEquals(a.cred.get(),b.cred.get());
261                         }
262                 }
263         }
264
265 }