2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.logging;
24 import org.junit.Test;
28 import static org.junit.Assert.*;
30 public class LoggingContextTest {
32 private static final int MAX_STORED_CONTEXTS = 100;
35 public void testStopWatch() {
37 LoggingContext.stopWatchStop();
38 throw new AssertionError("No exception thrown when LoggingContext.stopWatchStop() called without a prior LoggingContext.stopWatchStart()");
39 } catch (StopWatchNotStartedException e) {
43 LoggingContext.stopWatchStart();
45 assertTrue(LoggingContext.stopWatchStop() >= 0);
48 LoggingContext.stopWatchStop();
49 throw new AssertionError("No exception thrown when LoggingContext.stopWatchStop() twice in succession");
50 } catch (StopWatchNotStartedException e) {
56 public void testRequestId() throws Exception { //AKA Transaction ID
57 final String sUuid = "57d51eaa-edc6-4f50-a69d-f2d4d2445120";
59 LoggingContext.requestId(sUuid);
61 assertEquals(LoggingContext.requestId(), UUID.fromString(sUuid));
63 final UUID uuid = UUID.randomUUID();
65 LoggingContext.requestId(uuid);
67 assertEquals(LoggingContext.requestId(), uuid);
69 LoggingContext.requestId("foo"); //Illegal - this will result in a new, randomly
70 //generated UUID as per the logging spec
72 assertNotNull(LoggingContext.requestId()); //Make sure ANY UUID was assigned
73 assertNotEquals(LoggingContext.requestId(), uuid); //Make sure it actually changed from the last
78 public void testClear() {
79 LoggingContext.init();
80 LoggingContext.clear();
82 assertEquals(Collections.emptyMap(), LoggingContext.getCopy());
86 public void testSaveRestore() {
88 final Deque<Map<String, String>> contexts = new LinkedList<Map<String, String>> ();
90 LoggingContext.init();
92 for (int i = 0; i < MAX_STORED_CONTEXTS; i++) {
93 LoggingContext.customField1(String.valueOf(i));
95 assertEquals(LoggingContext.customField1(), String.valueOf(i));
97 LoggingContext.save();
99 contexts.push(LoggingContext.getCopy());
102 while (contexts.peek() != null) {
103 LoggingContext.restore();
105 assertEquals(LoggingContext.getCopy(), contexts.pop());