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