fccc6ba59ae8d50164bc53e81fa6e49af9524bef
[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 static org.testng.Assert.assertEquals;
20 import static org.testng.Assert.assertNull;
21
22 import java.util.EnumMap;
23 import java.util.Map;
24 import java.util.UUID;
25 import org.slf4j.MDC;
26
27 class ContextPropagationTestHelper {
28
29     // Set to "false" if an old version of logback implementation is being used.
30     // Explicit context propagation should be used when the context is not propagated to child threads.
31     // See https://jira.qos.ch/browse/LOGBACK-422 and https://jira.qos.ch/browse/LOGBACK-624
32     static final boolean IS_SUITABLE_LOGBACK_VERSION = true;
33
34     static final String EXPECT_PROPAGATED_TO_CHILD = "Expected the data to be propagated to the child thread's context";
35     static final String EXPECT_RETAINED_IN_CURRENT = "Expected the data to be retained in this thread";
36     static final String EXPECT_REPLACED_WITH_STORED = "Expected context data to be replaced with stored data";
37     static final String EXPECT_INNER_RUN = "Expected the inner thread to run";
38     static final String EXPECT_OUTER_RUN = "Expected the outer thread to run";
39     static final String EXPECT_NOT_COPIED = "Expected context data not to be copied to this thread";
40     static final String EXPECT_RETAINED_IN_PARENT = "Expected context data to be retained in parent thread";
41     static final String EXPECT_POPULATED = "Expected context data to be populated in this thread";
42     static final String EXPECT_EMPTY = "Expected context data to be empty";
43     static final String EXPECT_REMAIN_EMPTY = "Expected context data to remain empty in this thread";
44     static final String EXPECT_REVERTED_ON_EXCEPTION = "Expected context data to be reverted even in case of exception";
45     static final String EXPECT_EXCEPTION_FROM_INNER = "Expected the inner class to throw exception";
46
47     static Map<ContextField, String> putUniqueValues() {
48
49         Map<ContextField, String> values = new EnumMap<>(ContextField.class);
50
51         String random = UUID.randomUUID().toString();
52
53         for (ContextField key : ContextField.values()) {
54             String value = random + "-" + key.name();
55             values.put(key, value);
56             MDC.put(key.asKey(), value);
57         }
58
59         return values;
60     }
61
62     static void assertContextFields(Map<ContextField, String> values, String error) {
63
64         for (ContextField f : ContextField.values()) {
65             assertEquals(MDC.get(f.asKey()), values.get(f), error);
66         }
67     }
68
69     static void assertContextEmpty(String error) {
70
71         for (ContextField key : ContextField.values()) {
72             assertNull(MDC.get(key.asKey()), error);
73         }
74     }
75 }