8531e1d1e0a42aaf28c68b988210326d08bcb127
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_ServletContextAccess.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.cadi.test;
23
24 import static org.junit.Assert.*;
25
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.aaf.cadi.PropAccess;
29 import org.onap.aaf.cadi.ServletContextAccess;
30 import org.onap.aaf.cadi.Access.Level;
31 import org.onap.aaf.cadi.PropAccess.LogIt;
32
33 import static org.mockito.Mockito.*;
34 import static org.hamcrest.CoreMatchers.*;
35
36 import java.lang.reflect.Field;
37
38 import java.io.ByteArrayInputStream;
39 import java.io.PrintStream;
40 import java.util.Enumeration;
41 import java.util.Properties;
42 import java.util.StringTokenizer;
43
44 import javax.servlet.FilterConfig;
45 import javax.servlet.ServletContext;
46
47 @SuppressWarnings("unused")
48 public class JU_ServletContextAccess {
49
50         private FilterConfig filter_mock;
51         Enumeration<String> enumeration;
52         
53         private class CustomEnumeration implements Enumeration<String> {
54                 private int idx = 0;
55                 private final String[] elements = {"This", "is", "a", "test"};
56                 @Override
57                 public String nextElement() {
58                         return idx >= elements.length ? null : elements[idx++];
59                 }
60                 @Override
61                 public boolean hasMoreElements() {
62                         return idx < elements.length;
63                 }
64         }
65
66         @Before
67         public void setup() {
68                 enumeration = new CustomEnumeration();
69                 filter_mock = mock(FilterConfig.class);
70                 when(filter_mock.getInitParameterNames()).thenReturn(enumeration);
71         }
72         
73         @Test
74         public void ConstructorTest() throws Exception {
75                 ServletContextAccess sca = new ServletContextAccess(filter_mock);
76         }
77
78         @Test
79         public void logTest() throws Exception {
80                 ServletContext sc_mock = mock(ServletContext.class);
81                 when(filter_mock.getServletContext()).thenReturn(sc_mock);
82                 ServletContextAccess sca = new ServletContextAccess(filter_mock);
83
84                 sca.log(Level.DEBUG);
85
86                 sca.setLogLevel(Level.DEBUG);
87                 sca.log(Level.DEBUG);
88
89                 try {
90                         sca.log(new Exception("This exception was thrown intentionally, please ignore it"));
91                 } catch(Exception e) {
92                         fail("Should have thrown an exception");
93                 }
94         }
95
96         @Test
97         public void contextTest() {
98                 ServletContext sc_mock = mock(ServletContext.class);
99                 when(filter_mock.getServletContext()).thenReturn(sc_mock);
100                 ServletContextAccess sca = new ServletContextAccess(filter_mock);
101                 assertThat(sca.context(), instanceOf(ServletContext.class));
102         }
103
104 }