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 org.openecomp.sdc.logging.api.LoggingContext;
20 import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField;
22 import org.testng.annotations.Test;
24 import java.util.UUID;
26 import static org.testng.Assert.assertEquals;
27 import static org.testng.Assert.assertNull;
33 public class LoggingContextTest {
36 public void returnMdcWrapperWhenToRunnableCalled() {
37 assertEquals(LoggingContext.copyToRunnable(() -> {}).getClass(), MDCRunnableWrapper.class);
40 @Test(expectedExceptions = NullPointerException.class)
41 public void throwNpeWhenToRunnableWithNull() {
42 LoggingContext.copyToRunnable(null);
46 public void returnMdcWrapperWhenToCallableCalled() {
47 assertEquals(LoggingContext.copyToCallable(() -> "").getClass(), MDCCallableWrapper.class);
50 @Test(expectedExceptions = NullPointerException.class)
51 public void throwNpeWhenToCallableWithNull() {
52 LoggingContext.copyToCallable(null);
56 public void keysClearedWhenContextCleared() {
58 String value = UUID.randomUUID().toString();
61 LoggingContext.putPartnerName(value);
62 LoggingContext.putServiceName(value);
63 LoggingContext.putRequestId(value);
64 LoggingContext.clear();
66 for (ContextField field : ContextField.values()) {
67 assertNull(MDC.get(field.asKey()));
76 public void unrelatedKeysRemainWhenContextCleared() {
78 String randomValue = UUID.randomUUID().toString();
79 String randomKey = "Key-" + randomValue;
83 MDC.put(randomKey, randomValue);
84 LoggingContext.clear();
85 assertEquals(MDC.get(randomKey), randomValue);
93 public void contextHasServiceNameWhenPut() {
95 String random = UUID.randomUUID().toString();
98 LoggingContext.putServiceName(random);
99 assertEquals(random, MDC.get(ContextField.SERVICE_NAME.asKey()));
105 @Test(expectedExceptions = NullPointerException.class)
106 public void throwNpeWhenServiceNameNull() {
107 LoggingContext.putServiceName(null);
111 public void contextHasRequestIdWhenPut() {
113 String random = UUID.randomUUID().toString();
116 LoggingContext.putRequestId(random);
117 assertEquals(random, MDC.get(ContextField.REQUEST_ID.asKey()));
123 @Test(expectedExceptions = NullPointerException.class)
124 public void throwNpeWhenRequestIdNull() {
125 LoggingContext.putRequestId(null);
129 public void contextHasPartnerNameWhenPut() {
131 String random = UUID.randomUUID().toString();
134 LoggingContext.putPartnerName(random);
135 assertEquals(random, MDC.get(ContextField.PARTNER_NAME.asKey()));
141 @Test(expectedExceptions = NullPointerException.class)
142 public void throwNpeWhenPartnerNameNull() {
143 LoggingContext.putPartnerName(null);