e93c1141934c0c90b91155d9518447aa1c7c7087
[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.slf4j.MDC;
21 import org.testng.annotations.Test;
22
23 import java.util.UUID;
24
25 import static org.testng.Assert.assertEquals;
26 import static org.testng.Assert.assertNull;
27
28 /**
29  * @author evitaliy
30  * @since 12/09/2016.
31  */
32 public class LoggingContextTest {
33
34     @Test
35     public void returnMdcWrapperWhenToRunnableCalled() {
36         assertEquals(LoggingContext.copyToRunnable(() -> {}).getClass(), MDCRunnableWrapper.class);
37     }
38
39     @Test(expectedExceptions = NullPointerException.class)
40     public void throwNpeWhenToRunnableWithNull() {
41         LoggingContext.copyToRunnable(null);
42     }
43
44     @Test
45     public void returnMdcWrapperWhenToCallableCalled() {
46         assertEquals(LoggingContext.copyToCallable(() -> "").getClass(), MDCCallableWrapper.class);
47     }
48
49     @Test(expectedExceptions = NullPointerException.class)
50     public void throwNpeWhenToCallableWithNull() {
51         LoggingContext.copyToCallable(null);
52     }
53
54     @Test
55     public void clearContextWhenClearCalled() {
56
57         String random = UUID.randomUUID().toString();
58
59         try {
60             LoggingContext.put(random, random);
61             LoggingContext.clear();
62             assertNull(MDC.get(random));
63             assertNull(LoggingContext.get(random));
64         } finally {
65             MDC.remove(random);
66         }
67     }
68
69     @Test
70     public void returnContextWhenGetCalled() {
71
72         String random = UUID.randomUUID().toString();
73
74         try {
75             LoggingContext.put(random, random);
76             assertEquals(random, MDC.get(random));
77             assertEquals(random, LoggingContext.get(random));
78         } finally {
79             MDC.remove(random);
80         }
81     }
82
83     @Test
84     public void removeContextWhenRemoveCalled() {
85
86         String random = UUID.randomUUID().toString();
87
88         try {
89             LoggingContext.put(random, random);
90             LoggingContext.remove(random);
91             assertNull(MDC.get(random));
92             assertNull(LoggingContext.get(random));
93         } finally {
94             MDC.remove(random);
95         }
96     }
97
98     @Test(expectedExceptions = NullPointerException.class)
99     public void throwNpeWhenPutWithKeyNull() {
100         LoggingContext.put(null, "---");
101     }
102
103     @Test(expectedExceptions = NullPointerException.class)
104     public void throwNpeWhenGetWithKeyNull() {
105         LoggingContext.get(null);
106     }
107
108     @Test(expectedExceptions = NullPointerException.class)
109     public void throwNpeWhenRemoveWithKeyNull() {
110         LoggingContext.remove(null);
111     }
112 }