681b324d4a0a9e89ccd5da8e332f960a2f6b380d
[logging-analytics.git] / reference / logging-slf4j / src / test / java / org / onap / logging / ref / slf4j / ONAPLogConstantsTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.logging
4  * ================================================================================
5  * Copyright © 2018 Amdocs
6  * All rights reserved.
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
11  *
12  *    http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.logging.ref.slf4j;
23
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
26
27 import org.testng.Assert;
28 import org.testng.annotations.Test;
29
30 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.hamcrest.core.Is.is;
32 import static org.hamcrest.core.IsInstanceOf.instanceOf;
33
34 /**
35  * Tests for {@link ONAPLogConstants}.
36  */
37 public class ONAPLogConstantsTest {
38
39     @Test
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);
45     }
46
47     @Test
48     public void testConstructorUnsupported() throws Exception {
49         try {
50             Constructor<?> c = ONAPLogConstants.class.getDeclaredConstructors()[0];
51             c.setAccessible(true);
52             c.newInstance();
53             Assert.fail("Should fail for hidden constructor.");
54         }
55         catch (final InvocationTargetException e) {
56             assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
57         }
58     }
59
60     @Test
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"));
65     }
66
67     @Test
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 ]"));
74     }
75
76     @Test
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));
82     }
83
84     @Test
85     public void testInvocationModeToString() {
86         assertThat(ONAPLogConstants.InvocationMode.SYNCHRONOUS.toString(),
87                 is("SYNCHRONOUS"));
88     }
89
90     @Test
91     public void testResponseStatus() {
92         assertThat(ONAPLogConstants.ResponseStatus.COMPLETE.toString(), is("COMPLETE"));
93         assertThat(ONAPLogConstants.ResponseStatus.ERROR.toString(), is("ERROR"));
94     }
95
96     @Test
97     public void testMDCs() {
98
99         assertThat(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS.toString(), is("ClientIPAddress"));
100         assertThat(ONAPLogConstants.MDCs.SERVER_FQDN.toString(), is("ServerFQDN"));
101
102         assertThat(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP.toString(), is("EntryTimestamp"));
103         assertThat(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP.toString(), is("InvokeTimestamp"));
104
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"));
111
112     }
113
114     static void assertInaccessibleConstructor(final Class<?> c) throws Exception {
115         try {
116             c.getDeclaredConstructors()[0].newInstance();
117             Assert.fail("Should fail for hidden constructor.");
118         }
119         catch (final IllegalAccessException e) {
120
121         }
122
123         try {
124             final Constructor<?> constructor = c.getDeclaredConstructors()[0];
125             constructor.setAccessible(true);
126             constructor.newInstance();
127             Assert.fail("Should fail even when invoked.");
128         }
129         catch (final InvocationTargetException e) {
130             assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
131         }
132     }
133 }