Minor refactor of logging API
[sdc.git] / openecomp-be / lib / openecomp-sdc-logging-lib / openecomp-sdc-logging-core / src / test / java / org / openecomp / sdc / logging / slf4j / BaseContextPropagationTest.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 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 ctx implementation is being used.
32     // ctxPropagationFactory 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     static final String KEY = "test-data";
38
39     static final String EXPECT_PROPAGATED_TO_CHILD = "Expected the data to be propagated to the child thread's context";
40     static final String EXPECT_RETAINED_IN_CURRENT = "Expected the data to be retained in this thread";
41     static final String EXPECT_REPLACED_WITH_STORED = "Expected context data to be replaced with stored data";
42     static final String EXPECT_INNER_RUN = "Expected the inner thread to run";
43     static final String EXPECT_OUTER_RUN = "Expected the outer thread to run";
44     static final String EXPECT_NOT_COPIED = "Expected context data not to be copied to this thread";
45     static final String EXPECT_RETAINED_IN_PARENT = "Expected context data to be retained in parent thread";
46     static final String EXPECT_POPULATED = "Expected context data to be populated in this thread";
47     static final String EXPECT_EMPTY = "Expected context data to be empty";
48     static final String EXPECT_REMAIN_EMPTY = "Expected context data to remain empty in this thread";
49     static final String EXPECT_REVERTED_ON_EXCEPTION = "Expected context data to be reverted even in case of exception";
50     static final String EXPECT_EXCEPTION_FROM_INNER = "Expected the inner class to throw exception";
51
52     @DataProvider(name = PROVIDER)
53     public static Object[][] contextServices() {
54         // try both directly call the implementation and get it via the binding
55         return new Object[][] {
56                 { new SLF4JLoggingServiceProvider() },
57                 { new LoggingContextAdaptor() }
58         };
59     }
60
61     private static class LoggingContextAdaptor implements LoggingContextService {
62
63         @Override
64         public void put(String key, String value) {
65             LoggingContext.put(key, value);
66         }
67
68         @Override
69         public String get(String key) {
70             return LoggingContext.get(key);
71         }
72
73         @Override
74         public void remove(String key) {
75             LoggingContext.remove(key);
76         }
77
78         @Override
79         public void clear() {
80             LoggingContext.clear();
81         }
82
83         @Override
84         public Runnable copyToRunnable(Runnable runnable) {
85             return LoggingContext.copyToRunnable(runnable);
86         }
87
88         @Override
89         public <V> Callable<V> copyToCallable(Callable<V> callable) {
90             return LoggingContext.copyToCallable(callable);
91         }
92
93         @Override
94         public String toString() {
95             return this.getClass().getName();
96         }
97     }
98 }