2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.aai.logging;
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;
28 import java.util.Collections;
29 import java.util.Deque;
30 import java.util.LinkedList;
32 import java.util.UUID;
34 import org.junit.Test;
36 public class LoggingContextTest {
38 private static final int MAX_STORED_CONTEXTS = 100;
41 public void testStopWatch() {
43 LoggingContext.stopWatchStop();
44 throw new AssertionError("No exception thrown when LoggingContext.stopWatchStop() called without a prior LoggingContext.stopWatchStart()");
45 } catch (StopWatchNotStartedException e) {
49 LoggingContext.stopWatchStart();
51 assertTrue(LoggingContext.stopWatchStop() >= 0);
54 LoggingContext.stopWatchStop();
55 throw new AssertionError("No exception thrown when LoggingContext.stopWatchStop() twice in succession");
56 } catch (StopWatchNotStartedException e) {
62 public void testRequestId() { //AKA Transaction ID
63 final String sUuid = "57d51eaa-edc6-4f50-a69d-f2d4d2445120";
65 LoggingContext.requestId(sUuid);
67 assertEquals(LoggingContext.requestId(), UUID.fromString(sUuid));
69 final UUID uuid = UUID.randomUUID();
71 LoggingContext.requestId(uuid);
73 assertEquals(LoggingContext.requestId(), uuid);
75 LoggingContext.requestId("foo"); //Illegal - this will result in a new, randomly
76 //generated UUID as per the logging spec
78 assertNotNull(LoggingContext.requestId()); //Make sure ANY UUID was assigned
79 assertNotEquals(LoggingContext.requestId(), uuid); //Make sure it actually changed from the last
84 public void testClear() {
85 LoggingContext.init();
86 LoggingContext.clear();
88 assertEquals(Collections.emptyMap(), LoggingContext.getCopy());
92 public void testSaveRestore() {
94 final Deque<Map<String, String>> contexts = new LinkedList<Map<String, String>> ();
96 LoggingContext.init();
98 for (int i = 0; i < MAX_STORED_CONTEXTS; i++) {
99 LoggingContext.customField1(String.valueOf(i));
101 assertEquals(LoggingContext.customField1(), String.valueOf(i));
103 LoggingContext.save();
105 contexts.push(LoggingContext.getCopy());
108 while (contexts.peek() != null) {
109 LoggingContext.restore();
111 assertEquals(LoggingContext.getCopy(), contexts.pop());