52a794f93759a2ae0d5865228e5d4bfa231d3747
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 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.openecomp.sdc.logging.spi.LoggingContextService;
21 import org.testng.annotations.DataProvider;
22
23 import java.util.concurrent.Callable;
24
25 /**
26  * @author evitaliy
27  * @since 08 Jan 18
28  */
29 public abstract class BaseContextPropagationTest {
30
31     // Disable if an old version of logback implementation is being used.
32     // Context propagation should be used when ctx is not propagated to child threads.
33     // See https://jira.qos.ch/browse/LOGBACK-422 and https://jira.qos.ch/browse/LOGBACK-624
34     static final boolean ENABLED = false;
35
36     static final String PROVIDER = "context";
37
38     static final String EXPECT_PROPAGATED_TO_CHILD = "Expected the data to be propagated to the child thread's context";
39     static final String EXPECT_RETAINED_IN_CURRENT = "Expected the data to be retained in this thread";
40     static final String EXPECT_REPLACED_WITH_STORED = "Expected context data to be replaced with stored data";
41     static final String EXPECT_INNER_RUN = "Expected the inner thread to run";
42     static final String EXPECT_OUTER_RUN = "Expected the outer thread to run";
43     static final String EXPECT_NOT_COPIED = "Expected context data not to be copied to this thread";
44     static final String EXPECT_RETAINED_IN_PARENT = "Expected context data to be retained in parent thread";
45     static final String EXPECT_POPULATED = "Expected context data to be populated in this thread";
46     static final String EXPECT_EMPTY = "Expected context data to be empty";
47     static final String EXPECT_REMAIN_EMPTY = "Expected context data to remain empty in this thread";
48     static final String EXPECT_REVERTED_ON_EXCEPTION = "Expected context data to be reverted even in case of exception";
49     static final String EXPECT_EXCEPTION_FROM_INNER = "Expected the inner class to throw exception";
50
51     @DataProvider(name = PROVIDER)
52     public static Object[][] contextServices() {
53         // try both directly call the implementation and get it via the binding
54         return new Object[][]{
55             {new SLF4JLoggingServiceProvider()},
56             {new LoggingContextAdaptor()}
57         };
58     }
59
60     private static class LoggingContextAdaptor implements LoggingContextService {
61
62         @Override
63         public void putRequestId(String requestId) {
64             LoggingContext.putRequestId(requestId);
65         }
66
67         @Override
68         public void putServiceName(String serviceName) {
69             LoggingContext.putServiceName(serviceName);
70         }
71
72         @Override
73         public void putPartnerName(String partnerName) {
74             LoggingContext.putPartnerName(partnerName);
75         }
76
77         @Override
78         public void clear() {
79             LoggingContext.clear();
80         }
81
82         @Override
83         public Runnable copyToRunnable(Runnable runnable) {
84             return LoggingContext.copyToRunnable(runnable);
85         }
86
87         @Override
88         public <V> Callable<V> copyToCallable(Callable<V> callable) {
89             return LoggingContext.copyToCallable(callable);
90         }
91
92         @Override
93         public String toString() {
94             return this.getClass().getName();
95         }
96     }
97 }