Search in sources :

Example 1 with DashboardLogEntryDTOGroup

use of org.mockserver.dashboard.model.DashboardLogEntryDTOGroup in project mockserver by mock-server.

the class DashboardLogEntryDTOGroupSerializerTest method shouldSerialiseGroupWithSingleEvent.

@Test
public void shouldSerialiseGroupWithSingleEvent() throws JsonProcessingException {
    // given
    DashboardLogEntryDTOGroup dashboardLogEntryDTOGroup = new DashboardLogEntryDTOGroup(descriptionProcessor);
    LogEntry logEntry = new LogEntry().setLogLevel(Level.WARN).setEpochTime(epochTime).setType(LogEntry.LogMessageType.TEMPLATE_GENERATED).setMessageFormat("some random{}formatted string{}").setArguments("one", "two");
    dashboardLogEntryDTOGroup.getLogEntryDTOS().add(new DashboardLogEntryDTO(logEntry).setDescription(getDescription(logEntry)));
    // when
    String json = objectWriter.writeValueAsString(dashboardLogEntryDTOGroup);
    // then
    assertThat(json, is("{" + NEW_LINE + "  \"key\" : \"" + logEntry.id() + "_log\"," + NEW_LINE + "  \"value\" : {" + NEW_LINE + "    \"description\" : \"" + timeStamp + " TEMPLATE_GENERATED \"," + NEW_LINE + "    \"style\" : {" + NEW_LINE + "      \"paddingBottom\" : \"4px\"," + NEW_LINE + "      \"whiteSpace\" : \"nowrap\"," + NEW_LINE + "      \"overflow\" : \"auto\"," + NEW_LINE + "      \"color\" : \"rgb(241, 186, 27)\"," + NEW_LINE + "      \"paddingTop\" : \"4px\"" + NEW_LINE + "    }," + NEW_LINE + "    \"messageParts\" : [ {" + NEW_LINE + "      \"key\" : \"" + logEntry.id() + "_0msg\"," + NEW_LINE + "      \"value\" : \"some random\"" + NEW_LINE + "    }, {" + NEW_LINE + "      \"key\" : \"" + logEntry.id() + "_0arg\"," + NEW_LINE + "      \"multiline\" : false," + NEW_LINE + "      \"argument\" : true," + NEW_LINE + "      \"value\" : \"\\\"one\\\"\"" + NEW_LINE + "    }, {" + NEW_LINE + "      \"key\" : \"" + logEntry.id() + "_1msg\"," + NEW_LINE + "      \"value\" : \"formatted string\"" + NEW_LINE + "    }, {" + NEW_LINE + "      \"key\" : \"" + logEntry.id() + "_1arg\"," + NEW_LINE + "      \"multiline\" : false," + NEW_LINE + "      \"argument\" : true," + NEW_LINE + "      \"value\" : \"\\\"two\\\"\"" + NEW_LINE + "    } ]" + NEW_LINE + "  }" + NEW_LINE + "}"));
}
Also used : DashboardLogEntryDTO(org.mockserver.dashboard.model.DashboardLogEntryDTO) DashboardLogEntryDTOGroup(org.mockserver.dashboard.model.DashboardLogEntryDTOGroup) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 2 with DashboardLogEntryDTOGroup

use of org.mockserver.dashboard.model.DashboardLogEntryDTOGroup in project mockserver by mock-server.

the class DashboardLogEntryDTOGroupSerializerTest method shouldSerialiseGroupWithMultipleEvents.

