Search in sources :

Example 1 with ExpectationId

use of org.mockserver.model.ExpectationId in project mockserver by mock-server.

the class MockServerEventLog method verify.

public void verify(VerificationSequence verificationSequence, Consumer<String> resultConsumer) {
    if (verificationSequence != null) {
        final String logCorrelationId = UUIDService.getUUID();
        if (MockServerLogger.isEnabled(Level.INFO)) {
            mockServerLogger.logEvent(new LogEntry().setType(VERIFICATION).setLogLevel(Level.INFO).setCorrelationId(logCorrelationId).setHttpRequests(verificationSequence.getHttpRequests().toArray(new RequestDefinition[0])).setMessageFormat(VERIFICATION_REQUEST_SEQUENCES_MESSAGE_FORMAT).setArguments(verificationSequence));
        }
        if (verificationSequence.getExpectationIds() != null && !verificationSequence.getExpectationIds().isEmpty()) {
            retrieveAllRequests(verificationSequence.getExpectationIds().stream().map(ExpectationId::getId).collect(Collectors.toList()), allRequests -> {
                List<RequestDefinition> requestDefinitions = allRequests.stream().map(RequestAndExpectationId::getRequestDefinition).collect(Collectors.toList());
                try {
                    String failureMessage = "";
                    int requestLogCounter = 0;
                    for (ExpectationId expectationId : verificationSequence.getExpectationIds()) {
                        if (expectationId != null) {
                            boolean foundRequest = false;
                            for (; !foundRequest && requestLogCounter < allRequests.size(); requestLogCounter++) {
                                if (allRequests.get(requestLogCounter).matches(expectationId)) {
                                    // move on to next request
                                    foundRequest = true;
                                }
                            }
                            if (!foundRequest) {
                                failureMessage = verificationSequenceFailureMessage(verificationSequence, logCorrelationId, requestDefinitions);
                                break;
                            }
                        }
                    }
                    verificationSequenceSuccessMessage(verificationSequence, resultConsumer, logCorrelationId, failureMessage);
                } catch (Throwable throwable) {
                    verificationSequenceExceptionHandler(verificationSequence, resultConsumer, logCorrelationId, throwable, "exception:{} while processing verification sequence:{}", "exception while processing verification sequence");
                }
            });
        } else {
            retrieveAllRequests(false, allRequests -> {
                try {
                    String failureMessage = "";
                    int requestLogCounter = 0;
                    for (RequestDefinition verificationHttpRequest : verificationSequence.getHttpRequests()) {
                        if (verificationHttpRequest != null) {
                            verificationHttpRequest.withLogCorrelationId(logCorrelationId);
                            HttpRequestMatcher httpRequestMatcher = matcherBuilder.transformsToMatcher(verificationHttpRequest);
                            boolean foundRequest = false;
                            for (; !foundRequest && requestLogCounter < allRequests.size(); requestLogCounter++) {
                                if (httpRequestMatcher.matches(allRequests.get(requestLogCounter).cloneWithLogCorrelationId())) {
                                    // move on to next request
                                    foundRequest = true;
                                }
                            }
                            if (!foundRequest) {
                                failureMessage = verificationSequenceFailureMessage(verificationSequence, logCorrelationId, allRequests);
                                break;
                            }
                        }
                    }
                    verificationSequenceSuccessMessage(verificationSequence, resultConsumer, logCorrelationId, failureMessage);
                } catch (Throwable throwable) {
                    verificationSequenceExceptionHandler(verificationSequence, resultConsumer, logCorrelationId, throwable, "exception:{} while processing verification sequence:{}", "exception while processing verification sequence");
                }
            });
        }
    } else {
        resultConsumer.accept("");
    }
}
Also used : RequestAndExpectationId(org.mockserver.log.model.RequestAndExpectationId) ExpectationId(org.mockserver.model.ExpectationId) HttpRequestMatcher(org.mockserver.matchers.HttpRequestMatcher) RequestDefinition(org.mockserver.model.RequestDefinition) LogEntry(org.mockserver.log.model.LogEntry)

Example 2 with ExpectationId

use of org.mockserver.model.ExpectationId in project app-Tiger by gematik.

the class TigerProxy method removeRoute.

@Override
public void removeRoute(final String routeId) {
    if (!mockServer.isRunning()) {
        return;
    }
    mockServerClient.clear(new ExpectationId().withId(routeId));
    final TigerRoute route = tigerRouteMap.remove(routeId);
    log.info("Deleted route {}. Current # expectations {}", route, mockServerClient.retrieveActiveExpectations(request()).length);
}
Also used : ExpectationId(org.mockserver.model.ExpectationId) TigerRoute(de.gematik.test.tiger.common.data.config.tigerProxy.TigerRoute)

Example 3 with ExpectationId

use of org.mockserver.model.ExpectationId in project mockserver by mock-server.

the class VerificationSequenceSerializerIntegrationTest method shouldDeserializeCompleteObjectWithExpectationIds.

@Test
public void shouldDeserializeCompleteObjectWithExpectationIds() {
    // given
    String requestBytes = "{" + NEW_LINE + "  \"expectationIds\" : [ {" + NEW_LINE + "    \"id\" : \"one\"" + NEW_LINE + "  }, {" + NEW_LINE + "    \"id\" : \"two\"" + NEW_LINE + "  } ]" + NEW_LINE + "}";
    // when
    VerificationSequence verificationSequence = new VerificationSequenceSerializer(new MockServerLogger()).deserialize(requestBytes);
    // then
    assertEquals(new VerificationSequenceDTO().setExpectationIds(Arrays.asList(new ExpectationId().withId("one"), new ExpectationId().withId("two"))).buildObject(), verificationSequence);
}
Also used : ExpectationId(org.mockserver.model.ExpectationId) MockServerLogger(org.mockserver.logging.MockServerLogger) VerificationSequence(org.mockserver.verify.VerificationSequence) VerificationSequenceDTO(org.mockserver.serialization.model.VerificationSequenceDTO) Test(org.junit.Test)

Aggregations

ExpectationId (org.mockserver.model.ExpectationId)3 TigerRoute (de.gematik.test.tiger.common.data.config.tigerProxy.TigerRoute)1 Test (org.junit.Test)1 LogEntry (org.mockserver.log.model.LogEntry)1 RequestAndExpectationId (org.mockserver.log.model.RequestAndExpectationId)1 MockServerLogger (org.mockserver.logging.MockServerLogger)1 HttpRequestMatcher (org.mockserver.matchers.HttpRequestMatcher)1 RequestDefinition (org.mockserver.model.RequestDefinition)1 VerificationSequenceDTO (org.mockserver.serialization.model.VerificationSequenceDTO)1 VerificationSequence (org.mockserver.verify.VerificationSequence)1