use of clients.model.VehicleClass in project cvs-auto-svc by dvsa.
the class VehicleTechnicalRecordsClient method createTechRecord.
public Map<String, Object> createTechRecord(JSONObject restrictions) {
List<JsonPathAlteration> techRecordAlterations = new ArrayList<>();
String vehicleType = "";
try {
int length = restrictions.length();
if (length != 0) {
Iterator iterator = restrictions.keys();
Object[] names = new Object[length];
int i = 0;
while (iterator.hasNext()) {
names[i] = iterator.next();
if (names[i].toString().contentEquals("vehicleType")) {
vehicleType = restrictions.get(names[i].toString()).toString();
}
if (names[i].toString().contentEquals("vehicleClass")) {
String vehicleClassFields = "";
for (VehicleClass vehicleClass : VehicleClass.values()) {
if (vehicleClass.getCode().contentEquals(restrictions.get(names[i].toString()).toString())) {
vehicleClassFields = "{\"description\":\"" + vehicleClass.getDescription(restrictions.get(names[i].toString()).toString()) + "\"}";
break;
}
}
// add tech record alteration for each restriction set on the test type to be created
JsonPathAlteration additionalTechRecordAlteration = new JsonPathAlteration("$.techRecord[0]." + names[i].toString(), vehicleClassFields, "", "REPLACE");
techRecordAlterations.add(additionalTechRecordAlteration);
} else {
// add tech record alteration for each restriction set on the test type to be created
JsonPathAlteration additionalTechRecordAlteration = new JsonPathAlteration("$.techRecord[0]." + names[i].toString(), restrictions.get(names[i].toString()), "", "REPLACE");
techRecordAlterations.add(additionalTechRecordAlteration);
}
i += 1;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
String techRecordPayloadFile = "technical-records_" + vehicleType + ".json";
// CREATE TECH RECORD
Map<String, Object> testResultAttributes = new HashMap<>();
// generate random Vin
String randomVin = GenericData.generateRandomVin();
// generate random Vrm
String randomVrm = GenericData.generateRandomVrm();
if (!(vehicleType.contentEquals("hgv") || vehicleType.contentEquals("psv") || vehicleType.contentEquals("trl") || vehicleType.contentEquals("lgv") || vehicleType.contentEquals("car") || vehicleType.contentEquals("motorcycle"))) {
throw new AutomationException("Invalid vehicle type");
}
// read post request body from file
String postTechRecordBody = GenericData.readJsonValueFromFile(techRecordPayloadFile, "$");
// create alteration to change Vin in the request body with the random generated Vin
JsonPathAlteration alterationVin = new JsonPathAlteration("$.vin", randomVin, "", "REPLACE");
// create alteration to change primary vrm in the request body with the random generated primary vrm
JsonPathAlteration alterationVrm = new JsonPathAlteration("$.primaryVrm", randomVrm, "", "REPLACE");
// additional alterations for tech record
techRecordAlterations.add(alterationVin);
techRecordAlterations.add(alterationVrm);
Response postTechRecordsResponse = postVehicleTechnicalRecordsWithAlterations(postTechRecordBody, techRecordAlterations);
postTechRecordsResponse.prettyPrint();
if (postTechRecordsResponse.statusCode() != 201) {
postTechRecordsResponse.prettyPrint();
throw new AutomationException("The post tech records request was not successful, status code was " + postTechRecordsResponse.statusCode());
}
Response getTechRecordsResponse = getVehicleTechnicalRecords(randomVin);
getTechRecordsResponse.prettyPrint();
if (getTechRecordsResponse.statusCode() != 200) {
postTechRecordsResponse.prettyPrint();
throw new AutomationException("The get tech records request was not successful, status code was " + getTechRecordsResponse.statusCode());
}
String systemNumber = GenericData.extractStringValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].systemNumber");
String trailerId;
String firstUseDate;
if (vehicleType.equals("trl")) {
trailerId = GenericData.extractStringValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].trailerId");
firstUseDate = GenericData.extractStringValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].firstUseDate");
} else {
trailerId = null;
firstUseDate = null;
}
int noOfAxles = GenericData.extractIntegerValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].noOfAxles");
String vehicleClassCode = GenericData.extractStringValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].vehicleClass.code");
String vehicleClassDescription = GenericData.extractStringValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].vehicleClass.description");
JSONArray vehicleSubclass;
if (vehicleType.contentEquals("car") || vehicleType.contentEquals("lgv")) {
try {
vehicleSubclass = new JSONArray(GenericData.extractArrayListStringFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].vehicleSubclass").toString());
} catch (JSONException e) {
throw new AutomationException(e.getMessage());
}
} else {
vehicleSubclass = null;
}
String vehicleConfiguration;
if (!(vehicleType.contentEquals("car") || vehicleType.contentEquals("motorcycle") || vehicleType.contentEquals("lgv"))) {
vehicleConfiguration = GenericData.extractStringValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].vehicleConfiguration");
} else {
vehicleConfiguration = null;
}
String vehicleSize;
if (vehicleType.contentEquals("psv")) {
vehicleSize = GenericData.extractStringValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].vehicleSize");
} else {
vehicleSize = null;
}
String euVehicleCategory = GenericData.extractStringValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].euVehicleCategory");
String regnDate;
if (!vehicleType.contentEquals("trl")) {
regnDate = GenericData.extractStringValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].regnDate");
} else {
regnDate = null;
}
int numberOfWheelsDriven;
if (vehicleType.contentEquals("psv") || vehicleType.contentEquals("hgv") || vehicleType.contentEquals("motorcycle")) {
numberOfWheelsDriven = GenericData.extractIntegerValueFromJsonString(getTechRecordsResponse.prettyPrint(), "$[0].techRecord[0].numberOfWheelsDriven");
} else {
numberOfWheelsDriven = -1;
}
// add in map all attributes that we need to change in the test results payload so they match the values on the
// newly created tech record
testResultAttributes.put("vin", randomVin);
testResultAttributes.put("vrm", randomVrm);
testResultAttributes.put("systemNumber", systemNumber);
if (trailerId != null) {
testResultAttributes.put("trailerId", trailerId);
}
testResultAttributes.put("noOfAxles", noOfAxles);
testResultAttributes.put("vehicleClass.code", vehicleClassCode);
testResultAttributes.put("vehicleClass.description", vehicleClassDescription);
if (vehicleSubclass != null) {
testResultAttributes.put("vehicleSubclass", vehicleSubclass);
}
if (vehicleConfiguration != null) {
testResultAttributes.put("vehicleConfiguration", vehicleConfiguration);
}
if (vehicleSize != null) {
testResultAttributes.put("vehicleSize", vehicleSize);
}
testResultAttributes.put("euVehicleCategory", euVehicleCategory);
if (firstUseDate != null) {
testResultAttributes.put("firstUseDate", firstUseDate);
}
if (regnDate != null) {
testResultAttributes.put("regnDate", regnDate);
}
if (numberOfWheelsDriven != -1) {
testResultAttributes.put("numberOfWheelsDriven", numberOfWheelsDriven);
}
testResultAttributes.put("vehicleType", vehicleType);
return testResultAttributes;
}
Aggregations