/*- * ============LICENSE_START======================================================= * SDC * ================================================================================ * Copyright (C) 2019 Samsung. 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.sdc.common.onaplog; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.when; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.sdc.common.onaplog.enums.LogLevel; import org.onap.sdc.common.onaplog.enums.Severity; import org.slf4j.MDC; import uk.org.lidalia.slf4jtest.LoggingEvent; @RunWith(MockitoJUnitRunner.class) public class OnapLoogerMetric { private static OnapMDCWrapper onapMDCWrapper = OnapMDCWrapper.getInstance(); private static final String DUMMY_SERVER_FQDN = "onap.test.com"; private static final String DUMMY_AUTO_SERVER_IP_ADDRESS = "127.0.0.2"; private static final String INSTANCE_UUID = "testUUID"; private static final String TEST_CLASS_NAME = OnapLoggerAudit.class.getName(); private static final String PROCESS_KEY = "testKey"; private static final String CUSTOM_FIELD1 = "testField1"; private static final String CUSTOM_FIELD2 = "testField2"; private static final String CUSTOM_FIELD3 = "testField3"; private static final String CUSTOM_FIELD4 = "testField4"; private static final String REMOTE_HOST = "testHost"; private static final String SERVICE_NAME = "testServiceName"; private static final String STATUS_CODE = "200"; private static final String PARENT_NAME = "testParentName"; private static final String RESPONCE_DESC = "exampleDesc"; private static final String SERVICE_INSTANCE_ID = "1234"; private static final String LOG_MESSAGE = "Test Message"; private static final String KEY_REQUEST_ID = "4321"; private static final String TARGET_ENTITY = "testTargetEntity"; private static final String TARGET_SERVICE_NAME = "testTargetServiceName"; private static final String TARGET_VIRTUAL_ENTITY = "testVirtualEntity"; private static final int ALERT_SEVERITY = 2; private static final int RESPONSE_CODE = 2; @Mock private Severity severity; @Test public void testLoggerMetricFullFlow() { // given OnapLoggerMetric metric = OnapLoggerFactory.getLogger(OnapLoggerMetric.class); assertNotNull(metric); LoggerAdapter adapter = new LoggerAdapter(OnapLoggerMetric.class); when(severity.getSeverityType()).thenReturn(ALERT_SEVERITY); setupMDC(metric); validateMDC(); // when metric.startTimer(); metric.stopTimer(); metric.log(LogLevel.ERROR, TEST_CLASS_NAME, LOG_MESSAGE); metric.log(LogLevel.WARN, TEST_CLASS_NAME, LOG_MESSAGE); metric.log(LogLevel.INFO, TEST_CLASS_NAME, LOG_MESSAGE); metric.log(LogLevel.DEBUG, TEST_CLASS_NAME, LOG_MESSAGE); // then ImmutableList logs = adapter.getLoggingEvents(); validateMetricLog(LogLevel.ERROR, logs.get(0)); validateMetricLog(LogLevel.WARN, logs.get(1)); validateMetricLog(LogLevel.INFO, logs.get(2)); validateMetricLog(LogLevel.DEBUG, logs.get(3)); MDC.clear(); } @Test public void testCleanLogger() { // given OnapLoggerMetric error = OnapLoggerFactory.getLogger(OnapLoggerMetric.class); assertNotNull(error); error.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_TARGET_ENTITY); error.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_PROCESS_KEY); error.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_TARGET_VIRTUAL_ENTITY); error.setTargetEntity(TARGET_ENTITY); error.setOptProcessKey(PROCESS_KEY); error.setTargetVirtualEntity(TARGET_VIRTUAL_ENTITY); // when error.clear(); // then assertNull(MDC.get(OnapLogConfiguration.MDC_TARGET_ENTITY)); assertNull(MDC.get(OnapLogConfiguration.MDC_PROCESS_KEY)); assertNull(MDC.get(OnapLogConfiguration.MDC_TARGET_VIRTUAL_ENTITY)); } private void setupMDC(OnapLoggerMetric metric) { metric.setAutoServerFQDN(DUMMY_SERVER_FQDN) .setAutoServerIPAddress(DUMMY_AUTO_SERVER_IP_ADDRESS).setInstanceUUID(INSTANCE_UUID) .setOptClassName(TEST_CLASS_NAME).setOptProcessKey(PROCESS_KEY) .setOptAlertSeverity(severity).setOptCustomField1(CUSTOM_FIELD1) .setOptCustomField2(CUSTOM_FIELD2).setOptCustomField3(CUSTOM_FIELD3) .setOptCustomField4(CUSTOM_FIELD4).setRemoteHost(REMOTE_HOST) .setServiceName(SERVICE_NAME).setStatusCode(STATUS_CODE).setPartnerName(PARENT_NAME) .setResponseCode(RESPONSE_CODE).setResponseDesc(RESPONCE_DESC) .setOptServiceInstanceId(SERVICE_INSTANCE_ID).setKeyRequestId(KEY_REQUEST_ID) .setTargetEntity(TARGET_ENTITY).setTargetServiceName(TARGET_SERVICE_NAME) .setTargetVirtualEntity(TARGET_VIRTUAL_ENTITY); } private void validateMDC() { assertEquals(DUMMY_SERVER_FQDN, MDC.get(OnapLogConfiguration.MDC_SERVER_FQDN)); assertEquals(DUMMY_AUTO_SERVER_IP_ADDRESS, MDC.get(OnapLogConfiguration.MDC_SERVER_IP_ADDRESS)); assertEquals(INSTANCE_UUID, MDC.get(OnapLogConfiguration.MDC_INSTANCE_UUID)); assertEquals(TEST_CLASS_NAME, MDC.get(OnapLogConfiguration.MDC_CLASS_NAME)); assertEquals(PROCESS_KEY, MDC.get(OnapLogConfiguration.MDC_PROCESS_KEY)); assertEquals(String.valueOf(ALERT_SEVERITY), MDC.get(OnapLogConfiguration.MDC_ALERT_SEVERITY)); assertEquals(CUSTOM_FIELD1, MDC.get(OnapLogConfiguration.MDC_OPT_FIELD1)); assertEquals(CUSTOM_FIELD2, MDC.get(OnapLogConfiguration.MDC_OPT_FIELD2)); assertEquals(CUSTOM_FIELD3, MDC.get(OnapLogConfiguration.MDC_OPT_FIELD3)); assertEquals(CUSTOM_FIELD4, MDC.get(OnapLogConfiguration.MDC_OPT_FIELD4)); assertEquals(REMOTE_HOST, MDC.get(OnapLogConfiguration.MDC_REMOTE_HOST)); assertEquals(SERVICE_NAME, MDC.get(OnapLogConfiguration.MDC_SERVICE_NAME)); assertEquals(STATUS_CODE, MDC.get(OnapLogConfiguration.MDC_STATUS_CODE)); assertEquals(PARENT_NAME, MDC.get(OnapLogConfiguration.MDC_PARTNER_NAME)); assertEquals(String.valueOf(RESPONSE_CODE), MDC.get(OnapLogConfiguration.MDC_RESPONSE_CODE)); assertEquals(SERVICE_INSTANCE_ID, MDC.get(OnapLogConfiguration.MDC_SERVICE_INSTANCE_ID)); assertEquals(KEY_REQUEST_ID, MDC.get(OnapLogConfiguration.MDC_KEY_REQUEST_ID)); assertEquals(TARGET_ENTITY, MDC.get(OnapLogConfiguration.MDC_TARGET_ENTITY)); assertEquals(TARGET_SERVICE_NAME, MDC.get(OnapLogConfiguration.MDC_TARGET_SERVICE_NAME)); assertEquals(TARGET_VIRTUAL_ENTITY, MDC.get(OnapLogConfiguration.MDC_TARGET_VIRTUAL_ENTITY)); } private void validateMetricLog(LogLevel logLevel, LoggingEvent log) { assertEquals(logLevel.name(), log.getLevel().name()); assertEquals(LOG_MESSAGE, log.getMessage()); assertNotNull(log.getTimestamp()); ImmutableMap mdc = log.getMdc(); assertEquals(onapMDCWrapper.getFqdn(), mdc.get(OnapLogConfiguration.MDC_SERVER_FQDN)); assertEquals(onapMDCWrapper.getHostAddress(), mdc.get(OnapLogConfiguration.MDC_SERVER_IP_ADDRESS)); assertEquals(INSTANCE_UUID, mdc.get(OnapLogConfiguration.MDC_INSTANCE_UUID)); assertEquals(TEST_CLASS_NAME, mdc.get(OnapLogConfiguration.MDC_CLASS_NAME)); assertEquals(PROCESS_KEY, mdc.get(OnapLogConfiguration.MDC_PROCESS_KEY)); assertEquals(String.valueOf(ALERT_SEVERITY), mdc.get(OnapLogConfiguration.MDC_ALERT_SEVERITY)); assertEquals(CUSTOM_FIELD1, mdc.get(OnapLogConfiguration.MDC_OPT_FIELD1)); assertEquals(CUSTOM_FIELD2, mdc.get(OnapLogConfiguration.MDC_OPT_FIELD2)); assertEquals(CUSTOM_FIELD3, mdc.get(OnapLogConfiguration.MDC_OPT_FIELD3)); assertEquals(CUSTOM_FIELD4, mdc.get(OnapLogConfiguration.MDC_OPT_FIELD4)); assertEquals(REMOTE_HOST, mdc.get(OnapLogConfiguration.MDC_REMOTE_HOST)); assertEquals(SERVICE_NAME, mdc.get(OnapLogConfiguration.MDC_SERVICE_NAME)); assertEquals(PARENT_NAME, mdc.get(OnapLogConfiguration.MDC_PARTNER_NAME)); assertEquals(String.valueOf(RESPONSE_CODE), mdc.get(OnapLogConfiguration.MDC_RESPONSE_CODE)); assertEquals(SERVICE_INSTANCE_ID, mdc.get(OnapLogConfiguration.MDC_SERVICE_INSTANCE_ID)); assertEquals(KEY_REQUEST_ID, mdc.get(OnapLogConfiguration.MDC_KEY_REQUEST_ID)); assertEquals(TARGET_ENTITY, mdc.get(OnapLogConfiguration.MDC_TARGET_ENTITY)); assertEquals(TARGET_SERVICE_NAME, mdc.get(OnapLogConfiguration.MDC_TARGET_SERVICE_NAME)); assertEquals(TARGET_VIRTUAL_ENTITY, mdc.get(OnapLogConfiguration.MDC_TARGET_VIRTUAL_ENTITY)); assertNotNull(mdc.get(OnapLogConfiguration.MDC_BEGIN_TIMESTAMP)); assertNotNull(MDC.get(OnapLogConfiguration.MDC_END_TIMESTAMP)); assertNotNull(MDC.get(OnapLogConfiguration.MDC_ELAPSED_TIME)); } }