Search in sources :

Example 16 with LogEntry

use of org.mockserver.log.model.LogEntry in project mockserver by mock-server.

the class MockServerLoggerTest method shouldFormatErrorLogMessagesForRequest.

@Test
public void shouldFormatErrorLogMessagesForRequest() {
    Level originalLevel = logLevel();
    try {
        // given
        logLevel("INFO");
        Logger mockLogger = mock(Logger.class);
        MockServerLogger logFormatter = new MockServerLogger(mockLogger);
        HttpRequest request = request("some_path");
        // when
        logFormatter.logEvent(new LogEntry().setLogLevel(Level.ERROR).setHttpRequest(request).setMessageFormat("some random message with{}and{}").setArguments("some" + NEW_LINE + "multi-line" + NEW_LINE + "object", "another" + NEW_LINE + "multi-line" + NEW_LINE + "object"));
        // then
        String message = "some random message with" + NEW_LINE + NEW_LINE + "  some" + NEW_LINE + "  multi-line" + NEW_LINE + "  object" + NEW_LINE + NEW_LINE + " and" + NEW_LINE + NEW_LINE + "  another" + NEW_LINE + "  multi-line" + NEW_LINE + "  object" + NEW_LINE;
        verify(mockLogger).error(message, (Throwable) null);
    } finally {
        logLevel(originalLevel.toString());
    }
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) Level(org.slf4j.event.Level) ConfigurationProperties.logLevel(org.mockserver.configuration.ConfigurationProperties.logLevel) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Logger(org.slf4j.Logger) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 17 with LogEntry

use of org.mockserver.log.model.LogEntry in project mockserver by mock-server.

the class MockServerLoggerTest method shouldSendEventToStateHandler.

@Test
public void shouldSendEventToStateHandler() {
    Level originalLevel = logLevel();
    try {
        // given
        logLevel("INFO");
        HttpState mockHttpStateHandler = mock(HttpState.class);
        MockServerLogger logFormatter = new MockServerLogger(mockHttpStateHandler);
        HttpRequest request = request("some_path");
        // when
        logFormatter.logEvent(new LogEntry().setLogLevel(Level.INFO).setHttpRequest(request).setMessageFormat("some random message with{}and{}").setArguments("some" + NEW_LINE + "multi-line" + NEW_LINE + "object", "another" + NEW_LINE + "multi-line" + NEW_LINE + "object"));
        // then
        ArgumentCaptor<LogEntry> captor = ArgumentCaptor.forClass(LogEntry.class);
        verify(mockHttpStateHandler, times(1)).log(captor.capture());
        LogEntry messageLogEntry = captor.getValue();
        assertThat(messageLogEntry.getHttpRequests(), is(new HttpRequest[] { request }));
        assertThat(messageLogEntry.getMessage(), containsString("some random message with" + NEW_LINE + NEW_LINE + "  some" + NEW_LINE + "  multi-line" + NEW_LINE + "  object" + NEW_LINE + NEW_LINE + " and" + NEW_LINE + NEW_LINE + "  another" + NEW_LINE + "  multi-line" + NEW_LINE + "  object" + NEW_LINE));
        assertThat(messageLogEntry.getMessageFormat(), containsString("some random message with{}and{}"));
        assertThat(messageLogEntry.getArguments(), arrayContaining(new Object[] { "some" + NEW_LINE + "multi-line" + NEW_LINE + "object", "another" + NEW_LINE + "multi-line" + NEW_LINE + "object" }));
    } finally {
        logLevel(originalLevel.toString());
    }
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) HttpState(org.mockserver.mock.HttpState) Level(org.slf4j.event.Level) ConfigurationProperties.logLevel(org.mockserver.configuration.ConfigurationProperties.logLevel) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 18 with LogEntry

use of org.mockserver.log.model.LogEntry in project mockserver by mock-server.

the class MockServerEventLogRequestLogEntryVerificationSequenceTest method shouldPassVerificationWithNullRequest.

@Test
public void shouldPassVerificationWithNullRequest() {
    // when
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("one")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("multi")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("three")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("multi")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("four")).setType(RECEIVED_REQUEST));
    // then
    assertThat(verify((VerificationSequence) null), is(""));
}
Also used : VerificationSequence(org.mockserver.verify.VerificationSequence) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 19 with LogEntry

use of org.mockserver.log.model.LogEntry in project mockserver by mock-server.

the class MockServerEventLogRequestLogEntryVerificationSequenceTest method shouldFailVerificationSequenceWithTwoRequestsFirstIncorrect.

@Test
public void shouldFailVerificationSequenceWithTwoRequestsFirstIncorrect() {
    // when
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("one")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("multi")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("three")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("multi")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("four")).setType(RECEIVED_REQUEST));
    // then - next to each other
    assertThat(verify(new VerificationSequence().withRequests(request("zero"), request("multi"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"zero\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "} ]> but was:<[ {" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"three\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "} ]>"));
    assertThat(verify(new VerificationSequence().withRequests(request("zero"), request("three"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"zero\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"three\"" + NEW_LINE + "} ]> but was:<[ {" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"three\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "} ]>"));
    assertThat(verify(new VerificationSequence().withRequests(request("zero"), request("four"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"zero\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "} ]> but was:<[ {" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"three\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "} ]>"));
}
Also used : VerificationSequence(org.mockserver.verify.VerificationSequence) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 20 with LogEntry

use of org.mockserver.log.model.LogEntry in project mockserver by mock-server.

the class MockServerEventLogRequestLogEntryVerificationSequenceTest method shouldPassVerificationSequenceWithOneRequest.

@Test
public void shouldPassVerificationSequenceWithOneRequest() {
    // when
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("one")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("multi")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("three")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("multi")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("four")).setType(RECEIVED_REQUEST));
    // then
    assertThat(verify(new VerificationSequence().withRequests(request("one"))), is(""));
    assertThat(verify(new VerificationSequence().withRequests(request("multi"))), is(""));
    assertThat(verify(new VerificationSequence().withRequests(request("three"))), is(""));
    assertThat(verify(new VerificationSequence().withRequests(request("four"))), is(""));
}
Also used : VerificationSequence(org.mockserver.verify.VerificationSequence) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Aggregations

LogEntry (org.mockserver.log.model.LogEntry)269 Test (org.junit.Test)114 HttpRequest (org.mockserver.model.HttpRequest)79 HttpResponse (org.mockserver.model.HttpResponse)58 Level (org.slf4j.event.Level)34 MockServerLogger (org.mockserver.logging.MockServerLogger)32 Expectation (org.mockserver.mock.Expectation)32 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)25 VerificationSequence (org.mockserver.verify.VerificationSequence)20 DashboardLogEntryDTO (org.mockserver.dashboard.model.DashboardLogEntryDTO)17 OpenAPIExpectation.openAPIExpectation (org.mockserver.mock.OpenAPIExpectation.openAPIExpectation)16 MockServerEventLog (org.mockserver.log.MockServerEventLog)14 List (java.util.List)13 CompletableFuture (java.util.concurrent.CompletableFuture)13 IOException (java.io.IOException)12 InetSocketAddress (java.net.InetSocketAddress)12 ArrayList (java.util.ArrayList)10 Date (java.util.Date)9 ResponseWriter (org.mockserver.responsewriter.ResponseWriter)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)8