2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.aai.logging;
22 import org.junit.Test;
26 import static org.junit.Assert.*;
28 public class LoggingContextTest {
30 private static final int MAX_STORED_CONTEXTS = 100;
33 public void testStopWatch() {
35 LoggingContext.stopWatchStop();
36 throw new AssertionError("No exception thrown when LoggingContext.stopWatchStop() called without a prior LoggingContext.stopWatchStart()");
37 } catch (StopWatchNotStartedException e) {
41 LoggingContext.stopWatchStart();
43 assertTrue(LoggingContext.stopWatchStop() >= 0);
46 LoggingContext.stopWatchStop();
47 throw new AssertionError("No exception thrown when LoggingContext.stopWatchStop() twice in succession");
48 } catch (StopWatchNotStartedException e) {
54 public void testRequestId() { //AKA Transaction ID
55 final String sUuid = "57d51eaa-edc6-4f50-a69d-f2d4d2445120";
57 LoggingContext.requestId(sUuid);
59 assertEquals(LoggingContext.requestId(), UUID.fromString(sUuid));
61 final UUID uuid = UUID.randomUUID();
63 LoggingContext.requestId(uuid);
65 assertEquals(LoggingContext.requestId(), uuid);
67 LoggingContext.requestId("foo"); //Illegal - this will result in a new, randomly
68 //generated UUID as per the logging spec
70 assertNotNull(LoggingContext.requestId()); //Make sure ANY UUID was assigned
71 assertNotEquals(LoggingContext.requestId(), uuid); //Make sure it actually changed from the last
76 public void testClear() {
77 LoggingContext.init();
78 LoggingContext.clear();
80 assertEquals(Collections.emptyMap(), LoggingContext.getCopy());
84 public void testSaveRestore() {
86 final Deque<Map<String, String>> contexts = new LinkedList<Map<String, String>> ();
88 LoggingContext.init();
90 for (int i = 0; i < MAX_STORED_CONTEXTS; i++) {
91 LoggingContext.customField1(String.valueOf(i));
93 assertEquals(LoggingContext.customField1(), String.valueOf(i));
95 LoggingContext.save();
97 contexts.push(LoggingContext.getCopy());
100 while (contexts.peek() != null) {
101 LoggingContext.restore();
103 assertEquals(LoggingContext.getCopy(), contexts.pop());