Increase code coverage on aai-common: aai-els-onap-logging library
[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 org.junit.After;
24 import org.junit.Test;
25 import org.slf4j.MDC;
26
27 import java.util.concurrent.TimeUnit;
28
29 import static org.junit.Assert.*;
30
31 public class LoggingContextTest {
32
33     @After
34     public void cleanup(){
35         MDC.clear();
36     }
37     @Test
38     public void elapsedTimeTest() {
39         LoggingContext.elapsedTime(300, TimeUnit.MILLISECONDS);
40         assertEquals(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()), "300");
41         LoggingContext.init();
42         assertTrue(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()) == null);
43     }
44     @Test
45     public void stopWatchTest() {
46         LoggingContext.init();
47         assertFalse(LoggingContext.isStopWatchStarted());
48
49         LoggingContext.stopWatchStart();
50         assertTrue(LoggingContext.isStopWatchStarted());
51
52         double elapsedTime = LoggingContext.stopWatchStop();
53         assertFalse(LoggingContext.isStopWatchStarted());
54         assertTrue(elapsedTime > 0);
55     }
56     @Test
57     public void putClearTest() {
58         String testServiceName = "TEST-SVC-NAME";
59         LoggingContext.put(LoggingContext.LoggingField.SERVICE_NAME.toString(), testServiceName);
60         assertEquals(testServiceName, MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()));
61
62         LoggingContext.clear();
63         assertTrue(MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()) == null);
64
65     }
66
67     @Test
68     public void removeTest() {
69         String testServiceName = "TEST-SVC-NAME";
70         LoggingContext.put(LoggingContext.LoggingField.SERVICE_NAME.toString(), testServiceName);
71         assertEquals(testServiceName, MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()));
72
73         LoggingContext.remove(LoggingContext.LoggingField.SERVICE_NAME.toString());
74         assertTrue(MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()) == null);
75
76     }
77
78 }