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 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.IsNot.not;
40 import static org.hamcrest.core.IsNull.notNullValue;
41 import static org.hamcrest.core.IsNull.nullValue;
42 import static org.hamcrest.core.IsSame.sameInstance;
45 * Tests for {@link ONAPLogAdapter}.
47 public class ONAPLogAdapterTest {
50 * Ensure that MDCs are cleared after each testcase.
53 public void resetMDCs() {
61 public void testCheckNotNull() {
63 ONAPLogAdapter.checkNotNull("");
66 ONAPLogAdapter.checkNotNull(null);
67 Assert.fail("Should throw NullPointerException");
69 catch (final NullPointerException e) {
75 * Test defaulting of nulls.
78 public void testDefaultToEmpty() {
79 assertThat(ONAPLogAdapter.defaultToEmpty("123"), is("123"));
80 assertThat(ONAPLogAdapter.defaultToEmpty(Integer.valueOf(1984)), is("1984"));
81 assertThat(ONAPLogAdapter.defaultToEmpty(null), is(""));
85 * Test defaulting of nulls.
88 public void testDefaultToUUID() {
89 assertThat(ONAPLogAdapter.defaultToUUID("123"), is("123"));
90 UUID.fromString(ONAPLogAdapter.defaultToUUID(null));
97 public void testEntering() {
99 final Logger logger = LoggerFactory.getLogger(this.getClass());
100 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
101 final MockHttpServletRequest http = new MockHttpServletRequest();
102 http.setRequestURI("uri123");
103 http.setServerName("local123");
104 http.setRemoteAddr("remote123");
105 http.addHeader("X-ONAP-RequestID", "request123");
106 http.addHeader("X-ONAP-InvocationID", "invocation123");
107 http.addHeader("X-ONAP-PartnerName", "partner123");
110 adapter.getServiceDescriptor().setServiceName("uri123");
111 adapter.entering(http);
112 final Map<String, String> mdcs = MDC.getCopyOfContextMap();
113 assertThat(mdcs.get("RequestID"), is("request123"));
114 assertThat(mdcs.get("InvocationID"), is("invocation123"));
115 assertThat(mdcs.get("PartnerName"), is("partner123"));
116 assertThat(mdcs.get("ServiceName"), is("uri123"));
117 assertThat(mdcs.get("ServerFQDN"), is("local123"));
118 assertThat(mdcs.get("ClientIPAddress"), is("remote123"));
126 public void testSetServiceDescriptor() {
127 final ONAPLogAdapter.ServiceDescriptor override = new ONAPLogAdapter.ServiceDescriptor();
128 final Logger logger = LoggerFactory.getLogger(this.getClass());
129 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
130 final ONAPLogAdapter.ServiceDescriptor before = adapter.getServiceDescriptor();
131 adapter.setServiceDescriptor(override);
132 final ONAPLogAdapter.ServiceDescriptor after = adapter.getServiceDescriptor();
133 assertThat(after, not(sameInstance(before)));
134 assertThat(after, is(override));
138 public void testSetResponseDescriptor() {
139 final ONAPLogAdapter.ResponseDescriptor override = new ONAPLogAdapter.ResponseDescriptor();
140 final Logger logger = LoggerFactory.getLogger(this.getClass());
141 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
142 final ONAPLogAdapter.ResponseDescriptor before = adapter.getResponseDescriptor();
143 adapter.setResponseDescriptor(override);
144 final ONAPLogAdapter.ResponseDescriptor after = adapter.getResponseDescriptor();
145 assertThat(after, not(sameInstance(before)));
146 assertThat(after, is(override));
150 public void testUnwrap() {
151 final Logger logger = LoggerFactory.getLogger(this.getClass());
152 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
153 assertThat(adapter.unwrap(), is(logger));
160 public void testExiting() {
162 final Logger logger = LoggerFactory.getLogger(this.getClass());
163 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
166 MDC.put("somekey", "somevalue");
167 assertThat(MDC.get("somekey"), is("somevalue"));
169 assertThat(MDC.get("somekey"), nullValue());
180 public void testInvokeSyncAsyncNull() {
182 final Logger logger = LoggerFactory.getLogger(this.getClass());
183 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
185 final UUID syncUUID = adapter.invoke(ONAPLogConstants.InvocationMode.SYNCHRONOUS);
186 assertThat(syncUUID, notNullValue());
188 final UUID asyncUUID = adapter.invoke(ONAPLogConstants.InvocationMode.SYNCHRONOUS);
189 assertThat(asyncUUID, notNullValue());
191 final UUID agnosticUUID = adapter.invoke((ONAPLogConstants.InvocationMode)null);
192 assertThat(agnosticUUID, notNullValue());
197 * Test INVOKE, with RequestAdapter.
200 public void testInvokeWithAdapter() throws Exception {
202 final Logger logger = LoggerFactory.getLogger(this.getClass());
203 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
205 final Map<String, String> headers = new HashMap<>();
206 final ONAPLogAdapter.RequestBuilder builder = new ONAPLogAdapter.RequestBuilder<ONAPLogAdapter.RequestBuilder>() {
208 public ONAPLogAdapter.RequestBuilder setHeader(final String name, final String value) {
209 headers.put(name, value);
215 final UUID uuid = adapter.invoke(builder, ONAPLogConstants.InvocationMode.SYNCHRONOUS);
216 assertThat(uuid, notNullValue());
217 assertThat(headers.get(ONAPLogConstants.Headers.INVOCATION_ID), is(uuid.toString()));
218 assertThat(headers.containsKey(ONAPLogConstants.Headers.PARTNER_NAME), is(true));
219 assertThat(headers.containsKey(ONAPLogConstants.Headers.REQUEST_ID), is(true));
227 * Test INVOKE, with RequestAdapter.
230 public void testInvokeWithAdapterAndNull() throws Exception {
232 final Logger logger = LoggerFactory.getLogger(this.getClass());
233 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
235 final Map<String, String> headers = new HashMap<>();
236 final ONAPLogAdapter.RequestBuilder builder = new ONAPLogAdapter.RequestBuilder<ONAPLogAdapter.RequestBuilder>() {
238 public ONAPLogAdapter.RequestBuilder setHeader(final String name, final String value) {
239 headers.put(name, value);
245 final UUID uuid = adapter.invoke(builder);
246 assertThat(uuid, notNullValue());
247 assertThat(headers.get(ONAPLogConstants.Headers.INVOCATION_ID), is(uuid.toString()));
248 assertThat(headers.containsKey(ONAPLogConstants.Headers.PARTNER_NAME), is(true));
249 assertThat(headers.containsKey(ONAPLogConstants.Headers.REQUEST_ID), is(true));
257 public void testHttpServletRequestAdapter() {
259 final UUID uuid = UUID.randomUUID();
260 final MockHttpServletRequest request = new MockHttpServletRequest();
261 request.addHeader("uuid", uuid.toString());
262 request.setRequestURI("/ctx0");
263 request.setServerName("srv0");
265 final ONAPLogAdapter.HttpServletRequestAdapter adapter
266 = new ONAPLogAdapter.HttpServletRequestAdapter(request);
267 assertThat(adapter.getHeader("uuid"), is(uuid.toString()));
268 assertThat(adapter.getRequestURI(), is("/ctx0"));
269 assertThat(adapter.getServerAddress(), is("srv0"));
273 public void testServiceDescriptor() {
274 final String uuid = UUID.randomUUID().toString();
276 final ONAPLogAdapter.ServiceDescriptor adapter
277 = new ONAPLogAdapter.ServiceDescriptor();
278 adapter.setServiceUUID(uuid);
279 adapter.setServiceName("name0");
281 assertThat(MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME), nullValue());
282 assertThat(MDC.get(ONAPLogConstants.MDCs.INSTANCE_UUID), nullValue());
286 assertThat(MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME), is("name0"));
287 assertThat(MDC.get(ONAPLogConstants.MDCs.INSTANCE_UUID), is(uuid));
291 public void testResponseDescriptor() {
292 final String uuid = UUID.randomUUID().toString();
294 final ONAPLogAdapter.ResponseDescriptor adapter
295 = new ONAPLogAdapter.ResponseDescriptor();
296 adapter.setResponseCode("code0");
297 adapter.setResponseDescription("desc0");
298 adapter.setResponseSeverity(Level.INFO);
299 adapter.setResponseStatus(ONAPLogConstants.ResponseStatus.COMPLETED);
301 assertThat(MDC.get(ONAPLogConstants.MDCs.RESPONSE_CODE), nullValue());
302 assertThat(MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION), nullValue());
303 assertThat(MDC.get(ONAPLogConstants.MDCs.RESPONSE_SEVERITY), nullValue());
304 assertThat(MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE), nullValue());
308 assertThat(MDC.get(ONAPLogConstants.MDCs.RESPONSE_CODE), is("code0"));
309 assertThat(MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION), is("desc0"));
310 assertThat(MDC.get(ONAPLogConstants.MDCs.RESPONSE_SEVERITY), is("INFO"));
311 assertThat(MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE), is("COMPLETED"));
315 * Exercise the contract, for a caller that's happy to have their
316 * service name automatically derived. (This validates nothing
317 * and achieves nothing; it's just to provide an example of minimal usage).
320 public void testContract() {
322 // Note no wrapper around HttpServletRequest, which will work for
323 // most invocations (since they come via HTTP), but otherwise
324 // can implement your own RequestAdapter.
326 final Logger logger = LoggerFactory.getLogger(this.getClass());
327 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
328 final MockHttpServletRequest http = new MockHttpServletRequest();
330 // Immediately log ENTERING marker, with global MDCs.
332 adapter.entering(http);
335 // Generate (and log) an invocationID, then use it to
336 // invoke another component.
338 final RESTClient client = new RESTClient(); // implements ONAPLogAdapter.RequestBuilder<RESTClient>.
339 adapter.invoke(client, ONAPLogConstants.InvocationMode.SYNCHRONOUS);
340 final RESTRequest request = null; // TODO: build real request.
341 final RESTResponse response = client.execute(request); // TODO: handle real response.
343 // Set response details prior to #exiting.
344 // (Obviously there'd be errorhandling, etc. IRL).
346 adapter.getResponseDescriptor()
347 .setResponseCode((String)null)
348 .setResponseSeverity(Level.INFO)
349 .setResponseStatus(ONAPLogConstants.ResponseStatus.COMPLETED);
353 // Return, logging EXIT marker, with response MDCs.
360 * Dummy class, for example code.
362 static class RESTClient implements ONAPLogAdapter.RequestBuilder<RESTClient> {
365 public RESTClient setHeader(final String name, final String value) {
369 RESTResponse execute(RESTRequest request) {
375 * Dummy class, for example code.
377 static class RESTRequest {
382 * Dummy class, for example code.
384 static class RESTResponse {