[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / common / openecomp-logging-lib / openecomp-logging-core / src / test / java / org / openecomp / core / logging / context / MDCPropagationFactoryTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.logging.context;
22
23 import org.slf4j.MDC;
24 import org.testng.annotations.Test;
25
26 import java.util.UUID;
27 import java.util.concurrent.atomic.AtomicBoolean;
28
29 import static org.testng.Assert.*;
30
31 /**
32  * @author evitaliy
33  * @since 12/09/2016.
34  */
35 public class MDCPropagationFactoryTest {
36
37   @Test
38   public void testNoPropagation() throws InterruptedException {
39
40     String uuid = UUID.randomUUID().toString();
41     AtomicBoolean complete = new AtomicBoolean(false);
42     MDC.put("data", uuid);
43
44     Runnable runnable = () -> {
45       assertNull(MDC.get("data"));
46       complete.set(true);
47     };
48
49     Thread thread = new Thread(runnable);
50     thread.start();
51     thread.join();
52
53     assertEquals(MDC.get("data"), uuid);
54     assertTrue(complete.get());
55   }
56
57   @Test
58   public void testPropagation() throws InterruptedException {
59
60     String uuid = UUID.randomUUID().toString();
61     AtomicBoolean complete = new AtomicBoolean(false);
62     MDC.put("data", uuid);
63
64     MdcPropagationService factory = new MdcPropagationService();
65     Runnable runnable = factory.create(() -> {
66       assertEquals(MDC.get("data"), uuid);
67       complete.set(true);
68     });
69
70     Thread thread = new Thread(runnable);
71     thread.start();
72     thread.join();
73
74     assertEquals(MDC.get("data"), uuid);
75     assertTrue(complete.get());
76   }
77
78   @Test
79   public void testReplacement() throws InterruptedException {
80
81     String innerUuid = UUID.randomUUID().toString();
82     AtomicBoolean innerComplete = new AtomicBoolean(false);
83     AtomicBoolean outerComplete = new AtomicBoolean(false);
84
85     MDC.put("data", innerUuid);
86
87     MdcPropagationService factory = new MdcPropagationService();
88
89     // should run with the context of main thread
90     Runnable inner = factory.create(() -> {
91       assertEquals(MDC.get("data"), innerUuid);
92       innerComplete.set(true);
93     });
94
95     // pushes its own context, but runs the inner runnable
96     Runnable outer = () -> {
97       String outerUuid = UUID.randomUUID().toString();
98       MDC.put("data", outerUuid);
99       inner.run();
100       assertEquals(MDC.get("data"), outerUuid);
101       outerComplete.set(true);
102     };
103
104
105     Thread thread = new Thread(outer);
106     thread.start();
107     thread.join();
108
109     assertEquals(MDC.get("data"), innerUuid);
110     assertTrue(outerComplete.get());
111     assertTrue(innerComplete.get());
112   }
113
114   @Test
115   public void testEmpty() throws InterruptedException {
116
117     final AtomicBoolean complete = new AtomicBoolean(false);
118
119     MDC.remove("data");
120     assertNull(MDC.get("data"));
121
122     MdcPropagationService factory = new MdcPropagationService();
123     Runnable runnable = factory.create(() -> {
124       assertNull(MDC.get("data"));
125       complete.set(true);
126     });
127
128     Thread thread = new Thread(runnable);
129     thread.start();
130     thread.join();
131
132     assertNull(MDC.get("data"));
133     assertTrue(complete.get());
134   }
135
136   @Test
137   public void testCleanup() throws Exception {
138
139     String innerUuid = UUID.randomUUID().toString();
140     AtomicBoolean innerComplete = new AtomicBoolean(false);
141     AtomicBoolean outerComplete = new AtomicBoolean(false);
142
143     MDC.put("data", innerUuid);
144
145     MdcPropagationService factory = new MdcPropagationService();
146
147     // should run with the context of main thread
148     Runnable inner = factory.create(() -> {
149       assertEquals(MDC.get("data"), innerUuid);
150       innerComplete.set(true);
151     });
152
153     // pushes its own context, but runs the inner runnable
154     Runnable outer = () -> {
155       assertNull(MDC.get("data"));
156       inner.run();
157       assertNull(MDC.get("data"));
158       outerComplete.set(true);
159     };
160
161     Thread thread = new Thread(outer);
162     thread.start();
163     thread.join();
164
165     assertEquals(MDC.get("data"), innerUuid);
166     assertTrue(outerComplete.get());
167     assertTrue(innerComplete.get());
168   }
169
170   @Test
171   public void testCleanupAfterError() throws Exception {
172
173     String innerUuid = UUID.randomUUID().toString();
174     AtomicBoolean innerComplete = new AtomicBoolean(false);
175     AtomicBoolean outerComplete = new AtomicBoolean(false);
176     AtomicBoolean exceptionThrown = new AtomicBoolean(false);
177
178     MDC.put("data", innerUuid);
179
180     MdcPropagationService factory = new MdcPropagationService();
181
182     // should run with the context of main thread
183     Runnable inner = factory.create(() -> {
184       assertEquals(MDC.get("data"), innerUuid);
185       innerComplete.set(true);
186       throw new RuntimeException();
187     });
188
189     // pushes its own context, but runs the inner runnable
190     Runnable outer = () -> {
191
192       String outerUuid = UUID.randomUUID().toString();
193       MDC.put("data", outerUuid);
194       assertEquals(MDC.get("data"), outerUuid);
195
196       try {
197         inner.run();
198       } catch (RuntimeException e) {
199         exceptionThrown.set(true);
200       } finally {
201         assertEquals(MDC.get("data"), outerUuid);
202         outerComplete.set(true);
203       }
204     };
205
206     Thread thread = new Thread(outer);
207     thread.start();
208     thread.join();
209
210     assertEquals(MDC.get("data"), innerUuid);
211     assertTrue(outerComplete.get());
212     assertTrue(innerComplete.get());
213     assertTrue(exceptionThrown.get());
214   }
215
216 }