Search in sources :

Example 1 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 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(ExpectationId... expectationIds) throws AssertionError {
    if (expectationIds == null || expectationIds.length == 0 || expectationIds[0] == null) {
        throw new IllegalArgumentException("verify(RequestDefinition...) requires a non-null non-empty array of RequestDefinition objects");
    }
    try {
        VerificationSequence verificationSequence = new VerificationSequence().withExpectationIds(expectationIds);
        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 2 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 requestDefinitions the http requests that must be matched for this verification to pass
 * @throws AssertionError if the request has not been found
 */
public MockServerClient verify(RequestDefinition... requestDefinitions) throws AssertionError {
    if (requestDefinitions == null || requestDefinitions.length == 0 || requestDefinitions[0] == null) {
        throw new IllegalArgumentException("verify(RequestDefinition...) requires a non-null non-empty array of RequestDefinition objects");
    }
    try {
        VerificationSequence verificationSequence = new VerificationSequence().withRequests(requestDefinitions);
        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 3 with VerificationSequence

use of org.mockserver.verify.VerificationSequence 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 4 with VerificationSequence

use of org.mockserver.verify.VerificationSequence 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 5 with VerificationSequence

use of org.mockserver.verify.VerificationSequence 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

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