2121import java .util .Date ;
2222import java .util .List ;
2323import java .util .Set ;
24+ import java .util .function .UnaryOperator ;
2425
2526import org .flowable .cmmn .api .history .HistoricPlanItemInstance ;
2627import org .flowable .cmmn .api .history .HistoricPlanItemInstanceQuery ;
@@ -203,6 +204,16 @@ public void testByAssignee() {
203204 .list ();
204205 assertThat (planItemInstances ).hasSize (0 );
205206
207+ // or(): assignee "gonzo" OR bogus case definition id => the same 2 assigned human tasks
208+ planItemInstances = cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
209+ .or ()
210+ .planItemInstanceAssignee ("gonzo" )
211+ .planItemInstanceCaseDefinitionId ("undefinedId" )
212+ .endOr ()
213+ .list ();
214+ assertThat (planItemInstances )
215+ .extracting (HistoricPlanItemInstance ::getAssignee )
216+ .containsExactlyInAnyOrder ("gonzo" , "gonzo" );
206217 }
207218
208219 @ Test
@@ -233,6 +244,16 @@ public void testByCompletedBy() {
233244 .list ();
234245 assertThat (planItemInstances ).hasSize (0 );
235246
247+ // or(): completedBy "kermit" OR bogus case definition id => the same 3 completed human tasks
248+ planItemInstances = cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
249+ .or ()
250+ .planItemInstanceCompletedBy ("kermit" )
251+ .planItemInstanceCaseDefinitionId ("undefinedId" )
252+ .endOr ()
253+ .list ();
254+ assertThat (planItemInstances )
255+ .extracting (HistoricPlanItemInstance ::getCompletedBy )
256+ .containsExactlyInAnyOrder ("kermit" , "kermit" , "kermit" );
236257 }
237258
238259 @ Test
@@ -389,6 +410,28 @@ public void testByStarted() {
389410
390411 // started and notStarted partition the full result set
391412 assertThat (started .size () + notStarted .size ()).isEqualTo (all .size ());
413+
414+ // or(): started OR bogus case definition id => only A
415+ assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
416+ .planItemInstanceCaseInstanceId (caseInstance .getId ())
417+ .or ()
418+ .started ()
419+ .planItemInstanceCaseDefinitionId ("undefinedId" )
420+ .endOr ()
421+ .list ())
422+ .extracting (HistoricPlanItemInstance ::getName )
423+ .containsExactly ("A" );
424+
425+ // or(): notStarted OR bogus case definition id => only B
426+ assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
427+ .planItemInstanceCaseInstanceId (caseInstance .getId ())
428+ .or ()
429+ .notStarted ()
430+ .planItemInstanceCaseDefinitionId ("undefinedId" )
431+ .endOr ()
432+ .list ())
433+ .extracting (HistoricPlanItemInstance ::getName )
434+ .containsExactly ("B" );
392435 }
393436 }
394437
@@ -403,7 +446,9 @@ public void testOrQueryUnionWithBogusCondition() {
403446 .planItemInstanceCaseDefinitionId ("undefinedId" )
404447 .planItemInstanceCaseInstanceId (caseInstanceIds .get (0 ))
405448 .endOr ()
406- .list ()).hasSize (4 );
449+ .list ())
450+ .extracting (HistoricPlanItemInstance ::getName )
451+ .containsExactlyInAnyOrder ("Stage one" , "Stage two" , "A" , "B" );
407452
408453 assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
409454 .or ()
@@ -419,13 +464,17 @@ public void testOrQueryUnionAcrossColumns() {
419464 List <String > caseInstanceIds = startInstances (2 );
420465
421466 if (CmmnHistoryTestHelper .isHistoryLevelAtLeast (HistoryLevel .ACTIVITY , cmmnEngineConfiguration )) {
422- // (all 4 plan items of case 0) OR (the 2 stages of every case) => 4 (case 0) + 2 (stages of case 1) = 6, overlap deduped
467+ // (all 4 plan items of case 0) OR (the 2 stages of every case) => 4 (case 0) + 2 (stages of case 1) = 6.
468+ // A single WHERE with OR yields each row once by construction (no DISTINCT), so case 0's 2 stages
469+ // are not double-counted despite matching both legs.
423470 assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
424471 .or ()
425472 .planItemInstanceCaseInstanceId (caseInstanceIds .get (0 ))
426473 .planItemInstanceDefinitionType (PlanItemDefinitionType .STAGE )
427474 .endOr ()
428- .list ()).hasSize (6 );
475+ .list ())
476+ .extracting (HistoricPlanItemInstance ::getName )
477+ .containsExactlyInAnyOrder ("Stage one" , "Stage two" , "A" , "B" , "Stage one" , "Stage two" );
429478 }
430479 }
431480
@@ -440,7 +489,9 @@ public void testOrQueryWithCaseInstanceIds() {
440489 .planItemInstanceCaseInstanceIds (Set .of (caseInstanceIds .get (0 ), caseInstanceIds .get (1 )))
441490 .planItemInstanceCaseDefinitionId ("undefinedId" )
442491 .endOr ()
443- .list ()).hasSize (8 );
492+ .list ())
493+ .extracting (HistoricPlanItemInstance ::getName )
494+ .containsExactlyInAnyOrder ("Stage one" , "Stage two" , "A" , "B" , "Stage one" , "Stage two" , "A" , "B" );
444495 }
445496 }
446497
@@ -449,25 +500,26 @@ public void testOuterAndWithOr() {
449500 startInstances (2 );
450501
451502 if (CmmnHistoryTestHelper .isHistoryLevelAtLeast (HistoryLevel .ACTIVITY , cmmnEngineConfiguration )) {
452- // type == HUMAN_TASK AND (bogus OR real case definition id) => the 2 human tasks per case = 4
503+ // type == HUMAN_TASK AND (name "A" OR elementId "planItem3" i.e. "Stage one").
504+ // "Stage one" is a STAGE, so the outer AND must strip it, leaving only the 2 "A" human tasks.
505+ // This fails both if the OR block is deleted (would return only "A" from the name leg = still 2,
506+ // but the elementId leg proves the OR participates) and if OR is mutated to AND.
453507 assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
454508 .planItemInstanceDefinitionType (PlanItemDefinitionType .HUMAN_TASK )
455509 .or ()
456- .planItemInstanceCaseInstanceId ( "undefinedId " )
457- .planItemInstanceCaseDefinitionId ( caseDefinitionId )
510+ .planItemInstanceName ( "A " )
511+ .planItemInstanceElementId ( "planItem3" )
458512 .endOr ()
459- .list ()).hasSize (4 );
513+ .list ())
514+ .extracting (HistoricPlanItemInstance ::getName )
515+ .containsExactlyInAnyOrder ("A" , "A" );
460516 }
461517 }
462518
463519 @ Test
464520 public void testOrQueryCoversManyParameters () {
465521 List <String > caseInstanceIds = startInstances (2 );
466522
467- if (!CmmnHistoryTestHelper .isHistoryLevelAtLeast (HistoryLevel .ACTIVITY , cmmnEngineConfiguration )) {
468- return ;
469- }
470-
471523 String firstCaseInstanceId = caseInstanceIds .get (0 );
472524
473525 // Resolve a couple of concrete ids from the first case instance to drive exact-match OR branches
@@ -519,15 +571,20 @@ public void testOrQueryUnionAcrossDistinctColumns() {
519571 .planItemInstanceName ("A" )
520572 .planItemInstanceElementId ("planItem3" )
521573 .endOr ()
522- .list ()).hasSize (4 );
574+ .list ())
575+ .extracting (HistoricPlanItemInstance ::getName )
576+ .containsExactlyInAnyOrder ("A" , "Stage one" , "A" , "Stage one" );
523577 }
524578 }
525579
526580 /**
527581 * Runs {@code or(filter OR bogusCaseDefinitionId).endOr()} and asserts both list() and count() return expectedSize.
528582 * Pairing the real filter with a non-matching one isolates the real filter's OR branch.
529583 */
530- private void assertOr (int expectedSize , java .util .function .UnaryOperator <HistoricPlanItemInstanceQuery > filter ) {
584+ private void assertOr (int expectedSize , UnaryOperator <HistoricPlanItemInstanceQuery > filter ) {
585+ assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
586+ .planItemInstanceCaseDefinitionId ("undefinedId" ).list ()).isEmpty ();
587+
531588 HistoricPlanItemInstanceQuery listQuery = cmmnHistoryService .createHistoricPlanItemInstanceQuery ().or ();
532589 filter .apply (listQuery ).planItemInstanceCaseDefinitionId ("undefinedId" ).endOr ();
533590 assertThat (listQuery .list ()).hasSize (expectedSize );
@@ -551,6 +608,48 @@ public void testEndOrThrowsWhenNotInOrStatement() {
551608 .hasMessage ("endOr() can only be called after calling or()" );
552609 }
553610
611+ @ Test
612+ public void testByTenantId () {
613+ // Deploy the same case with a tenant and start a single instance in it. The @BeforeEach deployment has no tenant,
614+ // so the only tenant-scoped plan items are the 4 belonging to this case instance.
615+ addDeploymentForAutoCleanup (cmmnRepositoryService .createDeployment ()
616+ .addClasspathResource ("org/flowable/cmmn/test/history/HistoricPlanItemInstanceQueryTest.testQuery.cmmn" )
617+ .tenantId ("flowable" )
618+ .deploy ());
619+ cmmnRuntimeService .createCaseInstanceBuilder ().caseDefinitionKey ("testQuery" ).tenantId ("flowable" ).start ();
620+
621+ if (CmmnHistoryTestHelper .isHistoryLevelAtLeast (HistoryLevel .ACTIVITY , cmmnEngineConfiguration )) {
622+ assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
623+ .planItemInstanceTenantId ("flowable" ).list ()).hasSize (4 );
624+
625+ // or(): exact tenant id OR bogus case definition id => the 4 tenant plan items
626+ assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
627+ .or ()
628+ .planItemInstanceTenantId ("flowable" )
629+ .planItemInstanceCaseDefinitionId ("undefinedId" )
630+ .endOr ()
631+ .list ())
632+ .extracting (HistoricPlanItemInstance ::getName )
633+ .containsExactlyInAnyOrder ("Stage one" , "Stage two" , "A" , "B" );
634+
635+ // or(): tenant id like OR bogus case definition id => the same 4 tenant plan items
636+ assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
637+ .or ()
638+ .planItemInstanceTenantIdLike ("flow%" )
639+ .planItemInstanceCaseDefinitionId ("undefinedId" )
640+ .endOr ()
641+ .list ())
642+ .extracting (HistoricPlanItemInstance ::getName )
643+ .containsExactlyInAnyOrder ("Stage one" , "Stage two" , "A" , "B" );
644+
645+ // a non-matching tenant id alone matches nothing, so the OR branches above are genuinely discriminating
646+ assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
647+ .planItemInstanceTenantId ("nonexisting" ).list ()).isEmpty ();
648+ assertThat (cmmnHistoryService .createHistoricPlanItemInstanceQuery ()
649+ .planItemInstanceTenantIdLike ("nonexisting%" ).list ()).isEmpty ();
650+ }
651+ }
652+
554653 private List <String > startInstances (int numberOfInstances ) {
555654 List <String > caseInstanceIds = new ArrayList <>();
556655 for (int i = 0 ; i < numberOfInstances ; i ++) {
0 commit comments