Search in sources :

Example 1 with HTTPStubMapping

use of io.irontest.models.HTTPStubMapping in project irontest by zheng-wang.

the class HTTPStubsSetupTeststepRunner method run.

@Override
public BasicTeststepRun run() {
    WireMockServer wireMockServer = getTestcaseRunContext().getWireMockServer();
    // reset mock server
    wireMockServer.resetAll();
    // load stub mappings into mock server
    Map<Short, UUID> httpStubMappingInstanceIds = getTestcaseRunContext().getHttpStubMappingInstanceIds();
    HTTPStubsSetupTeststepProperties otherProperties = (HTTPStubsSetupTeststepProperties) getTeststep().getOtherProperties();
    wireMockServer.loadMappingsUsing(stubMappings -> {
        for (HTTPStubMapping stubMapping : otherProperties.getHttpStubMappings()) {
            StubMapping stubInstance = IronTestUtils.createStubInstance(stubMapping.getId(), stubMapping.getNumber(), stubMapping.getSpec());
            stubMappings.addMapping(stubInstance);
            httpStubMappingInstanceIds.put(stubMapping.getNumber(), stubInstance.getId());
        }
    });
    return new BasicTeststepRun();
}
Also used : HTTPStubMapping(io.irontest.models.HTTPStubMapping) HTTPStubsSetupTeststepProperties(io.irontest.models.teststep.HTTPStubsSetupTeststepProperties) HTTPStubMapping(io.irontest.models.HTTPStubMapping) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) UUID(java.util.UUID) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer)

Example 2 with HTTPStubMapping

use of io.irontest.models.HTTPStubMapping in project irontest by zheng-wang.

the class HTTPStubMappingMapper method map.

@Override
public HTTPStubMapping map(ResultSet rs, StatementContext ctx) throws SQLException {
    String specJSON = rs.getString("spec_json");
    StubMapping spec = StubMapping.buildFrom(specJSON);
    HTTPStubMapping httpStubMapping = new HTTPStubMapping(rs.getLong("id"), rs.getLong("testcase_id"), rs.getShort("number"), spec, rs.getString("request_body_main_pattern_value"), rs.getShort("expected_hit_count"));
    return httpStubMapping;
}
Also used : HTTPStubMapping(io.irontest.models.HTTPStubMapping) HTTPStubMapping(io.irontest.models.HTTPStubMapping) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping)

Example 3 with HTTPStubMapping

use of io.irontest.models.HTTPStubMapping in project irontest by zheng-wang.

the class TestcaseRunner method preProcessing.

