aboutsummaryrefslogtreecommitdiffstats
path: root/reference/logging-slf4j/src/test/java/org/onap/logging/ref/slf4j/ONAPLogAdapterOutputTest.java
blob: bab74bb856b8f02fe4b21e4131177a2a7eec30b3 (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
/**
 * ============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;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.xml.bind.DatatypeConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.mock.web.MockHttpServletRequest;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.number.OrderingComparison.greaterThan;

/**
 * Smoketest output, though the embedded configuration isn't necessarily
 * canonical.
 *
 * <p>There are more comprehensive tests in the <tt>logging-slf4j-demo</tt>
 * project.</p>
 */
public class ONAPLogAdapterOutputTest {

    /** Temporary directory into which logfiles are written. */
    private static File sDir;

    @BeforeSuite
    public static void setUp() throws Exception {
        sDir = Files.createTempDirectory(ONAPLogAdapterOutputTest.class.getName()).toFile();
        System.getProperties().setProperty("SLF4J_OUTPUT_DIRECTORY", sDir.getAbsolutePath());
        LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class).info("Starting.");
    }

    @AfterSuite
    public static void tearDown() throws Exception {
        LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class).info("Ending.");
        Thread.sleep(1000L);
        if (sDir != null) {
            System.err.println("Should be deleting [" + sDir.getAbsolutePath() + "]...");
        }
    }

    @Test
    public void testOutput() throws Exception {

        assertThat(sDir, notNullValue());
        assertThat(sDir.isDirectory(), is(true));

        final String uuid = UUID.randomUUID().toString();
        final String errorcode = UUID.randomUUID().toString();
        final Logger logger = LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class);

        try {
            MDC.put("uuid", uuid);
            final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
            final ONAPLogAdapter.HttpServletRequestAdapter http
                    = new ONAPLogAdapter.HttpServletRequestAdapter(new MockHttpServletRequest());
            adapter.entering(http);
            adapter.unwrap().warn("a_warning");
            try {
                throw new Exception("errorcode=" + errorcode);
            }
            catch (final Exception e) {
                adapter.unwrap().error("an_error", e);
            }

            Thread.sleep(1000L);
        }
        finally {
            MDC.clear();
        }

        final List<String> lines = new ArrayList<>();
        for (final File f : sDir.listFiles()) {
            try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.contains(uuid)) {
                        lines.add(line);
                    }
                }
            }
        }

        assertThat(lines.size(), is(3));

        assertThat(lines.get(0), containsString("ENTRY"));
        final String [] line0 = lines.get(0).split("\t", -1);
        assertThat(line0.length, is(9));
        DatatypeConverter.parseDateTime(line0[0]);
        assertThat(line0[1].trim().length(), greaterThan(1));
        assertThat(line0[2], is("INFO"));
        assertThat(line0[3], is(this.getClass().getName()));
        assertThat(line0[4], containsString("uuid=" + uuid));
        assertThat(line0[5], is(""));
        assertThat(line0[6], is(""));
        assertThat(line0[7], is("ENTRY"));
        System.err.println(lines.get(0));

        assertThat(lines.get(1), not(containsString("ENTRY")));
        assertThat(lines.get(1), containsString("a_warning"));
        final String [] line1 = lines.get(1).split("\t", -1);
        assertThat(line1.length, is(9));
        DatatypeConverter.parseDateTime(line1[0]);
        assertThat(line1[1].trim().length(), greaterThan(1));
        assertThat(line1[2], is("WARN"));
        assertThat(line1[3], is(this.getClass().getName()));
        assertThat(line1[4], containsString("uuid=" + uuid));
        assertThat(line1[5], is("a_warning"));
        assertThat(line1[6], is(""));
        assertThat(line1[7], is(""));
        System.err.println(lines.get(1));

        assertThat(lines.get(2), not(containsString("ENTRY")));
        assertThat(lines.get(2), containsString("an_error"));
        final String [] line2 = lines.get(2).split("\t", -1);
        assertThat(line2.length, is(9));
        DatatypeConverter.parseDateTime(line2[0]);
        assertThat(line2[1].trim().length(), greaterThan(1));
        assertThat(line2[2], is("ERROR"));
        assertThat(line2[3], is(this.getClass().getName()));
        assertThat(line2[4], containsString("uuid=" + uuid));
        assertThat(line2[5], is("an_error"));
        assertThat(line2[6], containsString("errorcode=" + errorcode));
        assertThat(line2[7], is(""));
        System.err.println(lines.get(2));
    }
}