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.common;
24 import java.util.HashMap;
26 import java.util.UUID;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import org.slf4j.event.Level;
32 import org.springframework.mock.web.MockHttpServletRequest;
33 import org.testng.Assert;
34 import org.testng.annotations.AfterMethod;
35 import org.testng.annotations.Test;
37 import static org.hamcrest.MatcherAssert.assertThat;
38 import static org.hamcrest.core.Is.is;
39 import static org.hamcrest.core.IsNull.notNullValue;
40 import static org.hamcrest.core.IsNull.nullValue;
43 * Tests for {@link ONAPLogAdapter}.
45 public class ONAPLogAdapterTest {
48 * Ensure that MDCs are cleared after each testcase.
51 public void resetMDCs() {
59 public void testCheckNotNull() {
61 ONAPLogAdapter.checkNotNull("");
64 ONAPLogAdapter.checkNotNull(null);
65 Assert.fail("Should throw NullPointerException");
67 catch (final NullPointerException e) {
73 * Test defaulting of nulls.
76 public void testDefaultToEmpty() {
77 assertThat(ONAPLogAdapter.defaultToEmpty("123"), is("123"));
78 assertThat(ONAPLogAdapter.defaultToEmpty(Integer.valueOf(1984)), is("1984"));
79 assertThat(ONAPLogAdapter.defaultToEmpty(null), is(""));
83 * Test defaulting of nulls.
86 public void testDefaultToUUID() {
87 assertThat(ONAPLogAdapter.defaultToUUID("123"), is("123"));
88 UUID.fromString(ONAPLogAdapter.defaultToUUID(null));
95 public void testEntering() {
97 final Logger logger = LoggerFactory.getLogger(this.getClass());
98 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
99 final MockHttpServletRequest http = new MockHttpServletRequest();
100 http.setRequestURI("uri123");
101 http.setServerName("local123");
102 http.setRemoteAddr("remote123");
103 http.addHeader("X-ONAP-RequestID", "request123");
104 http.addHeader("X-ONAP-InvocationID", "invocation123");
105 http.addHeader("X-ONAP-PartnerName", "partner123");
108 adapter.getServiceDescriptor().setServiceName("uri123");
109 adapter.entering(http);
110 final Map<String, String> mdcs = MDC.getCopyOfContextMap();
111 assertThat(mdcs.get("RequestID"), is("request123"));
112 assertThat(mdcs.get("InvocationID"), is("invocation123"));
113 assertThat(mdcs.get("PartnerName"), is("partner123"));
114 assertThat(mdcs.get("ServiceName"), is("uri123"));
115 assertThat(mdcs.get("ServerFQDN"), is("local123"));
116 assertThat(mdcs.get("ClientIPAddress"), is("remote123"));
127 public void testExiting() {
129 final Logger logger = LoggerFactory.getLogger(this.getClass());
130 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
133 MDC.put("somekey", "somevalue");
134 assertThat(MDC.get("somekey"), is("somevalue"));
136 assertThat(MDC.get("somekey"), nullValue());
147 public void testInvokeSyncAsyncNull() {
149 final Logger logger = LoggerFactory.getLogger(this.getClass());
150 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
152 final UUID syncUUID = adapter.invoke(ONAPLogConstants.InvocationMode.SYNCHRONOUS);
153 assertThat(syncUUID, notNullValue());
155 final UUID asyncUUID = adapter.invoke(ONAPLogConstants.InvocationMode.SYNCHRONOUS);
156 assertThat(asyncUUID, notNullValue());
158 final UUID agnosticUUID = adapter.invoke((ONAPLogConstants.InvocationMode)null);
159 assertThat(agnosticUUID, notNullValue());
164 * Test INVOKE, with RequestAdapter.
167 public void testInvokeWithAdapter() throws Exception {
169 final Logger logger = LoggerFactory.getLogger(this.getClass());
170 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
172 final Map<String, String> headers = new HashMap<>();
173 final ONAPLogAdapter.RequestBuilder builder = new ONAPLogAdapter.RequestBuilder<ONAPLogAdapter.RequestBuilder>() {
175 public ONAPLogAdapter.RequestBuilder setHeader(final String name, final String value) {
176 headers.put(name, value);
182 final UUID uuid = adapter.invoke(builder, ONAPLogConstants.InvocationMode.SYNCHRONOUS);
183 assertThat(uuid, notNullValue());
184 assertThat(headers.get(ONAPLogConstants.Headers.INVOCATION_ID), is(uuid.toString()));
185 assertThat(headers.containsKey(ONAPLogConstants.Headers.PARTNER_NAME), is(true));
186 assertThat(headers.containsKey(ONAPLogConstants.Headers.REQUEST_ID), is(true));
194 * Exercise the contract, for a caller that's happy to have their
195 * service name automatically derived. (This validates nothing
196 * and achieves nothing; it's just to provide an example of minimal usage).
199 public void testContract() {
201 // Note no wrapper around HttpServletRequest, which will work for
202 // most invocations (since they come via HTTP), but otherwise
203 // can implement your own RequestAdapter.
205 final Logger logger = LoggerFactory.getLogger(this.getClass());
206 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
207 final MockHttpServletRequest http = new MockHttpServletRequest();
209 // Immediately log ENTERING marker, with global MDCs.
211 adapter.entering(http);
214 // Generate (and log) an invocationID, then use it to
215 // invoke another component.
217 final RESTClient client = new RESTClient(); // implements ONAPLogAdapter.RequestBuilder<RESTClient>.
218 adapter.invoke(client, ONAPLogConstants.InvocationMode.SYNCHRONOUS);
219 final RESTRequest request = null; // TODO: build real request.
220 final RESTResponse response = client.execute(request); // TODO: handle real response.
222 // Set response details prior to #exiting.
223 // (Obviously there'd be errorhandling, etc. IRL).
225 adapter.getResponseDescriptor()
226 .setResponseCode((String)null)
227 .setResponseSeverity(Level.INFO)
228 .setResponseStatus(ONAPLogConstants.ResponseStatus.COMPLETED);
232 // Return, logging EXIT marker, with response MDCs.
239 * Dummy class, for example code.
241 static class RESTClient implements ONAPLogAdapter.RequestBuilder<RESTClient> {
244 public RESTClient setHeader(final String name, final String value) {
248 RESTResponse execute(RESTRequest request) {
254 * Dummy class, for example code.
256 static class RESTRequest {
261 * Dummy class, for example code.
263 static class RESTResponse {