1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 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====================================================
21 ******************************************************************************/
23 package org.onap.aaf.auth.dao;
25 import static org.junit.Assert.*;
26 import static org.hamcrest.CoreMatchers.*;
27 import static org.mockito.Mockito.*;
30 // import org.junit.runner.RunWith;
31 // import org.powermock.modules.junit4.PowerMockRunner;
33 import java.util.Date;
34 import java.util.List;
35 import java.util.ArrayList;
37 import java.util.Timer;
39 import org.onap.aaf.auth.cache.Cache;
40 import org.onap.aaf.auth.cache.Cache.Dated;
41 import org.onap.aaf.auth.dao.CIDAO;
42 import org.onap.aaf.auth.dao.Cached;
43 import org.onap.aaf.auth.dao.Cached.Getter;
44 import org.onap.aaf.auth.dao.JU_Cached.DataStub;
45 import org.onap.aaf.auth.dao.cass.CacheableData;
46 import org.onap.aaf.auth.env.AuthzEnv;
47 import org.onap.aaf.auth.env.AuthzTrans;
48 import org.onap.aaf.auth.layer.Result;
49 import org.onap.aaf.misc.env.Trans;
51 // @RunWith(PowerMockRunner.class)
52 public class JU_Cached {
55 CIDAO<Trans> ciDaoMock;
58 AuthzEnv authzEnvMock;
61 CIDAO<AuthzTrans> cidaoATMock;
63 String name = "nameString";
67 MockitoAnnotations.initMocks(this);
71 public void testCachedIdx(){
72 Cached<Trans, DataStub> cached = new Cached<Trans, DataStub>(ciDaoMock, name, 1, 30000L);
73 assertThat(cached.cacheIdx("1234567890"), is(0));
77 public void testInvalidate(){
78 Cached<Trans, DataStub> cached = new Cached<Trans, DataStub>(ciDaoMock, name, 5, 30000L);
79 cached.add("test", new ArrayList<>());
80 cached.invalidate("test");
81 cached.invalidate("test1");
84 @SuppressWarnings("static-access")
86 public void testStopTimer(){
87 Cached<Trans, DataStub> cached = new Cached<Trans, DataStub>(ciDaoMock, name, 1, 30000L);
92 @SuppressWarnings("static-access")
94 public void testStartRefresh(){
95 Cached<Trans, DataStub> cached = new Cached<Trans, DataStub>(ciDaoMock, name, 1, 30000L);
96 cached.startRefresh(authzEnvMock, cidaoATMock);
102 // Getter<DAO> getterMock;
105 // public void testGet(){
106 // cached.get(transMock, name, getterMock);
107 // fail("not implemented");
110 // @SuppressWarnings("unchecked")
111 // public Result<List<DATA>> get(TRANS trans, String key, Getter<DATA> getter) {
112 // List<DATA> ld = null;
113 // Result<List<DATA>> rld = null;
115 // int cacheIdx = cacheIdx(key);
116 // Map<String, Dated> map = ((Map<String,Dated>)cache[cacheIdx]);
118 // // Check for saved element in cache
119 // Dated cached = map.get(key);
120 // // Note: These Segment Timestamps are kept up to date with DB
121 // Date dbStamp = info.get(trans, name,cacheIdx);
123 // // Check for cache Entry and whether it is still good (a good Cache Entry is same or after DBEntry, so we use "before" syntax)
124 // if(cached!=null && dbStamp.before(cached.timestamp)) {
125 // ld = (List<DATA>)cached.data;
126 // rld = Result.ok(ld);
128 // rld = getter.get();
129 // if(rld.isOK()) { // only store valid lists
130 // map.put(key, new Dated(rld.value)); // successful item found gets put in cache
131 //// } else if(rld.status == Result.ERR_Backend){
132 //// map.remove(key);
138 class DataStub extends CacheableData {
139 @Override public int[] invalidate(Cached<?, ?> cache) { return null; }