com.github.tomakehurst.wiremock.stubbing.ServeEvent Java Examples

The following examples show how to use com.github.tomakehurst.wiremock.stubbing.ServeEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HTTPStubRequestsCheckTeststepRunner.java    From irontest with Apache License 2.0 6 votes vote down vote up
@Override
public BasicTeststepRun run() {
    BasicTeststepRun basicTeststepRun = new BasicTeststepRun();

    WireMockServer wireMockServer = getTestcaseRunContext().getWireMockServer();
    WireMockServerAPIResponse response = new WireMockServerAPIResponse();

    List<ServeEvent> allServeEvents = wireMockServer.getAllServeEvents();
    for (ServeEvent serveEvent: allServeEvents) {
        response.getAllServeEvents().add(IronTestUtils.updateUnmatchedStubRequest(serveEvent, wireMockServer));
    }

    basicTeststepRun.setResponse(response);

    return basicTeststepRun;
}
 
Example #2
Source File: HttpErrorDetailRule.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            try {
                base.evaluate();
            } catch (final CamelExecutionException camelError) {
                final Throwable cause = camelError.getCause();
                if (cause instanceof HttpOperationFailedException) {
                    final HttpOperationFailedException httpError = (HttpOperationFailedException) cause;

                    final List<ServeEvent> events = WireMock.getAllServeEvents();
                    final String message = "Received HTTP status: " + httpError.getStatusCode() + " " + httpError.getStatusText() + "\n\n"
                        + httpError.getResponseBody() + "\nRequests received:\n"
                        + Joiner.on('\n').join(events.stream().map(e -> e.getRequest()).iterator());

                    throw new AssertionError(message, httpError);
                }

                throw camelError;
            }
        }
    };
}
 
Example #3
Source File: AllHTTPStubRequestsMatchedAssertionVerifier.java    From irontest with Apache License 2.0 6 votes vote down vote up
@Override
public AssertionVerificationResult verify(Object... inputs) {
    AllHTTPStubRequestsMatchedAssertionVerificationResult result = new AllHTTPStubRequestsMatchedAssertionVerificationResult();

    List<ServeEvent> allStubRequests = (List<ServeEvent>) inputs[0];
    List<ServeEvent> unmatchedStubRequests = new ArrayList<>();
    for (ServeEvent serveEvent: allStubRequests) {
        if (!serveEvent.getWasMatched()) {
            unmatchedStubRequests.add(serveEvent);
        }
    }

    result.setUnmatchedStubRequests(unmatchedStubRequests);

    if (unmatchedStubRequests.isEmpty()) {
        result.setResult(TestResult.PASSED);
    } else {
        result.setResult(TestResult.FAILED);
    }

    return result;
}
 
Example #4
Source File: HTTPStubsHitInOrderAssertionVerifier.java    From irontest with Apache License 2.0 6 votes vote down vote up
@Override
public AssertionVerificationResult verify(Object... inputs) {
    HTTPStubsHitInOrderAssertionVerificationResult result = new HTTPStubsHitInOrderAssertionVerificationResult();
    HTTPStubsHitInOrderAssertionProperties otherProperties =
            (HTTPStubsHitInOrderAssertionProperties) getAssertion().getOtherProperties();

    Map<Date, Short> hitMap = new TreeMap<>();
    List<ServeEvent> allServeEvents = (List<ServeEvent>) inputs[0];
    for (ServeEvent serveEvent: allServeEvents) {
        if (serveEvent.getWasMatched()) {
            StubMapping stubMapping = serveEvent.getStubMapping();
            hitMap.put(serveEvent.getRequest().getLoggedDate(),
                    (Short) stubMapping.getMetadata().get(WIREMOCK_STUB_METADATA_ATTR_NAME_IRON_TEST_NUMBER));
        }
    }

    List<Short> actualHitOrder = new ArrayList(hitMap.values());
    result.setResult(otherProperties.getExpectedHitOrder().equals(actualHitOrder) ? TestResult.PASSED : TestResult.FAILED);
    result.setActualHitOrder(actualHitOrder);

    return result;
}
 
Example #5
Source File: HTTPStubHitAssertionVerifier.java    From irontest with Apache License 2.0 6 votes vote down vote up
@Override
public AssertionVerificationResult verify(Object ...inputs) {
    HTTPStubHitAssertionVerificationResult result = new HTTPStubHitAssertionVerificationResult();

    HTTPStubHitAssertionProperties otherProperties = (HTTPStubHitAssertionProperties) getAssertion().getOtherProperties();

    List<ServeEvent> allServeEvents = (List<ServeEvent>) inputs[0];
    UUID stubInstanceUUID = (UUID) inputs[1];
    short stubInstanceHitCount = 0;
    for (ServeEvent serveEvent: allServeEvents) {
        if (serveEvent.getStubMapping().getId().equals(stubInstanceUUID)) {    //  the stub instance has been hit
            stubInstanceHitCount++;
        }
    }
    result.setActualHitCount(stubInstanceHitCount);

    if (stubInstanceHitCount != otherProperties.getExpectedHitCount()) {
        result.setResult(TestResult.FAILED);
    } else {
        result.setResult(TestResult.PASSED);
    }

    return result;
}
 
