summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java')
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java110
1 files changed, 54 insertions, 56 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java
index 430d4d4c54..47386d4cf3 100644
--- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java
+++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/LoggingContextTest.java
@@ -16,22 +16,30 @@
package org.openecomp.sdc.logging.slf4j;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+
+import java.util.UUID;
+import org.openecomp.sdc.logging.api.ContextData;
import org.openecomp.sdc.logging.api.LoggingContext;
-import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField;
import org.slf4j.MDC;
+import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
-import java.util.UUID;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertNull;
-
/**
+ * Unit-testing logging context service via its facade.
+ *
* @author evitaliy
* @since 12 Sep 2016
*/
public class LoggingContextTest {
+ @AfterMethod
+ public void clearMdc() {
+ MDC.clear();
+ }
+
@Test
public void returnMdcWrapperWhenToRunnableCalled() {
assertEquals(LoggingContext.copyToRunnable(() -> {}).getClass(), MDCRunnableWrapper.class);
@@ -57,18 +65,12 @@ public class LoggingContextTest {
String value = UUID.randomUUID().toString();
- try {
- LoggingContext.putPartnerName(value);
- LoggingContext.putServiceName(value);
- LoggingContext.putRequestId(value);
- LoggingContext.clear();
-
- for (ContextField field : ContextField.values()) {
- assertNull(MDC.get(field.asKey()));
- }
+ ContextData context = ContextData.builder().partnerName(value).requestId(value).serviceName(value).build();
+ LoggingContext.put(context);
+ LoggingContext.clear();
- } finally {
- MDC.clear();
+ for (ContextField field : ContextField.values()) {
+ assertNull(MDC.get(field.asKey()));
}
}
@@ -78,68 +80,64 @@ public class LoggingContextTest {
String randomValue = UUID.randomUUID().toString();
String randomKey = "Key-" + randomValue;
- try {
-
- MDC.put(randomKey, randomValue);
- LoggingContext.clear();
- assertEquals(MDC.get(randomKey), randomValue);
-
- } finally {
- MDC.clear();
- }
+ MDC.put(randomKey, randomValue);
+ LoggingContext.clear();
+ assertEquals(MDC.get(randomKey), randomValue);
}
@Test
public void contextHasServiceNameWhenPut() {
String random = UUID.randomUUID().toString();
-
- try {
- LoggingContext.putServiceName(random);
- assertEquals(random, MDC.get(ContextField.SERVICE_NAME.asKey()));
- } finally {
- MDC.clear();
- }
+ ContextData context = ContextData.builder().serviceName(random).build();
+ LoggingContext.put(context);
+ assertEquals(random, MDC.get(ContextField.SERVICE_NAME.asKey()));
}
@Test(expectedExceptions = NullPointerException.class)
- public void throwNpeWhenServiceNameNull() {
- LoggingContext.putServiceName(null);
+ public void throwNpeWhenContextDataNull() {
+ LoggingContext.put(null);
}
@Test
public void contextHasRequestIdWhenPut() {
String random = UUID.randomUUID().toString();
-
- try {
- LoggingContext.putRequestId(random);
- assertEquals(random, MDC.get(ContextField.REQUEST_ID.asKey()));
- } finally {
- MDC.clear();
- }
- }
-
- @Test(expectedExceptions = NullPointerException.class)
- public void throwNpeWhenRequestIdNull() {
- LoggingContext.putRequestId(null);
+ ContextData context = ContextData.builder().requestId(random).build();
+ LoggingContext.put(context);
+ assertEquals(random, MDC.get(ContextField.REQUEST_ID.asKey()));
}
@Test
public void contextHasPartnerNameWhenPut() {
String random = UUID.randomUUID().toString();
+ ContextData context = ContextData.builder().partnerName(random).build();
+ LoggingContext.put(context);
+ assertEquals(random, MDC.get(ContextField.PARTNER_NAME.asKey()));
+ }
- try {
- LoggingContext.putPartnerName(random);
- assertEquals(random, MDC.get(ContextField.PARTNER_NAME.asKey()));
- } finally {
- MDC.clear();
- }
+ @Test
+ public void contextHasServerHostWhenPopulated() {
+
+ ContextData context = ContextData.builder().build();
+ LoggingContext.put(context);
+ assertNotNull(MDC.get(ContextField.SERVER.asKey()));
}
- @Test(expectedExceptions = NullPointerException.class)
- public void throwNpeWhenPartnerNameNull() {
- LoggingContext.putPartnerName(null);
+ @Test
+ public void contextHasServerAddressWhenPopulated() {
+
+ ContextData context = ContextData.builder().build();
+ LoggingContext.put(context);
+ assertNotNull(MDC.get(ContextField.SERVER_IP_ADDRESS.asKey()));
+ }
+
+ @Test
+ public void contextHasInstanceIdWhenPopulated() {
+
+ ContextData context = ContextData.builder().build();
+ LoggingContext.put(context);
+ assertNotNull(MDC.get(ContextField.INSTANCE_ID.asKey()));
}
}