430d4d4c54f80c025e63cdd9120c19d16e40afa0
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.logging.slf4j;
18
19 import org.openecomp.sdc.logging.api.LoggingContext;
20 import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField;
21 import org.slf4j.MDC;
22 import org.testng.annotations.Test;
23
24 import java.util.UUID;
25
26 import static org.testng.Assert.assertEquals;
27 import static org.testng.Assert.assertNull;
28
29 /**
30  * @author evitaliy
31  * @since 12 Sep 2016
32  */
33 public class LoggingContextTest {
34
35     @Test
36     public void returnMdcWrapperWhenToRunnableCalled() {
37         assertEquals(LoggingContext.copyToRunnable(() -> {}).getClass(), MDCRunnableWrapper.class);
38     }
39
40     @Test(expectedExceptions = NullPointerException.class)
41     public void throwNpeWhenToRunnableWithNull() {
42         LoggingContext.copyToRunnable(null);
43     }
44
45     @Test
46     public void returnMdcWrapperWhenToCallableCalled() {
47         assertEquals(LoggingContext.copyToCallable(() -> "").getClass(), MDCCallableWrapper.class);
48     }
49
50     @Test(expectedExceptions = NullPointerException.class)
51     public void throwNpeWhenToCallableWithNull() {
52         LoggingContext.copyToCallable(null);
53     }
54
55     @Test
56     public void keysClearedWhenContextCleared() {
57
58         String value = UUID.randomUUID().toString();
59
60         try {
61             LoggingContext.putPartnerName(value);
62             LoggingContext.putServiceName(value);
63             LoggingContext.putRequestId(value);
64             LoggingContext.clear();
65
66             for (ContextField field : ContextField.values()) {
67                 assertNull(MDC.get(field.asKey()));
68             }
69
70         } finally {
71             MDC.clear();
72         }
73     }
74
75     @Test
76     public void unrelatedKeysRemainWhenContextCleared() {
77
78         String randomValue = UUID.randomUUID().toString();
79         String randomKey = "Key-" + randomValue;
80
81         try {
82
83             MDC.put(randomKey, randomValue);
84             LoggingContext.clear();
85             assertEquals(MDC.get(randomKey), randomValue);
86
87         } finally {
88             MDC.clear();
89         }
90     }
91
92     @Test
93     public void contextHasServiceNameWhenPut() {
94
95         String random = UUID.randomUUID().toString();
96
97         try {
98             LoggingContext.putServiceName(random);
99             assertEquals(random, MDC.get(ContextField.SERVICE_NAME.asKey()));
100         } finally {
101             MDC.clear();
102         }
103     }
104
105     @Test(expectedExceptions = NullPointerException.class)
106     public void throwNpeWhenServiceNameNull() {
107         LoggingContext.putServiceName(null);
108     }
109
110     @Test
111     public void contextHasRequestIdWhenPut() {
112
113         String random = UUID.randomUUID().toString();
114
115         try {
116             LoggingContext.putRequestId(random);
117             assertEquals(random, MDC.get(ContextField.REQUEST_ID.asKey()));
118         } finally {
119             MDC.clear();
120         }
121     }
122
123     @Test(expectedExceptions = NullPointerException.class)
124     public void throwNpeWhenRequestIdNull() {
125         LoggingContext.putRequestId(null);
126     }
127
128     @Test
129     public void contextHasPartnerNameWhenPut() {
130
131         String random = UUID.randomUUID().toString();
132
133         try {
134             LoggingContext.putPartnerName(random);
135             assertEquals(random, MDC.get(ContextField.PARTNER_NAME.asKey()));
136         } finally {
137             MDC.clear();
138         }
139     }
140
141     @Test(expectedExceptions = NullPointerException.class)
142     public void throwNpeWhenPartnerNameNull() {
143         LoggingContext.putPartnerName(null);
144     }
145 }