Merge "Fix getManualTasks double-addition of baseUrl"
[vid.git] / vid-automation / src / test / java / org / onap / vid / more / LoggerFormatTest.java
1 package org.onap.vid.more;
2
3 import static org.hamcrest.CoreMatchers.is;
4 import static org.hamcrest.Matchers.greaterThan;
5 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
6 import static org.junit.Assert.assertThat;
7
8 import com.fasterxml.jackson.databind.JsonNode;
9 import java.net.URI;
10 import java.util.HashMap;
11 import java.util.Map;
12 import org.apache.commons.lang3.StringUtils;
13 import org.apache.logging.log4j.LogManager;
14 import org.apache.logging.log4j.Logger;
15 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
16 import org.onap.vid.api.BaseApiTest;
17 import org.springframework.web.client.RestTemplate;
18 import org.testng.annotations.BeforeClass;
19 import org.testng.annotations.Test;
20 import vid.automation.test.services.SimulatorApi;
21
22 public class LoggerFormatTest extends BaseApiTest {
23
24
25     // See: https://www.openecomp.org/KSAT/REST-based+Log+Checker
26     private final static String logChecker = "http://eelf.onap.org:31820/validate";
27     private final Logger logger = LogManager.getLogger(LoggerFormatTest.class);
28
29     @BeforeClass
30     public void login() {
31         super.login();
32     }
33
34     @BeforeClass
35     public void setAaiSubscribers() {
36         SimulatorApi.registerExpectationFromPreset(new PresetAAIGetSubscribersGet(), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
37     }
38
39     @Test
40     public void validateAuditLogsFormat() {
41         validateLogsFormat("audit");
42     }
43
44     @Test(enabled = false) // no total-score is returned for error-log
45     public void validateErrorLogsFormat() {
46         validateLogsFormat("error");
47     }
48
49     @Test
50     public void validateMetricsLogsFormat() {
51         validateLogsFormat("metrics", "metric");
52     }
53
54     private void validateLogsFormat(String logName) {
55         validateLogsFormat(logName, logName);
56     }
57
58     private void validateLogsFormat(String logName, String logType) {
59
60         String logLines = getLogLines(logName);
61         logger.info("logLines are: "+logLines);
62         JsonNode response = getCheckerResults(logType, logLines);
63         logger.info("Response is:" + response.toString());
64         double fieldscore = response.path("summary").path("score").path("fieldscore").asDouble();
65         double overall = response.path("summary").path("score").path("overallscore").asDouble();
66
67         assertThat(fieldscore, is(greaterThan(0.95)));
68         assertThat(overall, is(greaterThan(0.95)));
69
70     }
71
72     private String getLogLines(String logname) {
73         return getLogLines(logname, 5000, 30, restTemplate, uri);
74     }
75
76     public static String getLogLines(String logname, int maxRows, int minRows, RestTemplate restTemplate, URI uri) {
77         String logLines = restTemplate.getForObject(uri + "/logger/" + logname + "?limit={maxRows}", String.class, maxRows);
78         assertThat("expecting at least " + minRows + " rows in " + logname,
79                 StringUtils.countMatches(logLines, '\n') + 1,
80                 is(greaterThanOrEqualTo(minRows)));
81         return logLines;
82     }
83
84     private JsonNode getCheckerResults(String logtype, String logLines) {
85         Map<String, String> params = new HashMap<>();
86         params.put("format", "raw");
87         params.put("type", logtype);
88         params.put("component", "vid");
89         params.put("data", logLines);
90
91         return restTemplate.postForObject(logChecker, params, JsonNode.class);
92     }
93 }