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