@Test
public void shouldSerialiseGroupWithMultipleEvents() throws JsonProcessingException {
    // given
    DashboardLogEntryDTOGroup dashboardLogEntryDTOGroup = new DashboardLogEntryDTOGroup(descriptionProcessor);
    LogEntry logEntryForwardRequest = new LogEntry().setLogLevel(Level.WARN).setEpochTime(epochTime).setType(LogEntry.LogMessageType.FORWARDED_REQUEST).setMessageFormat("some random{}formatted string{}").setArguments("one", "two");
    dashboardLogEntryDTOGroup.getLogEntryDTOS().add(new DashboardLogEntryDTO(logEntryForwardRequest).setDescription(getDescription(logEntryForwardRequest)));
    LogEntry logEntryExpectationNotMatched = new LogEntry().setLogLevel(Level.WARN).setEpochTime(epochTime).setType(LogEntry.LogMessageType.EXPECTATION_NOT_MATCHED).setMessageFormat("some random{}formatted string{}").setArguments("one", "two");
    dashboardLogEntryDTOGroup.getLogEntryDTOS().add(new DashboardLogEntryDTO(logEntryExpectationNotMatched).setDescription(getDescription(logEntryExpectationNotMatched)));
    // when
    String json = objectWriter.writeValueAsString(dashboardLogEntryDTOGroup);
    // then
    assertThat(json, is("{\n" + "  \"key\" : \"" + logEntryForwardRequest.id() + "_log_group\",\n" + "  \"group\" : {\n" + "    \"key\" : \"" + logEntryForwardRequest.id() + "_log\",\n" + "    \"value\" : {\n" + "      \"description\" : \"" + timeStamp + " FORWARDED_REQUEST   \",\n" + "      \"style\" : {\n" + "        \"paddingBottom\" : \"4px\",\n" + "        \"whiteSpace\" : \"nowrap\",\n" + "        \"overflow\" : \"auto\",\n" + "        \"color\" : \"rgb(152, 208, 255)\",\n" + "        \"paddingTop\" : \"4px\"\n" + "      }\n" + "    }\n" + "  },\n" + "  \"value\" : [ {\n" + "    \"key\" : \"" + logEntryForwardRequest.id() + "_log\",\n" + "    \"value\" : {\n" + "      \"description\" : \"" + timeStamp + " FORWARDED_REQUEST   \",\n" + "      \"style\" : {\n" + "        \"paddingBottom\" : \"4px\",\n" + "        \"whiteSpace\" : \"nowrap\",\n" + "        \"overflow\" : \"auto\",\n" + "        \"color\" : \"rgb(152, 208, 255)\",\n" + "        \"paddingTop\" : \"4px\"\n" + "      },\n" + "      \"messageParts\" : [ {\n" + "        \"key\" : \"" + logEntryForwardRequest.id() + "_0msg\",\n" + "        \"value\" : \"some random\"\n" + "      }, {\n" + "        \"key\" : \"" + logEntryForwardRequest.id() + "_0arg\",\n" + "        \"multiline\" : false,\n" + "        \"argument\" : true,\n" + "        \"value\" : \"\\\"one\\\"\"\n" + "      }, {\n" + "        \"key\" : \"" + logEntryForwardRequest.id() + "_1msg\",\n" + "        \"value\" : \"formatted string\"\n" + "      }, {\n" + "        \"key\" : \"" + logEntryForwardRequest.id() + "_1arg\",\n" + "        \"multiline\" : false,\n" + "        \"argument\" : true,\n" + "        \"value\" : \"\\\"two\\\"\"\n" + "      } ]\n" + "    }\n" + "  }, {\n" + "    \"key\" : \"" + logEntryExpectationNotMatched.id() + "_log\",\n" + "    \"value\" : {\n" + "      \"description\" : \"" + timeStamp + " EXPECTATION_NOT_MATCHED \",\n" + "      \"style\" : {\n" + "        \"paddingBottom\" : \"4px\",\n" + "        \"whiteSpace\" : \"nowrap\",\n" + "        \"overflow\" : \"auto\",\n" + "        \"color\" : \"rgb(204,165,163)\",\n" + "        \"paddingTop\" : \"4px\"\n" + "      },\n" + "      \"messageParts\" : [ {\n" + "        \"key\" : \"" + logEntryExpectationNotMatched.id() + "_0msg\",\n" + "        \"value\" : \"some random\"\n" + "      }, {\n" + "        \"key\" : \"" + logEntryExpectationNotMatched.id() + "_0arg\",\n" + "        \"multiline\" : false,\n" + "        \"argument\" : true,\n" + "        \"value\" : \"\\\"one\\\"\"\n" + "      }, {\n" + "        \"key\" : \"" + logEntryExpectationNotMatched.id() + "_1msg\",\n" + "        \"value\" : \"formatted string\"\n" + "      }, {\n" + "        \"key\" : \"" + logEntryExpectationNotMatched.id() + "_1arg\",\n" + "        \"multiline\" : false,\n" + "        \"argument\" : true,\n" + "        \"value\" : \"\\\"two\\\"\"\n" + "      } ]\n" + "    }\n" + "  } ]\n" + "}"));
}
Also used : DashboardLogEntryDTO(org.mockserver.dashboard.model.DashboardLogEntryDTO) DashboardLogEntryDTOGroup(org.mockserver.dashboard.model.DashboardLogEntryDTOGroup) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 3 with DashboardLogEntryDTOGroup

