AT&T 2.0.19 Code drop, stage 4
[aaf/authz.git] / authz-cass / src / test / java / org / onap / aaf / dao / aaf / test / JU_DelegateDAO.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 \r
26 import static org.junit.Assert.assertEquals;\r
27 import static org.junit.Assert.assertTrue;\r
28 \r
29 import java.nio.ByteBuffer;\r
30 import java.util.Date;\r
31 import java.util.List;\r
32 \r
33 import org.junit.Test;\r
34 import org.onap.aaf.authz.layer.Result;\r
35 import org.onap.aaf.dao.aaf.cass.DelegateDAO;\r
36 import org.onap.aaf.dao.aaf.cass.DelegateDAO.Data;\r
37 \r
38 \r
39 public class JU_DelegateDAO  extends AbsJUCass {\r
40         @Test\r
41         public void testCRUD() throws Exception {\r
42                 DelegateDAO dao = new DelegateDAO(trans, cluster, AUTHZ);\r
43                 DelegateDAO.Data data = new DelegateDAO.Data();\r
44                 data.user = "myname";\r
45                 data.delegate = "yourname";\r
46                 data.expires = new Date();\r
47                 \r
48 //        Bytification\r
49         ByteBuffer bb = data.bytify();\r
50         Data bdata = new DelegateDAO.Data();\r
51         bdata.reconstitute(bb);\r
52         compare(data, bdata);\r
53 \r
54                 try {\r
55                         // Test create\r
56                         Result<Data> ddcr = dao.create(trans,data);\r
57                         assertTrue(ddcr.isOK());\r
58                         \r
59                         \r
60                         // Read by User\r
61                         Result<List<DelegateDAO.Data>> records = dao.read(trans,data.user);\r
62                         assertTrue(records.isOKhasData());\r
63                         for(DelegateDAO.Data rdata : records.value) \r
64                                 compare(data,rdata);\r
65 \r
66                         // Read by Delegate\r
67                         records = dao.readByDelegate(trans,data.delegate);\r
68                         assertTrue(records.isOKhasData());\r
69                         for(DelegateDAO.Data rdata : records.value) \r
70                                 compare(data,rdata);\r
71                         \r
72                         // Update\r
73                         data.delegate = "hisname";\r
74                         data.expires = new Date();\r
75                         assertTrue(dao.update(trans, data).isOK());\r
76 \r
77                         // Read by User\r
78                         records = dao.read(trans,data.user);\r
79                         assertTrue(records.isOKhasData());\r
80                         for(DelegateDAO.Data rdata : records.value) \r
81                                 compare(data,rdata);\r
82 \r
83                         // Read by Delegate\r
84                         records = dao.readByDelegate(trans,data.delegate);\r
85                         assertTrue(records.isOKhasData());\r
86                         for(DelegateDAO.Data rdata : records.value) \r
87                                 compare(data,rdata);\r
88 \r
89                         // Test delete\r
90                         dao.delete(trans,data, true);\r
91                         records = dao.read(trans,data.user);\r
92                         assertTrue(records.isEmpty());\r
93                         \r
94                         \r
95                 } finally {\r
96                         dao.close(trans);\r
97                 }\r
98         }\r
99         \r
100         private void compare(Data d1, Data d2) {\r
101                 assertEquals(d1.user, d2.user);\r
102                 assertEquals(d1.delegate, d2.delegate);\r
103                 assertEquals(d1.expires,d2.expires);\r
104         }\r
105 \r
106 \r
107 }\r