Search in sources :

Example 6 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project georocket by georocket.

the class RemoteElasticsearchClientTest method createIndexWithSettings.

/**
 * Test if the client can create an index with settings
 * @param context the test context
 */
@Test
public void createIndexWithSettings(TestContext context) {
    StubMapping settings = stubFor(put(urlEqualTo("/" + INDEX)).withRequestBody(equalToJson(SETTINGS_WRAPPER.encode())).willReturn(aResponse().withBody(ACKNOWLEDGED.encode()).withStatus(200)));
    Async async = context.async();
    client.createIndex(SETTINGS).subscribe(ok -> {
        context.assertTrue(ok);
        verify(putRequestedFor(settings.getRequest().getUrlMatcher()));
        async.complete();
    }, context::fail);
}
Also used : Async(io.vertx.ext.unit.Async) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) Test(org.junit.Test)

Example 7 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project blueocean-plugin by jenkinsci.

the class GithubMockBase method addPerTestStub.

/**
 * Add a StubMapping to Wiremock corresponding to the supplied builder.
 * Any mappings added will automatically be removed when @After fires.
 * @param builder
 */
protected void addPerTestStub(MappingBuilder builder) {
    StubMapping mapping = githubApi.stubFor(builder);
    perTestStubMappings.add(mapping);
}
Also used : StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping)

Example 8 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project irontest by zheng-wang.

the class HTTPStubsHitInOrderAssertionVerifier method verify.

@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;
}
Also used : HTTPStubsHitInOrderAssertionVerificationResult(io.irontest.models.assertion.HTTPStubsHitInOrderAssertionVerificationResult) HTTPStubsHitInOrderAssertionProperties(io.irontest.models.assertion.HTTPStubsHitInOrderAssertionProperties) ServeEvent(com.github.tomakehurst.wiremock.stubbing.ServeEvent) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping)

Example 9 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project irontest by zheng-wang.

the class IronTestUtils method createStubInstance.

/**
 * Create (clone) a new instance out of the stub spec, with UUID generated for the instance.
 * The instance also has the ironTestId as metadata.
 * The spec is not changed.
 * @param spec
 * @return
 */
public static StubMapping createStubInstance(long ironTestId, short ironTestNumber, StubMapping spec) {
    StubMapping stubInstance = StubMapping.buildFrom(StubMapping.buildJsonStringFor(spec));
    stubInstance.setMetadata(metadata().attr(WIREMOCK_STUB_METADATA_ATTR_NAME_IRON_TEST_ID, ironTestId).attr(WIREMOCK_STUB_METADATA_ATTR_NAME_IRON_TEST_NUMBER, ironTestNumber).build());
    stubInstance.setDirty(false);
    return stubInstance;
}
Also used : StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping)

Example 10 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project irontest by zheng-wang.

the class HTTPStubResource method loadAll.

@POST
@Path("testcases/{testcaseId}/httpstubs/loadAll")
@PermitAll
public void loadAll(@PathParam("testcaseId") long testcaseId) throws IOException {
    // gather referenceable string properties
    List<UserDefinedProperty> testcaseUDPs = udpDAO.findByTestcaseId(testcaseId);
    Map<String, String> referenceableStringProperties = IronTestUtils.udpListToMap(testcaseUDPs);
    DataTable dataTable = dataTableDAO.getTestcaseDataTable(testcaseId, true);
    if (dataTable.getRows().size() > 0) {
        IronTestUtils.checkDuplicatePropertyNameBetweenDataTableAndUPDs(referenceableStringProperties.keySet(), dataTable);
        referenceableStringProperties.putAll(dataTable.getStringPropertiesInRow(0));
    }
    List<HTTPStubMapping> stubs = httpStubMappingDAO.findByTestcaseId(testcaseId);
    // resolve string property references in HTTPStubMapping objects
    List<String> undefinedStringProperties = new ArrayList<>();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    IronTestUtils.addMixInsForWireMock(objectMapper);
    String httpStubMappingsJSON = objectMapper.writeValueAsString(stubs);
    MapValueLookup propertyReferenceResolver = new MapValueLookup(referenceableStringProperties, true);
    String resolvedHttpStubMappingsJSON = new StrSubstitutor(propertyReferenceResolver).replace(httpStubMappingsJSON);
    stubs = objectMapper.readValue(resolvedHttpStubMappingsJSON, new TypeReference<List<HTTPStubMapping>>() {
    });
    undefinedStringProperties.addAll(propertyReferenceResolver.getUnfoundKeys());
    if (!undefinedStringProperties.isEmpty()) {
        throw new RuntimeException("String properties " + undefinedStringProperties + " not defined.");
    }
    IronTestUtils.substituteRequestBodyMainPatternValue(stubs);
    // load stubs
    final List<HTTPStubMapping> finalStubs = stubs;
    wireMockServer.loadMappingsUsing(stubMappings -> {
        for (HTTPStubMapping stub : finalStubs) {
            // delete old instances if exist
            List<StubMapping> existingInstances = wireMockServer.findStubMappingsByMetadata(matchingJsonPath("$." + WIREMOCK_STUB_METADATA_ATTR_NAME_IRON_TEST_ID, equalTo(Long.toString(stub.getId()))));
            for (StubMapping existingInstance : existingInstances) {
                wireMockServer.removeStubMapping(existingInstance);
            }
            StubMapping stubInstance = IronTestUtils.createStubInstance(stub.getId(), stub.getNumber(), stub.getSpec());
            stubMappings.addMapping(stubInstance);
        }
    });
}
Also used : DataTable(io.irontest.models.DataTable) UserDefinedProperty(io.irontest.models.UserDefinedProperty) ArrayList(java.util.ArrayList) HTTPStubMapping(io.irontest.models.HTTPStubMapping) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) HTTPStubMapping(io.irontest.models.HTTPStubMapping) StrSubstitutor(org.apache.commons.text.StrSubstitutor) MapValueLookup(io.irontest.core.MapValueLookup) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) WireMock.matchingJsonPath(com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath) PermitAll(javax.annotation.security.PermitAll)

Aggregations

StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)12 Test (org.junit.Test)4 HTTPStubMapping (io.irontest.models.HTTPStubMapping)3 ArrayList (java.util.ArrayList)3 Async (io.vertx.ext.unit.Async)2 URL (java.net.URL)2 Calendar (java.util.Calendar)2 TimeZone (java.util.TimeZone)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 ConfigurationListener (org.apache.logging.log4j.core.config.ConfigurationListener)2 ConfigurationScheduler (org.apache.logging.log4j.core.config.ConfigurationScheduler)2 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)2 HttpWatcher (org.apache.logging.log4j.core.config.HttpWatcher)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)1 WireMock.matchingJsonPath (com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath)1 MatchResult (com.github.tomakehurst.wiremock.matching.MatchResult)1 ServeEvent (com.github.tomakehurst.wiremock.stubbing.ServeEvent)1