This commit is contained in:
James Agnew
2025-02-12 13:58:42 -05:00
parent ac5dbb975e
commit 3cdd23368a
2 changed files with 702 additions and 705 deletions

View File

@@ -7,7 +7,11 @@ import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.client.interceptor.UrlTenantSelectionInterceptor;
import ca.uhn.fhir.rest.server.provider.ProviderConstants;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.CodeType;
import org.hl7.fhir.r4.model.IntegerType;
import org.hl7.fhir.r4.model.Parameters;
import org.hl7.fhir.r4.model.Patient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -19,89 +23,86 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {Application.class}, properties =
{
"spring.datasource.url=jdbc:h2:mem:dbr4-mt",
"hapi.fhir.fhir_version=r4",
"hapi.fhir.subscription.websocket_enabled=true",
"hapi.fhir.cr_enabled=false",
"hapi.fhir.partitioning.partitioning_include_in_search_hashes=false",
})
{
"spring.datasource.url=jdbc:h2:mem:dbr4-mt",
"hapi.fhir.fhir_version=r4",
"hapi.fhir.subscription.websocket_enabled=true",
"hapi.fhir.cr_enabled=false",
"hapi.fhir.partitioning.partitioning_include_in_search_hashes=false",
"hapi.fhir.partitioning.request_tenant_partitioning_mode=true",
})
class MultitenantServerR4IT {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerDstu2IT.class);
private IGenericClient ourClient;
private FhirContext ourCtx;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerDstu2IT.class);
private static UrlTenantSelectionInterceptor ourClientTenantInterceptor;
private IGenericClient ourClient;
private FhirContext ourCtx;
@LocalServerPort
private int port;
@LocalServerPort
private int port;
private static UrlTenantSelectionInterceptor ourClientTenantInterceptor;
@Test
void testCreateAndReadInTenantA() {
@Test
void testCreateAndReadInTenantA() {
// Create tenant A
ourClientTenantInterceptor.setTenantId("DEFAULT");
ourClient
.operation()
.onServer()
.named(ProviderConstants.PARTITION_MANAGEMENT_CREATE_PARTITION)
.withParameter(Parameters.class, ProviderConstants.PARTITION_MANAGEMENT_PARTITION_ID, new IntegerType(1))
.andParameter(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_NAME, new CodeType("TENANT-A"))
.execute();
// Create tenant A
ourClientTenantInterceptor.setTenantId("DEFAULT");
ourClient
.operation()
.onServer()
.named(ProviderConstants.PARTITION_MANAGEMENT_CREATE_PARTITION)
.withParameter(Parameters.class, ProviderConstants.PARTITION_MANAGEMENT_PARTITION_ID, new IntegerType(1))
.andParameter(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_NAME, new CodeType("TENANT-A"))
.execute();
ourClientTenantInterceptor.setTenantId("TENANT-A");
Patient pt = new Patient();
pt.addName().setFamily("Family A");
ourClient.create().resource(pt).execute().getId();
Bundle searchResult = ourClient.search().forResource(Patient.class).returnBundle(Bundle.class).cacheControl(new CacheControlDirective().setNoCache(true)).execute();
assertEquals(1, searchResult.getEntry().size());
Patient pt2 = (Patient) searchResult.getEntry().get(0).getResource();
assertEquals("Family A", pt2.getName().get(0).getFamily());
}
@Test
void testCreateAndReadInTenantB() {
ourClientTenantInterceptor.setTenantId("TENANT-A");
Patient pt = new Patient();
pt.addName().setFamily("Family A");
ourClient.create().resource(pt).execute().getId();
Bundle searchResult = ourClient.search().forResource(Patient.class).returnBundle(Bundle.class).cacheControl(new CacheControlDirective().setNoCache(true)).execute();
assertEquals(1, searchResult.getEntry().size());
Patient pt2 = (Patient) searchResult.getEntry().get(0).getResource();
assertEquals("Family A", pt2.getName().get(0).getFamily());
}
@Test
void testCreateAndReadInTenantB() {
// Create tenant A
ourClientTenantInterceptor.setTenantId("DEFAULT");
ourClient
.operation()
.onServer()
.named(ProviderConstants.PARTITION_MANAGEMENT_CREATE_PARTITION)
.withParameter(Parameters.class, ProviderConstants.PARTITION_MANAGEMENT_PARTITION_ID, new IntegerType(2))
.andParameter(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_NAME, new CodeType("TENANT-B"))
.execute();
// Create tenant A
ourClientTenantInterceptor.setTenantId("DEFAULT");
ourClient
.operation()
.onServer()
.named(ProviderConstants.PARTITION_MANAGEMENT_CREATE_PARTITION)
.withParameter(Parameters.class, ProviderConstants.PARTITION_MANAGEMENT_PARTITION_ID, new IntegerType(2))
.andParameter(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_NAME, new CodeType("TENANT-B"))
.execute();
ourClientTenantInterceptor.setTenantId("TENANT-B");
Patient pt = new Patient();
pt.addName().setFamily("Family B");
ourClient.create().resource(pt).execute().getId();
Bundle searchResult = ourClient.search().forResource(Patient.class).returnBundle(Bundle.class).cacheControl(new CacheControlDirective().setNoCache(true)).execute();
assertEquals(1, searchResult.getEntry().size());
Patient pt2 = (Patient) searchResult.getEntry().get(0).getResource();
assertEquals("Family B", pt2.getName().get(0).getFamily());
}
ourClientTenantInterceptor.setTenantId("TENANT-B");
Patient pt = new Patient();
pt.addName().setFamily("Family B");
ourClient.create().resource(pt).execute().getId();
@BeforeEach
void beforeEach() {
Bundle searchResult = ourClient.search().forResource(Patient.class).returnBundle(Bundle.class).cacheControl(new CacheControlDirective().setNoCache(true)).execute();
assertEquals(1, searchResult.getEntry().size());
Patient pt2 = (Patient) searchResult.getEntry().get(0).getResource();
assertEquals("Family B", pt2.getName().get(0).getFamily());
}
@BeforeEach
void beforeEach() {
ourClientTenantInterceptor = new UrlTenantSelectionInterceptor();
ourCtx = FhirContext.forR4();
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
String ourServerBase = "http://localhost:" + port + "/fhir/";
ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true));
ourClient.registerInterceptor(ourClientTenantInterceptor);
}
ourClientTenantInterceptor = new UrlTenantSelectionInterceptor();
ourCtx = FhirContext.forR4();
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
String ourServerBase = "http://localhost:" + port + "/fhir/";
ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true));
ourClient.registerInterceptor(ourClientTenantInterceptor);
}
}