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