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