summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/validation/logging/TestApplicationLogger.java
blob: c31fcebed12ed7bfe51db857234463adb926f7ad (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2018-2019 European Software Marketing Ltd.
 * ================================================================================
 * 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.aai.validation.logging;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.IOException;
import java.util.Arrays;
import org.apache.commons.lang.time.StopWatch;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.mockito.Mockito;
import org.onap.aai.cl.api.LogFields;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.mdc.MdcOverride;
import org.onap.aai.validation.logging.LogHelper.TriConsumer;
import org.springframework.http.HttpHeaders;

/**
 * Simple test to log each of the validation messages in turn.
 *
 * This version tests only the error logger at INFO level.
 *
 */
public class TestApplicationLogger {

    @Rule
    public TestName name = new TestName();

    static {
        System.setProperty("APP_HOME", ".");
    }

    /**
     * Ensure that all of the EELF log files exist (and any log file rollover takes place) so that we can successfully
     * read from the log files (during the Test method).
     */
    @Before
    public void createLogFiles() {
        final String startMessage = "begin testing " + name.getMethodName();
        LogHelper.INSTANCE.debug(startMessage);
        LogHelper.INSTANCE.info(ApplicationMsgs.MESSAGE_AUDIT, startMessage);
        LogHelper.INSTANCE.logMetrics(startMessage);
        LogHelper.INSTANCE.logAuditSuccess(startMessage);
    }

    /**
     * Check that each message can be logged and that (by implication of successful logging) there is a corresponding
     * resource (message format).
     *
     * @throws IOException
     *             if an I/O error occurs when opening the log directory, or no files were found
     */
    @Test
    public void logAllMessages() throws IOException {
        Logger logger = LogHelper.INSTANCE;
        String logDirectory = LogHelper.getLogDirectory();
        LogReader errorReader = new LogReader(logDirectory, "error");
        LogReader debugReader = new LogReader(logDirectory, "debug");
        String[] args = {"1", "2", "3", "4"};
        for (ApplicationMsgs msg : Arrays.asList(ApplicationMsgs.values())) {
            if (msg.name().endsWith("ERROR")) {
                logger.error(msg, args);
                validateLoggedMessage(msg, errorReader, "ERROR");

                logger.error(msg, new RuntimeException("fred"), args);
                validateLoggedMessage(msg, errorReader, "fred");
            } else {
                logger.info(msg, args);
                validateLoggedMessage(msg, errorReader, "INFO");

                logger.warn(msg, args);
                validateLoggedMessage(msg, errorReader, "WARN");
            }

            if (logger.isDebugEnabled()) {
                logger.debug(msg, args);
                validateLoggedMessage(msg, debugReader, "DEBUG");
            }

            // The trace level is not enabled
            logger.trace(msg, args);
        }
    }

    /**
     * Check that each message can be logged and that (by implication of successful logging) there is a corresponding
     * resource (message format).
     *
     * @throws IOException
     */
    @Test
    public void logDebugMessages() throws IOException {
        org.junit.Assume.assumeTrue(LogHelper.INSTANCE.isDebugEnabled());
        LogReader reader = new LogReader(LogHelper.getLogDirectory(), "debug");
        LogHelper.INSTANCE.debug("a message");
        String str = reader.getNewLines();
        assertThat(str, is(notNullValue()));
    }


    /**
     * Check logAudit with HTTP headers
     *
     * @throws IOException
     */
    @Test
    public void logAuditMessage() throws IOException {
        final LogHelper logger = LogHelper.INSTANCE;
        final LogReader reader = new LogReader(LogHelper.getLogDirectory(), "audit");

        HttpHeaders headers = Mockito.mock(HttpHeaders.class);
        Mockito.when(headers.getFirst("X-ECOMP-RequestID")).thenReturn("ecomp-request-id");
        Mockito.when(headers.getFirst("X-FromAppId")).thenReturn("app-id");

        // Call logAudit without first calling startAudit
        logger.logAuditSuccess("first call: bob");
        String str = reader.getNewLines();
        assertThat(str, is(notNullValue()));
        assertThat("audit message log level", str, containsString("INFO"));
        assertThat("audit message content", str, containsString("bob"));

        // This time call the start method
        logger.startAudit(headers, null);
        logger.logAuditSuccess("second call: foo");
        str = reader.getNewLines();
        assertThat(str, is(notNullValue()));
        assertThat("audit message log level", str, containsString("INFO"));
        assertThat("audit message content", str, containsString("foo"));
        assertThat("audit message content", str, containsString("ecomp-request-id"));
        assertThat("audit message content", str, containsString("app-id"));
    }

    /**
     * Check logAudit with no HTTP headers
     *
     * @throws IOException
     */
    @Test
    public void logAuditMessageWithoutHeaders() throws IOException {
        LogHelper logger = LogHelper.INSTANCE;
        LogReader reader = new LogReader(LogHelper.getLogDirectory(), "audit");
        logger.startAudit(null, null);
        logger.logAuditSuccess("foo");
        String str = reader.getNewLines();
        assertThat(str, is(notNullValue()));
        assertThat("audit message log level", str, containsString("INFO"));
        assertThat("audit message content", str, containsString("foo"));
    }

    /**
     * Check logMetrics
     *
     * @throws IOException
     */
    @Test
    public void logMetricsMessage() throws IOException {
        LogReader reader = new LogReader(LogHelper.getLogDirectory(), "metrics");
        LogHelper logger = LogHelper.INSTANCE;
        logger.logMetrics("metrics: fred");
        String str = reader.getNewLines();
        assertThat(str, is(notNullValue()));
        assertThat("metrics message log level", str, containsString("INFO"));
        assertThat("metrics message content", str, containsString("fred"));
    }

    @Test
    public void logMetricsMessageWithStopwatch() throws IOException {
        LogReader reader = new LogReader(LogHelper.getLogDirectory(), "metrics");
        LogHelper logger = LogHelper.INSTANCE;
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        logger.logMetrics(stopWatch, "joe", "bloggs");
        String logLine = reader.getNewLines();
        assertThat(logLine, is(notNullValue()));
        assertThat("metrics message log level", logLine, containsString("INFO"));
        assertThat("metrics message content", logLine, containsString("joe"));
    }

    @Test
    public void callUnsupportedMethods() throws IOException {
        LogHelper logger = LogHelper.INSTANCE;
        ApplicationMsgs dummyMsg = ApplicationMsgs.LOAD_PROPERTIES;
        callUnsupportedOperationMethod(logger::error, dummyMsg);
        callUnsupportedOperationMethod(logger::info, dummyMsg);
        callUnsupportedOperationMethod(logger::warn, dummyMsg);
        callUnsupportedOperationMethod(logger::debug, dummyMsg);
        callUnsupportedOperationMethod(logger::trace, dummyMsg);
        try {
            logger.error(dummyMsg, new LogFields(), new RuntimeException("test"), "");
        } catch (UnsupportedOperationException e) {
            // Expected to reach here
        }
        try {
            logger.info(dummyMsg, new LogFields(), new MdcOverride(), "");
        } catch (UnsupportedOperationException e) {
            // Expected to reach here
        }
        try {
            logger.formatMsg(dummyMsg, "");
        } catch (UnsupportedOperationException e) {
            // Expected to reach here
        }
    }

    /**
     * Call a logger method which is expected to throw an UnsupportedOperationException
     *
     * @param logMethod
     * @param dummyMsg
     */
    private void callUnsupportedOperationMethod(TriConsumer<Enum<?>, LogFields, String[]> logMethod,
            ApplicationMsgs dummyMsg) {
        try {
            logMethod.accept(dummyMsg, new LogFields(), new String[] {""});
            org.junit.Assert.fail("method should have thrown execption"); // NOSONAR as code not reached
        } catch (UnsupportedOperationException e) {
            // Expected to reach here
        }
    }

    /**
     * Assert that a log message was logged to the expected log file at the expected severity
     *
     * @param msg
     * @param reader
     * @param severity
     * @throws IOException
     */
    private void validateLoggedMessage(ApplicationMsgs msg, LogReader reader, String severity) throws IOException {
        String str = reader.getNewLines();
        assertThat(str, is(notNullValue()));
        assertThat(msg.toString() + " log level", str, containsString(severity));
    }
}