From 7f44447a90cff3175af2c44971b513c9ff521af6 Mon Sep 17 00:00:00 2001 From: Amichai Hemli Date: Thu, 14 Nov 2019 11:28:03 +0200 Subject: improve debug logging format Issue-ID: VID-646 Signed-off-by: Amichai Hemli Change-Id: Ifa9105455dabdfad3179c36b516fe9833e0e6e60 Signed-off-by: Amichai Hemli --- .../org/onap/vid/controller/LoggerController.java | 2 +- .../java/org/onap/vid/controller/WebConfig.java | 6 +- .../onap/vid/controller/LoggerControllerTest.java | 15 +++ .../java/org/onap/vid/more/LoggerFormatTest.java | 146 +++++++++++---------- 4 files changed, 94 insertions(+), 75 deletions(-) diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/LoggerController.java b/vid-app-common/src/main/java/org/onap/vid/controller/LoggerController.java index 7233a67e3..f5b325b4a 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/LoggerController.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/LoggerController.java @@ -66,7 +66,7 @@ public class LoggerController extends RestrictedBaseController { this.logfilePathCreator = logfilePathCreator; } - @GetMapping(value = "/{loggerName:audit|audit2019|error|metrics|metrics2019}") + @GetMapping(value = "/{loggerName:audit|audit2019|error|metrics|metrics2019|debug}") public String getLog(@PathVariable String loggerName, HttpServletRequest request, @RequestParam(value="limit", defaultValue = "5000") Integer limit) throws IOException { diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java b/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java index c50578e82..cfb848007 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java @@ -69,6 +69,7 @@ import org.onap.vid.utils.SystemPropertiesWrapper; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.togglz.core.manager.FeatureManager; @@ -222,7 +223,8 @@ public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(new VidLoggingInterceptor( - new ControllersUtils(new SystemPropertiesWrapper()))); + registry.addInterceptor( + new VidLoggingInterceptor(new ControllersUtils(new SystemPropertiesWrapper())) + ).order(Ordered.HIGHEST_PRECEDENCE); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java index fdc0f44d1..f0d840929 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java @@ -108,4 +108,19 @@ public class LoggerControllerTest { .andExpect(content().string("")) .andExpect(status().isOk()); } + + @Test + public void shouldReturnEmptyString_whenDebugLogFileIsEmpty() throws Exception { + List list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1")); + + given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list); + given(provider.userPermissionIsReadLogs(list)).willReturn(true); + given(creator.getLogfilePath("debug")).willReturn(EMPTY_LOG_PATH); + + mockMvc.perform(get("/logger/debug") + .with(req -> {req.setRequestedSessionId("id1"); + return req;})) + .andExpect(content().string("")) + .andExpect(status().isOk()); + } } diff --git a/vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java b/vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java index d9e25578c..b65e797b8 100644 --- a/vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java +++ b/vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java @@ -18,6 +18,8 @@ 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; @@ -34,6 +36,7 @@ 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; @@ -45,7 +48,7 @@ public class LoggerFormatTest extends BaseApiTest { private final int PRIORITY_LAST = 999; public enum LogName { - audit2019, error, metrics2019 + audit2019, error, metrics2019, debug } @BeforeClass @@ -58,15 +61,29 @@ public class LoggerFormatTest extends BaseApiTest { SimulatorApi.registerExpectationFromPreset(new PresetAAIGetSubscribersGet(), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET); } - @Test(priority = PRIORITY_LAST) - public void validateAudit2019LogsFormat() { - String logLines = validateLogsFormat(LogName.audit2019, "audit-ELS-2019.11"); - moreValidationsForAuditFormat(logLines); + @DataProvider + public static Object[][] logsAndFormats(Method test) { + return new Object[][]{ + {LogName.debug, "debug", 0.65 }, + {LogName.metrics2019, "metric-ELS-2019.11", 0.95}, + {LogName.audit2019, "audit-ELS-2019.11", 0.95} + }; + } + + + @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); + } } //more validations for log format that logcheck doesn't verify - private void moreValidationsForAuditFormat(String logLines) { - splitLogLines(logLines).forEach(line->{ + private void moreValidationsForAuditFormat (String logLines){ + splitLogLines(logLines).forEach(line -> { String[] records = line.split("\\|"); assertThat("server name shall be empty", records[5], emptyOrNullString()); @@ -81,28 +98,14 @@ public class LoggerFormatTest extends BaseApiTest { }); } - @Test(priority = PRIORITY_LAST, enabled = false) // no total-score is returned for error-log - public void validateErrorLogsFormat() { - validateLogsFormat(LogName.error); - } - - @Test(priority = PRIORITY_LAST) - public void validateMetrics2019LogsFormat() { - validateLogsFormat(LogName.metrics2019, "metric-ELS-2019.11"); - } - - private void validateLogsFormat(LogName logName) { - validateLogsFormat(logName, logName.name()); - } - - private String validateLogsFormat(LogName logName, String logType) { + private String validateLogsFormat (LogName logName, String logType){ return validateLogsFormat(logName, logType, 0.95); } - private String 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()); @@ -110,27 +113,27 @@ 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, - is(greaterThanOrEqualTo(minRows))); + StringUtils.countMatches(logLines, '\n') + 1, + is(greaterThanOrEqualTo(minRows))); return logLines; } /** * @return Chronological-ordered list of recent log-lines */ - public static List getLogLinesAsList(LogName logname, int maxRows, int minRows, RestTemplate restTemplate, URI uri) { + public static List getLogLinesAsList (LogName logname,int maxRows, int minRows, RestTemplate restTemplate, URI uri){ String logLines = LoggerFormatTest.getLogLines(logname, maxRows, minRows, restTemplate, uri); List lines = splitLogLines(logLines); @@ -141,7 +144,7 @@ public class LoggerFormatTest extends BaseApiTest { } @NotNull - private static List splitLogLines(String logLines) { + private static List splitLogLines (String logLines){ return new ArrayList<>(Arrays.asList(logLines.split("(\\r?\\n)"))); } @@ -149,7 +152,7 @@ public class LoggerFormatTest extends BaseApiTest { /** * @return Chronological-ordered list of recent log-lines of a given requestId */ - public static List getRequestLogLines(String requestId, LogName logname, RestTemplate restTemplate, URI uri) { + public static List getRequestLogLines (String requestId, LogName logname, RestTemplate restTemplate, URI uri){ List lines = getLogLinesAsList(logname, 30, 1, restTemplate, uri); @@ -159,39 +162,38 @@ public class LoggerFormatTest extends BaseApiTest { return lines; } - public static void verifyExistenceOfIncomingReqsInAuditLogs(RestTemplate restTemplate, URI uri, String requestId, - String path){ + public static void verifyExistenceOfIncomingReqsInAuditLogs (RestTemplate restTemplate, URI uri, String requestId, String path){ List 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) { + + " \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 logLines = - getRequestLogLines(requestId, LogName.metrics2019, restTemplate, uri); + getRequestLogLines(requestId, LogName.metrics2019, restTemplate, uri); List requests = retrieveRecordedRequests(); List 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")); @@ -199,7 +201,7 @@ public class LoggerFormatTest extends BaseApiTest { List allInvocationIds = new LinkedList<>(); - underTestRequests.forEach(request->{ + underTestRequests.forEach(request -> { List invocationIds = request.headers.get("X-InvocationID"); assertThat(invocationIds, hasSize(1)); @@ -212,25 +214,25 @@ public class LoggerFormatTest extends BaseApiTest { //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 logLines, String requestId, String invocationId) { + 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) { + 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){ Map params = new HashMap<>(); params.put("format", "raw"); params.put("type", logtype); -- cgit 1.2.3-korg