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.rserv.test;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.anyString;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
32 import java.io.IOException;
33 import java.io.RandomAccessFile;
34 import java.lang.reflect.Field;
35 import java.util.NavigableMap;
36 import java.util.concurrent.ConcurrentSkipListMap;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.onap.aaf.auth.rserv.CachingFileAccess;
45 import org.onap.aaf.auth.rserv.HttpCode;
46 import org.onap.aaf.auth.rserv.Match;
47 import org.onap.aaf.misc.env.EnvJAXB;
48 import org.onap.aaf.misc.env.LogTarget;
49 import org.onap.aaf.misc.env.Store;
50 import org.onap.aaf.misc.env.Trans;
51 import org.powermock.modules.junit4.PowerMockRunner;
53 import junit.framework.Assert;
56 @RunWith(PowerMockRunner.class)
57 public class JU_CachingFileAccess {
58 CachingFileAccess cachingFileAccess;
65 public void setUp() throws IOException{
66 trans = mock(Trans.class);
67 HttpCode hCode = mock(HttpCode.class);
68 envJ = mock(EnvJAXB.class);
69 LogTarget log = mock(LogTarget.class);
70 Long lng = (long) 1234134;
71 when(envJ.get(envJ.staticSlot("aaf_cfa_cache_check_interval"),600000L)).thenReturn(lng);
72 when(envJ.get(envJ.staticSlot("aaf_cfa_max_size"), 512000)).thenReturn(512000);
73 when(envJ.get(envJ.staticSlot("aaf_cfa_web_path"))).thenReturn("TEST");
74 when(envJ.getProperty("aaf_cfa_clear_command",null)).thenReturn("null");
75 when(envJ.init()).thenReturn(log);
76 doNothing().when(log).log((String)any());
77 cachingFileAccess = new CachingFileAccess(envJ,"test");
84 public void testSetEnv() {
85 Store store = mock(Store.class);
86 Store store1 = mock(Store.class);
87 Store store2 = mock(Store.class);
88 String test[] = {"aaf_cfa_web_path","aaf_cfa_cache_check_interval","aaf_cfa_max_size"};
89 String test1[] = {"aaf_cfa_cache_check_interval"};
90 String test2[] = {"aaf_cfa_max_size"};
91 cachingFileAccess.setEnv(store, test);
92 cachingFileAccess.setEnv(store1, test1); //These don't reach all the branches for some reason
93 cachingFileAccess.setEnv(store2, test2);
97 public void testHandle() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
98 HttpServletRequest req = mock(HttpServletRequest.class);
99 Trans trans = mock(Trans.class);
100 HttpServletResponse resp = mock(HttpServletResponse.class);
101 when(req.getPathInfo()).thenReturn("path/to/file");
103 Field matchField = HttpCode.class.getDeclaredField("match");
104 matchField.setAccessible(true);
105 Match match = mock(Match.class);
106 when(match.param(anyString(), anyString())).thenReturn("null/");
107 matchField.set(cachingFileAccess, match);
108 cachingFileAccess.handle(trans, req, resp);
109 when(match.param(anyString(), anyString())).thenReturn("clear");
110 cachingFileAccess.handle(trans, req, resp);
114 public void testWebPath() {
115 EnvJAXB envJ = mock(EnvJAXB.class);
116 String web_path_test = "TEST";
117 Assert.assertEquals(web_path_test, cachingFileAccess.webPath());
121 public void testCleanupParams() {
122 NavigableMap<String,org.onap.aaf.auth.rserv.Content> content = new ConcurrentSkipListMap<>();
123 cachingFileAccess.cleanupParams(50, 500); //TODO: find right input
127 public void testLoad() throws IOException {
128 cachingFileAccess.load(null, null, "1220227200L/1220227200L", null, 1320227200L );
129 String filePath = "test/output_key";
130 File keyfile = new File(filePath);
131 RandomAccessFile randFile = new RandomAccessFile (keyfile,"rw");
133 String dPath = "test/";
134 File directoryPath = new File(dPath);
135 directoryPath.mkdir();
136 cachingFileAccess.load(null, dPath, "-", null, -1);
137 randFile.setLength(1024 * 1024 * 8);
138 cachingFileAccess.load(null, filePath, "-", null, -1);
140 directoryPath.delete();
141 String filePath1 = "test/output_key";
142 File keyfile1 = new File(filePath1);
143 keyfile1.createNewFile();
144 cachingFileAccess.load(null, filePath1, "-", "test", -1);
149 public void testInvalidate() {
150 //NavigableMap<String,org.onap.aaf.auth.rserv.Content> content = new ConcurrentSkipListMap<>();
151 //Content con = mock(Content.class);
152 //content.put("hello", con);
153 cachingFileAccess.invalidate("hello");