Search in sources :

Example 1 with Verification

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

the class MockServerClient method verify.

/**
 * Verify a request has been sent for example:
 * <pre>
 * mockServerClient
 *  .verify(
 *      request()
 *          .withPath("/some_path")
 *          .withBody("some_request_body"),
 *      VerificationTimes.exactly(3)
 *  );
 * </pre>
 * VerificationTimes supports multiple static factory methods:
 * <p>
 * once()      - verify the request was only received once
 * exactly(n)  - verify the request was only received exactly n times
 * atLeast(n)  - verify the request was only received at least n times
 *
 * @param requestDefinition the http request that must be matched for this verification to pass
 * @param times             the number of times this request must be matched
 * @throws AssertionError if the request has not been found
 */
@SuppressWarnings("DuplicatedCode")
public MockServerClient verify(RequestDefinition requestDefinition, VerificationTimes times) throws AssertionError {
    if (requestDefinition == null) {
        throw new IllegalArgumentException("verify(RequestDefinition, VerificationTimes) requires a non null RequestDefinition object");
    }
    if (times == null) {
        throw new IllegalArgumentException("verify(RequestDefinition, VerificationTimes) requires a non null VerificationTimes object");
    }
    try {
        Verification verification = verification().withRequest(requestDefinition).withTimes(times);
        String result = sendRequest(request().withMethod("PUT").withContentType(APPLICATION_JSON_UTF_8).withPath(calculatePath("verify")).withBody(verificationSerializer.serialize(verification), 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) Verification(org.mockserver.verify.Verification)

Example 2 with Verification

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

the class MockServerClient method verify.

/**
 * Verify a request has been sent for example:
 * <pre>
 * mockServerClient
 *  .verify(
 *      request()
 *          .withPath("/some_path")
 *          .withBody("some_request_body"),
 *      VerificationTimes.exactly(3)
 *  );
 * </pre>
 * VerificationTimes supports multiple static factory methods:
 * <p>
 * once()      - verify the request was only received once
 * exactly(n)  - verify the request was only received exactly n times
 * atLeast(n)  - verify the request was only received at least n times
 *
 * @param expectationId the http request that must be matched for this verification to pass
 * @param times         the number of times this request must be matched
 * @throws AssertionError if the request has not been found
 */
@SuppressWarnings("DuplicatedCode")
public MockServerClient verify(ExpectationId expectationId, VerificationTimes times) throws AssertionError {
    if (expectationId == null) {
        throw new IllegalArgumentException("verify(RequestDefinition, VerificationTimes) requires a non null RequestDefinition object");
    }
    if (times == null) {
        throw new IllegalArgumentException("verify(RequestDefinition, VerificationTimes) requires a non null VerificationTimes object");
    }
    try {
        Verification verification = verification().withExpectationId(expectationId).withTimes(times);
        String result = sendRequest(request().withMethod("PUT").withContentType(APPLICATION_JSON_UTF_8).withPath(calculatePath("verify")).withBody(verificationSerializer.serialize(verification), 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) Verification(org.mockserver.verify.Verification)

Example 3 with Verification

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

the class VerificationSerializationErrorsTest method shouldHandleExceptionWhileSerializingObject.

@Test
public void shouldHandleExceptionWhileSerializingObject() throws IOException {
    // given
    thrown.expect(RuntimeException.class);
    thrown.expectMessage("Exception while serializing verification to JSON with value {" + NEW_LINE + "  \"times\" : {" + NEW_LINE + "    \"atLeast\" : 1" + NEW_LINE + "  }" + NEW_LINE + "}");
    // and
    when(objectWriter.writeValueAsString(any(VerificationDTO.class))).thenThrow(new RuntimeException("TEST EXCEPTION"));
    // when
    verificationSerializer.serialize(new Verification());
}
Also used : VerificationDTO(org.mockserver.serialization.model.VerificationDTO) Verification(org.mockserver.verify.Verification) Test(org.junit.Test)

Example 4 with Verification

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

the class VerificationSerializerTest method serializeHandlesException.

@Test
public void serializeHandlesException() throws IOException {
    // given
    thrown.expect(RuntimeException.class);
    thrown.expectMessage("Exception while serializing verification to JSON with value {" + NEW_LINE + "  \"times\" : {" + NEW_LINE + "    \"atLeast\" : 1" + NEW_LINE + "  }" + NEW_LINE + "}");
    // and
    when(objectWriter.writeValueAsString(any(VerificationDTO.class))).thenThrow(new RuntimeException("TEST EXCEPTION"));
    // when
    verificationSerializer.serialize(new Verification());
}
Also used : VerificationDTO(org.mockserver.serialization.model.VerificationDTO) Verification(org.mockserver.verify.Verification) Test(org.junit.Test)

Example 5 with Verification

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

the class VerificationSerializerTest method deserialize.

@Test
public void deserialize() throws IOException {
    // given
    when(verificationValidator.isValid(eq("requestBytes"))).thenReturn("");
    when(objectMapper.readValue(eq("requestBytes"), same(VerificationDTO.class))).thenReturn(fullVerificationDTO);
    // when
    Verification verification = verificationSerializer.deserialize("requestBytes");
    // then
    assertEquals(fullVerification, verification);
}
Also used : VerificationDTO(org.mockserver.serialization.model.VerificationDTO) Verification(org.mockserver.verify.Verification) Test(org.junit.Test)

Aggregations

Verification (org.mockserver.verify.Verification)20 Test (org.junit.Test)15 LogEntry (org.mockserver.log.model.LogEntry)6 AuthenticationException (org.mockserver.authentication.AuthenticationException)5 MockServerEventLog (org.mockserver.log.MockServerEventLog)5 HttpRequest (org.mockserver.model.HttpRequest)5 VerificationDTO (org.mockserver.serialization.model.VerificationDTO)5 CompletableFuture (java.util.concurrent.CompletableFuture)2 MockServerLogger (org.mockserver.logging.MockServerLogger)2 HttpRequestDTO (org.mockserver.serialization.model.HttpRequestDTO)2 VerificationTimes (org.mockserver.verify.VerificationTimes)2 VerificationTimesDTO (org.mockserver.serialization.model.VerificationTimesDTO)1