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.doAnswer;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.io.RandomAccessFile;
36 import java.lang.reflect.Field;
37 import java.util.NavigableMap;
38 import java.util.concurrent.ConcurrentSkipListMap;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.invocation.InvocationOnMock;
47 import org.mockito.stubbing.Answer;
48 import org.onap.aaf.auth.rserv.CachingFileAccess;
49 import org.onap.aaf.auth.rserv.HttpCode;
50 import org.onap.aaf.auth.rserv.Match;
51 import org.onap.aaf.misc.env.EnvJAXB;
52 import org.onap.aaf.misc.env.LogTarget;
53 import org.onap.aaf.misc.env.Store;
54 import org.onap.aaf.misc.env.Trans;
55 import org.powermock.modules.junit4.PowerMockRunner;
57 import junit.framework.Assert;
60 @RunWith(PowerMockRunner.class)
61 public class JU_CachingFileAccess {
62 CachingFileAccess cachingFileAccess;
69 public void setUp() throws IOException{
70 trans = mock(Trans.class);
71 HttpCode hCode = mock(HttpCode.class);
72 envJ = mock(EnvJAXB.class);
73 LogTarget log = mock(LogTarget.class);
74 Long lng = (long) 1234134;
75 when(envJ.get(envJ.staticSlot("aaf_cfa_cache_check_interval"),600000L)).thenReturn(lng);
76 when(envJ.get(envJ.staticSlot("aaf_cfa_max_size"), 512000)).thenReturn(512000);
77 when(envJ.get(envJ.staticSlot("aaf_cfa_web_path"))).thenReturn("TEST");
78 when(envJ.getProperty("aaf_cfa_clear_command",null)).thenReturn("null");
79 when(envJ.init()).thenReturn(log);
80 doNothing().when(log).log((String)any());
81 cachingFileAccess = new CachingFileAccess(envJ,"test");
88 public void testSetEnv() {
89 Store store = mock(Store.class);
90 Store store1 = mock(Store.class);
91 Store store2 = mock(Store.class);
92 String test[] = {"aaf_cfa_web_path","aaf_cfa_cache_check_interval","aaf_cfa_max_size"};
93 String test1[] = {"aaf_cfa_cache_check_interval"};
94 String test2[] = {"aaf_cfa_max_size"};
95 cachingFileAccess.setEnv(store, test);
96 cachingFileAccess.setEnv(store1, test1); //These don't reach all the branches for some reason
97 cachingFileAccess.setEnv(store2, test2);
101 public void testHandle() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
102 HttpServletRequest req = mock(HttpServletRequest.class);
103 Trans trans = mock(Trans.class);
104 HttpServletResponse resp = mock(HttpServletResponse.class);
105 when(req.getPathInfo()).thenReturn("path/to/file");
107 Field matchField = HttpCode.class.getDeclaredField("match");
108 matchField.setAccessible(true);
109 Match match = mock(Match.class);
110 when(match.param(anyString(), anyString())).thenReturn("null/");
111 matchField.set(cachingFileAccess, match);
112 cachingFileAccess.handle(trans, req, resp);
113 when(match.param(anyString(), anyString())).thenReturn("clear");
114 cachingFileAccess.handle(trans, req, resp);
118 public void testWebPath() {
119 EnvJAXB envJ = mock(EnvJAXB.class);
120 String web_path_test = "TEST";
121 Assert.assertEquals(web_path_test, cachingFileAccess.webPath());
125 public void testCleanupParams() {
126 NavigableMap<String,org.onap.aaf.auth.rserv.Content> content = new ConcurrentSkipListMap<>();
127 cachingFileAccess.cleanupParams(50, 500); //TODO: find right input
131 public void testLoad() throws IOException {
132 cachingFileAccess.load(null, null, "1220227200L/1220227200L", null, 1320227200L );
133 String filePath = "test/output_key";
134 File keyfile = new File(filePath);
135 RandomAccessFile randFile = new RandomAccessFile (keyfile,"rw");
137 String dPath = "test/";
138 File directoryPath = new File(dPath);
139 directoryPath.mkdir();
140 cachingFileAccess.load(null, dPath, "-", null, -1);
141 randFile.setLength(1024 * 1024 * 8);
142 cachingFileAccess.load(null, filePath, "-", null, -1);
144 directoryPath.delete();
145 String filePath1 = "test/output_key";
146 File keyfile1 = new File(filePath1);
147 keyfile1.createNewFile();
148 cachingFileAccess.load(null, filePath1, "-", "test", -1);
153 public void testLoadOrDefault() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ClassNotFoundException, InstantiationException {
154 String filePath = "test/output_key";
155 File keyfile = new File(filePath);
156 cachingFileAccess.loadOrDefault(trans, filePath, "-", null, null);
159 Trans trans = mock(Trans.class);
161 String filePath1 = "test/output_key.txt";
162 //File keyfile1 = new File(filePath1);
163 doAnswer(new Answer<Void>() {
164 public Void answer(InvocationOnMock invocation) throws FileNotFoundException {
165 throw new FileNotFoundException();
167 }).when(trans).info();
168 //cachingFileAccess.loadOrDefault(trans, "bs", "also bs", "test", null); //TODO: Needs more testing AAF-111
173 public void testInvalidate() {
174 //NavigableMap<String,org.onap.aaf.auth.rserv.Content> content = new ConcurrentSkipListMap<>();
175 //Content con = mock(Content.class);
176 //content.put("hello", con);
177 cachingFileAccess.invalidate("hello");