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