// process the test case before starting to run it
void preProcessing() {
    if (!testcase.getHttpStubMappings().isEmpty()) {
        // add HTTPStubsSetup step
        Teststep httpStubsSetupStep = new Teststep(Teststep.TYPE_HTTP_STUBS_SETUP);
        httpStubsSetupStep.setName("Set up HTTP stubs");
        HTTPStubsSetupTeststepProperties stubsSetupTeststepProperties = new HTTPStubsSetupTeststepProperties();
        stubsSetupTeststepProperties.setHttpStubMappings(testcase.getHttpStubMappings());
        httpStubsSetupStep.setOtherProperties(stubsSetupTeststepProperties);
        testcase.getTeststeps().add(0, httpStubsSetupStep);
        // add HTTPStubRequestsCheck step and its assertions
        Teststep stubRequestsCheckStep = new Teststep(Teststep.TYPE_HTTP_STUB_REQUESTS_CHECK);
        testcase.getTeststeps().add(testcase.getTeststeps().size(), stubRequestsCheckStep);
        stubRequestsCheckStep.setName("Check HTTP stub requests");
        for (HTTPStubMapping stub : testcase.getHttpStubMappings()) {
            Assertion stubHitAssertion = new Assertion(Assertion.TYPE_HTTP_STUB_HIT);
            stubHitAssertion.setName("Stub was hit");
            stubHitAssertion.setOtherProperties(new HTTPStubHitAssertionProperties(stub.getNumber(), stub.getExpectedHitCount()));
            stubRequestsCheckStep.getAssertions().add(stubHitAssertion);
        }
        if (testcase.getHttpStubMappings().size() > 1 && testcase.isCheckHTTPStubsHitOrder()) {
            Assertion stubsHitInOrderAssertion = new Assertion(Assertion.TYPE_HTTP_STUBS_HIT_IN_ORDER);
            stubsHitInOrderAssertion.setName("Stubs were hit in order");
            List<Short> expectedHitOrder = new ArrayList<>();
            for (HTTPStubMapping stub : testcase.getHttpStubMappings()) {
                expectedHitOrder.add(stub.getNumber());
            }
            stubsHitInOrderAssertion.setOtherProperties(new HTTPStubsHitInOrderAssertionProperties(expectedHitOrder));
            stubRequestsCheckStep.getAssertions().add(stubsHitInOrderAssertion);
        }
        Assertion allStubRequestsMatchedAssertion = new Assertion(Assertion.TYPE_ALL_HTTP_STUB_REQUESTS_MATCHED);
        allStubRequestsMatchedAssertion.setName("All stub requests were matched");
        stubRequestsCheckStep.getAssertions().add(allStubRequestsMatchedAssertion);
    }
    for (Teststep teststep : testcase.getTeststeps()) {
        if (Teststep.TYPE_IIB.equals(teststep.getType()) && Teststep.ACTION_WAIT_FOR_PROCESSING_COMPLETION.equals(teststep.getAction())) {
            Teststep waitUntilNextSecondStep = new Teststep(Teststep.TYPE_WAIT_UNTIL_NEXT_SECOND);
            waitUntilNextSecondStep.setName("Wait until next second");
            testcase.getTeststeps().add(0, waitUntilNextSecondStep);
            break;
        }
    }
}
Also used : Teststep(io.irontest.models.teststep.Teststep) HTTPStubMapping(io.irontest.models.HTTPStubMapping) HTTPStubsSetupTeststepProperties(io.irontest.models.teststep.HTTPStubsSetupTeststepProperties)

Example 4 with HTTPStubMapping

use of io.irontest.models.HTTPStubMapping 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)

Example 5 with HTTPStubMapping

use of io.irontest.models.HTTPStubMapping in project irontest by zheng-wang.

the class TestcaseDAO method findById_Complete.

@Transaction
default Testcase findById_Complete(long id) {
    Testcase result = _findById(id);
    result.setFolderPath(getFolderPath(id));
    List<UserDefinedProperty> udps = udpDAO().findByTestcaseId(id);
    result.setUdps(udps);
    List<Teststep> teststeps = teststepDAO().findByTestcaseId_Complete(id);
    result.setTeststeps(teststeps);
    DataTable dataTable = dataTableDAO().getTestcaseDataTable(id, false);
    result.setDataTable(dataTable);
    List<HTTPStubMapping> httpStubMappings = httpStubMappingDAO().findByTestcaseId(id);
    result.setHttpStubMappings(httpStubMappings);
    return result;
}
Also used : Teststep(io.irontest.models.teststep.Teststep) DataTable(io.irontest.models.DataTable) HTTPStubMapping(io.irontest.models.HTTPStubMapping) UserDefinedProperty(io.irontest.models.UserDefinedProperty) Testcase(io.irontest.models.Testcase) Transaction(org.jdbi.v3.sqlobject.transaction.Transaction)

Aggregations

HTTPStubMapping (io.irontest.models.HTTPStubMapping)6 StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)3 UserDefinedProperty (io.irontest.models.UserDefinedProperty)3 Teststep (io.irontest.models.teststep.Teststep)3 DataTable (io.irontest.models.DataTable)2 HTTPStubsSetupTeststepProperties (io.irontest.models.teststep.HTTPStubsSetupTeststepProperties)2 Transaction (org.jdbi.v3.sqlobject.transaction.Transaction)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 MapValueLookup (io.irontest.core.MapValueLookup)1 Testcase (io.irontest.models.Testcase)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 PermitAll (javax.annotation.security.PermitAll)1 StrSubstitutor (org.apache.commons.text.StrSubstitutor)1