Example #6
Source File: IronTestUtils.java    From irontest with Apache License 2.0 6 votes vote down vote up
/**
 * By default, unmatched WireMock stub request (ServeEvent) does not have the actual response headers or response body.
 * This method update the unmatched serveEvent obtained from the WireMockServer by changing its response headers and body to the actual values.
 * @param serveEvent
 * @return the input serveEvent if it was matched;
 *         a new ServeEvent object with all fields same as the input serveEvent, except for the response headers and body, if it was unmatched.
 */
public static ServeEvent updateUnmatchedStubRequest(ServeEvent serveEvent, WireMockServer wireMockServer) {
    if (serveEvent.getWasMatched()) {
        return serveEvent;
    } else {
        PlainTextStubNotMatchedRenderer renderer = (PlainTextStubNotMatchedRenderer) wireMockServer.getOptions()
                .getNotMatchedRenderer();
        ResponseDefinition responseDefinition = renderer.render(wireMockServer, serveEvent.getRequest());
        LoggedResponse response = serveEvent.getResponse();
        com.github.tomakehurst.wiremock.http.HttpHeaders updatedHeaders = responseDefinition.getHeaders();
        String updatedBody = responseDefinition.getBody().substring(2);  //  remove the leading \r\n
        LoggedResponse updatedResponse = new LoggedResponse(response.getStatus(), updatedHeaders,
                Encoding.encodeBase64(updatedBody.getBytes()), response.getFault(), null);
        ServeEvent updatedServeEvent = new ServeEvent(serveEvent.getId(), serveEvent.getRequest(),
                serveEvent.getStubMapping(), serveEvent.getResponseDefinition(), updatedResponse,
                serveEvent.getWasMatched(), serveEvent.getTiming());
        return updatedServeEvent;
    }
}
 
Example #7
Source File: RecordableTest.java    From devops-cm-client with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() {

    if(! isRecording()) {

        for(ServeEvent e : wireMockRule.getAllServeEvents())
            if(e.isNoExactMatch()) throw new RuntimeException("There was an unmatched request: " + e.getRequest().getAbsoluteUrl());

        WireMock.resetAllRequests();
        WireMock.resetAllScenarios();
    }
}
 
Example #8
Source File: Webhooks.java    From wiremock-webhooks-extension with Apache License 2.0 5 votes vote down vote up
@Override
public void doAction(final ServeEvent serveEvent, final Admin admin, final Parameters parameters) {
    final Notifier notifier = notifier();

    scheduler.schedule(
        new Runnable() {
            @Override
            public void run() {
                WebhookDefinition definition = parameters.as(WebhookDefinition.class);
                for (WebhookTransformer transformer: transformers) {
                    definition = transformer.transform(serveEvent, definition);
                }
                HttpUriRequest request = buildRequest(definition);

                try {
                    HttpResponse response = httpClient.execute(request);
                    notifier.info(
                        String.format("Webhook %s request to %s returned status %s\n\n%s",
                            definition.getMethod(),
                            definition.getUrl(),
                            response.getStatusLine(),
                            EntityUtils.toString(response.getEntity())
                        )
                    );
                } catch (IOException e) {
                    throwUnchecked(e);
                }
            }
        },
        0L,
        SECONDS
    );
}
 
Example #9
Source File: MockServerResource.java    From irontest with Apache License 2.0 5 votes vote down vote up
@GET @Path("unmatchedStubRequests")
@JsonView(ResourceJsonViews.MockServerUnmatchedRequestList.class)
public List<ServeEvent> findAllUnmatchedStubRequests() {
    List<ServeEvent> result = new ArrayList<>();
    List<ServeEvent> serveEvents = wireMockServer.getAllServeEvents();
    for (ServeEvent serveEvent: serveEvents) {
        if (!serveEvent.getWasMatched()) {
            result.add(serveEvent);
        }
    }
    return result;
}
 
Example #10
Source File: MockServerResource.java    From irontest with Apache License 2.0 5 votes vote down vote up
@GET @Path("stubInstances/{stubInstanceId}/stubRequests")
@JsonView(ResourceJsonViews.MockServerStubRequestList.class)
public List<ServeEvent> findMatchedRequestsForStubInstance(@PathParam("stubInstanceId") UUID stubInstanceId) {
    List<ServeEvent> result = new ArrayList<>();
    List<ServeEvent> serveEvents = wireMockServer.getAllServeEvents();
    for (ServeEvent serveEvent: serveEvents) {
        if (serveEvent.getStubMapping().getId().equals(stubInstanceId)) {
            result.add(serveEvent);
        }
    }
    return result;
}
 
