Merge "k8s entrypoint wgets from git not jira now"
[logging-analytics.git] / reference / logging-slf4j / src / test / java / org / onap / logging / ref / slf4j / ONAPLogAdapterOutputTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.logging
4  * ================================================================================
5  * Copyright © 2018 Amdocs
6  * All rights reserved.
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
11  *
12  *    http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.logging.ref.slf4j;
23
24 import java.io.BufferedReader;
25 import java.io.File;
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;
31
32 import javax.xml.bind.DatatypeConverter;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.slf4j.MDC;
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;
41
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;
48
49 /**
50  * Smoketest output, though the embedded configuration isn't necessarily
51  * canonical.
52  *
53  * <p>There are more comprehensive tests in the <tt>logging-slf4j-demo</tt>
54  * project.</p>
55  */
56 public class ONAPLogAdapterOutputTest {
57
58     /** Temporary directory into which logfiles are written. */
59     private static File sDir;
60
61     @BeforeSuite
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.");
66     }
67
68     @AfterSuite
69     public static void tearDown() throws Exception {
70         LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class).info("Ending.");
71         Thread.sleep(1000L);
72         if (sDir != null) {
73             System.err.println("Should be deleting [" + sDir.getAbsolutePath() + "]...");
74         }
75     }
76
77     @Test
78     public void testOutput() throws Exception {
79
80         assertThat(sDir, notNullValue());
81         assertThat(sDir.isDirectory(), is(true));
82
83         final String uuid = UUID.randomUUID().toString();
84         final String errorcode = UUID.randomUUID().toString();
85         final Logger logger = LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class);
86
87         try {
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");
94             try {
95                 throw new Exception("errorcode=" + errorcode);
96             }
97             catch (final Exception e) {
98                 adapter.unwrap().error("an_error", e);
99             }
100
101             Thread.sleep(1000L);
102         }
103         finally {
104             MDC.clear();
105         }
106
107         final List<String> lines = new ArrayList<>();
108         for (final File f : sDir.listFiles()) {
109             try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
110                 String line;
111                 while ((line = reader.readLine()) != null) {
112                     if (line.contains(uuid)) {
113                         lines.add(line);
114                     }
115                 }
116             }
117         }
118
119         assertThat(lines.size(), is(3));
120
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));
133
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));
147
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));
161     }
162 }