11package com .johnlpage .memex .cucumber .steps ;
22
3+ import static org .junit .jupiter .api .Assertions .*;
4+
35import com .fasterxml .jackson .core .JsonProcessingException ;
46import com .fasterxml .jackson .databind .JsonNode ;
57import com .fasterxml .jackson .databind .ObjectMapper ;
68import com .johnlpage .memex .cucumber .service .VehicleInspectionIdRangeValidator ;
79import com .johnlpage .memex .model .VehicleInspection ;
810import io .cucumber .java .en .Then ;
911import io .cucumber .java .en .When ;
12+ import jakarta .annotation .PostConstruct ;
1013import org .assertj .core .api .Assertions ;
1114import org .springframework .beans .factory .annotation .Autowired ;
12- import org .springframework .data .mongodb .core .MongoTemplate ;
13- import org .springframework .data .mongodb .core .query .Criteria ;
14- import org .springframework .data .mongodb .core .query .Query ;
15+ import org .springframework .beans .factory .annotation .Value ;
16+ import org .springframework .core .ParameterizedTypeReference ;
17+ import org .springframework .http .MediaType ;
18+ import org .springframework .http .ResponseEntity ;
1519import org .springframework .kafka .core .KafkaTemplate ;
20+ import org .springframework .web .client .RestClient ;
1621
1722import java .util .Iterator ;
1823import java .util .List ;
1924import java .util .Map ;
2025
21-
2226public class KafkaConsumerSteps {
2327
28+ @ Value ("${memex.base-url}" )
29+ private String apiBaseUrl ;
30+
2431 @ Autowired
2532 private KafkaTemplate <String , String > kafkaTemplate ;
2633
2734 @ Autowired
28- private MongoTemplate mongoTemplate ;
35+ private RestClient .Builder restClientBuilder ;
36+
37+ private RestClient restClient ;
38+
39+ @ PostConstruct
40+ public void init () {
41+ this .restClient = restClientBuilder .baseUrl (apiBaseUrl ).build ();
42+ }
2943
3044 @ Autowired
3145 private ObjectMapper objectMapper ;
3246
3347 @ Autowired
3448 private VehicleInspectionIdRangeValidator idRangeValidator ;
3549
36- // TODO: Use public API to save the vehicle inspections
3750 @ When ("I send {int} vehicle inspections starting with id {long} to kafka with:" )
3851 public void sendVehicleInspectionsToKafka (int count , long startId , String jsonTemplate ) throws JsonProcessingException {
3952 idRangeValidator .validate (startId );
@@ -50,26 +63,40 @@ public void sendVehicleInspectionsToKafka(int count, long startId, String jsonTe
5063 }
5164 }
5265
53- // TODO: Use public API to verify the vehicle inspections are saved
54- @ Then ("verify {int} vehicle inspections are saved starting from id {long} in mongo with:" )
66+ @ Then ("verify {int} vehicle inspections starting from id {long} do exist with:" )
5567 public void verifyVehicleInspectionsSaved (int count , long startId , String expectedJson ) throws JsonProcessingException {
5668 long endId = startId + count - 1 ;
5769 idRangeValidator .validateRange (startId , endId );
5870
5971 JsonNode expectedNode = objectMapper .readTree (expectedJson );
6072
61- Query query = Query .query (Criteria .where ("testid" ).gte (startId ).lte (endId ));
62- List <VehicleInspection > inspections = mongoTemplate .find (query , VehicleInspection .class );
63-
64- Assertions .assertThat (inspections ).hasSize (count );
65-
66- for (VehicleInspection inspection : inspections ) {
67- JsonNode inspectionJson = objectMapper .readTree (objectMapper .writeValueAsString (inspection ));
68- assertJsonContains (expectedNode , inspectionJson );
69- }
73+ String rangeCheck = "\" _id\" : {\" $gte\" : " + startId + ", \" $lte\" : " + endId + "}" ;
74+ String mongoQueryJson = String .format ("{\" filter\" : {%s}}" , rangeCheck );
75+
76+ ResponseEntity <List <VehicleInspection >> responseEntity = restClient .post ()
77+ .uri (apiBaseUrl + "/api/inspections/query" )
78+ .contentType (MediaType .APPLICATION_JSON )
79+ .body (mongoQueryJson )
80+ .retrieve ()
81+ .toEntity (new ParameterizedTypeReference <List <VehicleInspection >>() {
82+ });
83+
84+ assertTrue (responseEntity .getStatusCode ().is2xxSuccessful ());
85+ List <VehicleInspection > inspections = responseEntity .getBody ();
86+
87+ assertNotNull (inspections );
88+ assertEquals (count , inspections .size ());
89+ inspections .forEach ((inspection ) -> {
90+ JsonNode actualJson = null ;
91+ try {
92+ actualJson = objectMapper .readTree (objectMapper .writeValueAsString (inspection ));
93+ } catch (JsonProcessingException e ) {
94+ throw new RuntimeException ("Vehicle inspection verification failed for testid: " + inspection .getTestid (), e );
95+ }
96+ assertJsonContains (expectedNode , actualJson );
97+ });
7098 }
7199
72-
73100 private void assertJsonContains (JsonNode expected , JsonNode actual ) {
74101 for (Iterator <Map .Entry <String , JsonNode >> it = expected .fields (); it .hasNext (); ) {
75102 Map .Entry <String , JsonNode > field = it .next ();
@@ -89,4 +116,11 @@ private void assertJsonContains(JsonNode expected, JsonNode actual) {
89116 }
90117 }
91118
92- }
119+ public ResponseEntity <VehicleInspection > makeGetVehicleInspectionByIdRequest (long id ) {
120+ return restClient .get ()
121+ .uri (apiBaseUrl + "/api/inspections/id/" + id )
122+ .retrieve ()
123+ .toEntity (VehicleInspection .class );
124+ }
125+
126+ }
0 commit comments