50a2d053fbbb5348f29777734b32cc3ca2f340c3
[aai/aai-common.git] / aai-els-onap-logging / src / test / java / org / onap / aai / logging / StopWatchTest.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 static java.lang.Thread.sleep;
28 import static org.junit.Assert.*;
29
30 public class StopWatchTest {
31
32     @After
33     public void cleanup() {
34         MDC.clear();
35     }
36     @Test
37     public void elapsedTimeTest() throws InterruptedException {
38         StopWatch.start();
39         sleep(1010);
40         StopWatch.stop();
41         assertTrue(Long.parseLong(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString())) >= 1000L);
42     }
43     @Test
44     public void elapsedTimeConditionalTest() throws InterruptedException {
45         StopWatch.conditionalStart();
46         sleep(1010);
47         StopWatch.stopIfStarted();
48         String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
49         long elapsedTime = Long.parseLong(elapsedTimeStr);
50         assertTrue(elapsedTime >= 1000L);
51     }
52     @Test
53     public void clearTest() throws InterruptedException {
54         StopWatch.start();
55         sleep(1010);
56         StopWatch.stop();
57         assertNotNull( MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()));
58
59         StopWatch.clear();
60         assertNull(MDC.get(LoggingContext.LoggingField.STOP_WATCH_START.toString()));
61         assertNull(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()));
62
63     }
64
65     @Test
66     public void stopTest() throws InterruptedException {
67         StopWatch.start();
68         sleep(1010);
69         StopWatch.stop();
70
71         String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
72         long elapsedTime1 = Long.parseLong(elapsedTimeStr);
73
74         StopWatch.stopIfStarted();
75         elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
76         long elapsedTime2 = Long.parseLong(elapsedTimeStr);
77         assertTrue(elapsedTime1 == elapsedTime2);
78     }
79
80     @Test
81     public void startTest() throws InterruptedException {
82         StopWatch.start();
83         sleep(1010);
84         StopWatch.conditionalStart();
85         StopWatch.stop();
86
87         String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
88         long elapsedTime = Long.parseLong(elapsedTimeStr);
89
90         assertTrue(elapsedTime >= 1000L);
91     }
92
93 }