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