From f51ee23763fd379388753dae419c2e7358fce8cd Mon Sep 17 00:00:00 2001 From: Sai Gandham <sg481n@att.com> Date: Thu, 26 Apr 2018 16:19:49 +0000 Subject: Increase coverage for Env module Issue-ID: AAF-219 Change-Id: Ic41009cdefa30d2b43ec95f0fc21e3f9e8fb472e Signed-off-by: Sai Gandham <sg481n@att.com> --- .../org/onap/aaf/misc/env/JU_LogTargetTest.java | 87 +++++++++++++ .../onap/aaf/misc/env/impl/JU_BasicEnvTest.java | 140 +++++++++++++++++++++ .../org/onap/aaf/misc/env/util/JU_ChronoTest.java | 69 ++++++++++ 3 files changed, 296 insertions(+) create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/JU_LogTargetTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_BasicEnvTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_ChronoTest.java (limited to 'misc/env/src/test/java/org/onap') diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/JU_LogTargetTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_LogTargetTest.java new file mode 100644 index 00000000..474f646c --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_LogTargetTest.java @@ -0,0 +1,87 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +public class JU_LogTargetTest { + + @Mock + Throwable t; + + @Before + public void setup() { + t = mock(Throwable.class); + } + + @Test + public void testLogTargetNull() { + LogTarget nullTarget = LogTarget.NULL; + + // Expect methods doing nothing as no implemenation provided. + nullTarget.log(new Throwable(), null, null); + nullTarget.log("String", null); + nullTarget.printf(null, null, null); + + assertFalse(nullTarget.isLoggable()); + } + + @Test + public void testLogTargetSysOut() { + LogTarget outTarget = LogTarget.SYSOUT; + + outTarget.printf("format", new Date()); + outTarget.log("null", null, null); + + outTarget.log(t); + outTarget.log(t, "First String Object"); + + assertTrue(outTarget.isLoggable()); + + verify(t, times(2)).printStackTrace(System.out); + } + + @Test + public void testLogTargetSysErr() { + LogTarget errTarget = LogTarget.SYSERR; + + errTarget.printf("format", new Date()); + errTarget.log("null", "null"); + + errTarget.log(t); + errTarget.log(t, "First String Object"); + + assertTrue(errTarget.isLoggable()); + + verify(t, times(2)).printStackTrace(System.err); + } + +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_BasicEnvTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_BasicEnvTest.java new file mode 100644 index 00000000..5eab5dd8 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_BasicEnvTest.java @@ -0,0 +1,140 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; + +import java.applet.Applet; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.onap.aaf.misc.env.Decryptor; +import org.onap.aaf.misc.env.Encryptor; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.TimeTaken; + +public class JU_BasicEnvTest { + + @Mock + Decryptor decrypt; + + @Mock + Encryptor encrypt; + + @Before + public void setup() { + decrypt = mock(Decryptor.class); + encrypt = mock(Encryptor.class); + } + + @Test + public void testLogTarget() { + Properties prop = new Properties(); + BasicEnv env = new BasicEnv(prop); + + assertEquals(env.fatal(), LogTarget.SYSERR); + assertEquals(env.error(), LogTarget.SYSERR); + assertEquals(env.audit(), LogTarget.SYSOUT); + assertEquals(env.warn(), LogTarget.SYSERR); + assertEquals(env.init(), LogTarget.SYSOUT); + assertEquals(env.info(), LogTarget.SYSOUT); + assertEquals(env.debug(), LogTarget.NULL); + assertEquals(env.trace(), LogTarget.NULL); + + env.debug(LogTarget.SYSOUT); + assertEquals(env.debug(), LogTarget.SYSOUT); + + assertNull(env.getProperty("key")); + assertEquals("default", env.getProperty("key", "default")); + + env.setProperty("key", "value"); + assertEquals("value", env.getProperty("key", "default")); + + Properties filteredProperties = env.getProperties("key"); + assertEquals(filteredProperties.size(), 1); + + env.setProperty("key", null); + assertEquals("default", env.getProperty("key", "default")); + + filteredProperties = env.getProperties("key1"); + assertEquals(filteredProperties.size(), 0); + + filteredProperties = env.getProperties(); + assertEquals(filteredProperties.size(), 0); + + } + + @Test + public void testBasicEnv() { + Applet applet = null; + + BasicEnv env = new BasicEnv(applet, "tag1", "tag2"); + + TimeTaken tt = env.start("Name", 2); + + long end = tt.end(); + StringBuilder sb = new StringBuilder(); + + assertEquals(tt.toString(), "Name " + (end - tt.start) / 1000000f + "ms "); + tt.output(sb); + assertEquals(sb.toString(), "XML Name " + (end - tt.start) / 1000000f + "ms"); + + env.set(decrypt); + assertEquals(env.decryptor(), decrypt); + env.set(encrypt); + assertEquals(env.encryptor(), encrypt); + } + + @Test + public void testBasicEnvDiffFlag() { + Properties prop = new Properties(); + + BasicEnv env = new BasicEnv("tag1", prop); + + TimeTaken tt = env.start("Name", 1); + + long end = tt.end(); + StringBuilder sb = new StringBuilder(); + + assertEquals(tt.toString(), "Name " + (end - tt.start) / 1000000f + "ms "); + tt.output(sb); + assertEquals(sb.toString(), "REMOTE Name " + (end - tt.start) / 1000000f + "ms"); + + tt = env.start("New Name", 4); + tt.size(10); + sb = new StringBuilder(); + tt.output(sb); + assertEquals(tt.toString(), "New Name " + (end - tt.start) / 1000000f + "ms 10"); + assertEquals(sb.toString(), "JSON New Name " + (end - tt.start) / 1000000f + "ms size: 10"); + + if (System.getProperties().keySet().iterator().hasNext()) { + String key = (String) System.getProperties().keySet().iterator().next(); + + env.loadFromSystemPropsStartsWith(key); + assertEquals(env.getProperty(key), System.getProperties().get(key)); + } + } + +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_ChronoTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_ChronoTest.java new file mode 100644 index 00000000..389e7f75 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_ChronoTest.java @@ -0,0 +1,69 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env.util; + +import static org.junit.Assert.assertEquals; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +import javax.xml.datatype.XMLGregorianCalendar; + +import org.junit.Before; +import org.junit.Test; + +public class JU_ChronoTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testFormatter8601() { + Chrono.Formatter8601 formatter = new Chrono.Formatter8601(); + + LogRecord record = new LogRecord(Level.WARNING, "Log Record to test log formating"); + + Date date = new Date(118, 02, 02); + long time = date.getTime(); + + record.setMillis(time); + + String expectedString = Chrono.dateFmt.format(date) + " " + record.getThreadID() + " " + record.getLevel() + + ": " + record.getMessage() + "\n"; + assertEquals(expectedString, formatter.format(record)); + } + + @Test + public void testTimeStampWithDate() { + Date date = Calendar.getInstance().getTime(); + XMLGregorianCalendar timeStamp = Chrono.timeStamp(date); + + GregorianCalendar gc = new GregorianCalendar(); + gc.setTime(date); + XMLGregorianCalendar expectedCalendar = Chrono.xmlDatatypeFactory.newXMLGregorianCalendar(gc); + + assertEquals(expectedCalendar, timeStamp); + } +} -- cgit 1.2.3-korg