Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-els-onap-logging / src / test / java / org / onap / aai / logging / LoggingContextTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.logging;
22
23 import static org.junit.Assert.*;
24
25 import java.util.concurrent.TimeUnit;
26
27 import org.junit.After;
28 import org.junit.Test;
29 import org.slf4j.MDC;
30
31 public class LoggingContextTest {
32
33     @After
34     public void cleanup() {
35         MDC.clear();
36     }
37
38     @Test
39     public void elapsedTimeTest() {
40         LoggingContext.elapsedTime(300, TimeUnit.MILLISECONDS);
41         assertEquals(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()), "300");
42         LoggingContext.init();
43         assertNull(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()));
44     }
45
46     @Test
47     public void stopWatchTest() {
48         LoggingContext.init();
49         assertFalse(LoggingContext.isStopWatchStarted());
50
51         LoggingContext.stopWatchStart();
52         assertTrue(LoggingContext.isStopWatchStarted());
53
54         double elapsedTime = LoggingContext.stopWatchStop();
55         assertFalse(LoggingContext.isStopWatchStarted());
56         assertTrue(elapsedTime > 0);
57     }
58
59     @Test
60     public void putClearTest() {
61         String testServiceName = "TEST-SVC-NAME";
62         LoggingContext.put(LoggingContext.LoggingField.SERVICE_NAME.toString(), testServiceName);
63         assertEquals(testServiceName, MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()));
64
65         LoggingContext.clear();
66         assertNull(MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()));
67
68     }
69
70     @Test
71     public void removeTest() {
72         String testServiceName = "TEST-SVC-NAME";
73         LoggingContext.put(LoggingContext.LoggingField.SERVICE_NAME.toString(), testServiceName);
74         assertEquals(testServiceName, MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()));
75
76         LoggingContext.remove(LoggingContext.LoggingField.SERVICE_NAME.toString());
77         assertNull(MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()));
78
79     }
80
81 }