Post Init Service Starter
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / JU_Cached.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
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
23 package org.onap.aaf.auth.dao;
24
25 import static org.junit.Assert.*;
26 import static org.hamcrest.CoreMatchers.*;
27 import static org.mockito.Mockito.*;
28 import org.junit.*;
29 import org.mockito.*;
30 // import org.junit.runner.RunWith;
31 // import org.powermock.modules.junit4.PowerMockRunner;
32
33 import java.util.Date;
34 import java.util.List;
35 import java.util.ArrayList;
36 import java.util.Map;
37 import java.util.Timer;
38
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;
50
51 // @RunWith(PowerMockRunner.class)
52 public class JU_Cached {
53
54     @Mock
55     CIDAO<Trans> ciDaoMock;
56
57     @Mock
58     AuthzEnv authzEnvMock;
59
60     @Mock
61     CIDAO<AuthzTrans> cidaoATMock;
62     
63     String name = "nameString";
64
65     @Before
66     public void setUp(){
67         MockitoAnnotations.initMocks(this);
68     }
69     
70     @Test
71     public void testCachedIdx(){
72         Cached<Trans, DataStub> cached = new Cached<Trans, DataStub>(ciDaoMock, name, 1, 30000L);
73         assertThat(cached.cacheIdx("1234567890"), is(0));
74     }
75     
76     @Test
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");
82     }
83
84 /*
85  * DO NOT ATTEMPT TO TEST Timer Threads in JUNIT!!!!!
86     @SuppressWarnings("static-access")
87     @Test
88     public void testStopTimer(){
89         Cached<Trans, DataStub> cached = new Cached<Trans, DataStub>(ciDaoMock, name, 1, 30000L);
90         cached.stopTimer();
91         assertTrue(true);
92     }
93
94     @SuppressWarnings("static-access")
95     @Test
96     public void testStartRefresh(){
97         Cached<Trans, DataStub> cached = new Cached<Trans, DataStub>(ciDaoMock, name, 1, 30000L);
98         cached.startRefresh(authzEnvMock, cidaoATMock);
99         assertTrue(true);
100     }
101 */
102 //    @Mock
103 //    Trans transMock;
104 //    @Mock
105 //    Getter<DAO> getterMock;
106 //    
107 //    @Test
108 //    public void testGet(){
109 //        cached.get(transMock, name, getterMock);
110 //        fail("not implemented");
111 //    }
112 //    
113 //    @SuppressWarnings("unchecked")
114 //    public Result<List<DATA>> get(TRANS trans, String key, Getter<DATA> getter) {
115 //        List<DATA> ld = null;
116 //        Result<List<DATA>> rld = null;
117 //        
118 //        int cacheIdx = cacheIdx(key);
119 //        Map<String, Dated> map = ((Map<String,Dated>)cache[cacheIdx]);
120 //        
121 //        // Check for saved element in cache
122 //        Dated cached = map.get(key);
123 //        // Note: These Segment Timestamps are kept up to date with DB
124 //        Date dbStamp = info.get(trans, name,cacheIdx);
125 //        
126 //        // Check for cache Entry and whether it is still good (a good Cache Entry is same or after DBEntry, so we use "before" syntax)
127 //        if (cached!=null && dbStamp.before(cached.timestamp)) {
128 //            ld = (List<DATA>)cached.data;
129 //            rld = Result.ok(ld);
130 //        } else {
131 //            rld = getter.get();
132 //            if (rld.isOK()) { // only store valid lists
133 //                map.put(key, new Dated(rld.value));  // successful item found gets put in cache
134 ////            } else if (rld.status == Result.ERR_Backend){
135 ////                map.remove(key);
136 //            }
137 //        }
138 //        return rld;
139 //    }
140
141     class DataStub extends CacheableData {
142         @Override public int[] invalidate(Cached<?, ?> cache) { return null; }
143     }
144 }