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