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 audit2019, error, metrics2019
52 public void setAaiSubscribers() {
53 SimulatorApi.registerExpectationFromPreset(new PresetAAIGetSubscribersGet(), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
57 public void validateAudit2019LogsFormat() {
58 validateLogsFormat(LogName.audit2019, "audit-ELS-2019.11", 0.8);
61 @Test(enabled = false) // no total-score is returned for error-log
62 public void validateErrorLogsFormat() {
63 validateLogsFormat(LogName.error);
67 public void validateMetrics2019LogsFormat() {
68 validateLogsFormat(LogName.metrics2019, "metric-ELS-2019.11");
71 private void validateLogsFormat(LogName logName) {
72 validateLogsFormat(logName, logName.name());
75 private void validateLogsFormat(LogName logName, String logType) {
76 validateLogsFormat(logName, logType, 0.95);
79 private void validateLogsFormat(LogName logName, String logType, double score) {
81 String logLines = getLogLines(logName);
82 logger.info("logLines are: "+logLines);
83 JsonNode response = getCheckerResults(logType, logLines);
84 logger.info("Response is:" + response.toString());
86 int total_records = response.path("summary").path("total_records").asInt();
87 int valid_records = response.path("summary").path("valid_records").asInt();
89 assertThat(total_records, greaterThan(30)); //make sure we have at least 30 total records
90 assertThat((double)valid_records/total_records, is(greaterThanOrEqualTo(score)));
93 private String getLogLines(LogName logname) {
94 return getLogLines(logname, 5000, 30, restTemplate, uri);
97 public static String getLogLines(LogName logname, int maxRows, int minRows, RestTemplate restTemplate, URI uri) {
98 String logLines = restTemplate.getForObject(uri + "/logger/" + logname.name() + "?limit={maxRows}", String.class, maxRows);
99 assertThat("expecting at least " + minRows + " rows in " + logname.name(),
100 StringUtils.countMatches(logLines, '\n') + 1,
101 is(greaterThanOrEqualTo(minRows)));
106 * @return Chronological-ordered list of recent log-lines of a given requestId
108 public static List<String> getRequestLogLines(String requestId, LogName logname, RestTemplate restTemplate, URI uri) {
109 String logLines = LoggerFormatTest.getLogLines(logname, 30, 1, restTemplate, uri);
112 List<String> lines = new ArrayList<>(Arrays.asList(logLines.split("(\\r?\\n)")));
115 lines.removeIf(line -> !StringUtils.containsIgnoreCase(line, requestId));
123 public static void assertHeadersAndMetricLogs(RestTemplate restTemplate, URI uri, String requestId, String path, int requestsSize) {
124 List<String> logLines =
125 getRequestLogLines(requestId, LogName.metrics2019, restTemplate, uri);
127 List<RecordedRequests> requests = retrieveRecordedRequests();
128 List<RecordedRequests> underTestRequests =
129 requests.stream().filter(x->x.path.contains(path)).collect(toList());
131 assertThat(underTestRequests, hasSize(requestsSize));
133 underTestRequests.forEach(request-> {
134 assertThat("X-ONAP-RequestID", request.headers.get("X-ONAP-RequestID"), contains(requestId));
135 assertThat("X-ECOMP-RequestID", request.headers.get("X-ECOMP-RequestID"), contains(requestId));
136 assertThat("X-ONAP-PartnerName", request.headers.get("X-ONAP-PartnerName"), contains("VID.VID"));
139 List<String> allInvocationIds = new LinkedList<>();
141 underTestRequests.forEach(request->{
143 List<String> invocationIds = request.headers.get("X-InvocationID");
144 assertThat(invocationIds, hasSize(1));
146 String invocationId = invocationIds.get(0);
147 allInvocationIds.add(invocationId);
149 assertThat("request id and invocation id must be found in exactly two rows",
151 containsInRelativeOrder(
153 containsString("RequestID="+requestId),
154 containsString("InvocationID="+ invocationId),
155 containsString("Invoke")),
157 containsString("RequestID="+requestId),
158 containsString("InvocationID="+ invocationId),
159 containsString("InvokeReturn"))
162 //make sure no InvocationId is repeated twice
163 assertThat("expect all InvocationIds to be unique",
164 allInvocationIds, containsInAnyOrder(new HashSet<>(allInvocationIds).toArray()));
167 private JsonNode getCheckerResults(String logtype, String logLines) {
168 Map<String, String> params = new HashMap<>();
169 params.put("format", "raw");
170 params.put("type", logtype);
171 params.put("component", "vid");
172 params.put("data", logLines);
174 return restTemplate.postForObject(logChecker, params, JsonNode.class);