2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.logging.slf4j;
19 import static org.testng.Assert.assertEquals;
20 import static org.testng.Assert.assertNotNull;
21 import static org.testng.Assert.assertNull;
23 import java.util.UUID;
24 import org.openecomp.sdc.logging.api.ContextData;
25 import org.openecomp.sdc.logging.api.LoggingContext;
27 import org.testng.annotations.AfterMethod;
28 import org.testng.annotations.Test;
31 * Unit-testing logging context service via its facade.
36 public class LoggingContextTest {
39 public void clearMdc() {
44 public void returnMdcWrapperWhenToRunnableCalled() {
45 assertEquals(LoggingContext.copyToRunnable(() -> { }).getClass(), MDCRunnableWrapper.class);
48 @Test(expectedExceptions = NullPointerException.class)
49 public void throwNpeWhenToRunnableWithNull() {
50 LoggingContext.copyToRunnable(null);
54 public void returnMdcWrapperWhenToCallableCalled() {
55 assertEquals(LoggingContext.copyToCallable(() -> "").getClass(), MDCCallableWrapper.class);
58 @Test(expectedExceptions = NullPointerException.class)
59 public void throwNpeWhenToCallableWithNull() {
60 LoggingContext.copyToCallable(null);
64 public void keysClearedWhenContextCleared() {
66 String value = UUID.randomUUID().toString();
68 ContextData context = ContextData.builder().partnerName(value).requestId(value).serviceName(value).build();
69 LoggingContext.put(context);
70 LoggingContext.clear();
72 for (ContextField field : ContextField.values()) {
73 assertNull(MDC.get(field.asKey()));
78 public void unrelatedKeysRemainWhenContextCleared() {
80 String randomValue = UUID.randomUUID().toString();
81 String randomKey = "Key-" + randomValue;
83 MDC.put(randomKey, randomValue);
84 LoggingContext.clear();
85 assertEquals(MDC.get(randomKey), randomValue);
89 public void contextHasServiceNameWhenPut() {
91 String random = UUID.randomUUID().toString();
92 ContextData context = ContextData.builder().serviceName(random).build();
93 LoggingContext.put(context);
94 assertEquals(random, MDC.get(ContextField.SERVICE_NAME.asKey()));
97 @Test(expectedExceptions = NullPointerException.class)
98 public void throwNpeWhenContextDataNull() {
99 LoggingContext.put(null);
103 public void contextHasRequestIdWhenPut() {
105 String random = UUID.randomUUID().toString();
106 ContextData context = ContextData.builder().requestId(random).build();
107 LoggingContext.put(context);
108 assertEquals(random, MDC.get(ContextField.REQUEST_ID.asKey()));
112 public void contextHasPartnerNameWhenPut() {
114 String random = UUID.randomUUID().toString();
115 ContextData context = ContextData.builder().partnerName(random).build();
116 LoggingContext.put(context);
117 assertEquals(random, MDC.get(ContextField.PARTNER_NAME.asKey()));
121 public void contextHasServerHostWhenPopulated() {
123 ContextData context = ContextData.builder().build();
124 LoggingContext.put(context);
125 assertNotNull(MDC.get(ContextField.SERVER.asKey()));
129 public void contextHasServerAddressWhenPopulated() {
131 ContextData context = ContextData.builder().build();
132 LoggingContext.put(context);
133 assertNotNull(MDC.get(ContextField.SERVER_IP_ADDRESS.asKey()));
137 public void contextHasInstanceIdWhenPopulated() {
139 ContextData context = ContextData.builder().build();
140 LoggingContext.put(context);
141 assertNotNull(MDC.get(ContextField.INSTANCE_ID.asKey()));
145 public void contextReturnsServiceNameWhenPut() {
147 String random = UUID.randomUUID().toString();
148 ContextData context = ContextData.builder().serviceName(random).build();
149 LoggingContext.put(context);
150 assertEquals(context, LoggingContext.get());
154 public void contextReturnsRequestIdWhenPut() {
156 String random = UUID.randomUUID().toString();
157 ContextData context = ContextData.builder().requestId(random).build();
158 LoggingContext.put(context);
159 assertEquals(context, LoggingContext.get());
163 public void contextReturnsPartnerNameWhenPut() {
165 String random = UUID.randomUUID().toString();
166 ContextData context = ContextData.builder().partnerName(random).build();
167 LoggingContext.put(context);
168 assertEquals(context, LoggingContext.get());