Remove spaces from dockerbuild script
[aaf/authz.git] / auth / auth-cass / src / test / java / com / att / dao / aaf / test / JU_HistoryDAO.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package com.att.dao.aaf.test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.nio.ByteBuffer;
29 import java.security.SecureRandom;
30 import java.util.List;
31
32 import org.junit.Test;
33 import org.onap.aaf.auth.dao.cass.HistoryDAO;
34 import org.onap.aaf.auth.layer.Result;
35
36 public class JU_HistoryDAO  extends AbsJUCass {
37         
38         @Test
39         public void testCreate() throws Exception {
40                 HistoryDAO historyDAO = new HistoryDAO(trans, cluster, AUTHZ);
41                 HistoryDAO.Data data = createHistoryData();
42                 
43                 try {
44                         historyDAO.create(trans,data);                  
45                         Thread.sleep(200);// History Create is Async
46                         Result<List<HistoryDAO.Data>> records = historyDAO.readByUser(trans,data.user,data.yr_mon);
47                         assertTrue(records.isOKhasData());
48                         for(HistoryDAO.Data d : records.value) {
49                                 assertHistory(data, d);
50                         }
51                 } finally {
52                         historyDAO.close(trans);
53                 }
54         }
55         
56         @Test
57         public void tesReadByUser() throws Exception {
58                 HistoryDAO historyDAO = new HistoryDAO(trans,cluster, AUTHZ);
59                 HistoryDAO.Data data = createHistoryData();
60                 
61                 try {
62                         historyDAO.create(trans,data);
63                         Thread.sleep(200);// History Create is Async
64                         Result<List<HistoryDAO.Data>> records = historyDAO.readByUser(trans, data.user,data.yr_mon);
65                         assertTrue(records.isOKhasData());
66                         for(HistoryDAO.Data d : records.value) {
67                                 assertHistory(data, d);
68                         }
69                 } finally {
70                         historyDAO.close(trans);
71                 }
72         }
73         
74 /*
75         @Test
76         public void readByUserAndMonth() throws Exception {
77                 HistoryDAO historyDAO = new HistoryDAO(trans,cluster, AUTHZ);
78                 HistoryDAO.Data data = createHistoryData();
79                 
80                 try {
81                         historyDAO.create(trans,data);                  
82                         Thread.sleep(200);// History Create is Async
83                         Result<List<HistoryDAO.Data>> records = historyDAO.readByUserAndMonth(trans,
84                                         data.user, Integer.valueOf(String.valueOf(data.yr_mon).substring(0, 4)),
85                                         Integer.valueOf(String.valueOf(data.yr_mon).substring(4, 6)));
86                         assertTrue(records.isOKhasData());
87                         for(HistoryDAO.Data d : records.value) {
88                                 assertHistory(data, d);
89                         }
90                 } finally {
91                         historyDAO.close(trans);
92                 }
93         }
94 */      
95         //TODO readadd this
96 //      @Test
97 //      public void readByUserAndDay() throws Exception {
98 //              HistoryDAO historyDAO = new HistoryDAO(trans, cluster, AUTHZ);
99 //              HistoryDAO.Data data = createHistoryData();
100 //              
101 //              try {
102 //                      historyDAO.create(trans, data);         
103 //                      Thread.sleep(200);// History Create is Async
104 //                      
105 //                      String dayTime = String.valueOf(data.day_time);
106 //                      String day = null;
107 //                      if (dayTime.length() < 8)
108 //                              day = dayTime.substring(0, 1);
109 //                      else 
110 //                              day = dayTime.substring(0, 2);
111 //                      
112 //                      List<HistoryDAO.Data> records = historyDAO.readByUserBetweenDates(trans,
113 //                                                      data.user, Integer.valueOf(String.valueOf(data.yr_mon).substring(0, 4)),
114 //                                                      Integer.valueOf(String.valueOf(data.yr_mon).substring(4, 6)),
115 //                                                      Integer.valueOf(day), 0);
116 //                      assertEquals(1,records.size());
117 //                      for(HistoryDAO.Data d : records) {
118 //                              assertHistory(data, d);
119 //                      }
120 //              } finally {
121 //                      historyDAO.close(trans);
122 //              }
123 //      }
124         private HistoryDAO.Data createHistoryData() {
125                 HistoryDAO.Data data = HistoryDAO.newInitedData();
126                 SecureRandom random = new SecureRandom();
127                 data.user = "test" + random.nextInt();
128                 data.action = "add";
129                 data.target = "history";
130                 data.memo = "adding a row into history table";
131 //              data.detail().put("id", "test");
132 //              data.detail().put("name", "test");
133                 //String temp = "Test Blob Message";
134                 data.reconstruct = ByteBuffer.wrap("Temp Blob Message".getBytes());             
135                 return data;
136         }
137         
138         private void assertHistory(HistoryDAO.Data ip, HistoryDAO.Data op) {
139                 assertEquals(ip.yr_mon, op.yr_mon);             
140 //              assertEquals(ip.day_time, op.day_time);         
141                 assertEquals(ip.user, op.user);         
142                 assertEquals(ip.action, op.action);
143                 assertEquals(ip.target, op.target);
144                 assertEquals(ip.memo, op.memo);
145                 //TODO : have to see if third party assert utility can be used
146 //              assertTrue(CollectionUtils.isEqualCollection(ip.detail, op.detail));
147 //              for (String key : ip.detail().keySet()) {
148 //                      assertNotNull(op.detail().get(key));
149 //              }
150                 assertNotNull(op.reconstruct);
151         }
152         
153 }