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