743477306f59b2d1a7cc7707bde6a841029a8021
[aai/aai-common.git] / aai-core / 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.*;
26
27 import org.junit.Test;
28
29 public class LoggingContextTest {
30
31     private static final int MAX_STORED_CONTEXTS = 100;
32
33     @Test
34     public void testStopWatch() {
35         try {
36             LoggingContext.stopWatchStop();
37             throw new AssertionError(
38                     "No exception thrown when LoggingContext.stopWatchStop() called without a prior LoggingContext.stopWatchStart()");
39         } catch (StopWatchNotStartedException e) {
40             // Expected
41         }
42
43         LoggingContext.stopWatchStart();
44
45         assertTrue(LoggingContext.stopWatchStop() >= 0);
46
47         try {
48             LoggingContext.stopWatchStop();
49             throw new AssertionError("No exception thrown when LoggingContext.stopWatchStop() twice in succession");
50         } catch (StopWatchNotStartedException e) {
51             // Expected
52         }
53     }
54
55     @Test
56     public void testRequestId() { // AKA Transaction ID
57         final String sUuid = "57d51eaa-edc6-4f50-a69d-f2d4d2445120";
58
59         LoggingContext.requestId(sUuid);
60
61         assertEquals(LoggingContext.requestId(), UUID.fromString(sUuid));
62
63         final UUID uuid = UUID.randomUUID();
64
65         LoggingContext.requestId(uuid);
66
67         assertEquals(LoggingContext.requestId(), uuid);
68
69         LoggingContext.requestId("foo"); // Illegal - this will result in a new, randomly
70                                          // generated UUID as per the logging spec
71
72         assertNotNull(LoggingContext.requestId()); // Make sure ANY UUID was assigned
73         assertNotEquals(LoggingContext.requestId(), uuid); // Make sure it actually changed from the last
74                                                            // known valid UUID
75     }
76
77     @Test
78     public void testClear() {
79         LoggingContext.init();
80         LoggingContext.clear();
81
82         assertEquals(Collections.emptyMap(), LoggingContext.getCopy());
83     }
84
85     @Test
86     public void testSaveRestore() {
87
88         final Deque<Map<String, String>> contexts = new LinkedList<Map<String, String>>();
89
90         LoggingContext.init();
91
92         for (int i = 0; i < MAX_STORED_CONTEXTS; i++) {
93             LoggingContext.customField1(String.valueOf(i));
94
95             assertEquals(LoggingContext.customField1(), String.valueOf(i));
96
97             LoggingContext.save();
98
99             contexts.push(LoggingContext.getCopy());
100         }
101
102         while (contexts.peek() != null) {
103             LoggingContext.restore();
104
105             assertEquals(LoggingContext.getCopy(), contexts.pop());
106         }
107     }
108 }