2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package com.att.dao.aaf.test;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import java.nio.ByteBuffer;
29 import java.security.SecureRandom;
30 import java.util.List;
32 import org.junit.Test;
33 import org.onap.aaf.auth.dao.cass.HistoryDAO;
34 import org.onap.aaf.auth.layer.Result;
36 public class JU_HistoryDAO extends AbsJUCass {
39 public void testCreate() throws Exception {
40 HistoryDAO historyDAO = new HistoryDAO(trans, cluster, AUTHZ);
41 HistoryDAO.Data data = createHistoryData();
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);
52 historyDAO.close(trans);
57 public void tesReadByUser() throws Exception {
58 HistoryDAO historyDAO = new HistoryDAO(trans,cluster, AUTHZ);
59 HistoryDAO.Data data = createHistoryData();
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);
70 historyDAO.close(trans);
76 public void readByUserAndMonth() throws Exception {
77 HistoryDAO historyDAO = new HistoryDAO(trans,cluster, AUTHZ);
78 HistoryDAO.Data data = createHistoryData();
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);
91 historyDAO.close(trans);
97 // public void readByUserAndDay() throws Exception {
98 // HistoryDAO historyDAO = new HistoryDAO(trans, cluster, AUTHZ);
99 // HistoryDAO.Data data = createHistoryData();
102 // historyDAO.create(trans, data);
103 // Thread.sleep(200);// History Create is Async
105 // String dayTime = String.valueOf(data.day_time);
106 // String day = null;
107 // if (dayTime.length() < 8)
108 // day = dayTime.substring(0, 1);
110 // day = dayTime.substring(0, 2);
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);
121 // historyDAO.close(trans);
124 private HistoryDAO.Data createHistoryData() {
125 HistoryDAO.Data data = HistoryDAO.newInitedData();
126 SecureRandom random = new SecureRandom();
127 data.user = "test" + random.nextInt();
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());
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));
150 assertNotNull(op.reconstruct);