Merge "change management cypress test"
[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.containsInRelativeOrder;
11 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
12 import static org.hamcrest.Matchers.hasSize;
13 import static vid.automation.test.services.SimulatorApi.retrieveRecordedRequests;
14
15 import com.fasterxml.jackson.databind.JsonNode;
16 import java.net.URI;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
26 import org.onap.vid.api.BaseApiTest;
27 import org.springframework.web.client.RestTemplate;
28 import org.testng.annotations.BeforeClass;
29 import org.testng.annotations.Test;
30 import vid.automation.test.infra.SkipTestUntil;
31 import vid.automation.test.services.SimulatorApi;
32 import vid.automation.test.services.SimulatorApi.RecordedRequests;
33
34 public class LoggerFormatTest extends BaseApiTest {
35
36     private final static String logChecker = System.getProperty("EELF_LOG_CHECKER", "http://my-logchecker:8888/validate");
37     private final Logger logger = LogManager.getLogger(LoggerFormatTest.class);
38
39     public enum LogName {
40         audit, error, audit2019, metrics2019, metrics
41     }
42
43     @BeforeClass
44     public void login() {
45         super.login();
46     }
47
48     @BeforeClass
49     public void setAaiSubscribers() {
50         SimulatorApi.registerExpectationFromPreset(new PresetAAIGetSubscribersGet(), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
51     }
52
53     @SkipTestUntil("2019-09-24")
54     @Test
55     public void validateAuditLogsFormat() {
56         validateLogsFormat(LogName.audit);
57     }
58
59     @Test
60     public void validateAudit2019LogsFormat() {
61         validateLogsFormat(LogName.audit2019, "audit-ELS-2019.11", 0);
62     }
63
64     @Test(enabled = false) // no total-score is returned for error-log
65     public void validateErrorLogsFormat() {
66         validateLogsFormat(LogName.error);
67     }
68
69     @SkipTestUntil("2019-09-24")
70     @Test
71     public void validateMetricsLogsFormat() {
72         validateLogsFormat(LogName.metrics, "metric");
73     }
74
75     private void validateLogsFormat(LogName logName) {
76         validateLogsFormat(logName, logName.name());
77     }
78
79     private void validateLogsFormat(LogName logName, String logType) {
80         validateLogsFormat(logName, logType, 0.95);
81     }
82
83     private void validateLogsFormat(LogName logName, String logType, double score) {
84
85         String logLines = getLogLines(logName);
86         logger.info("logLines are: "+logLines);
87         JsonNode response = getCheckerResults(logType, logLines);
88         logger.info("Response is:" + response.toString());
89         double fieldscore = response.path("summary").path("score").path("fieldscore").asDouble();
90         double overall = response.path("summary").path("score").path("overallscore").asDouble();
91
92         assertThat(fieldscore, is(greaterThanOrEqualTo(score)));
93         assertThat(overall, is(greaterThanOrEqualTo(score)));
94
95     }
96
97     private String getLogLines(LogName logname) {
98         return getLogLines(logname, 5000, 30, restTemplate, uri);
99     }
100
101     public static String getLogLines(LogName logname, int maxRows, int minRows, RestTemplate restTemplate, URI uri) {
102         String logLines = restTemplate.getForObject(uri + "/logger/" + logname.name() + "?limit={maxRows}", String.class, maxRows);
103         assertThat("expecting at least " + minRows + " rows in " + logname.name(),
104                 StringUtils.countMatches(logLines, '\n') + 1,
105                 is(greaterThanOrEqualTo(minRows)));
106         return logLines;
107     }
108
109     /**
110      * @return Chronological-ordered list of recent log-lines of a given requestId
111      */
112     public static List<String> getRequestLogLines(String requestId, LogName logname, RestTemplate restTemplate, URI uri) {
113         String logLines = LoggerFormatTest.getLogLines(logname, 30, 1, restTemplate, uri);
114
115         // Split
116         List<String> lines = new ArrayList<>(Arrays.asList(logLines.split("(\\r?\\n)")));
117
118         // Filter
119         lines.removeIf(line -> !StringUtils.containsIgnoreCase(line, requestId));
120
121         // Reverse
122         reverse(lines);
123
124         return lines;
125     }
126
127     public static void assertHeadersAndMetricLogs(RestTemplate restTemplate, URI uri, String requestId, String path, int requestsSize) {
128         List<String> logLines =
129             getRequestLogLines(requestId, LogName.metrics2019, restTemplate, uri);
130
131         List<RecordedRequests> requests = retrieveRecordedRequests();
132         List<RecordedRequests> underTestRequests =
133             requests.stream().filter(x->x.path.startsWith(path)).collect(toList());
134
135         assertThat(underTestRequests, hasSize(requestsSize));
136
137         underTestRequests.forEach(request-> {
138             assertThat("X-ONAP-RequestID", request.headers.get("X-ONAP-RequestID"), contains(requestId));
139             assertThat("X-ECOMP-RequestID", request.headers.get("X-ECOMP-RequestID"), contains(requestId));
140             assertThat("X-ONAP-PartnerName", request.headers.get("X-ONAP-PartnerName"), contains("VID.VID"));
141         });
142
143         underTestRequests.forEach(request->{
144
145             List<String> invocationIds = request.headers.get("X-ONAP-InvocationID");
146             assertThat(invocationIds, hasSize(1));
147
148             String invocationId = invocationIds.get(0);
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     }
163
164     private JsonNode getCheckerResults(String logtype, String logLines) {
165         Map<String, String> params = new HashMap<>();
166         params.put("format", "raw");
167         params.put("type", logtype);
168         params.put("component", "vid");
169         params.put("data", logLines);
170
171         return restTemplate.postForObject(logChecker, params, JsonNode.class);
172     }
173 }