Increase code coverage on aai-common: aai-els-onap-logging library
[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 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.PARTNER_NAME, is("X-ONAP-PartnerName"));
64     }
65
66     @Test
67     public void testMarkers() {
68         assertThat(ONAPLogConstants.Markers.ENTRY.toString(), is("ENTRY"));
69         assertThat(ONAPLogConstants.Markers.EXIT.toString(), is("EXIT"));
70         assertThat(ONAPLogConstants.Markers.INVOKE.toString(), is("INVOKE"));
71         assertThat(ONAPLogConstants.Markers.INVOKE_ASYNCHRONOUS.toString(), is("INVOKE [ ASYNCHRONOUS ]"));
72         assertThat(ONAPLogConstants.Markers.INVOKE_SYNCHRONOUS.toString(), is("INVOKE [ SYNCHRONOUS ]"));
73     }
74
75     @Test
76     public void testInvocationMode() {
77         assertThat(ONAPLogConstants.InvocationMode.SYNCHRONOUS.getMarker(),
78                 is(ONAPLogConstants.Markers.INVOKE_SYNCHRONOUS));
79         assertThat(ONAPLogConstants.InvocationMode.ASYNCHRONOUS.getMarker(),
80                 is(ONAPLogConstants.Markers.INVOKE_ASYNCHRONOUS));
81     }
82
83     @Test
84     public void testInvocationModeToString() {
85         assertThat(ONAPLogConstants.InvocationMode.SYNCHRONOUS.toString(),
86                 is("SYNCHRONOUS"));
87     }
88
89     @Test
90     public void testResponseStatus() {
91         assertThat(ONAPLogConstants.ResponseStatus.COMPLETE.toString(), is("COMPLETE"));
92         assertThat(ONAPLogConstants.ResponseStatus.ERROR.toString(), is("ERROR"));
93     }
94
95     @Test
96     public void testMDCs() {
97
98         assertThat(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS.toString(), is("ClientIPAddress"));
99         assertThat(ONAPLogConstants.MDCs.SERVER_FQDN.toString(), is("ServerFQDN"));
100
101         assertThat(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP.toString(), is("EntryTimestamp"));
102         assertThat(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP.toString(), is("InvokeTimestamp"));
103
104         assertThat(ONAPLogConstants.MDCs.REQUEST_ID.toString(), is("RequestID"));
105         assertThat(ONAPLogConstants.MDCs.INVOCATION_ID.toString(), is("InvocationID"));
106         assertThat(ONAPLogConstants.MDCs.PARTNER_NAME.toString(), is("PartnerName"));
107         assertThat(ONAPLogConstants.MDCs.INSTANCE_UUID.toString(), is("InstanceID"));
108         assertThat(ONAPLogConstants.MDCs.SERVICE_NAME.toString(), is("ServiceName"));
109         assertThat(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME.toString(), is("TargetServiceName"));
110
111     }
112
113     static void assertInaccessibleConstructor(final Class<?> c) throws Exception {
114         try {
115             c.getDeclaredConstructors()[0].newInstance();
116             Assert.fail("Should fail for hidden constructor.");
117         }
118         catch (final IllegalAccessException e) {
119
120         }
121
122         try {
123             final Constructor<?> constructor = c.getDeclaredConstructors()[0];
124             constructor.setAccessible(true);
125             constructor.newInstance();
126             Assert.fail("Should fail even when invoked.");
127         }
128         catch (final InvocationTargetException e) {
129             assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
130         }
131     }
132 }