diff options
author | Dan McMillan <dm3821@att.com> | 2018-03-28 19:34:16 +0000 |
---|---|---|
committer | Randa Maher <rx196w@att.com> | 2018-03-28 21:09:47 +0000 |
commit | 534aa6f86103ef93f0678f8d5b4b52bed082bab7 (patch) | |
tree | ff0a97c1a8f399b45ee3d1e5a7c7de3370a1777c | |
parent | 0623e356a4194a573cbb1e763abae91c6edae656 (diff) |
Add junit coverage to LoggingUtils class
Introduce junit-tests for LoggingUtils class
Change-Id: Ieb4a14fb258abbef77e9b67398851317e3f08098
Issue-ID: APPC-802
Signed-off-by: Dan McMillan <dm3821@att.com>
-rw-r--r-- | appc-common/src/test/java/org/onap/appc/logging/LoggingUtilsTest.java | 357 |
1 files changed, 357 insertions, 0 deletions
diff --git a/appc-common/src/test/java/org/onap/appc/logging/LoggingUtilsTest.java b/appc-common/src/test/java/org/onap/appc/logging/LoggingUtilsTest.java new file mode 100644 index 000000000..cc7216018 --- /dev/null +++ b/appc-common/src/test/java/org/onap/appc/logging/LoggingUtilsTest.java @@ -0,0 +1,357 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * 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.appc.logging; + +import org.junit.Test; +import org.powermock.reflect.Whitebox; +import org.slf4j.MDC; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; +import java.lang.Class; +import java.lang.reflect.Method; +import java.time.Instant; + +public class LoggingUtilsTest { + @Test(expected = IllegalAccessError.class) + public void testConstructor() throws Exception { + Whitebox.invokeConstructor(LoggingUtils.class); + } + + @Test + public void testLogErrorMessageStringStringStringStringStringString() { + try { + LoggingUtils.logErrorMessage("ERROR_CODE", "ERROR_DESCRIPTION", "TARGET_ENTITY", "TARGET_SERVICE_NAME", "ADDITIONAL_MESSAGE", "CLASS_NAME"); + assertNull(MDC.get(LoggingConstants.MDCKeys.CLASS_NAME)); + } catch (Exception e) { + fail("Exception invoking logErrorMessage: " + e.toString()); + } + } + + @Test + public void testLogErrorMessageStringStringStringString() { + try { + LoggingUtils.logErrorMessage("TARGET_ENTITY", "TARGET_SERVICE_NAME", "ADDITIONAL_MESSAGE", "CLASS_NAME"); + assertNull(MDC.get(LoggingConstants.MDCKeys.CLASS_NAME)); + } catch (Exception e) { + fail("Exception invoking logErrorMessage: " + e.toString()); + } + } + + @Test + public void testLogErrorMessageStringStringString() { + try { + LoggingUtils.logErrorMessage("TARGET_SERVICE_NAME", "ADDITIONAL_MESSAGE", "CLASS_NAME"); + assertNull(MDC.get(LoggingConstants.MDCKeys.CLASS_NAME)); + } catch (Exception e) { + fail("Exception invoking logErrorMessage: " + e.toString()); + } + } + + @Test + public void testLogError() { + try { + Class<?>[] paramString = { String.class, String.class, String.class, String.class, String.class, String.class }; + Method m = LoggingUtils.class.getDeclaredMethod("logError", paramString); + m.setAccessible(true); + m.invoke(null, "ERROR_CODE", "ERROR_DESCRIPTION", "TARGET_ENTITY", "TARGET_SERVICE_NAME", "ADDITIONAL_MESSAGE", "CLASS_NAME"); + assertNull(MDC.get(LoggingConstants.MDCKeys.CLASS_NAME)); + } catch (Exception e) { + fail("Exception invoking logError: " + e.toString()); + } + } + + @Test + public void testLogAuditMessage() { + try { + Class<?>[] paramString = { Instant.class, Instant.class, String.class, String.class, String.class }; + Method m = LoggingUtils.class.getDeclaredMethod("logAuditMessage", paramString); + m.setAccessible(true); + java.util.Date timestamp = new java.util.Date(); + m.invoke(null, timestamp.toInstant(), timestamp.toInstant(), "CODE", "RESPONSE_DESCRIPTION", "CLASS_NAME"); + assertNull(MDC.get(LoggingConstants.MDCKeys.CLASS_NAME)); + } catch (Exception e) { + fail("Exception invoking logAuditMessage: " + e.toString()); + } + } + + @Test + public void testAuditInfo() { + try { + EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); + auditLogger.info("Audit logging test info"); + } catch (Exception e) { + fail("Exception invoking testAuditInfo: " + e.toString()); + } + } + + @Test + public void testAuditWarn() { + try { + EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); + auditLogger.warn("Audit logging test warning"); + } catch (Exception e) { + fail("Exception invoking testAuditWarn: " + e.toString()); + } + } + + @Test + public void testLogMetricsMessage() { + try { + java.util.Date timestamp = new java.util.Date(); + LoggingUtils.logMetricsMessage(timestamp.toInstant(), timestamp.toInstant(), "TARGET_ENTITY", "TARGET_SERVICE_NAME", "STATUS_CODE", "RESPONSE_CODE", "RESPONSE_DESCRIPTION", "CLASS_NAME"); + assertNull(MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)); + + } catch (Exception e) { + fail("Exception invoking logMetricsMessage: " + e.toString()); + } + } + + @Test + public void testPopulateAuditLogContext() { + try { + Class<?>[] paramString = { Instant.class, Instant.class, String.class, String.class, String.class }; + Method m = LoggingUtils.class.getDeclaredMethod("populateAuditLogContext", paramString); + m.setAccessible(true); + java.util.Date timestamp = new java.util.Date(); + m.invoke(null, timestamp.toInstant(), timestamp.toInstant(), "100", "RESPONSE_DESCRIPTION", "CLASS_NAME"); + assertEquals("COMPLETE", MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)); + assertEquals("100", MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)); + assertEquals("RESPONSE_DESCRIPTION", MDC.get(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION)); + } catch (Exception e) { + fail("Exception invoking populateAuditLogContext: " + e.toString()); + } + } + + @Test + public void testCleanAuditErrorContext() { + try { + Method m = LoggingUtils.class.getDeclaredMethod("cleanAuditErrorContext"); + m.setAccessible(true); + MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, "STATUS_CODE"); + MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, "RESPONSE_CODE"); + MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, "RESPONSE_DESCRIPTION"); + MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, "CLASS_NAME"); + m.invoke(null); + assertNull(MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)); + assertNull(MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)); + assertNull(MDC.get(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION)); + assertNull(MDC.get(LoggingConstants.MDCKeys.CLASS_NAME)); + } catch (Exception e) { + fail("Exception invoking cleanAuditErrorLogContext: " + e.toString()); + } + } + + @Test + public void testPopulateErrorLogContext() { + try { + Class<?>[] paramString = { String.class, String.class, String.class, String.class, String.class }; + Method m = LoggingUtils.class.getDeclaredMethod("populateErrorLogContext", paramString); + m.setAccessible(true); + m.invoke(null, "ERROR_CODE", "ERROR_DESCRIPTION", "TARGET_ENTITY", "TARGET_SERVICENAME", "CLASS_NAME"); + assertEquals("CLASS_NAME", MDC.get(LoggingConstants.MDCKeys.CLASS_NAME)); + } catch (Exception e) { + fail("Exception invoking populateErrorLogContext: " + e.toString()); + } + } + + @Test + public void testCleanErrorLogContext() { + try { + Method m = LoggingUtils.class.getDeclaredMethod("cleanErrorLogContext"); + m.setAccessible(true); + MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, "CLASS_NAME"); + m.invoke(null); + assertNull(MDC.get(LoggingConstants.MDCKeys.CLASS_NAME)); + } catch (Exception e) { + fail("Exception invoking cleanErrorLogContext: " + e.toString()); + } + } + + @Test + public void testPopulateMetricLogContext() { + try { + Class<?>[] paramString = { Instant.class, Instant.class, String.class, String.class, String.class, + String.class, String.class, String.class }; + Method m = LoggingUtils.class.getDeclaredMethod("populateMetricLogContext", paramString); + m.setAccessible(true); + java.util.Date timestamp = new java.util.Date(); + m.invoke(null, timestamp.toInstant(), timestamp.toInstant(), "TARGET_ENTITY", "TARGET_SERVICE_NAME", "STATUS_CODE", "RESPONSE_CODE", "RESPONSE_DESCRIPTION", "CLASS_NAME"); + assertEquals("STATUS_CODE", MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)); + assertEquals("RESPONSE_CODE", MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)); + assertEquals("RESPONSE_DESCRIPTION", MDC.get(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION)); + } catch (Exception e) { + fail("Exception invoking populateMetricLogContext: " + e.toString()); + } + } + + @Test + public void testCleanMetricContext() { + try { + Method m = LoggingUtils.class.getDeclaredMethod("cleanMetricContext"); + m.setAccessible(true); + MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, "CLASS_NAME"); + m.invoke(null); + assertNull(MDC.get(LoggingConstants.MDCKeys.CLASS_NAME)); + } catch (Exception e) { + fail("Exception invoking cleanMetricContext: " + e.toString()); + } + } + + @Test + public void testPopulateTargetContext() { + try { + Class<?>[] paramString = { String.class, String.class }; + Method m = LoggingUtils.class.getDeclaredMethod("populateTargetContext", paramString); + m.setAccessible(true); + m.invoke(null, "TARGET_ENTITY", "TARGET_SERVICE_NAME"); + assertEquals("TARGET_ENTITY", MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY)); + assertEquals("TARGET_SERVICE_NAME", MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME)); + } catch (Exception e) { + fail("Exception invoking populateTargetContext: " + e.toString()); + } + } + + @Test + public void testCleanTargetContext() { + try { + Method m = LoggingUtils.class.getDeclaredMethod("cleanTargetContext"); + m.setAccessible(true); + MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, "TARGET_ENTITY"); + MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, "TARGET_SERVICE_NAME"); + m.invoke(null); + assertNull(MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY)); + assertNull(MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME)); + } catch (Exception e) { + fail("Exception invoking cleanTargetContext: " + e.toString()); + } + } + + @Test + public void testPopulateTimeContext() { + try { + Class<?>[] paramString = { Instant.class, Instant.class }; + Method m = LoggingUtils.class.getDeclaredMethod("populateTimeContext", paramString); + m.setAccessible(true); + java.util.Date timestamp = new java.util.Date(); + m.invoke(null, timestamp.toInstant(), timestamp.toInstant()); + } catch (Exception e) { + fail("Exception invoking populateTimeContext: " + e.toString()); + } + } + + @Test + public void testGenerateTimestampStr() { + try { + Class<?>[] paramString = { Instant.class }; + Method m = LoggingUtils.class.getDeclaredMethod("generateTimestampStr", paramString); + m.setAccessible(true); + java.util.Date timestamp = new java.util.Date(); + assertNotNull((String) m.invoke(null, timestamp.toInstant())); + } catch (Exception e) { + fail("Exception invoking testGenerateTimestampStr: " + e.toString()); + } + + } + + @Test + public void testCleanTimeContext() { + try { + Method m = LoggingUtils.class.getDeclaredMethod("cleanTimeContext"); + m.setAccessible(true); + MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, "BEGIN_TIMESTAMP"); + MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, "END_TIMESTAMP"); + MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, "ELAPSED_TIME"); + m.invoke(null); + assertNull(MDC.get(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP)); + assertNull(MDC.get(LoggingConstants.MDCKeys.END_TIMESTAMP)); + assertNull(MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME)); + } catch (Exception e) { + fail("Exception invoking cleanErrorContext: " + e.toString()); + } + } + + @Test + public void testPopulateResponseContext() { + try { + Class<?>[] paramString = { String.class, String.class, String.class }; + Method m = LoggingUtils.class.getDeclaredMethod("populateResponseContext", paramString); + m.setAccessible(true); + m.invoke(null, "STATUS_CODE", "RESPONSE_CODE", "RESPONSE_DESCRIPTION"); + assertEquals("STATUS_CODE", MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)); + assertEquals("RESPONSE_CODE", MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)); + assertEquals("RESPONSE_DESCRIPTION", MDC.get(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION)); + } catch (Exception e) { + fail("Exception invoking populateResponseContext: " + e.toString()); + } + } + + @Test + public void testCleanResponseContext() { + try { + Method m = LoggingUtils.class.getDeclaredMethod("cleanResponseContext"); + m.setAccessible(true); + MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, "STATUS_CODE"); + MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, "RESPONSE_CODE"); + MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, "RESPONSE_DESCRIPTION"); + m.invoke(null); + assertNull(MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)); + assertNull(MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)); + assertNull(MDC.get(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION)); + } catch (Exception e) { + fail("Exception invoking cleanErrorContext: " + e.toString()); + } + } + + @Test + public void testPopulateErrorContext() { + try { + Class<?>[] paramString = { String.class, String.class }; + Method m = LoggingUtils.class.getDeclaredMethod("populateErrorContext", paramString); + m.setAccessible(true); + m.invoke(null, "ERROR_CODE", "ERROR_DESCRIPTION"); + assertEquals("ERROR_CODE", MDC.get(LoggingConstants.MDCKeys.ERROR_CODE)); + assertEquals("ERROR_DESCRIPTION", MDC.get(LoggingConstants.MDCKeys.ERROR_DESCRIPTION)); + } catch (Exception e) { + fail("Exception invoking populateErrorContext: " + e.toString()); + } + } + + @Test + public void testCleanErrorContext() { + try { + Method m = LoggingUtils.class.getDeclaredMethod("cleanErrorContext"); + m.setAccessible(true); + MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, "ERROR_CODE"); + MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, "ERROR_DESCRIPTION"); + m.invoke(null); + assertNull(MDC.get(LoggingConstants.MDCKeys.ERROR_CODE)); + assertNull(MDC.get(LoggingConstants.MDCKeys.ERROR_DESCRIPTION)); + } catch (Exception e) { + fail("Exception invoking cleanErrorContext: " + e.toString()); + } + } + +} |