7b5da6c1f0c930d971243b0c47c577c45fdeef58
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_PropAccess.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 import org.junit.Test;
26 import org.onap.aaf.cadi.PropAccess;
27 import org.onap.aaf.cadi.Access.Level;
28 import org.onap.aaf.cadi.PropAccess.LogIt;
29
30 import static org.mockito.Mockito.*;
31 import static org.hamcrest.CoreMatchers.*;
32
33 import java.lang.reflect.Field;
34
35 import java.io.ByteArrayInputStream;
36 import java.io.PrintStream;
37 import java.util.Properties;
38
39 @SuppressWarnings("unused")
40 public class JU_PropAccess {
41         // Note: We can't actually get coverage of the protected constructor - 
42         // that will be done later, when testing the child class "ServletContextAccess"
43         
44         
45         @Test
46         public void ConstructorTest() throws Exception {
47                 PropAccess prop = new PropAccess();
48                 assertThat(prop.getProperties(), is(not(nullValue())));
49         }
50
51         @Test
52         public void noPrintStreamConstructionTest() throws Exception {
53                 // Test for coverage
54                 PropAccess prop = new PropAccess((PrintStream)null, new String[]{"Invalid argument"});
55         }
56
57         @Test
58         public void noLogItConstructionTest() throws Exception {
59                 // Test for coverage
60                 PropAccess prop = new PropAccess((LogIt)null, new String[]{"Invalid argument"});
61         }
62
63         @Test
64         public void propertiesConstructionTest() throws Exception {
65                 // Coverage tests
66                 PropAccess prop = new PropAccess(System.getProperties());
67                 prop = new PropAccess((PrintStream)null, System.getProperties());
68         }
69
70         @Test
71         public void stringConstructionTest() throws Exception {
72                 Properties testSystemProps = new Properties(System.getProperties());
73                 testSystemProps.setProperty("cadi_name", "user");
74                 System.setProperties(testSystemProps);
75                 PropAccess prop = new PropAccess("cadi_keyfile=src/test/resources/keyfile", "cadi_loglevel=DEBUG", "cadi_prop_files=test/cadi.properties:not_a_file");
76         }
77
78         @Test
79         public void loadTest() throws Exception {
80                 // Coverage tests
81                 Properties props = mock(Properties.class);
82                 when(props.getProperty("cadi_prop_files")).thenReturn("test/cadi.properties").thenReturn(null);
83                 PropAccess pa = new PropAccess();
84                 Field props_field = PropAccess.class.getDeclaredField("props");
85                 props_field.setAccessible(true);
86                 props_field.set(pa, props);
87                 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
88                 pa.load(bais);
89         }
90         
91         @Test
92         public void specialConversionsTest() throws Exception {
93                 // Coverage tests
94                 Properties testSystemProps = new Properties(System.getProperties());
95                 testSystemProps.setProperty("java.specification.version", "1.7");
96                 System.setProperties(testSystemProps);
97                 PropAccess pa = new PropAccess("AFT_LATITUDE=1", "AFT_LONGITUDE=1", "cadi_protocols=TLSv1.2");
98         }
99
100         @Test
101         public void logTest() throws Exception {
102                 // Coverage tests
103                 PropAccess pa = new PropAccess();
104
105                 pa.log(Level.DEBUG);
106                 pa.printf(Level.DEBUG, "not a real format string");
107
108                 pa.setLogLevel(Level.DEBUG);
109                 pa.log(Level.DEBUG);
110                 pa.log(Level.DEBUG, 1, " ", null, "");
111                 pa.log(Level.DEBUG, "This is a string", "This is another");
112                 pa.set(new LogIt() {
113                         @Override public void push(Level level, Object ... elements) {}
114                 });
115                 try {
116                         pa.log(new Exception("This exception was thrown intentionally, please ignore it"));
117                 } catch(Exception e) {
118                         fail("Should have thrown an exception");
119                 }
120         }
121
122         @Test
123         public void classLoaderTest() {
124                 PropAccess pa = new PropAccess();
125                 assertThat(pa.classLoader(), instanceOf(ClassLoader.class));
126         }
127
128         @Test
129         public void encryptionTest() throws Exception {
130                 PropAccess pa = new PropAccess();
131                 String plainText = "This is a secret message";
132                 String secret_message = pa.encrypt(plainText);
133                 String modified = secret_message.substring(4);
134                 // Plenty of assertions to hit all branches
135                 assertThat(pa.decrypt(secret_message, false), is(plainText));
136                 assertThat(pa.decrypt(null, false), is(nullValue()));
137                 assertThat(pa.decrypt(modified, true), is(plainText));
138                 assertThat(pa.decrypt(modified, false), is(modified));
139         }
140
141         @Test
142         public void setPropertyTest() {
143                 PropAccess pa = new PropAccess();
144                 pa.setProperty("test", null);
145                 String prop = "New Property";
146                 String val ="And it's faithful value";
147                 pa.setProperty(prop, val);
148
149                 assertThat(pa.getProperty(prop), is(val));
150         }
151 }