X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Fcore%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Ftest%2FJU_PropAccess.java;h=45f221c470a0c33527bc7a274e7d488525f701a5;hb=7e966914050e66219689001ff4ab601a49eef0ac;hp=7b5da6c1f0c930d971243b0c47c577c45fdeef58;hpb=ceda6e8bc270202bcb24340b86617110289c902e;p=aaf%2Fauthz.git diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_PropAccess.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_PropAccess.java index 7b5da6c1..45f221c4 100644 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_PropAccess.java +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_PropAccess.java @@ -19,6 +19,7 @@ * * * * ******************************************************************************/ + package org.onap.aaf.cadi.test; import static org.junit.Assert.*; @@ -38,114 +39,114 @@ import java.util.Properties; @SuppressWarnings("unused") public class JU_PropAccess { - // Note: We can't actually get coverage of the protected constructor - - // that will be done later, when testing the child class "ServletContextAccess" - - - @Test - public void ConstructorTest() throws Exception { - PropAccess prop = new PropAccess(); - assertThat(prop.getProperties(), is(not(nullValue()))); - } - - @Test - public void noPrintStreamConstructionTest() throws Exception { - // Test for coverage - PropAccess prop = new PropAccess((PrintStream)null, new String[]{"Invalid argument"}); - } - - @Test - public void noLogItConstructionTest() throws Exception { - // Test for coverage - PropAccess prop = new PropAccess((LogIt)null, new String[]{"Invalid argument"}); - } - - @Test - public void propertiesConstructionTest() throws Exception { - // Coverage tests - PropAccess prop = new PropAccess(System.getProperties()); - prop = new PropAccess((PrintStream)null, System.getProperties()); - } - - @Test - public void stringConstructionTest() throws Exception { - Properties testSystemProps = new Properties(System.getProperties()); - testSystemProps.setProperty("cadi_name", "user"); - System.setProperties(testSystemProps); - PropAccess prop = new PropAccess("cadi_keyfile=src/test/resources/keyfile", "cadi_loglevel=DEBUG", "cadi_prop_files=test/cadi.properties:not_a_file"); - } - - @Test - public void loadTest() throws Exception { - // Coverage tests - Properties props = mock(Properties.class); - when(props.getProperty("cadi_prop_files")).thenReturn("test/cadi.properties").thenReturn(null); - PropAccess pa = new PropAccess(); - Field props_field = PropAccess.class.getDeclaredField("props"); - props_field.setAccessible(true); - props_field.set(pa, props); - ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); - pa.load(bais); - } - - @Test - public void specialConversionsTest() throws Exception { - // Coverage tests - Properties testSystemProps = new Properties(System.getProperties()); - testSystemProps.setProperty("java.specification.version", "1.7"); - System.setProperties(testSystemProps); - PropAccess pa = new PropAccess("AFT_LATITUDE=1", "AFT_LONGITUDE=1", "cadi_protocols=TLSv1.2"); - } - - @Test - public void logTest() throws Exception { - // Coverage tests - PropAccess pa = new PropAccess(); - - pa.log(Level.DEBUG); - pa.printf(Level.DEBUG, "not a real format string"); - - pa.setLogLevel(Level.DEBUG); - pa.log(Level.DEBUG); - pa.log(Level.DEBUG, 1, " ", null, ""); - pa.log(Level.DEBUG, "This is a string", "This is another"); - pa.set(new LogIt() { - @Override public void push(Level level, Object ... elements) {} - }); - try { - pa.log(new Exception("This exception was thrown intentionally, please ignore it")); - } catch(Exception e) { - fail("Should have thrown an exception"); - } - } - - @Test - public void classLoaderTest() { - PropAccess pa = new PropAccess(); - assertThat(pa.classLoader(), instanceOf(ClassLoader.class)); - } - - @Test - public void encryptionTest() throws Exception { - PropAccess pa = new PropAccess(); - String plainText = "This is a secret message"; - String secret_message = pa.encrypt(plainText); - String modified = secret_message.substring(4); - // Plenty of assertions to hit all branches - assertThat(pa.decrypt(secret_message, false), is(plainText)); - assertThat(pa.decrypt(null, false), is(nullValue())); - assertThat(pa.decrypt(modified, true), is(plainText)); - assertThat(pa.decrypt(modified, false), is(modified)); - } - - @Test - public void setPropertyTest() { - PropAccess pa = new PropAccess(); - pa.setProperty("test", null); - String prop = "New Property"; - String val ="And it's faithful value"; - pa.setProperty(prop, val); - - assertThat(pa.getProperty(prop), is(val)); - } + // Note: We can't actually get coverage of the protected constructor - + // that will be done later, when testing the child class "ServletContextAccess" + + + @Test + public void ConstructorTest() throws Exception { + PropAccess prop = new PropAccess(); + assertThat(prop.getProperties(), is(not(nullValue()))); + } + + @Test + public void noPrintStreamConstructionTest() throws Exception { + // Test for coverage + PropAccess prop = new PropAccess((PrintStream)null, new String[]{"Invalid argument"}); + } + + @Test + public void noLogItConstructionTest() throws Exception { + // Test for coverage + PropAccess prop = new PropAccess((LogIt)null, new String[]{"Invalid argument"}); + } + + @Test + public void propertiesConstructionTest() throws Exception { + // Coverage tests + PropAccess prop = new PropAccess(System.getProperties()); + prop = new PropAccess((PrintStream)null, System.getProperties()); + } + + @Test + public void stringConstructionTest() throws Exception { + Properties testSystemProps = new Properties(System.getProperties()); + testSystemProps.setProperty("cadi_name", "user"); + System.setProperties(testSystemProps); + PropAccess prop = new PropAccess("cadi_keyfile=src/test/resources/keyfile", "cadi_loglevel=DEBUG", "cadi_prop_files=test/cadi.properties:not_a_file"); + } + + @Test + public void loadTest() throws Exception { + // Coverage tests + Properties props = mock(Properties.class); + when(props.getProperty("cadi_prop_files")).thenReturn("test/cadi.properties").thenReturn(null); + PropAccess pa = new PropAccess(); + Field props_field = PropAccess.class.getDeclaredField("props"); + props_field.setAccessible(true); + props_field.set(pa, props); + ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); + pa.load(bais); + } + + @Test + public void specialConversionsTest() throws Exception { + // Coverage tests + Properties testSystemProps = new Properties(System.getProperties()); + testSystemProps.setProperty("java.specification.version", "1.7"); + System.setProperties(testSystemProps); + PropAccess pa = new PropAccess("AFT_LATITUDE=1", "AFT_LONGITUDE=1", "cadi_protocols=TLSv1.2"); + } + + @Test + public void logTest() throws Exception { + // Coverage tests + PropAccess pa = new PropAccess(); + + pa.log(Level.DEBUG); + pa.printf(Level.DEBUG, "not a real format string"); + + pa.setLogLevel(Level.DEBUG); + pa.log(Level.DEBUG); + pa.log(Level.DEBUG, 1, " ", null, ""); + pa.log(Level.DEBUG, "This is a string", "This is another"); + pa.set(new LogIt() { + @Override public void push(Level level, Object ... elements) {} + }); + try { + pa.log(new Exception("This exception was thrown intentionally, please ignore it")); + } catch (Exception e) { + fail("Should have thrown an exception"); + } + } + + @Test + public void classLoaderTest() { + PropAccess pa = new PropAccess(); + assertThat(pa.classLoader(), instanceOf(ClassLoader.class)); + } + + @Test + public void encryptionTest() throws Exception { + PropAccess pa = new PropAccess(); + String plainText = "This is a secret message"; + String secret_message = pa.encrypt(plainText); + String modified = secret_message.substring(4); + // Plenty of assertions to hit all branches + assertThat(pa.decrypt(secret_message, false), is(plainText)); + assertThat(pa.decrypt(null, false), is(nullValue())); + assertThat(pa.decrypt(modified, true), is(plainText)); + assertThat(pa.decrypt(modified, false), is(modified)); + } + + @Test + public void setPropertyTest() { + PropAccess pa = new PropAccess(); + pa.setProperty("test", null); + String prop = "New Property"; + String val ="And it's faithful value"; + pa.setProperty(prop, val); + + assertThat(pa.getProperty(prop), is(val)); + } }