remove 2016 logger aspects + verify audit score
[vid.git] / vid-automation / src / test / java / org / onap / vid / more / LoggerFormatTest.java
1 package org.onap.vid.more;
2
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;
16
17 import com.fasterxml.jackson.databind.JsonNode;
18 import java.net.URI;
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;
25 import java.util.Map;
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;
36
37 public class LoggerFormatTest extends BaseApiTest {
38
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);
41
42     public enum LogName {
43         audit2019, error, metrics2019
44     }
45
46     @BeforeClass
47     public void login() {
48         super.login();
49     }
50
51     @BeforeClass
52     public void setAaiSubscribers() {
53         SimulatorApi.registerExpectationFromPreset(new PresetAAIGetSubscribersGet(), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
54     }
55
56     @Test
57     public void validateAudit2019LogsFormat() {
58         validateLogsFormat(LogName.audit2019, "audit-ELS-2019.11", 0.8);
59     }
60
61     @Test(enabled = false) // no total-score is returned for error-log
62     public void validateErrorLogsFormat() {
63         validateLogsFormat(LogName.error);
64     }
65
66     @Test
67     public void validateMetrics2019LogsFormat() {
68         validateLogsFormat(LogName.metrics2019, "metric-ELS-2019.11");
69     }
70
71     private void validateLogsFormat(LogName logName) {
72         validateLogsFormat(logName, logName.name());
73     }
74
75     private void validateLogsFormat(LogName logName, String logType) {
76         validateLogsFormat(logName, logType, 0.95);
77     }
78
79     private void validateLogsFormat(LogName logName, String logType, double score) {
80
81         String logLines = getLogLines(logName);
82         logger.info("logLines are: "+logLines);
83         JsonNode response = getCheckerResults(logType, logLines);
84         logger.info("Response is:" + response.toString());
85
86         int total_records = response.path("summary").path("total_records").asInt();
87         int valid_records = response.path("summary").path("valid_records").asInt();
88
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)));
91     }
92
93     private String getLogLines(LogName logname) {
94         return getLogLines(logname, 5000, 30, restTemplate, uri);
95     }
96
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)));
102         return logLines;
103     }
104
105     /**
106      * @return Chronological-ordered list of recent log-lines of a given requestId
107      */
108     public static List<String> getRequestLogLines(String requestId, LogName logname, RestTemplate restTemplate, URI uri) {
109         String logLines = LoggerFormatTest.getLogLines(logname, 30, 1, restTemplate, uri);
110
111         // Split
112         List<String> lines = new ArrayList<>(Arrays.asList(logLines.split("(\\r?\\n)")));
113
114         // Filter
115         lines.removeIf(line -> !StringUtils.containsIgnoreCase(line, requestId));
116
117         // Reverse
118         reverse(lines);
119
120         return lines;
121     }
122
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);
126
127         List<RecordedRequests> requests = retrieveRecordedRequests();
128         List<RecordedRequests> underTestRequests =
129             requests.stream().filter(x->x.path.contains(path)).collect(toList());
130
131         assertThat(underTestRequests, hasSize(requestsSize));
132
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"));
137         });
138
139         List<String> allInvocationIds = new LinkedList<>();
140
141         underTestRequests.forEach(request->{
142
143             List<String> invocationIds = request.headers.get("X-InvocationID");
144             assertThat(invocationIds, hasSize(1));
145
146             String invocationId = invocationIds.get(0);
147             allInvocationIds.add(invocationId);
148
149             assertThat("request id  and invocation id must be found in exactly two rows",
150                 logLines,
151                 containsInRelativeOrder(
152                     allOf(
153                         containsString("RequestID="+requestId),
154                         containsString("InvocationID="+ invocationId),
155                         containsString("Invoke")),
156                     allOf(
157                         containsString("RequestID="+requestId),
158                         containsString("InvocationID="+ invocationId),
159                         containsString("InvokeReturn"))
160                 ));
161         });
162         //make sure no InvocationId is repeated twice
163         assertThat("expect all InvocationIds to be unique",
164             allInvocationIds, containsInAnyOrder(new HashSet<>(allInvocationIds).toArray()));
165     }
166
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);
173
174         return restTemplate.postForObject(logChecker, params, JsonNode.class);
175     }
176 }