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.io.BufferedReader;
26 import java.io.FileReader;
27 import java.nio.file.Files;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.UUID;
32 import javax.xml.bind.DatatypeConverter;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import org.springframework.mock.web.MockHttpServletRequest;
38 import org.testng.annotations.AfterSuite;
39 import org.testng.annotations.BeforeSuite;
40 import org.testng.annotations.Test;
42 import static org.hamcrest.MatcherAssert.assertThat;
43 import static org.hamcrest.core.Is.is;
44 import static org.hamcrest.core.IsNot.not;
45 import static org.hamcrest.core.IsNull.notNullValue;
46 import static org.hamcrest.core.StringContains.containsString;
47 import static org.hamcrest.number.OrderingComparison.greaterThan;
50 * Smoketest output, though the embedded configuration isn't necessarily
53 * <p>There are more comprehensive tests in the <tt>logging-slf4j-demo</tt>
56 public class ONAPLogAdapterOutputTest {
58 /** Temporary directory into which logfiles are written. */
59 private static File sDir;
62 public static void setUp() throws Exception {
63 sDir = Files.createTempDirectory(ONAPLogAdapterOutputTest.class.getName()).toFile();
64 System.getProperties().setProperty("SLF4J_OUTPUT_DIRECTORY", sDir.getAbsolutePath());
65 LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class).info("Starting.");
69 public static void tearDown() throws Exception {
70 LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class).info("Ending.");
73 System.err.println("Should be deleting [" + sDir.getAbsolutePath() + "]...");
78 public void testOutput() throws Exception {
80 assertThat(sDir, notNullValue());
81 assertThat(sDir.isDirectory(), is(true));
83 final String uuid = UUID.randomUUID().toString();
84 final String errorcode = UUID.randomUUID().toString();
85 final Logger logger = LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class);
88 MDC.put("uuid", uuid);
89 final ONAPLogAdapter adapter = new ONAPLogAdapter(logger);
90 final ONAPLogAdapter.HttpServletRequestAdapter http
91 = new ONAPLogAdapter.HttpServletRequestAdapter(new MockHttpServletRequest());
92 adapter.entering(http);
93 adapter.unwrap().warn("a_warning");
95 throw new Exception("errorcode=" + errorcode);
97 catch (final Exception e) {
98 adapter.unwrap().error("an_error", e);
107 final List<String> lines = new ArrayList<>();
108 for (final File f : sDir.listFiles()) {
109 try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
111 while ((line = reader.readLine()) != null) {
112 if (line.contains(uuid)) {
119 assertThat(lines.size(), is(3));
121 assertThat(lines.get(0), containsString("ENTRY"));
122 final String [] line0 = lines.get(0).split("\t", -1);
123 assertThat(line0.length, is(9));
124 DatatypeConverter.parseDateTime(line0[0]);
125 assertThat(line0[1].trim().length(), greaterThan(1));
126 assertThat(line0[2], is("INFO"));
127 assertThat(line0[3], is(this.getClass().getName()));
128 assertThat(line0[4], containsString("uuid=" + uuid));
129 assertThat(line0[5], is(""));
130 assertThat(line0[6], is(""));
131 assertThat(line0[7], is("ENTRY"));
132 System.err.println(lines.get(0));
134 assertThat(lines.get(1), not(containsString("ENTRY")));
135 assertThat(lines.get(1), containsString("a_warning"));
136 final String [] line1 = lines.get(1).split("\t", -1);
137 assertThat(line1.length, is(9));
138 DatatypeConverter.parseDateTime(line1[0]);
139 assertThat(line1[1].trim().length(), greaterThan(1));
140 assertThat(line1[2], is("WARN"));
141 assertThat(line1[3], is(this.getClass().getName()));
142 assertThat(line1[4], containsString("uuid=" + uuid));
143 assertThat(line1[5], is("a_warning"));
144 assertThat(line1[6], is(""));
145 assertThat(line1[7], is(""));
146 System.err.println(lines.get(1));
148 assertThat(lines.get(2), not(containsString("ENTRY")));
149 assertThat(lines.get(2), containsString("an_error"));
150 final String [] line2 = lines.get(2).split("\t", -1);
151 assertThat(line2.length, is(9));
152 DatatypeConverter.parseDateTime(line2[0]);
153 assertThat(line2[1].trim().length(), greaterThan(1));
154 assertThat(line2[2], is("ERROR"));
155 assertThat(line2[3], is(this.getClass().getName()));
156 assertThat(line2[4], containsString("uuid=" + uuid));
157 assertThat(line2[5], is("an_error"));
158 assertThat(line2[6], containsString("errorcode=" + errorcode));
159 assertThat(line2[7], is(""));
160 System.err.println(lines.get(2));