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