summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authork.kedron <k.kedron@partner.samsung.com>2019-06-18 09:24:41 +0200
committerOfir Sonsino <ofir.sonsino@intl.att.com>2019-06-23 10:00:22 +0000
commita5b875badca6bc73d18d609a91cdfddfb5a0f396 (patch)
tree2fab10de2f35b1cb2bbec4c48f06eff11dfb3344 /src/test
parentbbb56de1b8f5ab2322fea0a7537bfe1b9c23a366 (diff)
Improved unit tests for onaplog package.
Added new tests to OnapLoggerDebug, OnapLoggerError and OnapLoggerMetric test. Updated unit tests for the OnapLoggerAudit and OnapLoggerGeneric. Issue-ID: SDC-2327 Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com> Change-Id: Icc6fa07fd9b91be0b441956df9f01a9bff9b5723
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/onap/sdc/common/onaplog/LoggerAdapter.java2
-rw-r--r--src/test/java/org/onap/sdc/common/onaplog/OnapLoggerAuditTest.java70
-rw-r--r--src/test/java/org/onap/sdc/common/onaplog/OnapLoggerDebugTest.java93
-rw-r--r--src/test/java/org/onap/sdc/common/onaplog/OnapLoggerErrorTest.java129
-rw-r--r--src/test/java/org/onap/sdc/common/onaplog/OnapLoggerGenericTest.java4
-rw-r--r--src/test/java/org/onap/sdc/common/onaplog/OnapLoogerMetric.java206
6 files changed, 473 insertions, 31 deletions
diff --git a/src/test/java/org/onap/sdc/common/onaplog/LoggerAdapter.java b/src/test/java/org/onap/sdc/common/onaplog/LoggerAdapter.java
index ceb3d98..59475b9 100644
--- a/src/test/java/org/onap/sdc/common/onaplog/LoggerAdapter.java
+++ b/src/test/java/org/onap/sdc/common/onaplog/LoggerAdapter.java
@@ -37,4 +37,4 @@ class LoggerAdapter {
ImmutableList<LoggingEvent> getLoggingEvents() {
return logger.getLoggingEvents();
}
-} \ No newline at end of file
+}
diff --git a/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerAuditTest.java b/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerAuditTest.java
index 49ca61a..8fe3b72 100644
--- a/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerAuditTest.java
+++ b/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerAuditTest.java
@@ -26,8 +26,8 @@ import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanHashCodeFor;
import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
@@ -45,16 +45,17 @@ import org.onap.sdc.common.onaplog.Enums.Severity;
import org.slf4j.MDC;
import uk.org.lidalia.slf4jtest.LoggingEvent;
-import uk.org.lidalia.slf4jtest.TestLogger;
-import uk.org.lidalia.slf4jtest.TestLoggerFactory;
@RunWith(MockitoJUnitRunner.class)
public class OnapLoggerAuditTest {
- private static final String SERVER_FQDN = "onap.test.com";
- private static final String AUTO_SERVER_IP_ADDRESS = "127.0.0.2";
+ 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 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";
@@ -97,21 +98,22 @@ public class OnapLoggerAuditTest {
}
@Test
- public void testLoggerAuditTestFullFlow() {
+ public void testLoggerAuditFullFlow() {
// given
- OnapLoggerAudit audit = new OnapLoggerAudit(new OnapMDCWrapper(new Stopwatch()));
+ OnapLoggerAudit audit = OnapLoggerFactory.getLogger(OnapLoggerAudit.class);
+ assertNotNull(audit);
LoggerAdapter adapter = new LoggerAdapter(OnapLoggerAudit.class);
when(loggerErrorCode.getErrorCode()).thenReturn(RESPONSE_CODE);
when(severity.getSeverityType()).thenReturn(ALERT_SEVERITY);
- audit.initializeMandatoryFields();
setupMDC(audit);
validateMDC();
// when
audit.startTimer();
+ audit.stopTimer();
audit.log(LogLevel.ERROR, TEST_CLASS_NAME, LOG_MESSAGE);
audit.log(LogLevel.WARN, TEST_CLASS_NAME, LOG_MESSAGE);
audit.log(LogLevel.INFO, TEST_CLASS_NAME, LOG_MESSAGE);
@@ -127,9 +129,31 @@ public class OnapLoggerAuditTest {
MDC.clear();
}
+ @Test
+ public void testCleanLogger() {
+ // given
+ OnapLoggerAudit audit = OnapLoggerFactory.getLogger(OnapLoggerAudit.class);
+ assertNotNull(audit);
+
+ audit.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_KEY_REQUEST_ID);
+ audit.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_REMOTE_HOST);
+ audit.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_INSTANCE_UUID);
+ audit.setKeyRequestId(KEY_REQUEST_ID);
+ audit.setRemoteHost(REMOTE_HOST);
+ audit.setInstanceUUID(INSTANCE_UUID);
+
+ // when
+ audit.clear();
+
+ // then
+ assertNull(MDC.get(OnapLogConfiguration.MDC_KEY_REQUEST_ID));
+ assertNull(MDC.get(OnapLogConfiguration.MDC_REMOTE_HOST));
+ assertNull(MDC.get(OnapLogConfiguration.MDC_INSTANCE_UUID));
+ }
+
private void setupMDC(OnapLoggerAudit audit) {
- audit.setAutoServerFQDN(SERVER_FQDN)
- .setAutoServerIPAddress(AUTO_SERVER_IP_ADDRESS)
+ audit.setAutoServerFQDN(DUMMY_SERVER_FQDN)
+ .setAutoServerIPAddress(DUMMY_AUTO_SERVER_IP_ADDRESS)
.setInstanceUUID(INSTANCE_UUID)
.setOptClassName(TEST_CLASS_NAME)
.setOptProcessKey(PROCESS_KEY)
@@ -148,8 +172,9 @@ public class OnapLoggerAuditTest {
}
private void validateMDC() {
- assertEquals(SERVER_FQDN, MDC.get(OnapLogConfiguration.MDC_SERVER_FQDN));
- assertEquals(AUTO_SERVER_IP_ADDRESS, MDC.get(OnapLogConfiguration.MDC_SERVER_IP_ADDRESS));
+ 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));
@@ -172,8 +197,8 @@ public class OnapLoggerAuditTest {
assertNotNull(log.getTimestamp());
ImmutableMap<String, String> mdc = log.getMdc();
- assertNotEquals(SERVER_FQDN, mdc.get(OnapLogConfiguration.MDC_SERVER_FQDN));
- assertNotEquals(AUTO_SERVER_IP_ADDRESS, mdc.get(OnapLogConfiguration.MDC_SERVER_IP_ADDRESS));
+ 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));
@@ -191,18 +216,7 @@ public class OnapLoggerAuditTest {
assertEquals(KEY_REQUEST_ID, mdc.get(OnapLogConfiguration.MDC_KEY_REQUEST_ID));
assertNotNull(mdc.get(OnapLogConfiguration.MDC_BEGIN_TIMESTAMP));
- }
-
- class LoggerAdapter {
-
- TestLogger logger;
-
- LoggerAdapter(Class clazz) {
- this.logger = TestLoggerFactory.getTestLogger(clazz);
- }
-
- private ImmutableList<LoggingEvent> getLoggingEvents() {
- return logger.getLoggingEvents();
- }
+ assertNotNull(MDC.get(OnapLogConfiguration.MDC_END_TIMESTAMP));
+ assertNotNull(MDC.get(OnapLogConfiguration.MDC_ELAPSED_TIME));
}
} \ No newline at end of file
diff --git a/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerDebugTest.java b/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerDebugTest.java
new file mode 100644
index 0000000..5e81739
--- /dev/null
+++ b/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerDebugTest.java
@@ -0,0 +1,93 @@
+/*-
+ * ============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 com.google.common.collect.ImmutableList;
+
+import org.junit.Test;
+
+import org.onap.sdc.common.onaplog.Enums.LogLevel;
+import org.slf4j.MDC;
+
+import uk.org.lidalia.slf4jtest.LoggingEvent;
+
+public class OnapLoggerDebugTest {
+
+ private static final String TEST_CLASS_NAME = OnapLoggerAudit.class.getName();
+ private static final String LOG_MESSAGE = "Test Message";
+ private static final String KEY_REQUEST_ID = "4321";
+
+ @Test
+ public void testLoggerDebugTestFullFlow() {
+
+ // given
+ OnapLoggerDebug debug = OnapLoggerFactory.getLogger(OnapLoggerDebug.class);
+ assertNotNull(debug);
+ LoggerAdapter adapter = new LoggerAdapter(OnapLoggerDebug.class);
+
+ debug.setKeyRequestId(KEY_REQUEST_ID);
+ assertEquals(KEY_REQUEST_ID, MDC.get(OnapLogConfiguration.MDC_KEY_REQUEST_ID));
+
+ // when
+ debug.startTimer();
+ debug.log(LogLevel.ERROR, TEST_CLASS_NAME, LOG_MESSAGE);
+ debug.log(LogLevel.WARN, TEST_CLASS_NAME, LOG_MESSAGE);
+ debug.log(LogLevel.INFO, TEST_CLASS_NAME, LOG_MESSAGE);
+ debug.log(LogLevel.DEBUG, TEST_CLASS_NAME, LOG_MESSAGE);
+
+ // then
+ ImmutableList<LoggingEvent> logs = adapter.getLoggingEvents();
+ validateDebugLog(LogLevel.ERROR, logs.get(0));
+ validateDebugLog(LogLevel.WARN, logs.get(1));
+ validateDebugLog(LogLevel.INFO, logs.get(2));
+ validateDebugLog(LogLevel.DEBUG, logs.get(3));
+
+ MDC.clear();
+ }
+
+ @Test
+ public void testCleanLogger() {
+ // given
+ OnapLoggerDebug debug = OnapLoggerFactory.getLogger(OnapLoggerDebug.class);
+ assertNotNull(debug);
+
+ debug.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_KEY_REQUEST_ID);
+ debug.setKeyRequestId(KEY_REQUEST_ID);
+
+ // when
+ debug.clear();
+
+ // then
+ assertNull(MDC.get(OnapLogConfiguration.MDC_KEY_REQUEST_ID));
+ }
+
+ private void validateDebugLog(LogLevel logLevel, LoggingEvent log) {
+ assertEquals(logLevel.name(), log.getLevel().name());
+ assertEquals(LOG_MESSAGE + "|^\\n", log.getMessage());
+ assertNotNull(log.getTimestamp());
+
+ assertNotNull(log.getMdc().get(OnapLogConfiguration.MDC_BEGIN_TIMESTAMP));
+ }
+} \ No newline at end of file
diff --git a/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerErrorTest.java b/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerErrorTest.java
new file mode 100644
index 0000000..3d61abd
--- /dev/null
+++ b/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerErrorTest.java
@@ -0,0 +1,129 @@
+/*-
+ * ============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 com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+import org.junit.Test;
+
+import org.onap.sdc.common.onaplog.Enums.LogLevel;
+import org.onap.sdc.common.onaplog.Enums.OnapLoggerErrorCode;
+import org.slf4j.MDC;
+
+import uk.org.lidalia.slf4jtest.LoggingEvent;
+
+public class OnapLoggerErrorTest {
+
+ private static final String TEST_CLASS_NAME = OnapLoggerAudit.class.getName();
+
+ private static final String DUMMY_SERVICE_NAME = "testName";
+ private static final String EXPECTED_SERVICE_NAME = "DCAE";
+
+ private static final String LOG_MESSAGE = "Test Message";
+ private static final String KEY_REQUEST_ID = "4321";
+ private static final String TARGET_ENTITY = "testEntity";
+ private static final String ERROR_DESC = "error";
+
+ @Test
+ public void testLoggerErrorFullFlow() {
+
+ // given
+ OnapLoggerError error = OnapLoggerFactory.getLogger(OnapLoggerError.class);
+ assertNotNull(error);
+ LoggerAdapter adapter = new LoggerAdapter(OnapLoggerError.class);
+
+ setupMDC(error);
+ validateMDC();
+
+ // when
+ error.startTimer();
+ error.log(LogLevel.ERROR, TEST_CLASS_NAME, LOG_MESSAGE);
+ error.log(LogLevel.WARN, TEST_CLASS_NAME, LOG_MESSAGE);
+ error.log(LogLevel.INFO, TEST_CLASS_NAME, LOG_MESSAGE);
+ error.log(LogLevel.DEBUG, TEST_CLASS_NAME, LOG_MESSAGE);
+
+ // then
+ ImmutableList<LoggingEvent> logs = adapter.getLoggingEvents();
+ validateErrorLog(LogLevel.ERROR, logs.get(0));
+ validateErrorLog(LogLevel.WARN, logs.get(1));
+ validateErrorLog(LogLevel.INFO, logs.get(2));
+ validateErrorLog(LogLevel.DEBUG, logs.get(3));
+
+ MDC.clear();
+ }
+
+ @Test
+ public void testCleanLoggerDebug() {
+ // given
+ OnapLoggerError error = OnapLoggerFactory.getLogger(OnapLoggerError.class);
+ assertNotNull(error);
+
+ error.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_KEY_REQUEST_ID);
+ error.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_SERVICE_NAME);
+ error.onapMDCWrapper.setMandatoryField(OnapLogConfiguration.MDC_TARGET_ENTITY);
+ error.setKeyRequestId(KEY_REQUEST_ID);
+ error.setServiceName(DUMMY_SERVICE_NAME);
+ error.setTargetEntity(TARGET_ENTITY);
+
+ // when
+ error.clear();
+
+ // then
+ assertNull(MDC.get(OnapLogConfiguration.MDC_KEY_REQUEST_ID));
+ assertNull(MDC.get(OnapLogConfiguration.MDC_SERVICE_NAME));
+ assertNull(MDC.get(OnapLogConfiguration.MDC_TARGET_ENTITY));
+ }
+
+ private void setupMDC(OnapLoggerError error) {
+ error.setKeyRequestId(KEY_REQUEST_ID)
+ .setServiceName(DUMMY_SERVICE_NAME)
+ .setTargetEntity(TARGET_ENTITY);
+ }
+
+ private void validateMDC() {
+ assertEquals(KEY_REQUEST_ID, MDC.get(OnapLogConfiguration.MDC_KEY_REQUEST_ID));
+ assertEquals(DUMMY_SERVICE_NAME, MDC.get(OnapLogConfiguration.MDC_SERVICE_NAME));
+ assertEquals(TARGET_ENTITY, MDC.get(OnapLogConfiguration.MDC_TARGET_ENTITY));
+ }
+
+ private void validateErrorLog(LogLevel logLevel, LoggingEvent log) {
+ assertEquals(logLevel.name(), log.getLevel().name());
+ assertEquals(LOG_MESSAGE, log.getMessage());
+ assertNotNull(log.getTimestamp());
+
+ ImmutableMap<String, String> mdc = log.getMdc();
+
+ assertEquals(TEST_CLASS_NAME, mdc.get(OnapLogConfiguration.MDC_CLASS_NAME));
+ assertEquals(EXPECTED_SERVICE_NAME, mdc.get(OnapLogConfiguration.MDC_SERVICE_NAME));
+ assertEquals(KEY_REQUEST_ID, mdc.get(OnapLogConfiguration.MDC_KEY_REQUEST_ID));
+
+ assertEquals(Integer.toString(OnapLoggerErrorCode.UNKNOWN_ERROR.getErrorCode()),
+ mdc.get(OnapLogConfiguration.MDC_ERROR_CODE));
+ assertEquals(ERROR_DESC, mdc.get(OnapLogConfiguration.MDC_ERROR_DESC));
+
+ assertNotNull(mdc.get(OnapLogConfiguration.MDC_BEGIN_TIMESTAMP));
+ }
+} \ No newline at end of file
diff --git a/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerGenericTest.java b/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerGenericTest.java
index 76c33db..10f1812 100644
--- a/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerGenericTest.java
+++ b/src/test/java/org/onap/sdc/common/onaplog/OnapLoggerGenericTest.java
@@ -31,8 +31,8 @@ import uk.org.lidalia.slf4jtest.LoggingEvent;
public class OnapLoggerGenericTest {
- private static final String FIRST_ERROR_EXPECTED_MESSAGE = "call to stop without calling start first, " +
- "this is not compliant with EELF format";
+ private static final String FIRST_ERROR_EXPECTED_MESSAGE =
+ "call to stop without calling start first, this is not compliant with EELF format";
private static final String SECOND_ERROR_EXPECTED_MESSAGE = "failed to calculate elapsed time";
private static final String ERROR = "ERROR";
diff --git a/src/test/java/org/onap/sdc/common/onaplog/OnapLoogerMetric.java b/src/test/java/org/onap/sdc/common/onaplog/OnapLoogerMetric.java
new file mode 100644
index 0000000..eb49780
--- /dev/null
+++ b/src/test/java/org/onap/sdc/common/onaplog/OnapLoogerMetric.java
@@ -0,0 +1,206 @@
+/*-
+ * ============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<LoggingEvent> 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<String, String> 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));
+ }
+}