aboutsummaryrefslogtreecommitdiffstats
path: root/reference/slf4j-reference/src/test/java/org/onap/logging/ref/slf4j/common/ONAPLogAdapterTest.java
blob: 60ee60f1494b4f8051373ed78b92be07b46a2a86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
 * ============LICENSE_START=======================================================
 * org.onap.logging
 * ================================================================================
 * Copyright © 2018 Amdocs
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.logging.ref.slf4j.common;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.event.Level;
import org.springframework.mock.web.MockHttpServletRequest;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.IsNull.nullValue;

/**
 * Tests for {@link ONAPLogAdapter}.
 */
public class ONAPLogAdapterTest {

    /**
     * Ensure that MDCs are cleared after each testcase.
     */
    @AfterMethod
    public void resetMDCs() {
        MDC.clear();
    }

    /**
     * Test nullcheck.
     */
    @Test
    public void testCheckNotNull() {

        ONAPLogAdapter.checkNotNull("");

        try {
            ONAPLogAdapter.checkNotNull(null);
            Assert.fail("Should throw NullPointerException");
        }
        catch (final NullPointerException e) {

        }
    }

    /**
     * Test defaulting of nulls.
     */
    @Test
    public void testDefaultToEmpty() {
        assertThat(ONAPLogAdapter.defaultToEmpty("123"), is("123"));
        assertThat(ONAPLogAdapter.defaultToEmpty(Integer.valueOf(1984)), is("1984"));
        assertThat(ONAPLogAdapter.defaultToEmpty(null), is(""));
    }

    /**
     * Test defaulting of nulls.
     */
    @Test
    public void testDefaultToUUID() {
        assertThat(ONAPLogAdapter.defaultToUUID("123"), is("123"));
        UUID.fromString(ONAPLogAdapter.defaultToUUID(null));
    }

    /**
     * Test ENTERING.
     */
    @Test
    public void testEntering() {

        final Logger logger = LoggerFactory.getLogger(this.getClass());
        final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
        final MockHttpServletRequest http = new MockHttpServletRequest();
        http.setRequestURI("uri123");
        http.setServerName("local123");
        http.setRemoteAddr("remote123");
        http.addHeader("X-ONAP-RequestID", "request123");
        http.addHeader("X-ONAP-InvocationID", "invocation123");
        http.addHeader("X-ONAP-PartnerName", "partner123");

        try {
            adapter.getServiceDescriptor().setServiceName("uri123");
            adapter.entering(http);
            final Map<String, String> mdcs = MDC.getCopyOfContextMap();
            assertThat(mdcs.get("RequestID"), is("request123"));
            assertThat(mdcs.get("InvocationID"), is("invocation123"));
            assertThat(mdcs.get("PartnerName"), is("partner123"));
            assertThat(mdcs.get("ServiceName"), is("uri123"));
            assertThat(mdcs.get("ServerFQDN"), is("local123"));
            assertThat(mdcs.get("ClientIPAddress"), is("remote123"));
        }
        finally {
            MDC.clear();
        }
    }

    /**
     * Test EXITING.
     */
    @Test
    public void testExiting() {

        final Logger logger = LoggerFactory.getLogger(this.getClass());
        final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);

        try {
            MDC.put("somekey", "somevalue");
            assertThat(MDC.get("somekey"), is("somevalue"));
            adapter.exiting();
            assertThat(MDC.get("somekey"), nullValue());
        }
        finally {
            MDC.clear();
        }
    }

    /**
     * Test INVOKE.
     */
    @Test
    public void testInvokeSyncAsyncNull() {

        final Logger logger = LoggerFactory.getLogger(this.getClass());
        final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);

        final UUID syncUUID = adapter.invoke(ONAPLogConstants.InvocationMode.SYNCHRONOUS);
        assertThat(syncUUID, notNullValue());

        final UUID asyncUUID = adapter.invoke(ONAPLogConstants.InvocationMode.SYNCHRONOUS);
        assertThat(asyncUUID, notNullValue());

        final UUID agnosticUUID = adapter.invoke((ONAPLogConstants.InvocationMode)null);
        assertThat(agnosticUUID, notNullValue());

    }

    /**
     * Test INVOKE, with RequestAdapter.
     */
    @Test
    public void testInvokeWithAdapter() throws Exception {

        final Logger logger = LoggerFactory.getLogger(this.getClass());
        final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);

        final Map<String, String> headers = new HashMap<>();
        final ONAPLogAdapter.RequestBuilder builder = new ONAPLogAdapter.RequestBuilder<ONAPLogAdapter.RequestBuilder>() {
            @Override
            public ONAPLogAdapter.RequestBuilder setHeader(final String name, final String value) {
                headers.put(name, value);
                return this;
            }
        };

        try {
            final UUID uuid = adapter.invoke(builder, ONAPLogConstants.InvocationMode.SYNCHRONOUS);
            assertThat(uuid, notNullValue());
            assertThat(headers.get(ONAPLogConstants.Headers.INVOCATION_ID), is(uuid.toString()));
            assertThat(headers.containsKey(ONAPLogConstants.Headers.PARTNER_NAME), is(true));
            assertThat(headers.containsKey(ONAPLogConstants.Headers.REQUEST_ID), is(true));
        }
        finally {
            MDC.clear();
        }
    }

    /**
     * Exercise the contract, for a caller that's happy to have their
     * service name automatically derived. (This validates nothing
     * and achieves nothing; it's just to provide an example of minimal usage).
     */
    @Test
    public void testContract() {

        // Note no wrapper around HttpServletRequest, which will work for
        // most invocations (since they come via HTTP), but otherwise
        // can implement your own RequestAdapter.

        final Logger logger = LoggerFactory.getLogger(this.getClass());
        final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
        final MockHttpServletRequest http = new MockHttpServletRequest();

        // Immediately log ENTERING marker, with global MDCs.

        adapter.entering(http);
        try {

            // Generate (and log) an invocationID, then use it to
            // invoke another component.

            final RESTClient client = new RESTClient();             // implements ONAPLogAdapter.RequestBuilder<RESTClient>.
            adapter.invoke(client, ONAPLogConstants.InvocationMode.SYNCHRONOUS);
            final RESTRequest request = null;                       // TODO: build real request.
            final RESTResponse response = client.execute(request);  // TODO: handle real response.

            // Set response details prior to #exiting.
            // (Obviously there'd be errorhandling, etc. IRL).

            adapter.getResponseDescriptor()
                    .setResponseCode((String)null)
                    .setResponseSeverity(Level.INFO)
                    .setResponseStatus(ONAPLogConstants.ResponseStatus.COMPLETED);
        }
        finally {

            // Return, logging EXIT marker, with response MDCs.

            adapter.exiting();
        }
    }

    /**
     * Dummy class, for example code.
     */
    static class RESTClient implements ONAPLogAdapter.RequestBuilder<RESTClient> {

        @Override
        public RESTClient setHeader(final String name, final String value) {
            return null;
        }

        RESTResponse execute(RESTRequest request) {
            return null;
        }
    }

    /**
     * Dummy class, for example code.
     */
    static class RESTRequest {

    }

    /**
     * Dummy class, for example code.
     */
    static class RESTResponse {

    }
}