8d444950827e3ed2a48ebb6a00fbfd1724e2b157
[aaf/authz.git] / authz-cass / src / test / java / com / att / dao / aaf / test / JU_HistoryDAO.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.assertNotNull;\r
27 import static org.junit.Assert.assertTrue;\r
28 \r
29 import java.nio.ByteBuffer;\r
30 import java.util.List;\r
31 import java.util.Random;\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.HistoryDAO;\r
37 \r
38 public class JU_HistoryDAO  extends AbsJUCass {\r
39         \r
40         @Test\r
41         public void testCreate() throws Exception {\r
42                 HistoryDAO historyDAO = new HistoryDAO(trans, cluster, AUTHZ);\r
43                 HistoryDAO.Data data = createHistoryData();\r
44                 \r
45                 try {\r
46                         historyDAO.create(trans,data);                  \r
47                         Thread.sleep(200);// History Create is Async\r
48                         Result<List<HistoryDAO.Data>> records = historyDAO.readByUser(trans,data.user,data.yr_mon);\r
49                         assertTrue(records.isOKhasData());\r
50                         for(HistoryDAO.Data d : records.value) {\r
51                                 assertHistory(data, d);\r
52                         }\r
53                 } finally {\r
54                         historyDAO.close(trans);\r
55                 }\r
56         }\r
57         \r
58         @Test\r
59         public void tesReadByUser() throws Exception {\r
60                 HistoryDAO historyDAO = new HistoryDAO(trans,cluster, AUTHZ);\r
61                 HistoryDAO.Data data = createHistoryData();\r
62                 \r
63                 try {\r
64                         historyDAO.create(trans,data);\r
65                         Thread.sleep(200);// History Create is Async\r
66                         Result<List<HistoryDAO.Data>> records = historyDAO.readByUser(trans, data.user,data.yr_mon);\r
67                         assertTrue(records.isOKhasData());\r
68                         for(HistoryDAO.Data d : records.value) {\r
69                                 assertHistory(data, d);\r
70                         }\r
71                 } finally {\r
72                         historyDAO.close(trans);\r
73                 }\r
74         }\r
75         \r
76 /*\r
77         @Test\r
78         public void readByUserAndMonth() throws Exception {\r
79                 HistoryDAO historyDAO = new HistoryDAO(trans,cluster, AUTHZ);\r
80                 HistoryDAO.Data data = createHistoryData();\r
81                 \r
82                 try {\r
83                         historyDAO.create(trans,data);                  \r
84                         Thread.sleep(200);// History Create is Async\r
85                         Result<List<HistoryDAO.Data>> records = historyDAO.readByUserAndMonth(trans,\r
86                                         data.user, Integer.valueOf(String.valueOf(data.yr_mon).substring(0, 4)),\r
87                                         Integer.valueOf(String.valueOf(data.yr_mon).substring(4, 6)));\r
88                         assertTrue(records.isOKhasData());\r
89                         for(HistoryDAO.Data d : records.value) {\r
90                                 assertHistory(data, d);\r
91                         }\r
92                 } finally {\r
93                         historyDAO.close(trans);\r
94                 }\r
95         }\r
96 */      \r
97         //TODO readadd this\r
98 //      @Test\r
99 //      public void readByUserAndDay() throws Exception {\r
100 //              HistoryDAO historyDAO = new HistoryDAO(trans, cluster, AUTHZ);\r
101 //              HistoryDAO.Data data = createHistoryData();\r
102 //              \r
103 //              try {\r
104 //                      historyDAO.create(trans, data);         \r
105 //                      Thread.sleep(200);// History Create is Async\r
106 //                      \r
107 //                      String dayTime = String.valueOf(data.day_time);\r
108 //                      String day = null;\r
109 //                      if (dayTime.length() < 8)\r
110 //                              day = dayTime.substring(0, 1);\r
111 //                      else \r
112 //                              day = dayTime.substring(0, 2);\r
113 //                      \r
114 //                      List<HistoryDAO.Data> records = historyDAO.readByUserBetweenDates(trans,\r
115 //                                                      data.user, Integer.valueOf(String.valueOf(data.yr_mon).substring(0, 4)),\r
116 //                                                      Integer.valueOf(String.valueOf(data.yr_mon).substring(4, 6)),\r
117 //                                                      Integer.valueOf(day), 0);\r
118 //                      assertEquals(1,records.size());\r
119 //                      for(HistoryDAO.Data d : records) {\r
120 //                              assertHistory(data, d);\r
121 //                      }\r
122 //              } finally {\r
123 //                      historyDAO.close(trans);\r
124 //              }\r
125 //      }\r
126         private HistoryDAO.Data createHistoryData() {\r
127                 HistoryDAO.Data data = HistoryDAO.newInitedData();\r
128                 Random random = new Random();\r
129                 data.user = "test" + random.nextInt();\r
130                 data.action = "add";\r
131                 data.target = "history";\r
132                 data.memo = "adding a row into history table";\r
133 //              data.detail().put("id", "test");\r
134 //              data.detail().put("name", "test");\r
135                 //String temp = "Test Blob Message";\r
136                 data.reconstruct = ByteBuffer.wrap("Temp Blob Message".getBytes());             \r
137                 return data;\r
138         }\r
139         \r
140         private void assertHistory(HistoryDAO.Data ip, HistoryDAO.Data op) {\r
141                 assertEquals(ip.yr_mon, op.yr_mon);             \r
142 //              assertEquals(ip.day_time, op.day_time);         \r
143                 assertEquals(ip.user, op.user);         \r
144                 assertEquals(ip.action, op.action);\r
145                 assertEquals(ip.target, op.target);\r
146                 assertEquals(ip.memo, op.memo);\r
147                 //TODO : have to see if third party assert utility can be used\r
148 //              assertTrue(CollectionUtils.isEqualCollection(ip.detail, op.detail));\r
149 //              for (String key : ip.detail().keySet()) {\r
150 //                      assertNotNull(op.detail().get(key));\r
151 //              }\r
152                 assertNotNull(op.reconstruct);\r
153         }\r
154         \r
155 }\r