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