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 static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.core.Is.is;
26 import static org.hamcrest.core.IsInstanceOf.instanceOf;
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.InvocationTargetException;
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
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.");
54 } catch (final InvocationTargetException e) {
55 assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
60 public void testHeaders() {
61 assertThat(ONAPLogConstants.Headers.REQUEST_ID, is("X-ONAP-RequestID"));
62 assertThat(ONAPLogConstants.Headers.PARTNER_NAME, is("X-ONAP-PartnerName"));
66 public void testMarkers() {
67 assertThat(ONAPLogConstants.Markers.ENTRY.toString(), is("ENTRY"));
68 assertThat(ONAPLogConstants.Markers.EXIT.toString(), is("EXIT"));
69 assertThat(ONAPLogConstants.Markers.INVOKE.toString(), is("INVOKE"));
70 assertThat(ONAPLogConstants.Markers.INVOKE_ASYNCHRONOUS.toString(), is("INVOKE [ ASYNCHRONOUS ]"));
71 assertThat(ONAPLogConstants.Markers.INVOKE_SYNCHRONOUS.toString(), is("INVOKE [ SYNCHRONOUS ]"));
75 public void testInvocationMode() {
76 assertThat(ONAPLogConstants.InvocationMode.SYNCHRONOUS.getMarker(),
77 is(ONAPLogConstants.Markers.INVOKE_SYNCHRONOUS));
78 assertThat(ONAPLogConstants.InvocationMode.ASYNCHRONOUS.getMarker(),
79 is(ONAPLogConstants.Markers.INVOKE_ASYNCHRONOUS));
83 public void testInvocationModeToString() {
84 assertThat(ONAPLogConstants.InvocationMode.SYNCHRONOUS.toString(), is("SYNCHRONOUS"));
88 public void testResponseStatus() {
89 assertThat(ONAPLogConstants.ResponseStatus.COMPLETE.toString(), is("COMPLETE"));
90 assertThat(ONAPLogConstants.ResponseStatus.ERROR.toString(), is("ERROR"));
94 public void testMDCs() {
96 assertThat(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS.toString(), is("ClientIPAddress"));
97 assertThat(ONAPLogConstants.MDCs.SERVER_FQDN.toString(), is("ServerFQDN"));
99 assertThat(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP.toString(), is("EntryTimestamp"));
100 assertThat(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP.toString(), is("InvokeTimestamp"));
102 assertThat(ONAPLogConstants.MDCs.REQUEST_ID.toString(), is("RequestID"));
103 assertThat(ONAPLogConstants.MDCs.INVOCATION_ID.toString(), is("InvocationID"));
104 assertThat(ONAPLogConstants.MDCs.PARTNER_NAME.toString(), is("PartnerName"));
105 assertThat(ONAPLogConstants.MDCs.INSTANCE_UUID.toString(), is("InstanceID"));
106 assertThat(ONAPLogConstants.MDCs.SERVICE_NAME.toString(), is("ServiceName"));
107 assertThat(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME.toString(), is("TargetServiceName"));
111 static void assertInaccessibleConstructor(final Class<?> c) throws Exception {
113 c.getDeclaredConstructors()[0].newInstance();
114 Assert.fail("Should fail for hidden constructor.");
115 } catch (final IllegalAccessException e) {
120 final Constructor<?> constructor = c.getDeclaredConstructors()[0];
121 constructor.setAccessible(true);
122 constructor.newInstance();
123 Assert.fail("Should fail even when invoked.");
124 } catch (final InvocationTargetException e) {
125 assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));