2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.logging.ref.slf4j;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
27 import org.testng.Assert;
28 import org.testng.annotations.Test;
30 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.hamcrest.core.Is.is;
32 import static org.hamcrest.core.IsInstanceOf.instanceOf;
35 * Tests for {@link ONAPLogConstants}.
37 public class ONAPLogConstantsTest {
40 public void testConstructors() throws Exception {
41 assertInaccessibleConstructor(ONAPLogConstants.class);
42 assertInaccessibleConstructor(ONAPLogConstants.MDCs.class);
43 assertInaccessibleConstructor(ONAPLogConstants.Markers.class);
44 assertInaccessibleConstructor(ONAPLogConstants.Headers.class);
48 public void testConstructorUnsupported() throws Exception {
50 Constructor<?> c = ONAPLogConstants.class.getDeclaredConstructors()[0];
51 c.setAccessible(true);
53 Assert.fail("Should fail for hidden constructor.");
55 catch (final InvocationTargetException e) {
56 assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
61 public void testHeaders() {
62 assertThat(ONAPLogConstants.Headers.REQUEST_ID, is("X-ONAP-RequestID"));
63 assertThat(ONAPLogConstants.Headers.INVOCATION_ID, is("X-ONAP-InvocationID"));
64 assertThat(ONAPLogConstants.Headers.PARTNER_NAME, is("X-ONAP-PartnerName"));
68 public void testMarkers() {
69 assertThat(ONAPLogConstants.Markers.ENTRY.toString(), is("ENTRY"));
70 assertThat(ONAPLogConstants.Markers.EXIT.toString(), is("EXIT"));
71 assertThat(ONAPLogConstants.Markers.INVOKE.toString(), is("INVOKE"));
72 assertThat(ONAPLogConstants.Markers.INVOKE_ASYNCHRONOUS.toString(), is("INVOKE [ ASYNCHRONOUS ]"));
73 assertThat(ONAPLogConstants.Markers.INVOKE_SYNCHRONOUS.toString(), is("INVOKE [ SYNCHRONOUS ]"));
77 public void testInvocationMode() {
78 assertThat(ONAPLogConstants.InvocationMode.SYNCHRONOUS.getMarker(),
79 is(ONAPLogConstants.Markers.INVOKE_SYNCHRONOUS));
80 assertThat(ONAPLogConstants.InvocationMode.ASYNCHRONOUS.getMarker(),
81 is(ONAPLogConstants.Markers.INVOKE_ASYNCHRONOUS));
85 public void testInvocationModeToString() {
86 assertThat(ONAPLogConstants.InvocationMode.SYNCHRONOUS.toString(),
91 public void testResponseStatus() {
92 assertThat(ONAPLogConstants.ResponseStatus.COMPLETE.toString(), is("COMPLETE"));
93 assertThat(ONAPLogConstants.ResponseStatus.ERROR.toString(), is("ERROR"));
97 public void testMDCs() {
99 assertThat(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS.toString(), is("ClientIPAddress"));
100 assertThat(ONAPLogConstants.MDCs.SERVER_FQDN.toString(), is("ServerFQDN"));
102 assertThat(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP.toString(), is("EntryTimestamp"));
103 assertThat(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP.toString(), is("InvokeTimestamp"));
105 assertThat(ONAPLogConstants.MDCs.REQUEST_ID.toString(), is("RequestID"));
106 assertThat(ONAPLogConstants.MDCs.INVOCATION_ID.toString(), is("InvocationID"));
107 assertThat(ONAPLogConstants.MDCs.PARTNER_NAME.toString(), is("PartnerName"));
108 assertThat(ONAPLogConstants.MDCs.INSTANCE_UUID.toString(), is("InstanceID"));
109 assertThat(ONAPLogConstants.MDCs.SERVICE_NAME.toString(), is("ServiceName"));
110 assertThat(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME.toString(), is("TargetServiceName"));
114 static void assertInaccessibleConstructor(final Class<?> c) throws Exception {
116 c.getDeclaredConstructors()[0].newInstance();
117 Assert.fail("Should fail for hidden constructor.");
119 catch (final IllegalAccessException e) {
124 final Constructor<?> constructor = c.getDeclaredConstructors()[0];
125 constructor.setAccessible(true);
126 constructor.newInstance();
127 Assert.fail("Should fail even when invoked.");
129 catch (final InvocationTargetException e) {
130 assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));