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