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 org.junit.Test;
27 import static org.junit.Assert.*;
29 public class LoggingContextTest {
31 private static final int MAX_STORED_CONTEXTS = 100;
34 public void testStopWatch() {
36 LoggingContext.stopWatchStop();
37 throw new AssertionError("No exception thrown when LoggingContext.stopWatchStop() called without a prior LoggingContext.stopWatchStart()");
38 } catch (StopWatchNotStartedException e) {
42 LoggingContext.stopWatchStart();
44 assertTrue(LoggingContext.stopWatchStop() >= 0);
47 LoggingContext.stopWatchStop();
48 throw new AssertionError("No exception thrown when LoggingContext.stopWatchStop() twice in succession");
49 } catch (StopWatchNotStartedException e) {
55 public void testRequestId() { //AKA Transaction ID
56 final String sUuid = "57d51eaa-edc6-4f50-a69d-f2d4d2445120";
58 LoggingContext.requestId(sUuid);
60 assertEquals(LoggingContext.requestId(), UUID.fromString(sUuid));
62 final UUID uuid = UUID.randomUUID();
64 LoggingContext.requestId(uuid);
66 assertEquals(LoggingContext.requestId(), uuid);
68 LoggingContext.requestId("foo"); //Illegal - this will result in a new, randomly
69 //generated UUID as per the logging spec
71 assertNotNull(LoggingContext.requestId()); //Make sure ANY UUID was assigned
72 assertNotEquals(LoggingContext.requestId(), uuid); //Make sure it actually changed from the last
77 public void testClear() {
78 LoggingContext.init();
79 LoggingContext.clear();
81 assertEquals(Collections.emptyMap(), LoggingContext.getCopy());
85 public void testSaveRestore() {
87 final Deque<Map<String, String>> contexts = new LinkedList<Map<String, String>> ();
89 LoggingContext.init();
91 for (int i = 0; i < MAX_STORED_CONTEXTS; i++) {
92 LoggingContext.customField1(String.valueOf(i));
94 assertEquals(LoggingContext.customField1(), String.valueOf(i));
96 LoggingContext.save();
98 contexts.push(LoggingContext.getCopy());
101 while (contexts.peek() != null) {
102 LoggingContext.restore();
104 assertEquals(LoggingContext.getCopy(), contexts.pop());