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;
29 import javax.servlet.http.HttpServletRequest;
31 import org.onap.logging.ref.slf4j.analysis.CallGraphAnalyzer;
32 import org.onap.logging.ref.slf4j.analysis.CallGraphReportWriter;
33 import org.onap.logging.ref.slf4j.analysis.LogEntry;
34 import org.onap.logging.ref.slf4j.demo.bean.Request;
35 import org.onap.logging.ref.slf4j.demo.bean.Response;
36 import org.onap.logging.ref.slf4j.demo.component.AbstractComponentTest;
37 import org.onap.logging.ref.slf4j.demo.component.alpha.ComponentAlpha;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.mock.web.MockHttpServletRequest;
40 import org.testng.Assert;
41 import org.testng.annotations.AfterSuite;
42 import org.testng.annotations.BeforeSuite;
43 import org.testng.annotations.Test;
45 import static org.hamcrest.MatcherAssert.assertThat;
46 import static org.hamcrest.core.Is.is;
49 * Simple verification that we can easily get a call graph out of
50 * some calls to logging via <tt>ONAPLogAdapter</tt>.
52 public class CallGraphTest {
54 /** Temporary directory into which logfiles are written. */
55 private static File sDir;
58 public static void setUp() throws Exception {
59 AbstractComponentTest.setInProcess();
60 sDir = Files.createTempDirectory(CallGraphTest.class.getName()).toFile();
61 System.getProperties().setProperty("SLF4J_OUTPUT_DIRECTORY", sDir.getAbsolutePath());
62 LoggerFactory.getLogger(CallGraphTest.class).info("Starting.");
66 public static void tearDown() throws Exception {
67 LoggerFactory.getLogger(CallGraphTest.class).info("Ending.");
70 System.err.println("Should be deleting [" + sDir.getAbsolutePath() + "]...");
74 @Test(enabled = false)
75 public void testSimple() throws Exception {
77 final HttpServletRequest mock = new MockHttpServletRequest();
78 final ComponentAlpha a = new ComponentAlpha();
79 final Request request = new Request();
80 final Response response = a.execute(request, mock);
81 assertThat(response.getResponses().size(), is(0));
85 * A more complex (interesting) example of generating a call graph.
86 * @throws Exception test failure.
89 public void testComplex() throws Exception {
91 Assert.assertNotNull(sDir);
93 // Fan out some requests between test components.
95 final Request a = new Request();
96 a.setService("alpha");
98 final Request b = new Request();
101 final Request ac = new Request();
102 ac.setService("gamma");
104 final Request ad = new Request();
105 ad.setService("delta");
107 final Request bc1 = new Request();
108 bc1.setService("gamma");
110 final Request bc2 = new Request();
111 bc2.setService("gamma");
113 a.getRequests().add(b);
114 a.getRequests().add(ac);
115 a.getRequests().add(ad);
116 b.getRequests().add(bc1);
117 b.getRequests().add(bc2);
121 final Request xb = new Request();
122 xb.setService("beta");
124 final Request xg = new Request();
125 xg.setService("gamma");
127 final Request xd = new Request();
128 xd.setService("delta");
130 a.getRequests().add(xb);
131 xb.getRequests().add(xg);
132 xg.getRequests().add(xd);
136 final HttpServletRequest mock = new MockHttpServletRequest();
137 final ComponentAlpha component = new ComponentAlpha();
138 final Response response = component.execute(a, mock);
139 System.err.println(response);
141 assertThat(response.getResponses().size(), is(4));
148 for (final File candidate : sDir.listFiles()) {
149 if (candidate.getName().endsWith(".log")) {
155 Assert.assertNotNull(log);
157 System.err.println("READING LOGFILE: " + log.getAbsolutePath());
159 final CallGraphAnalyzer analyzer = new CallGraphAnalyzer();
160 try (final BufferedReader reader = new BufferedReader(new FileReader(log))) {
163 final String line = reader.readLine();
168 final LogEntry entry = new LogEntry(line);
174 // Debug during dev, but annoying the rest of the time.
176 // System.err.println("--------------------------------------------------");
177 // for (final LogEntry e : analyzer.getEntries()) {
178 // System.err.println(e.toShortString());
180 // System.err.println("--------------------------------------------------");
182 final CallGraphReportWriter writer = new CallGraphReportWriter(analyzer);
183 final String shortReport = writer.getShortReport();
184 final String longReport = writer.getLongReport();
188 System.out.println("----\nGraph:\n\n" + longReport + "\n----");
190 // Validate short report.
192 assertThat("Alpha\n" +
203 // Ensure output reaches System.xxx.