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

File diff suppressed because it is too large Load Diff

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