run logging test for the error log as well
[vid.git] / vid-automation / src / test / java / org / onap / vid / more / LoggerFormatTest.java
index 81d2553..9651002 100644 (file)
@@ -9,12 +9,17 @@ import static org.hamcrest.Matchers.allOf;
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.containsInRelativeOrder;
+import static org.hamcrest.Matchers.either;
+import static org.hamcrest.Matchers.emptyOrNullString;
 import static org.hamcrest.Matchers.greaterThan;
 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
 import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.matchesPattern;
 import static vid.automation.test.services.SimulatorApi.retrieveRecordedRequests;
 
 import com.fasterxml.jackson.databind.JsonNode;
+
+import java.lang.reflect.Method;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -26,10 +31,12 @@ import java.util.Map;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
+import org.jetbrains.annotations.NotNull;
 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
 import org.onap.vid.api.BaseApiTest;
 import org.springframework.web.client.RestTemplate;
 import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 import vid.automation.test.services.SimulatorApi;
 import vid.automation.test.services.SimulatorApi.RecordedRequests;
@@ -38,9 +45,10 @@ public class LoggerFormatTest extends BaseApiTest {
 
     private final static String logChecker = System.getProperty("EELF_LOG_CHECKER", "http://my-logchecker:8888/validate");
     private final Logger logger = LogManager.getLogger(LoggerFormatTest.class);
+    private final int PRIORITY_LAST = 999;
 
     public enum LogName {
-        audit2019, error, metrics2019
+        audit2019, error, metrics2019, debug
     }
 
     @BeforeClass
@@ -53,33 +61,52 @@ public class LoggerFormatTest extends BaseApiTest {
         SimulatorApi.registerExpectationFromPreset(new PresetAAIGetSubscribersGet(), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
     }
 
-    @Test
-    public void validateAudit2019LogsFormat() {
-        validateLogsFormat(LogName.audit2019, "audit-ELS-2019.11", 0.8);
+    @DataProvider
+    public static Object[][] logsAndFormats(Method test) {
+        return new Object[][]{
+                {LogName.debug, "debug", 0.95 },
+                {LogName.metrics2019, "metric-ELS-2019.11", 0.95},
+                {LogName.audit2019, "audit-ELS-2019.11", 0.95},
+                {LogName.error, "error", 0.75 }
+        };
     }
 
-    @Test(enabled = false) // no total-score is returned for error-log
-    public void validateErrorLogsFormat() {
-        validateLogsFormat(LogName.error);
-    }
 
-    @Test
-    public void validateMetrics2019LogsFormat() {
-        validateLogsFormat(LogName.metrics2019, "metric-ELS-2019.11");
+    @Test(dataProvider = "logsAndFormats", priority = PRIORITY_LAST)
+    public void validateLogsAndFormat(LogName logName, String logCheckerFormat, Double expectedRank){
+        String logLines = validateLogsFormat(logName, logCheckerFormat, expectedRank);
+
+        if (logName == LogName.audit2019)
+        {
+            moreValidationsForAuditFormat(logLines);
+        }
     }
 
-    private void validateLogsFormat(LogName logName) {
-        validateLogsFormat(logName, logName.name());
+    //more validations for log format that logcheck doesn't verify
+    private void moreValidationsForAuditFormat (String logLines){
+        splitLogLines(logLines).forEach(line -> {
+            String[] records = line.split("\\|");
+            assertThat("server name shall be empty", records[5], emptyOrNullString());
+
+            //authenticated request shall logs with userId.
+            final String serviceName = records[6];
+            if (StringUtils.containsAny(serviceName, "aai", "mso")) {
+                assertThat("Partner name shall be userId", records[7], matchesPattern("^[A-Za-z0-9]{4,15}$"));
+            }
+
+            assertThat("Severity shall be empty", records[13], emptyOrNullString());
+            assertThat("marker", records[21], either(is("ENTRY")).or(is("EXIT")));
+        });
     }
 
-    private void validateLogsFormat(LogName logName, String logType) {
-        validateLogsFormat(logName, logType, 0.95);
+    private String validateLogsFormat (LogName logName, String logType){
+        return validateLogsFormat(logName, logType, 0.95);
     }
 
-    private void validateLogsFormat(LogName logName, String logType, double score) {
+    private String validateLogsFormat (LogName logName, String logType,double score){
 
         String logLines = getLogLines(logName);
-        logger.info("logLines are: "+logLines);
+        logger.info("logLines are: " + logLines);
         JsonNode response = getCheckerResults(logType, logLines);
         logger.info("Response is:" + response.toString());
 
@@ -87,14 +114,16 @@ public class LoggerFormatTest extends BaseApiTest {
         int valid_records = response.path("summary").path("valid_records").asInt();
 
         assertThat(total_records, greaterThan(30)); //make sure we have at least 30 total records
-        assertThat((double)valid_records/total_records, is(greaterThanOrEqualTo(score)));
+        assertThat((double) valid_records / total_records, is(greaterThanOrEqualTo(score)));
+
+        return logLines;
     }
 
-    private String getLogLines(LogName logname) {
+    private String getLogLines (LogName logname){
         return getLogLines(logname, 5000, 30, restTemplate, uri);
     }
 
-    public static String getLogLines(LogName logname, int maxRows, int minRows, RestTemplate restTemplate, URI uri) {
+    public static String getLogLines (LogName logname,int maxRows, int minRows, RestTemplate restTemplate, URI uri){
         String logLines = restTemplate.getForObject(uri + "/logger/" + logname.name() + "?limit={maxRows}", String.class, maxRows);
         assertThat("expecting at least " + minRows + " rows in " + logname.name(),
                 StringUtils.countMatches(logLines, '\n') + 1,
@@ -102,35 +131,70 @@ public class LoggerFormatTest extends BaseApiTest {
         return logLines;
     }
 
+    /**
+     * @return Chronological-ordered list of recent log-lines
+     */
+    public static List<String> getLogLinesAsList (LogName logname,int maxRows, int minRows, RestTemplate restTemplate, URI uri){
+        String logLines = LoggerFormatTest.getLogLines(logname, maxRows, minRows, restTemplate, uri);
+        List<String> lines = splitLogLines(logLines);
+
+        // Reverse
+        reverse(lines);
+
+        return lines;
+    }
+
+    @NotNull
+    private static List<String> splitLogLines (String logLines){
+        return new ArrayList<>(Arrays.asList(logLines.split("(\\r?\\n)")));
+    }
+
+
     /**
      * @return Chronological-ordered list of recent log-lines of a given requestId
      */
-    public static List<String> getRequestLogLines(String requestId, LogName logname, RestTemplate restTemplate, URI uri) {
-        String logLines = LoggerFormatTest.getLogLines(logname, 30, 1, restTemplate, uri);
+    public static List<String> getRequestLogLines (String requestId, LogName logname, RestTemplate restTemplate, URI uri){
 
-        // Split
-        List<String> lines = new ArrayList<>(Arrays.asList(logLines.split("(\\r?\\n)")));
+        List<String> lines = getLogLinesAsList(logname, 30, 1, restTemplate, uri);
 
-        // Filter
+        //Filter
         lines.removeIf(line -> !StringUtils.containsIgnoreCase(line, requestId));
 
-        // Reverse
-        reverse(lines);
-
         return lines;
     }
 
-    public static void assertHeadersAndMetricLogs(RestTemplate restTemplate, URI uri, String requestId, String path, int requestsSize) {
+    public static void verifyExistenceOfIncomingReqsInAuditLogs (RestTemplate restTemplate, URI uri, String requestId, String path){
+        List<String> logLines = getRequestLogLines(requestId, LogName.audit2019, restTemplate, uri);
+        String requestIdPrefix = "RequestID=";
+        assertThat("\nENTRY & EXIT logs are expected to include RequestId: " + requestId
+                        + " \nAnd request path: "
+                        + path +
+                        "\nin exactly two rows - inside the audit log matching lines:\n"
+                        + String.join("\n", logLines) + "\n",
+                logLines,
+                contains(
+                        allOf(
+                                containsString(requestIdPrefix + requestId),
+                                containsString("ENTRY"),
+                                containsString(path)),
+                        allOf(
+                                containsString(requestIdPrefix + requestId),
+                                containsString("EXIT"),
+                                containsString(path))
+                ));
+    }
+
+    public static void assertHeadersAndMetricLogs (RestTemplate restTemplate, URI uri, String requestId, String path, int requestsSize){
         List<String> logLines =
-            getRequestLogLines(requestId, LogName.metrics2019, restTemplate, uri);
+                getRequestLogLines(requestId, LogName.metrics2019, restTemplate, uri);
 
         List<RecordedRequests> requests = retrieveRecordedRequests();
         List<RecordedRequests> underTestRequests =
-            requests.stream().filter(x->x.path.contains(path)).collect(toList());
+                requests.stream().filter(x -> x.path.contains(path)).collect(toList());
 
         assertThat(underTestRequests, hasSize(requestsSize));
 
-        underTestRequests.forEach(request-> {
+        underTestRequests.forEach(request -> {
             assertThat("X-ONAP-RequestID", request.headers.get("X-ONAP-RequestID"), contains(requestId));
             assertThat("X-ECOMP-RequestID", request.headers.get("X-ECOMP-RequestID"), contains(requestId));
             assertThat("X-ONAP-PartnerName", request.headers.get("X-ONAP-PartnerName"), contains("VID.VID"));
@@ -138,7 +202,7 @@ public class LoggerFormatTest extends BaseApiTest {
 
         List<String> allInvocationIds = new LinkedList<>();
 
-        underTestRequests.forEach(request->{
+        underTestRequests.forEach(request -> {
 
             List<String> invocationIds = request.headers.get("X-InvocationID");
             assertThat(invocationIds, hasSize(1));
@@ -146,25 +210,30 @@ public class LoggerFormatTest extends BaseApiTest {
             String invocationId = invocationIds.get(0);
             allInvocationIds.add(invocationId);
 
-            assertThat("request id  and invocation id must be found in exactly two rows",
-                logLines,
-                containsInRelativeOrder(
-                    allOf(
-                        containsString("RequestID="+requestId),
-                        containsString("InvocationID="+ invocationId),
-                        containsString("Invoke")),
-                    allOf(
-                        containsString("RequestID="+requestId),
-                        containsString("InvocationID="+ invocationId),
-                        containsString("InvokeReturn"))
-                ));
+            assertIdsInMetricsLog(logLines, requestId, invocationId);
         });
+
         //make sure no InvocationId is repeated twice
         assertThat("expect all InvocationIds to be unique",
-            allInvocationIds, containsInAnyOrder(new HashSet<>(allInvocationIds).toArray()));
+                allInvocationIds, containsInAnyOrder(new HashSet<>(allInvocationIds).toArray()));
+    }
+
+    public static void assertIdsInMetricsLog (List < String > logLines, String requestId, String invocationId){
+        assertThat("request id and invocation id must be found in exactly two rows in: \n" + String.join("\n", logLines),
+                logLines,
+                containsInRelativeOrder(
+                        allOf(
+                                containsString("RequestID=" + requestId),
+                                containsString("InvocationID=" + invocationId),
+                                containsString("Invoke")),
+                        allOf(
+                                containsString("RequestID=" + requestId),
+                                containsString("InvocationID=" + invocationId),
+                                containsString("InvokeReturn"))
+                ));
     }
 
-    private JsonNode getCheckerResults(String logtype, String logLines) {
+    private JsonNode getCheckerResults (String logtype, String logLines){
         Map<String, String> params = new HashMap<>();
         params.put("format", "raw");
         params.put("type", logtype);