Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-sdc-logging-lib / openecomp-sdc-logging-core / src / test / java / org / openecomp / sdc / logging / slf4j / LoggingContextTest.java
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 static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22
23 import java.util.UUID;
24 import org.junit.After;
25 import org.junit.Test;
26 import org.openecomp.sdc.logging.api.ContextData;
27 import org.openecomp.sdc.logging.api.LoggingContext;
28 import org.slf4j.MDC;
29
30 /**
31  * Unit-testing logging context service via its facade.
32  *
33  * @author evitaliy
34  * @since 12 Sep 2016
35  */
36 public class LoggingContextTest {
37
38     @After
39     public void clearMdc() {
40         MDC.clear();
41     }
42
43     @Test
44     public void returnMdcWrapperWhenToRunnableCalled() {
45         assertEquals(MDCRunnableWrapper.class, LoggingContext.copyToRunnable(() -> { }).getClass());
46     }
47
48     @Test(expected = NullPointerException.class)
49     public void throwNpeWhenToRunnableWithNull() {
50         LoggingContext.copyToRunnable(null);
51     }
52
53     @Test
54     public void returnMdcWrapperWhenToCallableCalled() {
55         assertEquals(MDCCallableWrapper.class, LoggingContext.copyToCallable(() -> "").getClass());
56     }
57
58     @Test(expected = NullPointerException.class)
59     public void throwNpeWhenToCallableWithNull() {
60         LoggingContext.copyToCallable(null);
61     }
62
63     @Test
64     public void keysClearedWhenContextCleared() {
65
66         String value = UUID.randomUUID().toString();
67
68         ContextData context = ContextData.builder().partnerName(value).requestId(value).serviceName(value).build();
69         LoggingContext.put(context);
70         LoggingContext.clear();
71
72         for (ContextField field : ContextField.values()) {
73             assertNull(MDC.get(field.asKey()));
74         }
75     }
76
77     @Test
78     public void unrelatedKeysRemainWhenContextCleared() {
79
80         String randomValue = UUID.randomUUID().toString();
81         String randomKey = "Key-" + randomValue;
82
83         MDC.put(randomKey, randomValue);
84         LoggingContext.clear();
85         assertEquals(randomValue, MDC.get(randomKey));
86     }
87
88     @Test
89     public void contextHasServiceNameWhenPut() {
90
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()));
95     }
96
97     @Test(expected = NullPointerException.class)
98     public void throwNpeWhenContextDataNull() {
99         LoggingContext.put(null);
100     }
101
102     @Test
103     public void contextHasRequestIdWhenPut() {
104
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()));
109     }
110
111     @Test
112     public void contextHasPartnerNameWhenPut() {
113
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()));
118     }
119
120     @Test
121     public void contextHasServerHostWhenPopulated() {
122
123         ContextData context = ContextData.builder().build();
124         LoggingContext.put(context);
125         assertNotNull(MDC.get(ContextField.SERVER.asKey()));
126     }
127
128     @Test
129     public void contextHasServerAddressWhenPopulated() {
130
131         ContextData context = ContextData.builder().build();
132         LoggingContext.put(context);
133         assertNotNull(MDC.get(ContextField.SERVER_IP_ADDRESS.asKey()));
134     }
135
136     @Test
137     public void contextHasInstanceIdWhenPopulated() {
138
139         ContextData context = ContextData.builder().build();
140         LoggingContext.put(context);
141         assertNotNull(MDC.get(ContextField.INSTANCE_ID.asKey()));
142     }
143
144     @Test
145     public void contextReturnsServiceNameWhenPut() {
146
147         String random = UUID.randomUUID().toString();
148         ContextData context = ContextData.builder().serviceName(random).build();
149         LoggingContext.put(context);
150         assertEquals(context, LoggingContext.get());
151     }
152
153     @Test
154     public void contextReturnsRequestIdWhenPut() {
155
156         String random = UUID.randomUUID().toString();
157         ContextData context = ContextData.builder().requestId(random).build();
158         LoggingContext.put(context);
159         assertEquals(context, LoggingContext.get());
160     }
161
162     @Test
163     public void contextReturnsPartnerNameWhenPut() {
164
165         String random = UUID.randomUUID().toString();
166         ContextData context = ContextData.builder().partnerName(random).build();
167         LoggingContext.put(context);
168         assertEquals(context, LoggingContext.get());
169     }
170
171 }