1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 ******************************************************************************/
22 package org.onap.aaf.cadi.test;
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;
30 import static org.mockito.Mockito.*;
31 import static org.hamcrest.CoreMatchers.*;
33 import java.lang.reflect.Field;
35 import java.io.ByteArrayInputStream;
36 import java.io.PrintStream;
37 import java.util.Properties;
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"
46 public void ConstructorTest() throws Exception {
47 PropAccess prop = new PropAccess();
48 assertThat(prop.getProperties(), is(not(nullValue())));
52 public void noPrintStreamConstructionTest() throws Exception {
54 PropAccess prop = new PropAccess((PrintStream)null, new String[]{"Invalid argument"});
58 public void noLogItConstructionTest() throws Exception {
60 PropAccess prop = new PropAccess((LogIt)null, new String[]{"Invalid argument"});
64 public void propertiesConstructionTest() throws Exception {
66 PropAccess prop = new PropAccess(System.getProperties());
67 prop = new PropAccess((PrintStream)null, System.getProperties());
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");
79 public void loadTest() throws Exception {
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]);
92 public void specialConversionsTest() throws Exception {
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");
101 public void logTest() throws Exception {
103 PropAccess pa = new PropAccess();
106 pa.printf(Level.DEBUG, "not a real format string");
108 pa.setLogLevel(Level.DEBUG);
110 pa.log(Level.DEBUG, 1, " ", null, "");
111 pa.log(Level.DEBUG, "This is a string", "This is another");
113 @Override public void push(Level level, Object ... elements) {}
116 pa.log(new Exception("This exception was thrown intentionally, please ignore it"));
117 } catch(Exception e) {
118 fail("Should have thrown an exception");
123 public void classLoaderTest() {
124 PropAccess pa = new PropAccess();
125 assertThat(pa.classLoader(), instanceOf(ClassLoader.class));
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));
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);
149 assertThat(pa.getProperty(prop), is(val));