use of org.mockserver.dashboard.model.DashboardLogEntryDTOGroup in project mockserver by mock-server.

the class DashboardWebSocketHandler method sendUpdate.

private void sendUpdate(ChannelOutboundInvoker ctx, RequestDefinition httpRequest, int retryCount) {
    DescriptionProcessor activeExpectationsDescriptionProcessor = new DescriptionProcessor();
    DescriptionProcessor logMessagesDescriptionProcessor = new DescriptionProcessor();
    DescriptionProcessor recordedRequestsDescriptionProcessor = new DescriptionProcessor();
    DescriptionProcessor proxiedRequestsDescriptionProcessor = new DescriptionProcessor();
    mockServerEventLog.retrieveLogEntriesInReverseForUI(httpRequest, logEntry -> true, DashboardLogEntryDTO::new, reverseLogEventsStream -> {
        List<ImmutableMap<String, Object>> activeExpectations = requestMatchers.retrieveRequestMatchers(httpRequest).stream().limit(UI_UPDATE_ITEM_LIMIT).map(requestMatcher -> {
            JsonNode expectationJsonNode = objectMapper.valueToTree(new ExpectationDTO(requestMatcher.getExpectation()));
            if (requestMatcher.getExpectation().getHttpRequest() instanceof OpenAPIDefinition) {
                JsonNode httpRequestJsonNode = expectationJsonNode.get("httpRequest");
                if (httpRequestJsonNode instanceof ObjectNode) {
                    ((ObjectNode) httpRequestJsonNode).set("requestMatchers", objectMapper.valueToTree(requestMatcher.getHttpRequests()));
                }
            }
            Description description = activeExpectationsDescriptionProcessor.description(requestMatcher.getExpectation().getHttpRequest(), requestMatcher.getExpectation().getId());
            return ImmutableMap.of("key", requestMatcher.getExpectation().getId(), "description", description != null ? description : requestMatcher.getExpectation().getId(), "value", expectationJsonNode);
        }).collect(Collectors.toList());
        List<Map<String, Object>> proxiedRequests = new LinkedList<>();
        List<Map<String, Object>> recordedRequests = new LinkedList<>();
        List<Object> logMessages = new LinkedList<>();
        Map<String, DashboardLogEntryDTOGroup> logEntryGroups = new HashMap<>();
        reverseLogEventsStream.forEach(logEntryDTO -> {
            if (logEntryDTO != null) {
                if (logMessages.size() < UI_UPDATE_ITEM_LIMIT) {
                    DashboardLogEntryDTO dashboardLogEntryDTO = logEntryDTO.setDescription(logMessagesDescriptionProcessor.description(logEntryDTO));
                    if (isNotBlank(logEntryDTO.getCorrelationId()) && logEntryDTO.getType() != TRACE) {
                        DashboardLogEntryDTOGroup logEntryGroup = logEntryGroups.get(logEntryDTO.getCorrelationId());
                        if (logEntryGroup == null) {
                            logEntryGroup = new DashboardLogEntryDTOGroup(logMessagesDescriptionProcessor);
                            logEntryGroups.put(logEntryDTO.getCorrelationId(), logEntryGroup);
                            logMessages.add(logEntryGroup);
                        }
                        logEntryGroup.getLogEntryDTOS().add(dashboardLogEntryDTO);
                    } else {
                        logMessages.add(dashboardLogEntryDTO);
                    }
                }
                if (recordedRequestsPredicate.test(logEntryDTO) && recordedRequests.size() < UI_UPDATE_ITEM_LIMIT) {
                    for (RequestDefinition request : logEntryDTO.getHttpRequests()) {
                        if (request != null) {
                            Map<String, Object> entry = new HashMap<>();
                            entry.put("key", logEntryDTO.getId() + "_request");
                            Description description = recordedRequestsDescriptionProcessor.description(request);
                            if (description != null) {
                                entry.put("description", description);
                            }
                            entry.put("value", request);
                            recordedRequests.add(entry);
                        }
                    }
                }
                if (proxiedRequestsPredicate.test(logEntryDTO) && proxiedRequests.size() < UI_UPDATE_ITEM_LIMIT) {
                    Map<String, Object> value = new HashMap<>();
                    if (logEntryDTO.getHttpRequest() != null) {
                        value.put("httpRequest", logEntryDTO.getHttpRequest());
                    }
                    if (logEntryDTO.getHttpResponse() != null) {
                        value.put("httpResponse", logEntryDTO.getHttpResponse());
                    }
                    Map<String, Object> entry = new HashMap<>();
                    entry.put("key", logEntryDTO.getId() + "_proxied");
                    Description description = proxiedRequestsDescriptionProcessor.description(logEntryDTO.getHttpRequest());
                    if (description != null) {
                        entry.put("description", description);
                    }
                    entry.put("value", value);
                    if (!value.isEmpty()) {
                        proxiedRequests.add(entry);
                    }
                }
            }
        });
        sendMessage(ctx, httpRequest, ImmutableMap.of("logMessages", logMessages, "activeExpectations", activeExpectations, "recordedRequests", recordedRequests, // reverse
        "proxiedRequests", // reverse
        proxiedRequests), retryCount);
    });
}
Also used : AttributeKey(io.netty.util.AttributeKey) ObjectMapperFactory(org.mockserver.serialization.ObjectMapperFactory) java.util(java.util) HttpRequest(org.mockserver.model.HttpRequest) HttpRequestSerializer(org.mockserver.serialization.HttpRequestSerializer) DashboardLogEntryDTO(org.mockserver.dashboard.model.DashboardLogEntryDTO) CircularHashMap(org.mockserver.collections.CircularHashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DashboardLogEntryDTOGroup(org.mockserver.dashboard.model.DashboardLogEntryDTOGroup) HttpRequest.request(org.mockserver.model.HttpRequest.request) HOST(com.google.common.net.HttpHeaders.HOST) Level(org.slf4j.event.Level) HttpState(org.mockserver.mock.HttpState) io.netty.channel(io.netty.channel) JsonNode(com.fasterxml.jackson.databind.JsonNode) LogMessageType(org.mockserver.log.model.LogEntry.LogMessageType) ExceptionHandling.connectionClosedException(org.mockserver.exception.ExceptionHandling.connectionClosedException) MockServerEventLog(org.mockserver.log.MockServerEventLog) OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ImmutableMap(com.google.common.collect.ImmutableMap) java.util.concurrent(java.util.concurrent) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MockServerMatcherNotifier(org.mockserver.mock.listeners.MockServerMatcherNotifier) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Collectors(java.util.stream.Collectors) org.mockserver.dashboard.serializers(org.mockserver.dashboard.serializers) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) io.netty.handler.codec.http.websocketx(io.netty.handler.codec.http.websocketx) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LogEntry(org.mockserver.log.model.LogEntry) RequestMatchers(org.mockserver.mock.RequestMatchers) MockServerLogListener(org.mockserver.mock.listeners.MockServerLogListener) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MockServerLogger(org.mockserver.logging.MockServerLogger) ExpectationDTO(org.mockserver.serialization.model.ExpectationDTO) MockServerMatcherListener(org.mockserver.mock.listeners.MockServerMatcherListener) SECONDS(java.util.concurrent.TimeUnit.SECONDS) RequestDefinition(org.mockserver.model.RequestDefinition) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CircularHashMap(org.mockserver.collections.CircularHashMap) RequestDefinition(org.mockserver.model.RequestDefinition) DashboardLogEntryDTOGroup(org.mockserver.dashboard.model.DashboardLogEntryDTOGroup) JsonNode(com.fasterxml.jackson.databind.JsonNode) ExpectationDTO(org.mockserver.serialization.model.ExpectationDTO) ImmutableMap(com.google.common.collect.ImmutableMap) DashboardLogEntryDTO(org.mockserver.dashboard.model.DashboardLogEntryDTO) OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) CircularHashMap(org.mockserver.collections.CircularHashMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

DashboardLogEntryDTO (org.mockserver.dashboard.model.DashboardLogEntryDTO)3 DashboardLogEntryDTOGroup (org.mockserver.dashboard.model.DashboardLogEntryDTOGroup)3 LogEntry (org.mockserver.log.model.LogEntry)3 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HOST (com.google.common.net.HttpHeaders.HOST)1 io.netty.channel (io.netty.channel)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 io.netty.handler.codec.http.websocketx (io.netty.handler.codec.http.websocketx)1 AttributeKey (io.netty.util.AttributeKey)1 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)1 java.util (java.util)1 java.util.concurrent (java.util.concurrent)1