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 ******************************************************************************/
22 package org.onap.aaf.auth.rserv.test;
24 import static org.junit.Assert.*;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Mockito.*;
27 import static org.mockito.Matchers.*;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.RandomAccessFile;
33 import java.lang.reflect.Field;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.NavigableMap;
37 import java.util.concurrent.ConcurrentSkipListMap;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
42 import org.mockito.Mock;
43 import org.mockito.invocation.InvocationOnMock;
44 import org.mockito.stubbing.Answer;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.onap.aaf.auth.rserv.CachingFileAccess;
49 import org.onap.aaf.auth.rserv.HttpCode;
50 import org.onap.aaf.auth.rserv.Match;
52 //import org.onap.aaf.auth.rserv.CachingFileAccess.Content;
53 import java.util.NavigableMap;
54 import org.onap.aaf.misc.env.EnvJAXB;
55 import org.onap.aaf.misc.env.LogTarget;
56 import org.onap.aaf.misc.env.StaticSlot;
57 import org.onap.aaf.misc.env.Store;
58 import org.onap.aaf.misc.env.Trans;
59 import org.powermock.modules.junit4.PowerMockRunner;
61 import junit.framework.Assert;
64 @RunWith(PowerMockRunner.class)
65 public class JU_CachingFileAccess {
66 CachingFileAccess cachingFileAccess;
73 public void setUp() throws IOException{
74 trans = mock(Trans.class);
75 HttpCode hCode = mock(HttpCode.class);
76 envJ = mock(EnvJAXB.class);
77 LogTarget log = mock(LogTarget.class);
78 Long lng = (long) 1234134;
79 when(envJ.get(envJ.staticSlot("aaf_cfa_cache_check_interval"),600000L)).thenReturn(lng);
80 when(envJ.get(envJ.staticSlot("aaf_cfa_max_size"), 512000)).thenReturn(512000);
81 when(envJ.get(envJ.staticSlot("aaf_cfa_web_path"))).thenReturn("TEST");
82 when(envJ.getProperty("aaf_cfa_clear_command",null)).thenReturn("null");
83 when(envJ.init()).thenReturn(log);
84 doNothing().when(log).log((String)any());
85 cachingFileAccess = new CachingFileAccess(envJ,"test");
92 public void testSetEnv() {
93 Store store = mock(Store.class);
94 Store store1 = mock(Store.class);
95 Store store2 = mock(Store.class);
96 String test[] = {"aaf_cfa_web_path","aaf_cfa_cache_check_interval","aaf_cfa_max_size"};
97 String test1[] = {"aaf_cfa_cache_check_interval"};
98 String test2[] = {"aaf_cfa_max_size"};
99 cachingFileAccess.setEnv(store, test);
100 cachingFileAccess.setEnv(store1, test1); //These don't reach all the branches for some reason
101 cachingFileAccess.setEnv(store2, test2);
105 public void testHandle() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
106 HttpServletRequest req = mock(HttpServletRequest.class);
107 Trans trans = mock(Trans.class);
108 HttpServletResponse resp = mock(HttpServletResponse.class);
109 when(req.getPathInfo()).thenReturn("path/to/file");
111 Field matchField = HttpCode.class.getDeclaredField("match");
112 matchField.setAccessible(true);
113 Match match = mock(Match.class);
114 when(match.param(anyString(), anyString())).thenReturn("null/");
115 matchField.set(cachingFileAccess, match);
116 cachingFileAccess.handle(trans, req, resp);
117 when(match.param(anyString(), anyString())).thenReturn("clear");
118 cachingFileAccess.handle(trans, req, resp);
122 public void testWebPath() {
123 EnvJAXB envJ = mock(EnvJAXB.class);
124 String web_path_test = "TEST";
125 Assert.assertEquals(web_path_test, cachingFileAccess.webPath());
129 public void testCleanupParams() {
130 NavigableMap<String,org.onap.aaf.auth.rserv.Content> content = new ConcurrentSkipListMap<String,org.onap.aaf.auth.rserv.Content>();
131 cachingFileAccess.cleanupParams(50, 500); //TODO: find right input
135 public void testLoad() throws IOException {
136 cachingFileAccess.load(null, null, "1220227200L/1220227200L", null, 1320227200L );
137 String filePath = "test/output_key";
138 File keyfile = new File(filePath);
139 RandomAccessFile randFile = new RandomAccessFile (keyfile,"rw");
141 String dPath = "test/";
142 File directoryPath = new File(dPath);
143 directoryPath.mkdir();
144 cachingFileAccess.load(null, dPath, "-", null, -1);
145 randFile.setLength(1024 * 1024 * 8);
146 cachingFileAccess.load(null, filePath, "-", null, -1);
148 directoryPath.delete();
149 String filePath1 = "test/output_key";
150 File keyfile1 = new File(filePath1);
151 keyfile1.createNewFile();
152 cachingFileAccess.load(null, filePath1, "-", "test", -1);
157 public void testLoadOrDefault() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ClassNotFoundException, InstantiationException {
158 String filePath = "test/output_key";
159 File keyfile = new File(filePath);
160 cachingFileAccess.loadOrDefault(trans, filePath, "-", null, null);
163 Trans trans = mock(Trans.class);
165 String filePath1 = "test/output_key.txt";
166 //File keyfile1 = new File(filePath1);
167 doAnswer(new Answer<Void>() {
168 public Void answer(InvocationOnMock invocation) throws FileNotFoundException {
169 throw new FileNotFoundException();
171 }).when(trans).info();
172 //cachingFileAccess.loadOrDefault(trans, "bs", "also bs", "test", null); //TODO: Needs more testing AAF-111
177 public void testInvalidate() {
178 //NavigableMap<String,org.onap.aaf.auth.rserv.Content> content = new ConcurrentSkipListMap<String,org.onap.aaf.auth.rserv.Content>();
179 //Content con = mock(Content.class);
180 //content.put("hello", con);
181 cachingFileAccess.invalidate("hello");