Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-els-onap-logging / 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 static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.core.Is.is;
26 import static org.hamcrest.core.IsInstanceOf.instanceOf;
27
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.InvocationTargetException;
30
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
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         } catch (final InvocationTargetException e) {
55             assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
56         }
57     }
58
59     @Test
60     public void testHeaders() {
61         assertThat(ONAPLogConstants.Headers.REQUEST_ID, is("X-ONAP-RequestID"));
62         assertThat(ONAPLogConstants.Headers.PARTNER_NAME, is("X-ONAP-PartnerName"));
63     }
64
65     @Test
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 ]"));
72     }
73
74     @Test
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));
80     }
81
82     @Test
83     public void testInvocationModeToString() {
84         assertThat(ONAPLogConstants.InvocationMode.SYNCHRONOUS.toString(), is("SYNCHRONOUS"));
85     }
86
87     @Test
88     public void testResponseStatus() {
89         assertThat(ONAPLogConstants.ResponseStatus.COMPLETE.toString(), is("COMPLETE"));
90         assertThat(ONAPLogConstants.ResponseStatus.ERROR.toString(), is("ERROR"));
91     }
92
93     @Test
94     public void testMDCs() {
95
96         assertThat(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS.toString(), is("ClientIPAddress"));
97         assertThat(ONAPLogConstants.MDCs.SERVER_FQDN.toString(), is("ServerFQDN"));
98
99         assertThat(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP.toString(), is("EntryTimestamp"));
100         assertThat(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP.toString(), is("InvokeTimestamp"));
101
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"));
108
109     }
110
111     static void assertInaccessibleConstructor(final Class<?> c) throws Exception {
112         try {
113             c.getDeclaredConstructors()[0].newInstance();
114             Assert.fail("Should fail for hidden constructor.");
115         } catch (final IllegalAccessException e) {
116
117         }
118
119         try {
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));
126         }
127     }
128 }