Example #11
Source File: MockServerResource.java    From irontest with Apache License 2.0 5 votes vote down vote up
@GET @Path("stubRequests/{stubRequestId}")
public ServeEvent findStubRequestById(@PathParam("stubRequestId") UUID stubRequestId) {
    List<ServeEvent> serveEvents = wireMockServer.getAllServeEvents();
    for (ServeEvent serveEvent: serveEvents) {
        if (serveEvent.getId().equals(stubRequestId)) {
            if (serveEvent.getWasMatched()) {
                return serveEvent;
            } else {
                return IronTestUtils.updateUnmatchedStubRequest(serveEvent, wireMockServer);
            }
        }
    }
    return null;
}
 
Example #12
Source File: IronTestUtils.java    From irontest with Apache License 2.0 5 votes vote down vote up
public static void addMixInsForWireMock(ObjectMapper objectMapper) {
    objectMapper.addMixIn(StubMapping.class, StubMappingMixIn.class);
    objectMapper.addMixIn(RequestPattern.class, RequestPatternMixIn.class);
    objectMapper.addMixIn(StringValuePattern.class, StringValuePatternMixIn.class);
    objectMapper.addMixIn(ResponseDefinition.class, ResponseDefinitionMixIn.class);
    objectMapper.addMixIn(ContentPattern.class, ContentPatternMixIn.class);
    objectMapper.addMixIn(LoggedResponse.class, LoggedResponseMixIn.class);
    objectMapper.addMixIn(ServeEvent.class, ServeEventMixIn.class);
    objectMapper.addMixIn(LoggedRequest.class, LoggedRequestMixIn.class);
}
 
Example #13
Source File: WireMockServerAPIResponse.java    From irontest with Apache License 2.0 4 votes vote down vote up
public void setAllServeEvents(List<ServeEvent> allServeEvents) {
    this.allServeEvents = allServeEvents;
}
 
Example #14
Source File: HttpTestBase.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
public List<LoggedRequest> requests() {
    return wireMockRule.getServeEvents().getRequests().stream()
            .map(ServeEvent::getRequest).collect(Collectors.toList());
}
 
Example #15
Source File: WireMockServerAPIResponse.java    From irontest with Apache License 2.0 4 votes vote down vote up
public List<ServeEvent> getAllServeEvents() {
    return allServeEvents;
}
 
Example #16
Source File: AllHTTPStubRequestsMatchedAssertionVerificationResult.java    From irontest with Apache License 2.0 4 votes vote down vote up
public void setUnmatchedStubRequests(List<ServeEvent> unmatchedStubRequests) {
    this.unmatchedStubRequests = unmatchedStubRequests;
}
 
Example #17
Source File: AllHTTPStubRequestsMatchedAssertionVerificationResult.java    From irontest with Apache License 2.0 4 votes vote down vote up
public List<ServeEvent> getUnmatchedStubRequests() {
    return unmatchedStubRequests;
}
 
Example #18
Source File: ConstantHttpHeaderWebhookTransformer.java    From wiremock-webhooks-extension with Apache License 2.0 4 votes vote down vote up
@Override
public WebhookDefinition transform(ServeEvent serveEvent, WebhookDefinition webhookDefinition) {
  return webhookDefinition.withHeader(key, value);
}
 
Example #19
Source File: ThrowingWebhookTransformer.java    From wiremock-webhooks-extension with Apache License 2.0 4 votes vote down vote up
@Override
public WebhookDefinition transform(ServeEvent serveEvent, WebhookDefinition webhookDefinition) {
  throw new RuntimeException("oh no");
}
 
Example #20
Source File: StubHttpConsumer.java    From eventeum with Apache License 2.0 4 votes vote down vote up
public String getLatestRequestBody() {
    List<ServeEvent> events = wireMockServer.getAllServeEvents();

    return events.get(events.size() - 1).getRequest().getBodyAsString();
}
 
Example #21
Source File: StubHttpConsumer.java    From eventeum with Apache License 2.0 4 votes vote down vote up
public String getLatestRequestBody() {
    List<ServeEvent> events = wireMockServer.getAllServeEvents();

    return events.get(events.size() - 1).getRequest().getBodyAsString();
}
 
Example #22
Source File: HttpMessageTest.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
private List<LoggedRequest> requests(String path) {
    return wireMockRule.getServeEvents().getRequests().stream().map(ServeEvent::getRequest)
            .filter(req -> req.getUrl().equalsIgnoreCase(path))
            .collect(Collectors.toList());
}
 
Example #23
Source File: WebhookTransformer.java    From wiremock-webhooks-extension with Apache License 2.0 votes vote down vote up
WebhookDefinition transform(ServeEvent serveEvent, WebhookDefinition webhookDefinition);