7070import org .mockito .InjectMocks ;
7171import org .mockito .Mock ;
7272import org .springframework .beans .factory .annotation .Autowired ;
73- import org .springframework .boot .test .mock .mockito .SpyBean ;
7473import org .springframework .mock .web .MockHttpServletRequest ;
74+ import org .springframework .test .context .bean .override .mockito .MockitoSpyBean ;
7575
7676import java .io .IOException ;
7777import java .nio .charset .StandardCharsets ;
112112public class MultitenantServerR4Test extends BaseMultitenantResourceProviderR4Test implements ITestDataBuilder {
113113 @ Captor
114114 private ArgumentCaptor <JpaPid > myMatchUrlCacheValueCaptor ;
115- @ SpyBean
115+ @ MockitoSpyBean
116116 private MemoryCacheService myMemoryCacheService ;
117117
118118 @ Autowired
@@ -183,6 +183,54 @@ public void testPartitioningDoesNotReturnDuplicatesOnPatientEverything(int theCo
183183 assertThat (foundIds ).hasSize (3 );
184184 }
185185
186+ /**
187+ * Tests Encounter/$everything operation with REQUEST_TENANT partitioning
188+ *
189+ * Steps:
190+ * 1. Create Patient in TENANT_A
191+ * 2. Create Encounter in TENANT_A referencing the Patient
192+ * 3. Call Encounter/$everything
193+ * 4. Validate response contains both Encounter and Patient
194+ */
195+ @ Test
196+ public void testEncounterEverything_withRequestTenantPartitioning_shouldReturnEncounterAndPatient () {
197+ // Arrange - Set tenant context to TENANT_A
198+ myTenantClientInterceptor .setTenantId (TENANT_A );
199+
200+ // Create Patient in TENANT_A
201+ IIdType patient = createPatient (withTenant (TENANT_A ), withActiveTrue ());
202+
203+ // Create Encounter in TENANT_A referencing the Patient
204+ IIdType encounter = createEncounter (withTenant (TENANT_A ), withSubject (patient .toUnqualifiedVersionless ().toString ()));
205+
206+ // Act - Call Encounter/$everything
207+ Bundle everythingBundle = myClient .operation ()
208+ .onInstance (encounter .toUnqualifiedVersionless ().toString ())
209+ .named ("$everything" )
210+ .withNoParameters (Parameters .class )
211+ .returnResourceType (Bundle .class )
212+ .execute ();
213+
214+ // Assert - Should return at least the Encounter and Patient resources
215+ assertThat (everythingBundle ).isNotNull ();
216+ assertThat (everythingBundle .getEntry ())
217+ .as ("Encounter/$everything should return at least Encounter and Patient resources" )
218+ .isNotEmpty ()
219+ .hasSizeGreaterThanOrEqualTo (2 );
220+
221+ // Verify Encounter is in the bundle
222+ boolean hasEncounter = everythingBundle .getEntry ().stream ()
223+ .anyMatch (entry -> entry .getResource () instanceof Encounter
224+ && entry .getResource ().getIdElement ().getIdPart ().equals (encounter .getIdPart ()));
225+ assertThat (hasEncounter ).as ("Bundle should contain the Encounter" ).isTrue ();
226+
227+ // Verify Patient is in the bundle (referenced by Encounter)
228+ boolean hasPatient = everythingBundle .getEntry ().stream ()
229+ .anyMatch (entry -> entry .getResource () instanceof Patient
230+ && entry .getResource ().getIdElement ().getIdPart ().equals (patient .getIdPart ()));
231+ assertThat (hasPatient ).as ("Bundle should contain the referenced Patient" ).isTrue ();
232+ }
233+
186234 @ Test
187235 public void testFetchCapabilityStatement () {
188236 myTenantClientInterceptor .setTenantId (TENANT_A );
@@ -202,7 +250,7 @@ public void testCreateAndRead_NamedTenant() {
202250
203251 runInTransaction (() -> {
204252 PartitionEntity partition = myPartitionDao .findForName (TENANT_A ).orElseThrow (IllegalStateException ::new );
205- ResourceTable resourceTable = myResourceTableDao .findById (idA .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
253+ ResourceTable resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idA .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
206254 assert resourceTable .getPartitionId ().getPartitionId () != null ;
207255 assertEquals (partition .getId (), resourceTable .getPartitionId ().getPartitionId ());
208256 });
@@ -240,7 +288,7 @@ public void testCreateAndRead_DefaultTenant() {
240288 createPatient (withTenant (TENANT_B ), withActiveFalse ());
241289
242290 runInTransaction (() -> {
243- ResourceTable resourceTable = myResourceTableDao .findById (idA .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
291+ ResourceTable resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idA .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
244292 assertNull (resourceTable .getPartitionId ().getPartitionId ());
245293 });
246294
@@ -460,7 +508,7 @@ public void testCreateAndRead_NonPartitionableResource_DefaultTenant() {
460508 IIdType idA = createResource ("NamingSystem" , withTenant (JpaConstants .DEFAULT_PARTITION_NAME ), withStatus ("draft" ));
461509
462510 runInTransaction (() -> {
463- ResourceTable resourceTable = myResourceTableDao .findById (idA .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
511+ ResourceTable resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idA .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
464512 assertNull (resourceTable .getPartitionId ().getPartitionId ());
465513 });
466514
@@ -514,10 +562,10 @@ public void testTransaction() {
514562 IdType idB = new IdType (response .getEntry ().get (1 ).getResponse ().getLocation ());
515563
516564 runInTransaction (() -> {
517- ResourceTable resourceTable = myResourceTableDao .findById (idA .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
565+ ResourceTable resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idA .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
518566 assert resourceTable .getPartitionId ().getPartitionId () != null ;
519567 assertEquals (1 , resourceTable .getPartitionId ().getPartitionId ());
520- resourceTable = myResourceTableDao .findById (idB .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
568+ resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idB .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
521569 assert resourceTable .getPartitionId ().getPartitionId () != null ;
522570 assertEquals (1 , resourceTable .getPartitionId ().getPartitionId ());
523571 });
@@ -702,9 +750,9 @@ public void testDirectDaoAccess_PartitionInRequestDetails_Create() {
702750 IIdType idB = myPatientDao .create ((Patient ) patientB , requestDetails ).getId ();
703751
704752 runInTransaction (() -> {
705- ResourceTable resourceTable = myResourceTableDao .findById (idA .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
753+ ResourceTable resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idA .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
706754 assertNull (resourceTable .getPartitionId ().getPartitionId ());
707- resourceTable = myResourceTableDao .findById (idB .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
755+ resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idB .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
708756 assert resourceTable .getPartitionId ().getPartitionId () != null ;
709757 assertEquals (2 , resourceTable .getPartitionId ().getPartitionId ());
710758 });
@@ -769,9 +817,9 @@ public void testDirectDaoAccess_PartitionInRequestDetails_Update() {
769817 myPatientDao .update ((Patient ) patientB , requestDetails );
770818
771819 runInTransaction (() -> {
772- ResourceTable resourceTable = myResourceTableDao .findById (idA .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
820+ ResourceTable resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idA .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
773821 assertNull (resourceTable .getPartitionId ().getPartitionId ());
774- resourceTable = myResourceTableDao .findById (idB .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
822+ resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idB .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
775823 assert resourceTable .getPartitionId ().getPartitionId () != null ;
776824 assertEquals (2 , resourceTable .getPartitionId ().getPartitionId ());
777825 });
@@ -833,10 +881,10 @@ public void testDirectDaoAccess_PartitionInRequestDetails_Transaction() {
833881 IdType idB = new IdType (response .getEntry ().get (1 ).getResponse ().getLocation ());
834882
835883 runInTransaction (() -> {
836- ResourceTable resourceTable = myResourceTableDao .findById (idA .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
884+ ResourceTable resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idA .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
837885 assert resourceTable .getPartitionId ().getPartitionId () != null ;
838886 assertEquals (1 , resourceTable .getPartitionId ().getPartitionId ());
839- resourceTable = myResourceTableDao .findById (idB .getIdPartAsLong ()).orElseThrow (IllegalStateException ::new );
887+ resourceTable = myResourceTableDao .findById (JpaPid . fromId ( idB .getIdPartAsLong () )).orElseThrow (IllegalStateException ::new );
840888 assert resourceTable .getPartitionId ().getPartitionId () != null ;
841889 assertEquals (1 , resourceTable .getPartitionId ().getPartitionId ());
842890 });
0 commit comments