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