11package io .cucumber .query ;
22
33import io .cucumber .messages .Convertor ;
4+ import io .cucumber .messages .types .Attachment ;
45import io .cucumber .messages .types .Envelope ;
56import io .cucumber .messages .types .Examples ;
67import io .cucumber .messages .types .Feature ;
78import io .cucumber .messages .types .GherkinDocument ;
9+ import io .cucumber .messages .types .Hook ;
10+ import io .cucumber .messages .types .Meta ;
811import io .cucumber .messages .types .Pickle ;
912import io .cucumber .messages .types .PickleStep ;
1013import io .cucumber .messages .types .Rule ;
@@ -67,7 +70,10 @@ public final class Query {
6770 private final Map <String , Step > stepById = new ConcurrentHashMap <>();
6871 private final Map <String , TestStep > testStepById = new ConcurrentHashMap <>();
6972 private final Map <String , PickleStep > pickleStepById = new ConcurrentHashMap <>();
73+ private final Map <String , Hook > hookById = new ConcurrentHashMap <>();
74+ private final Map <String , List <Attachment >> attachmentsByTestCaseStartedId = new ConcurrentHashMap <>();
7075 private final Map <Object , Lineage > lineageById = new ConcurrentHashMap <>();
76+ private Meta meta ;
7177 private TestRunStarted testRunStarted ;
7278 private TestRunFinished testRunFinished ;
7379
@@ -135,10 +141,29 @@ public List<TestStep> findAllTestSteps() {
135141 .collect (toList ());
136142 }
137143
144+ public List <Attachment > findAttachmentsBy (TestStepFinished testStepFinished ) {
145+ requireNonNull (testStepFinished );
146+ return attachmentsByTestCaseStartedId .getOrDefault (testStepFinished .getTestCaseStartedId (), emptyList ()).stream ()
147+ .filter (attachment -> attachment .getTestStepId ()
148+ .map (testStepId -> testStepFinished .getTestStepId ().equals (testStepId ))
149+ .orElse (false ))
150+ .collect (toList ());
151+ }
152+
138153 public Optional <Feature > findFeatureBy (TestCaseStarted testCaseStarted ) {
139154 return findLineageBy (testCaseStarted ).flatMap (Lineage ::feature );
140155 }
141156
157+ public Optional <Hook > findHookBy (TestStep testStep ) {
158+ requireNonNull (testStep );
159+ return testStep .getHookId ()
160+ .map (hookById ::get );
161+ }
162+
163+ public Optional <Meta > findMeta () {
164+ return ofNullable (meta );
165+ }
166+
142167 public Optional <TestStepResult > findMostSevereTestStepResultBy (TestCaseStarted testCaseStarted ) {
143168 requireNonNull (testCaseStarted );
144169 return findTestStepsFinishedBy (testCaseStarted )
@@ -330,6 +355,7 @@ public List<Entry<TestStepFinished, TestStep>> findTestStepFinishedAndTestStepBy
330355 }
331356
332357 public void update (Envelope envelope ) {
358+ envelope .getMeta ().ifPresent (this ::updateMeta );
333359 envelope .getTestRunStarted ().ifPresent (this ::updateTestRunStarted );
334360 envelope .getTestRunFinished ().ifPresent (this ::updateTestRunFinished );
335361 envelope .getTestCaseStarted ().ifPresent (this ::updateTestCaseStarted );
@@ -338,6 +364,8 @@ public void update(Envelope envelope) {
338364 envelope .getGherkinDocument ().ifPresent (this ::updateGherkinDocument );
339365 envelope .getPickle ().ifPresent (this ::updatePickle );
340366 envelope .getTestCase ().ifPresent (this ::updateTestCase );
367+ envelope .getHook ().ifPresent (this ::updateHook );
368+ envelope .getAttachment ().ifPresent (this ::updateAttachment );
341369 }
342370
343371 private Optional <Lineage > findLineageBy (GherkinDocument element ) {
@@ -382,6 +410,15 @@ private Optional<Lineage> findLineageBy(TestCaseStarted testCaseStarted) {
382410 .flatMap (this ::findLineageBy );
383411 }
384412
413+ private void updateAttachment (Attachment attachment ) {
414+ attachment .getTestCaseStartedId ()
415+ .ifPresent (testCaseStartedId -> this .attachmentsByTestCaseStartedId .compute (testCaseStartedId , updateList (attachment )));
416+ }
417+
418+ private void updateHook (Hook hook ) {
419+ this .hookById .put (hook .getId (), hook );
420+ }
421+
385422 private void updateTestCaseStarted (TestCaseStarted testCaseStarted ) {
386423 this .testCaseStarted .add (testCaseStarted );
387424 }
@@ -437,6 +474,10 @@ private void updateSteps(List<Step> steps) {
437474 steps .forEach (step -> stepById .put (step .getId (), step ));
438475 }
439476
477+ private void updateMeta (Meta event ) {
478+ this .meta = event ;
479+ }
480+
440481 private <K , E > BiFunction <K , List <E >, List <E >> updateList (E element ) {
441482 return (key , existing ) -> {
442483 if (existing != null ) {
0 commit comments