66import static io .restassured .RestAssured .given ;
77import static org .junit .jupiter .api .Assertions .*;
88
9- import com .johnlpage .memex .cucumber .CucumberTestsContainersConfig ;
9+ import com .fasterxml .jackson .core .JsonProcessingException ;
10+ import com .fasterxml .jackson .databind .ObjectMapper ;
11+ import com .johnlpage .memex .model .VehicleInspection ;
1012import io .cucumber .datatable .DataTable ;
1113import io .cucumber .java .ParameterType ;
1214import io .cucumber .java .en .Given ;
1315import io .cucumber .java .en .When ;
1416import io .cucumber .java .en .Then ;
15- import io .cucumber .spring .CucumberContextConfiguration ;
1617import io .restassured .http .ContentType ;
1718import io .restassured .path .json .JsonPath ;
1819import io .restassured .response .Response ;
20+
1921import java .time .ZonedDateTime ;
2022import java .time .format .DateTimeFormatter ;
2123import java .util .List ;
2224import java .util .Map ;
2325import java .util .concurrent .TimeUnit ;
2426
25- import org .springframework .boot .test .context .SpringBootTest ;
27+ import org .bson .Document ;
28+ import org .springframework .beans .factory .annotation .Autowired ;
29+ import org .springframework .beans .factory .annotation .Value ;
2630import org .springframework .boot .test .web .server .LocalServerPort ;
27- import org .springframework .test .context .ActiveProfiles ;
31+ import org .springframework .data .mongodb .core .MongoTemplate ;
32+ import org .springframework .data .mongodb .core .query .Criteria ;
33+ import org .springframework .data .mongodb .core .query .Query ;
34+ import org .springframework .data .mongodb .core .query .Update ;
2835
29- @ CucumberContextConfiguration
30- @ SpringBootTest (classes = CucumberTestsContainersConfig .class , webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
31- @ ActiveProfiles ("test" )
3236public class InspectionSteps {
3337
3438 @ LocalServerPort
3539 private int port ;
3640
41+ @ Autowired
42+ private MongoTemplate mongoTemplate ;
43+
44+ @ Autowired
45+ private ObjectMapper objectMapper ;
46+
47+ @ Autowired
48+ private VehicleInspectionIdRangeValidator idRangeValidator ;
49+
3750 private Response response ;
3851 private ZonedDateTime capturedTimestamp ;
3952
@@ -42,23 +55,72 @@ public String baseUrl() {
4255 }
4356
4457 @ ParameterType ("true|false" )
45- public Boolean bool (String bool ){
58+ public Boolean bool (String bool ) {
4659 return Boolean .parseBoolean (bool );
4760 }
4861
4962 @ Given ("the following vehicle inspections exist:" )
50- public void givenVehicleInspectionsExist (DataTable dataTable ) {
51- // test assumes it exists however it would be better to create it here
63+ public void givenVehicleInspectionsExist (DataTable dataTable ) throws JsonProcessingException {
64+ List <Map <String , String >> rows = dataTable .asMaps (String .class , String .class );
65+
66+ for (Map <String , String > row : rows ) {
67+ String json = row .get ("vehicleInspection" );
68+ VehicleInspection inspection = objectMapper .readValue (json , VehicleInspection .class );
69+ Long testId = inspection .getTestid ();
70+ assertNotNull (testId , "testid is expected to be part of the data input" );
71+
72+ idRangeValidator .validate (testId );
73+ Query query = Query .query (Criteria .where ("_id" ).is (testId ));
74+
75+ Document updateDoc = new Document (objectMapper .convertValue (inspection , Map .class ));
76+ Update update = Update .fromDocument (updateDoc );
77+
78+ mongoTemplate .upsert (query , update , VehicleInspection .class );
79+
80+ }
5281 }
5382
54- @ Given ("the following vehicle inspections exist and have historical data as of {string}:" )
55- public void givenVehicleInspectionsExist (String date , DataTable dataTable ) {
56- // test assumes it exists however it would be better to create it here
83+ @ Given ("the vehicle inspections in range {long}-{long} do not exist" )
84+ public void theFollowingVehicleInspectionsInRangeDoesNotExist (long startId , long endId ) {
85+ idRangeValidator .validateRange (startId , endId );
86+ Query query = new Query ();
87+ query .addCriteria (Criteria .where ("_id" ).gte (startId ).lte (endId ));
88+ mongoTemplate .remove (query , VehicleInspection .class );
89+ }
90+
91+ @ Given ("the vehicle inspection with id {long} does not exist" )
92+ public void theFollowingVehicleInspectionDoesNotExist (long testId ) {
93+ idRangeValidator .validate (testId );
94+ Query query = new Query ();
95+ query .addCriteria (Criteria .where ("_id" ).is (testId ));
96+ mongoTemplate .remove (query , VehicleInspection .class );
5797 }
5898
5999 @ Given ("the following vehicle inspections do not exist:" )
60- public void givenVehicleInspectionsDoNotExist (DataTable dataTable ) {
61- // test assumes it does not exist however it would be better to delete it here
100+ public void givenTheFollowingVehicleInspectionsDoNotExist (DataTable dataTable ) {
101+ for (Map <String , String > row : dataTable .asMaps ()) {
102+ if (row .size () != 1 ) {
103+ throw new IllegalArgumentException ("Only one column per row is supported in this step." );
104+ }
105+
106+ String key = row .keySet ().iterator ().next ();
107+ String value = row .get (key );
108+
109+ long rangeStart = idRangeValidator .getRangeStart ();
110+ long rangeEnd = idRangeValidator .getRangeEnd ();
111+
112+ Query query = Query .query (Criteria .where ("_id" ).gte (rangeStart ).lte (rangeEnd ));
113+
114+ if (key .equalsIgnoreCase ("testid" )) {
115+ long testid = Long .parseLong (value );
116+ idRangeValidator .validate (testid );
117+ query = Query .query (Criteria .where ("_id" ).is (testid ));
118+ } else {
119+ query .addCriteria (Criteria .where (key ).is (value ));
120+ }
121+ mongoTemplate .remove (query , VehicleInspection .class );
122+
123+ }
62124 }
63125
64126 @ Given ("I capture the current timestamp" )
@@ -156,7 +218,7 @@ public void eachItemInTheResponseArrayShouldContain(String key, String value) {
156218 List <Map <String , Object >> list = jsonPath .getList ("$" );
157219 assertFalse (list .isEmpty (), "Response array should not be empty for this check" );
158220 for (Map <String , Object > item : list ) {
159- if (key .indexOf ('.' ) > 0 ) {
221+ if (key .indexOf ('.' ) > 0 ) {
160222 // If the value is a vehicle field, we need to check the nested structure
161223 String [] parts = key .split ("\\ ." );
162224 assertThat (item , hasKey (parts [0 ]));
@@ -192,12 +254,12 @@ public void theResponseShouldBeAStreamOfValidJsonObjectsEachOnANewLine() {
192254 assertFalse (body .isEmpty (), "Response body should not be empty for stream check" );
193255
194256 // Split by any standard newline character(s)
195- String [] lines = body .split ("\\ r?\\ n" );
257+ String [] lines = body .split ("\\ r?\\ n" );
196258 assertTrue (lines .length > 0 , "Response body should contain at least one line for stream check" );
197259
198260 for (String line : lines ) {
199261 // Skip potentially empty lines that might result from splitting (e.g., trailing newline)
200- if (line .trim ().isEmpty ()) continue ;
262+ if (line .trim ().isEmpty ()) continue ;
201263 try {
202264 JsonPath .from (line ); // This will throw an exception if the line is not valid JSON
203265 } catch (Exception e ) {
0 commit comments