Throw exception if any resources in package have no url and no identifier.

This commit is contained in:
ianmarshall 2020-10-21 17:23:40 -04:00
parent 830ad70089
commit 115b09e4d4
3 changed files with 26 additions and 3 deletions

View File

@ -421,8 +421,12 @@ public class PackageInstallerSvcImpl implements IPackageInstallerSvc {
private TokenParam extractIdentifierFromOtherResourceTypes(IBaseResource resource) {
FhirTerser terser = myFhirContext.newTerser();
Identifier myIdentifier = (Identifier) terser.getSingleValueOrNull(resource, "identifier");
return new TokenParam(myIdentifier.getSystem(), myIdentifier.getValue());
Identifier identifier = (Identifier) terser.getSingleValueOrNull(resource, "identifier");
if (identifier != null) {
return new TokenParam(identifier.getSystem(), identifier.getValue());
} else {
throw new UnsupportedOperationException("Resources in a package must have a url or identifier to be loaded by the package installer.");
}
}
private boolean resourceHasUrlElement(IBaseResource resource) {

View File

@ -11,7 +11,6 @@ import ca.uhn.fhir.jpa.model.entity.NpmPackageEntity;
import ca.uhn.fhir.jpa.model.entity.NpmPackageVersionEntity;
import ca.uhn.fhir.jpa.model.entity.NpmPackageVersionResourceEntity;
import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
import ca.uhn.fhir.jpa.searchparam.registry.ISearchParamRegistry;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.ReferenceParam;
@ -62,6 +61,7 @@ import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
public class NpmTestR4 extends BaseJpaR4Test {
@ -272,6 +272,25 @@ public class NpmTestR4 extends BaseJpaR4Test {
}
@Test
public void testInstallR4Package_NoIdentifierNoUrl() throws Exception {
myDaoConfig.setAllowExternalReferences(true);
byte[] bytes = loadClasspathBytes("/packages/test-missing-identifier-package.tgz");
myFakeNpmServlet.myResponses.put("/test-organizations/1.0.0", bytes);
List<String> resourceList = new ArrayList<>();
resourceList.add("Organization");
PackageInstallationSpec spec = new PackageInstallationSpec().setName("test-organizations").setVersion("1.0.0").setInstallMode(PackageInstallationSpec.InstallModeEnum.STORE_AND_INSTALL);
spec.setInstallResourceTypes(resourceList);
try {
PackageInstallOutcomeJson outcome = igInstaller.install(spec);
fail();
} catch (ImplementationGuideInstallationException theE) {
assertThat(theE.getMessage(), containsString("Resources in a package must have a url or identifier to be loaded by the package installer."));
}
}
@Test
public void testInstallR4Package_DraftResourcesNotInstalled() throws Exception {
myDaoConfig.setAllowExternalReferences(true);