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 ******************************************************************************/
23 package org.onap.aaf.cadi.test;
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;
31 import static org.mockito.Mockito.*;
32 import static org.hamcrest.CoreMatchers.*;
34 import java.lang.reflect.Field;
36 import java.io.ByteArrayInputStream;
37 import java.io.PrintStream;
38 import java.util.Properties;
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"
47 public void ConstructorTest() throws Exception {
48 PropAccess prop = new PropAccess();
49 assertThat(prop.getProperties(), is(not(nullValue())));
53 public void noPrintStreamConstructionTest() throws Exception {
55 PropAccess prop = new PropAccess((PrintStream)null, new String[]{"Invalid argument"});
59 public void propertiesConstructionTest() throws Exception {
61 PropAccess prop = new PropAccess(System.getProperties());
62 prop = new PropAccess((PrintStream)null, System.getProperties());
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");
74 public void loadTest() throws Exception {
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]);
87 public void specialConversionsTest() throws Exception {
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");
96 public void logTest() throws Exception {
98 PropAccess pa = new PropAccess();
101 pa.printf(Level.DEBUG, "not a real format string");
103 pa.setLogLevel(Level.DEBUG);
105 pa.log(Level.DEBUG, 1, " ", null, "");
106 pa.log(Level.DEBUG, "This is a string", "This is another");
108 @Override public void push(Level level, Object ... elements) {}
111 pa.log(new Exception("This exception was thrown intentionally, please ignore it"));
112 } catch (Exception e) {
113 fail("Should have thrown an exception");
118 public void classLoaderTest() {
119 PropAccess pa = new PropAccess();
120 assertThat(pa.classLoader(), instanceOf(ClassLoader.class));
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));
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);
144 assertThat(pa.getProperty(prop), is(val));