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