Search in sources :

Example 16 with VerificationSequence

use of org.mockserver.verify.VerificationSequence in project mockserver by mock-server.

the class MockServerClient method verify.

/**
 * Verify a list of requests have been sent in the order specified for example:
 * <pre>
 * mockServerClient
 *  .verify(
 *      request()
 *          .withPath("/first_request")
 *          .withBody("some_request_body"),
 *      request()
 *          .withPath("/second_request")
 *          .withBody("some_request_body")
 *  );
 * </pre>
 *
 * @param maximumNumberOfRequestToReturnInVerificationFailure the maximum number requests return in the error response when the verification fails
 * @param expectationIds                                      the http requests that must be matched for this verification to pass
 * @throws AssertionError if the request has not been found
 */
public MockServerClient verify(Integer maximumNumberOfRequestToReturnInVerificationFailure, ExpectationId... expectationIds) throws AssertionError {
    if (expectationIds == null || expectationIds.length == 0 || expectationIds[0] == null) {
        throw new IllegalArgumentException("verify(ExpectationId...) requires a non-null non-empty array of ExpectationId objects");
    }
    try {
        VerificationSequence verificationSequence = new VerificationSequence().withExpectationIds(expectationIds).withMaximumNumberOfRequestToReturnInVerificationFailure(maximumNumberOfRequestToReturnInVerificationFailure);
        String result = sendRequest(request().withMethod("PUT").withContentType(APPLICATION_JSON_UTF_8).withPath(calculatePath("verifySequence")).withBody(verificationSequenceSerializer.serialize(verificationSequence), StandardCharsets.UTF_8)).getBodyAsString();
        if (result != null && !result.isEmpty()) {
            throw new AssertionError(result);
        }
    } catch (AuthenticationException authenticationException) {
        throw authenticationException;
    } catch (Throwable throwable) {
        throw new AssertionError(throwable.getMessage());
    }
    return clientClass.cast(this);
}
Also used : AuthenticationException(org.mockserver.authentication.AuthenticationException) VerificationSequence(org.mockserver.verify.VerificationSequence)

Example 17 with VerificationSequence

use of org.mockserver.verify.VerificationSequence in project mockserver by mock-server.

the class MockServerEventLogRequestLogEntryVerificationSequenceTest method shouldFailVerificationSequenceWithThreeRequestsDuplicateMissing.

@Test
public void shouldFailVerificationSequenceWithThreeRequestsDuplicateMissing() {
    // 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("multi"), request("multi"), request("multi"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + 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 + "} ]>"));
}
Also used : VerificationSequence(org.mockserver.verify.VerificationSequence) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 18 with VerificationSequence

use of org.mockserver.verify.VerificationSequence in project mockserver by mock-server.

the class MockServerEventLogRequestLogEntryVerificationSequenceTest method shouldFailVerificationSequenceWithLimitedReturnedRequestsViaVerificationSequence.

@Test
public void shouldFailVerificationSequenceWithLimitedReturnedRequestsViaVerificationSequence() {
    // 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("multi"), request("one")).withMaximumNumberOfRequestToReturnInVerificationFailure(1)), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
    assertThat(verify(new VerificationSequence().withRequests(request("four"), request("multi")).withMaximumNumberOfRequestToReturnInVerificationFailure(1)), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
    // then - not next to each other
    assertThat(verify(new VerificationSequence().withRequests(request("three"), request("one")).withMaximumNumberOfRequestToReturnInVerificationFailure(1)), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"three\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
    assertThat(verify(new VerificationSequence().withRequests(request("four"), request("one")).withMaximumNumberOfRequestToReturnInVerificationFailure(1)), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
    assertThat(verify(new VerificationSequence().withRequests(request("four"), request("three")).withMaximumNumberOfRequestToReturnInVerificationFailure(1)), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"three\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
}
Also used : VerificationSequence(org.mockserver.verify.VerificationSequence) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 19 with VerificationSequence

use of org.mockserver.verify.VerificationSequence in project mockserver by mock-server.

the class MockServerEventLogRequestLogEntryVerificationSequenceTest method shouldFailVerificationSequenceWithLimitedReturnedRequestsViaConfiguration.

@Test
public void shouldFailVerificationSequenceWithLimitedReturnedRequestsViaConfiguration() {
    Integer originalMaximumNumberOfRequestToReturnInVerificationFailure = ConfigurationProperties.maximumNumberOfRequestToReturnInVerificationFailure();
    try {
        // given
        ConfigurationProperties.maximumNumberOfRequestToReturnInVerificationFailure(1);
        // 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("multi"), request("one"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
        assertThat(verify(new VerificationSequence().withRequests(request("four"), request("multi"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
        // then - not next to each other
        assertThat(verify(new VerificationSequence().withRequests(request("three"), request("one"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"three\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
        assertThat(verify(new VerificationSequence().withRequests(request("four"), request("one"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
        assertThat(verify(new VerificationSequence().withRequests(request("four"), request("three"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"three\"" + NEW_LINE + "} ]> but was not found, found 5 other requests"));
    } finally {
        ConfigurationProperties.maximumNumberOfRequestToReturnInVerificationFailure(originalMaximumNumberOfRequestToReturnInVerificationFailure);
    }
}
Also used : VerificationSequence(org.mockserver.verify.VerificationSequence) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 20 with VerificationSequence

use of org.mockserver.verify.VerificationSequence in project mockserver by mock-server.

the class MockServerEventLogRequestLogEntryVerificationSequenceTest method shouldFailVerificationSequenceWithTwoRequestsWrongOrder.

@Test
public void shouldFailVerificationSequenceWithTwoRequestsWrongOrder() {
    // 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("multi"), request("one"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"multi\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"one\"" + 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("four"), request("multi"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"four\"" + 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 + "} ]>"));
    // then - not next to each other
    assertThat(verify(new VerificationSequence().withRequests(request("three"), request("one"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"three\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"one\"" + 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("four"), request("one"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"four\"" + NEW_LINE + "}, {" + NEW_LINE + "  \"path\" : \"one\"" + 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("four"), request("three"))), is("Request sequence not found, expected:<[ {" + NEW_LINE + "  \"path\" : \"four\"" + 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 + "} ]>"));
}
Also used : VerificationSequence(org.mockserver.verify.VerificationSequence) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Aggregations

VerificationSequence (org.mockserver.verify.VerificationSequence)35 Test (org.junit.Test)31 LogEntry (org.mockserver.log.model.LogEntry)18 VerificationSequenceDTO (org.mockserver.serialization.model.VerificationSequenceDTO)7 MockServerEventLog (org.mockserver.log.MockServerEventLog)5 AuthenticationException (org.mockserver.authentication.AuthenticationException)4 MockServerLogger (org.mockserver.logging.MockServerLogger)4 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 HttpRequest (org.mockserver.model.HttpRequest)2 HttpRequestDTO (org.mockserver.serialization.model.HttpRequestDTO)2 ExpectationId (org.mockserver.model.ExpectationId)1