Search in sources :

Example 1 with TestTypes

use of clients.model.TestTypes in project cvs-auto-svc by dvsa.

the class TestResultsClient method createTestRecord.

public String createTestRecord(String testStatus, String testResult, String testCode, boolean withWithoutDefects, Map<String, Object> testResultAttributes) {
    List<JsonPathAlteration> testResultAlterations = new ArrayList<>();
    String testResultPayloadFile = "test-results_" + testResultAttributes.get("vehicleType").toString() + ".json";
    for (TestTypes testType : TestTypes.values()) {
        if (testType.getTestCode().contentEquals(testCode.toLowerCase())) {
            String testTypeId = testType.getId();
            // create test result alteration to change testTypeId
            JsonPathAlteration alterationTestTypeId = new JsonPathAlteration("$.testTypes[0].testTypeId", testTypeId, "", "REPLACE");
            testResultAlterations.add(alterationTestTypeId);
            String testTypeName = GenericData.readJsonValueFromFile("test-type.json", "$..[?(@.id =='" + testTypeId + "')].testTypeName");
            // create test result alteration to change testTypeName
            JsonPathAlteration alterationTestTypeName = new JsonPathAlteration("$.testTypes[0].testTypeName", testTypeName, "", "REPLACE");
            testResultAlterations.add(alterationTestTypeName);
            String testName = GenericData.readJsonValueFromFile("test-type.json", "$..[?(@.id =='" + testTypeId + "')].name");
            // create test result alteration to change testName
            JsonPathAlteration alterationTestName = new JsonPathAlteration("$.testTypes[0].name", testName, "", "REPLACE");
            testResultAlterations.add(alterationTestName);
            break;
        }
    }
    // CREATE TEST RESULT RECORD
    String postTestResultBody = GenericData.readJsonValueFromFile(testResultPayloadFile, "$");
    String testResultId = UUID.randomUUID().toString();
    // create alteration to change testStatus
    JsonPathAlteration alterationTestStatus = new JsonPathAlteration("$.testStatus", testStatus, "", "REPLACE");
    // create alteration to change testResultId
    JsonPathAlteration alterationTestResultId = new JsonPathAlteration("$.testResultId", testResultId, "", "REPLACE");
    // create alteration to change testResult
    JsonPathAlteration alterationTestResult = new JsonPathAlteration("$.testTypes[0].testResult", testResult, "", "REPLACE");
    JsonPathAlteration alterationPreparerName = new JsonPathAlteration("$.preparerName", "test", "", "REPLACE");
    LocalDateTime currentDate = LocalDateTime.now(ZoneId.of("Europe/London"));
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    String endDate = currentDate.plusMinutes(50).format(formatter);
    String expiryDate = currentDate.plusYears(1).format(formatter);
    // create alteration to change testStartTimestamp
    JsonPathAlteration alterationTestStartTimestamp = new JsonPathAlteration("$.testStartTimestamp", currentDate.format(formatter), "", "REPLACE");
    // create alteration to change testEndTimestamp
    JsonPathAlteration alterationTestEndTimestamp = new JsonPathAlteration("$.testEndTimestamp", endDate, "", "REPLACE");
    JsonPathAlteration alterationTestTypeStartTimestamp = new JsonPathAlteration("$.testTypes[0].testTypeStartTimestamp", currentDate.format(formatter), "", "REPLACE");
    // create alteration to change testTypeEndTimestamp
    JsonPathAlteration alterationTestTypeEndTimestamp = new JsonPathAlteration("$.testTypes[0].testTypeEndTimestamp", endDate, "", "REPLACE");
    // create alteration to change testExpiryDate
    JsonPathAlteration alterationTestExpiryDate = new JsonPathAlteration("$.testTypes[0].testExpiryDate", expiryDate, "", "REPLACE");
    if (!(testResult.toLowerCase().contentEquals("pass"))) {
        alterationTestExpiryDate = new JsonPathAlteration("$.testTypes[0].testExpiryDate", "", "", "DELETE");
    }
    // Iterate over the HashMap to add all alteration for common attributes on tech records and test results
    for (Map.Entry<String, Object> entry : testResultAttributes.entrySet()) {
        JsonPathAlteration additionalTestResultAlteration = new JsonPathAlteration("$." + entry.getKey(), entry.getValue(), "", "REPLACE");
        testResultAlterations.add(additionalTestResultAlteration);
    }
    // add remaining alterations to list of test result alterations
    testResultAlterations.addAll(new ArrayList<>(Arrays.asList(alterationTestStatus, alterationTestResultId, alterationTestResult, alterationTestExpiryDate, alterationTestStartTimestamp, alterationTestEndTimestamp, alterationTestTypeStartTimestamp, alterationTestTypeEndTimestamp)));
    // add alteration for adding
    DocumentContext jsonContext = JsonPath.parse(postTestResultBody);
    HashMap testType = jsonContext.read("$.testTypes[0]");
    if (!(testType.containsKey("certficateNumber"))) {
        if ((testCode.toLowerCase().contentEquals("tiv")) || (testCode.toLowerCase().contentEquals("tit")) || (testCode.toLowerCase().contentEquals("trv")) || (testCode.toLowerCase().contentEquals("trt")) || (testCode.toLowerCase().contentEquals("rft")) || (testCode.toLowerCase().contentEquals("ddv")) || (testCode.toLowerCase().contentEquals("ddt")) || (testCode.toLowerCase().contentEquals("arv")) || (testCode.toLowerCase().contentEquals("art")) || (testCode.toLowerCase().contentEquals("drv")) || (testCode.toLowerCase().contentEquals("drt"))) {
            JsonPathAlteration alterationAddCertificateNumber = new JsonPathAlteration("$.testTypes[0]", "12345678", "certificateNumber", "ADD_FIELD");
            testResultAlterations.add(alterationAddCertificateNumber);
        }
    }
    if (testResult.toLowerCase().contentEquals("abandoned")) {
        // create alteration to change reasonForAbandoning
        JsonPathAlteration alterationReasonForAbandoning = new JsonPathAlteration("$.testTypes[0].reasonForAbandoning", "reason for abandoning", "", "REPLACE");
        // create alteration to change additionalCommentsForAbandon
        JsonPathAlteration alterationAdditionalCommentsForAbandon = new JsonPathAlteration("$.testTypes[0].additionalCommentsForAbandon", "additional comments for abandon", "", "REPLACE");
        testResultAlterations.add(alterationReasonForAbandoning);
        testResultAlterations.add(alterationAdditionalCommentsForAbandon);
    }
    if (!withWithoutDefects) {
        // create alteration to remove defects
        JsonPathAlteration alterationDefects = new JsonPathAlteration("$.testTypes[0].defects", "[]", "", "REPLACE");
        testResultAlterations.add(alterationDefects);
    }
    Response responsePostTestResults = postVehicleTestResultsWithAlterations(postTestResultBody, testResultAlterations);
    if (responsePostTestResults.statusCode() != 201) {
        responsePostTestResults.prettyPrint();
        throw new AutomationException("The post test results request was not successful, status code was " + responsePostTestResults.statusCode());
    }
    return testResultId;
}
Also used : LocalDateTime(java.time.LocalDateTime) TestTypes(clients.model.TestTypes) AutomationException(exceptions.AutomationException) Response(io.restassured.response.Response) DocumentContext(com.jayway.jsonpath.DocumentContext) DateTimeFormatter(java.time.format.DateTimeFormatter)

Aggregations

TestTypes (clients.model.TestTypes)1 DocumentContext (com.jayway.jsonpath.DocumentContext)1 AutomationException (exceptions.AutomationException)1 Response (io.restassured.response.Response)1 LocalDateTime (java.time.LocalDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1