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);
}
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);
}
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());
}
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());
}
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);
}
Aggregations