Improve Batches
[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 propertiesConstructionTest() throws Exception {
60         // Coverage tests
61         PropAccess prop = new PropAccess(System.getProperties());
62         prop = new PropAccess((PrintStream)null, System.getProperties());
63     }
64
65     @Test
66     public void stringConstructionTest() throws Exception {
67         Properties testSystemProps = new Properties(System.getProperties());
68         testSystemProps.setProperty("cadi_name", "user");
69         System.setProperties(testSystemProps);
70         PropAccess prop = new PropAccess("cadi_keyfile=src/test/resources/keyfile", "cadi_loglevel=DEBUG", "cadi_prop_files=test/cadi.properties:not_a_file");
71     }
72
73     @Test
74     public void loadTest() throws Exception {
75         // Coverage tests
76         Properties props = mock(Properties.class);
77         when(props.getProperty("cadi_prop_files")).thenReturn("test/cadi.properties").thenReturn(null);
78         PropAccess pa = new PropAccess();
79         Field props_field = PropAccess.class.getDeclaredField("props");
80         props_field.setAccessible(true);
81         props_field.set(pa, props);
82         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
83         pa.load(bais);
84     }
85     
86     @Test
87     public void specialConversionsTest() throws Exception {
88         // Coverage tests
89         Properties testSystemProps = new Properties(System.getProperties());
90         testSystemProps.setProperty("java.specification.version", "1.7");
91         System.setProperties(testSystemProps);
92         PropAccess pa = new PropAccess("AFT_LATITUDE=1", "AFT_LONGITUDE=1", "cadi_protocols=TLSv1.2");
93     }
94
95     @Test
96     public void logTest() throws Exception {
97         // Coverage tests
98         PropAccess pa = new PropAccess();
99
100         pa.log(Level.DEBUG);
101         pa.printf(Level.DEBUG, "not a real format string");
102
103         pa.setLogLevel(Level.DEBUG);
104         pa.log(Level.DEBUG);
105         pa.log(Level.DEBUG, 1, " ", null, "");
106         pa.log(Level.DEBUG, "This is a string", "This is another");
107         pa.set(new LogIt() {
108             @Override public void push(Level level, Object ... elements) {}
109         });
110         try {
111             pa.log(new Exception("This exception was thrown intentionally, please ignore it"));
112         } catch (Exception e) {
113             fail("Should have thrown an exception");
114         }
115     }
116
117     @Test
118     public void classLoaderTest() {
119         PropAccess pa = new PropAccess();
120         assertThat(pa.classLoader(), instanceOf(ClassLoader.class));
121     }
122
123     @Test
124     public void encryptionTest() throws Exception {
125         PropAccess pa = new PropAccess();
126         String plainText = "This is a secret message";
127         String secret_message = pa.encrypt(plainText);
128         String modified = secret_message.substring(4);
129         // Plenty of assertions to hit all branches
130         assertThat(pa.decrypt(secret_message, false), is(plainText));
131         assertThat(pa.decrypt(null, false), is(nullValue()));
132         assertThat(pa.decrypt(modified, true), is(plainText));
133         assertThat(pa.decrypt(modified, false), is(modified));
134     }
135
136     @Test
137     public void setPropertyTest() {
138         PropAccess pa = new PropAccess();
139         pa.setProperty("test", null);
140         String prop = "New Property";
141         String val ="And it's faithful value";
142         pa.setProperty(prop, val);
143
144         assertThat(pa.getProperty(prop), is(val));
145     }
146 }