|
| 1 | +package com.johnlpage.memex.cucumber.steps; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.johnlpage.memex.cucumber.service.VehicleInspectionIdRangeValidator; |
| 6 | +import com.johnlpage.memex.model.VehicleInspection; |
| 7 | +import com.mongodb.client.result.DeleteResult; |
| 8 | +import io.cucumber.datatable.DataTable; |
| 9 | +import io.cucumber.java.en.Given; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.bson.Document; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.data.mongodb.core.FindAndModifyOptions; |
| 14 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 15 | +import org.springframework.data.mongodb.core.query.Criteria; |
| 16 | +import org.springframework.data.mongodb.core.query.Query; |
| 17 | +import org.springframework.data.mongodb.core.query.Update; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +public class MongoPreConditionSteps { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private MongoTemplate mongoTemplate; |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private ObjectMapper objectMapper; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private VehicleInspectionIdRangeValidator idRangeValidator; |
| 35 | + |
| 36 | + @Given("the following vehicle inspections exist:") |
| 37 | + public void givenVehicleInspectionsExist(DataTable dataTable) throws JsonProcessingException { |
| 38 | + List<Map<String, String>> rows = dataTable.asMaps(String.class, String.class); |
| 39 | + |
| 40 | + for (Map<String, String> row : rows) { |
| 41 | + String json = row.get("vehicleInspection"); |
| 42 | + VehicleInspection inspection = objectMapper.readValue(json, VehicleInspection.class); |
| 43 | + Long testId = inspection.getTestid(); |
| 44 | + assertNotNull(testId, "testid is expected to be part of the data input"); |
| 45 | + |
| 46 | + idRangeValidator.validate(testId); |
| 47 | + Query query = Query.query(Criteria.where("_id").is(testId)); |
| 48 | + |
| 49 | + Document updateDoc = new Document(objectMapper.convertValue(inspection, Map.class)); |
| 50 | + Update update = Update.fromDocument(updateDoc); |
| 51 | + |
| 52 | + VehicleInspection existingInspection = mongoTemplate.findAndModify(query, update, |
| 53 | + FindAndModifyOptions.options().upsert(true).returnNew(true), VehicleInspection.class); |
| 54 | + |
| 55 | + log.info("Upserted VehicleInspection with id {}: {}", testId, existingInspection); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Given("the vehicle inspections in range {long}-{long} do not exist") |
| 60 | + public void theFollowingVehicleInspectionsInRangeDoesNotExist(long startId, long endId) { |
| 61 | + idRangeValidator.validateRange(startId, endId); |
| 62 | + Query query = new Query(); |
| 63 | + query.addCriteria(Criteria.where("_id").gte(startId).lte(endId)); |
| 64 | + DeleteResult result = mongoTemplate.remove(query, VehicleInspection.class); |
| 65 | + |
| 66 | + if(result.getDeletedCount() > 0) { |
| 67 | + log.info("Removed VehicleInspection within a range {}-{}: {}", startId, endId, result.getDeletedCount()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Given("the vehicle inspection with id {long} does not exist") |
| 72 | + public void theFollowingVehicleInspectionDoesNotExist(long testId) { |
| 73 | + idRangeValidator.validate(testId); |
| 74 | + Query query = new Query(); |
| 75 | + query.addCriteria(Criteria.where("_id").is(testId)); |
| 76 | + DeleteResult result = mongoTemplate.remove(query, VehicleInspection.class); |
| 77 | + |
| 78 | + if(result.getDeletedCount() > 0) { |
| 79 | + log.info("Removed VehicleInspection with id {}: {}", testId, result.getDeletedCount()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Given("the following vehicle inspections do not exist:") |
| 84 | + public void givenTheFollowingVehicleInspectionsDoNotExist(DataTable dataTable) { |
| 85 | + for (Map<String, String> row : dataTable.asMaps()) { |
| 86 | + if (row.size() != 1) { |
| 87 | + throw new IllegalArgumentException("Only one column per row is supported in this step."); |
| 88 | + } |
| 89 | + |
| 90 | + String key = row.keySet().iterator().next(); |
| 91 | + String value = row.get(key); |
| 92 | + |
| 93 | + long rangeStart = idRangeValidator.getRangeStart(); |
| 94 | + long rangeEnd = idRangeValidator.getRangeEnd(); |
| 95 | + |
| 96 | + Query query = Query.query(Criteria.where("_id").gte(rangeStart).lte(rangeEnd)); |
| 97 | + |
| 98 | + if (key.equalsIgnoreCase("testid")) { |
| 99 | + long testid = Long.parseLong(value); |
| 100 | + idRangeValidator.validate(testid); |
| 101 | + query = Query.query(Criteria.where("_id").is(testid)); |
| 102 | + } else { |
| 103 | + query.addCriteria(Criteria.where(key).is(value)); |
| 104 | + } |
| 105 | + DeleteResult result = mongoTemplate.remove(query, VehicleInspection.class); |
| 106 | + |
| 107 | + if (result.getDeletedCount() > 0) { |
| 108 | + log.info("Removed VehicleInspection with {}: {} - Count: {}", key, value, result.getDeletedCount()); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments