k8s entrypoint wgets from git not jira now
[logging-analytics.git] / reference / slf4j-reference / src / test / java / org / onap / logging / ref / slf4j / CallGraphTest.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
29 import javax.servlet.http.HttpServletRequest;
30
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;
44
45 import static org.hamcrest.MatcherAssert.assertThat;
46 import static org.hamcrest.core.Is.is;
47
48 /**
49  * Simple verification that we can easily get a call graph out of
50  * some calls to logging via <tt>ONAPLogAdapter</tt>.
51  */
52 public class CallGraphTest {
53
54     /** Temporary directory into which logfiles are written. */
55     private static File sDir;
56
57     @BeforeSuite
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.");
63     }
64
65     @AfterSuite
66     public static void tearDown() throws Exception {
67         LoggerFactory.getLogger(CallGraphTest.class).info("Ending.");
68         Thread.sleep(1000L);
69         if (sDir != null) {
70             System.err.println("Should be deleting [" + sDir.getAbsolutePath() + "]...");
71         }
72     }
73
74     @Test(enabled = false)
75     public void testSimple() throws Exception {
76
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));
82     }
83
84     /**
85      * A more complex (interesting) example of generating a call graph.
86      * @throws Exception test failure.
87      */
88     @Test
89     public void testComplex() throws Exception {
90
91         Assert.assertNotNull(sDir);
92
93         // Fan out some requests between test components.
94
95         final Request a = new Request();
96         a.setService("alpha");
97
98         final Request b = new Request();
99         b.setService("beta");
100
101         final Request ac = new Request();
102         ac.setService("gamma");
103
104         final Request ad = new Request();
105         ad.setService("delta");
106
107         final Request bc1 = new Request();
108         bc1.setService("gamma");
109
110         final Request bc2 = new Request();
111         bc2.setService("gamma");
112
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);
118
119         // Deeper.
120
121         final Request xb = new Request();
122         xb.setService("beta");
123
124         final Request xg = new Request();
125         xg.setService("gamma");
126
127         final Request xd = new Request();
128         xd.setService("delta");
129
130         a.getRequests().add(xb);
131         xb.getRequests().add(xg);
132         xg.getRequests().add(xd);
133
134         // Execute.
135
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);
140
141         assertThat(response.getResponses().size(), is(4));
142
143         Thread.sleep(1000L);
144
145         // Find logfile.
146
147         File log = null;
148         for (final File candidate : sDir.listFiles()) {
149             if (candidate.getName().endsWith(".log")) {
150                 log = candidate;
151                 break;
152             }
153         }
154
155         Assert.assertNotNull(log);
156
157         System.err.println("READING LOGFILE: " + log.getAbsolutePath());
158
159         final CallGraphAnalyzer analyzer = new CallGraphAnalyzer();
160         try (final BufferedReader reader = new BufferedReader(new FileReader(log))) {
161             while (true) {
162
163                 final String line = reader.readLine();
164                 if (line == null) {
165                     break;
166                 }
167
168                 final LogEntry entry = new LogEntry(line);
169                 analyzer.add(entry);
170             }
171         }
172
173         //
174         // Debug during dev, but annoying the rest of the time.
175         //
176         // System.err.println("--------------------------------------------------");
177         // for (final LogEntry e : analyzer.getEntries()) {
178         //     System.err.println(e.toShortString());
179         // }
180         // System.err.println("--------------------------------------------------");
181
182         final CallGraphReportWriter writer = new CallGraphReportWriter(analyzer);
183         final String shortReport = writer.getShortReport();
184         final String longReport = writer.getLongReport();
185
186         // Dump long report.
187
188         System.out.println("----\nGraph:\n\n" + longReport + "\n----");
189
190         // Validate short report.
191
192         assertThat("Alpha\n" +
193                 "    Beta\n" +
194                 "        Gamma\n" +
195                 "        Gamma\n" +
196                 "    Gamma\n" +
197                 "    Delta\n" +
198                 "    Beta\n" +
199                 "        Gamma\n" +
200                 "            Delta\n",
201                 is(shortReport));
202
203         // Ensure output reaches System.xxx.
204
205         Thread.sleep(1000L);
206     }
207 }