7899eebd51f0c533d33b912cbfa470546b79d9d4
[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.api;
18
19 import org.testng.annotations.Test;
20
21 import java.lang.reflect.Field;
22 import java.util.concurrent.Callable;
23
24 import static org.testng.Assert.assertEquals;
25 import static org.testng.Assert.assertNull;
26 import static org.testng.Assert.assertTrue;
27
28 /**
29  * @author EVITALIY
30  * @since 08 Jan 18
31  */
32 public class LoggingContextTest {
33
34     @Test
35     public void shouldHoldNoOpWhenNoBinding() throws Exception {
36         Field factory = LoggingContext.class.getDeclaredField("SERVICE");
37         factory.setAccessible(true);
38         Object impl = factory.get(null);
39         assertEquals(impl.getClass().getName(),
40                 "org.openecomp.sdc.logging.api.LoggingContext$NoOpLoggingContextService");
41     }
42
43     @Test
44     public void putDoesNotHaveEffectWhenNoBinding() {
45         final String key = "Key";
46         LoggingContext.put(key, "Dummy");
47         assertNull(LoggingContext.get(key));
48     }
49
50     @Test(expectedExceptions = NullPointerException.class)
51     public void throwNpeWhenPutWithKeyNull() {
52         LoggingContext.put(null, "value");
53     }
54
55     @Test
56     public void getAlwaysReturnsNull() {
57         assertNull(LoggingContext.get("GetKey"));
58     }
59
60     @Test(expectedExceptions = NullPointerException.class)
61     public void throwNpeWhenGetWithKeyNull() {
62         LoggingContext.get(null);
63     }
64
65     @Test
66     public void removeDoesNotFail() {
67         LoggingContext.remove("RemoveKey");
68     }
69
70     @Test(expectedExceptions = NullPointerException.class)
71     public void throwNpWhenRemoveWithKeyNull() {
72         LoggingContext.remove(null);
73     }
74
75     @Test
76     public void clearDoesNotFail() {
77         LoggingContext.clear();
78     }
79
80     @Test
81     public void toRunnableReturnsSameInstance() {
82         Runnable test = () -> { /* do nothing */ };
83         assertTrue(test == LoggingContext.toRunnable(test));
84     }
85
86     @Test(expectedExceptions = NullPointerException.class)
87     public void throwNpeWhenToRunnableWithNull() {
88         LoggingContext.toRunnable(null);
89     }
90
91     @Test
92     public void toCallableReturnsSameInstance() {
93         Callable<String> test = () -> "";
94         assertTrue(test == LoggingContext.toCallable(test));
95     }
96
97     @Test(expectedExceptions = NullPointerException.class)
98     public void throwNpeWhenToCallableWithNull() {
99         LoggingContext.toCallable(null);
100     }
101 }