2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.logging.slf4j;
19 import static org.testng.Assert.assertEquals;
20 import static org.testng.Assert.assertNull;
22 import java.util.EnumMap;
24 import java.util.UUID;
27 class ContextPropagationTestHelper {
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;
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";
47 static Map<ContextField, String> putUniqueValues() {
49 Map<ContextField, String> values = new EnumMap<>(ContextField.class);
51 String random = UUID.randomUUID().toString();
53 for (ContextField key : ContextField.values()) {
54 String value = random + "-" + key.name();
55 values.put(key, value);
56 MDC.put(key.asKey(), value);
62 static void assertContextFields(Map<ContextField, String> values, String error) {
64 for (ContextField f : ContextField.values()) {
65 assertEquals(MDC.get(f.asKey()), values.get(f), error);
69 static void assertContextEmpty(String error) {
71 for (ContextField key : ContextField.values()) {
72 assertNull(MDC.get(key.asKey()), error);