1 package org.onap.vid.more;
3 import static java.util.Collections.reverse;
4 import static java.util.stream.Collectors.toList;
5 import static org.hamcrest.CoreMatchers.containsString;
6 import static org.hamcrest.CoreMatchers.is;
7 import static org.hamcrest.MatcherAssert.assertThat;
8 import static org.hamcrest.Matchers.allOf;
9 import static org.hamcrest.Matchers.contains;
10 import static org.hamcrest.Matchers.containsInAnyOrder;
11 import static org.hamcrest.Matchers.containsInRelativeOrder;
12 import static org.hamcrest.Matchers.greaterThan;
13 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
14 import static org.hamcrest.Matchers.hasSize;
15 import static vid.automation.test.services.SimulatorApi.retrieveRecordedRequests;
17 import com.fasterxml.jackson.databind.JsonNode;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.LinkedList;
24 import java.util.List;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
29 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
30 import org.onap.vid.api.BaseApiTest;
31 import org.springframework.web.client.RestTemplate;
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.Test;
34 import vid.automation.test.services.SimulatorApi;
35 import vid.automation.test.services.SimulatorApi.RecordedRequests;
37 public class LoggerFormatTest extends BaseApiTest {
39 private final static String logChecker = System.getProperty("EELF_LOG_CHECKER", "http://my-logchecker:8888/validate");
40 private final Logger logger = LogManager.getLogger(LoggerFormatTest.class);
43 audit, error, audit2019, metrics2019, metrics
52 public void setAaiSubscribers() {
53 SimulatorApi.registerExpectationFromPreset(new PresetAAIGetSubscribersGet(), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
57 public void validateAuditLogsFormat() {
58 validateLogsFormat(LogName.audit);
62 public void validateAudit2019LogsFormat() {
63 validateLogsFormat(LogName.audit2019, "audit-ELS-2019.11", 0);
66 @Test(enabled = false) // no total-score is returned for error-log
67 public void validateErrorLogsFormat() {
68 validateLogsFormat(LogName.error);
72 public void validateMetricsLogsFormat() {
73 validateLogsFormat(LogName.metrics, "metric");
77 public void validateMetrics2019LogsFormat() {
78 validateLogsFormat(LogName.metrics2019, "metric-ELS-2019.11");
81 private void validateLogsFormat(LogName logName) {
82 validateLogsFormat(logName, logName.name());
85 private void validateLogsFormat(LogName logName, String logType) {
86 validateLogsFormat(logName, logType, 0.95);
89 private void validateLogsFormat(LogName logName, String logType, double score) {
91 String logLines = getLogLines(logName);
92 logger.info("logLines are: "+logLines);
93 JsonNode response = getCheckerResults(logType, logLines);
94 logger.info("Response is:" + response.toString());
96 int total_records = response.path("summary").path("total_records").asInt();
97 int valid_records = response.path("summary").path("valid_records").asInt();
99 assertThat(total_records, greaterThan(30)); //make sure we have at least 30 total records
100 assertThat((double)valid_records/total_records, is(greaterThanOrEqualTo(score)));
103 private String getLogLines(LogName logname) {
104 return getLogLines(logname, 5000, 30, restTemplate, uri);
107 public static String getLogLines(LogName logname, int maxRows, int minRows, RestTemplate restTemplate, URI uri) {
108 String logLines = restTemplate.getForObject(uri + "/logger/" + logname.name() + "?limit={maxRows}", String.class, maxRows);
109 assertThat("expecting at least " + minRows + " rows in " + logname.name(),
110 StringUtils.countMatches(logLines, '\n') + 1,
111 is(greaterThanOrEqualTo(minRows)));
116 * @return Chronological-ordered list of recent log-lines of a given requestId
118 public static List<String> getRequestLogLines(String requestId, LogName logname, RestTemplate restTemplate, URI uri) {
119 String logLines = LoggerFormatTest.getLogLines(logname, 30, 1, restTemplate, uri);
122 List<String> lines = new ArrayList<>(Arrays.asList(logLines.split("(\\r?\\n)")));
125 lines.removeIf(line -> !StringUtils.containsIgnoreCase(line, requestId));
133 public static void assertHeadersAndMetricLogs(RestTemplate restTemplate, URI uri, String requestId, String path, int requestsSize) {
134 List<String> logLines =
135 getRequestLogLines(requestId, LogName.metrics2019, restTemplate, uri);
137 List<RecordedRequests> requests = retrieveRecordedRequests();
138 List<RecordedRequests> underTestRequests =
139 requests.stream().filter(x->x.path.contains(path)).collect(toList());
141 assertThat(underTestRequests, hasSize(requestsSize));
143 underTestRequests.forEach(request-> {
144 assertThat("X-ONAP-RequestID", request.headers.get("X-ONAP-RequestID"), contains(requestId));
145 assertThat("X-ECOMP-RequestID", request.headers.get("X-ECOMP-RequestID"), contains(requestId));
146 assertThat("X-ONAP-PartnerName", request.headers.get("X-ONAP-PartnerName"), contains("VID.VID"));
149 List<String> allInvocationIds = new LinkedList<>();
151 underTestRequests.forEach(request->{
153 List<String> invocationIds = request.headers.get("X-InvocationID");
154 assertThat(invocationIds, hasSize(1));
156 String invocationId = invocationIds.get(0);
157 allInvocationIds.add(invocationId);
159 assertThat("request id and invocation id must be found in exactly two rows",
161 containsInRelativeOrder(
163 containsString("RequestID="+requestId),
164 containsString("InvocationID="+ invocationId),
165 containsString("Invoke")),
167 containsString("RequestID="+requestId),
168 containsString("InvocationID="+ invocationId),
169 containsString("InvokeReturn"))
172 //make sure no InvocationId is repeated twice
173 assertThat("expect all InvocationIds to be unique",
174 allInvocationIds, containsInAnyOrder(new HashSet<>(allInvocationIds).toArray()));
177 private JsonNode getCheckerResults(String logtype, String logLines) {
178 Map<String, String> params = new HashMap<>();
179 params.put("format", "raw");
180 params.put("type", logtype);
181 params.put("component", "vid");
182 params.put("data", logLines);
184 return restTemplate.postForObject(logChecker, params, JsonNode.class);