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