Handle deletes properly in HashMapResourceProvider

This commit is contained in:
James Agnew 2018-02-25 14:20:04 -05:00
parent 84c72203b7
commit 458401864b
4 changed files with 94 additions and 4 deletions

View File

@ -36,6 +36,45 @@ import ca.uhn.fhir.rest.api.RequestTypeEnum;
*/
public class BundleUtil {
/**
* @return Returns <code>null</code> if the link isn't found or has no value
*/
public static String getLinkUrlOfType(FhirContext theContext, IBaseBundle theBundle, String theLinkRelation) {
RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle);
BaseRuntimeChildDefinition entryChild = def.getChildByName("link");
List<IBase> links = entryChild.getAccessor().getValues(theBundle);
for (IBase nextLink : links) {
boolean isRightRel = false;
BaseRuntimeElementCompositeDefinition relDef = (BaseRuntimeElementCompositeDefinition) theContext.getElementDefinition(nextLink.getClass());
BaseRuntimeChildDefinition relChild = relDef.getChildByName("relation");
List<IBase> relValues = relChild.getAccessor().getValues(nextLink);
for (IBase next : relValues) {
IPrimitiveType<?> nextValue = (IPrimitiveType<?>)next;
if (theLinkRelation.equals(nextValue.getValueAsString())) {
isRightRel = true;
}
}
if (!isRightRel) {
continue;
}
BaseRuntimeElementCompositeDefinition linkDef = (BaseRuntimeElementCompositeDefinition) theContext.getElementDefinition(nextLink.getClass());
BaseRuntimeChildDefinition urlChild = linkDef.getChildByName("url");
List<IBase> values = urlChild.getAccessor().getValues(nextLink);
for (IBase nextUrl : values) {
IPrimitiveType<?> nextValue = (IPrimitiveType<?>)nextUrl;
if (isNotBlank(nextValue.getValueAsString())) {
return nextValue.getValueAsString();
}
}
}
return null;
}
@SuppressWarnings("unchecked")
public static List<Pair<String, IBaseResource>> getBundleEntryUrlsAndResources(FhirContext theContext, IBaseBundle theBundle) {
RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle);
@ -50,7 +89,7 @@ public class BundleUtil {
BaseRuntimeChildDefinition urlChild = requestDef.getChildByName("url");
List<Pair<String, IBaseResource>> retVal = new ArrayList<Pair<String,IBaseResource>>(entries.size());
List<Pair<String, IBaseResource>> retVal = new ArrayList<>(entries.size());
for (IBase nextEntry : entries) {
String url = null;
@ -88,7 +127,7 @@ public class BundleUtil {
* Extract all of the resources from a given bundle
*/
public static List<BundleEntryParts> toListOfEntries(FhirContext theContext, IBaseBundle theBundle) {
List<BundleEntryParts> retVal = new ArrayList<BundleEntryParts>();
List<BundleEntryParts> retVal = new ArrayList<>();
RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle);
BaseRuntimeChildDefinition entryChild = def.getChildByName("entry");
@ -145,7 +184,7 @@ public class BundleUtil {
*/
@SuppressWarnings("unchecked")
public static <T extends IBaseResource> List<T> toListOfResourcesOfType(FhirContext theContext, IBaseBundle theBundle, Class<T> theTypeToInclude) {
List<T> retVal = new ArrayList<T>();
List<T> retVal = new ArrayList<>();
RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle);
BaseRuntimeChildDefinition entryChild = def.getChildByName("entry");
@ -170,7 +209,7 @@ public class BundleUtil {
private final RequestTypeEnum myRequestType;
private final IBaseResource myResource;
private final String myUrl;
public BundleEntryParts(RequestTypeEnum theRequestType, String theUrl, IBaseResource theResource) {
BundleEntryParts(RequestTypeEnum theRequestType, String theUrl, IBaseResource theResource) {
super();
myRequestType = theRequestType;
myUrl = theUrl;

View File

@ -138,6 +138,9 @@ public class HashMapResourceProvider<T extends IBaseResource> implements IResour
private IIdType store(@ResourceParam T theResource, String theIdPart, Long theVersionIdPart) {
IIdType id = myFhirContext.getVersion().newIdType();
id.setParts(null, myResourceName, theIdPart, Long.toString(theVersionIdPart));
if (theResource != null) {
theResource.setId(id);
}
TreeMap<Long, T> versionToResource = getVersionToResource(theIdPart);
versionToResource.put(theVersionIdPart, theResource);

View File

@ -52,6 +52,21 @@ public class HashMapResourceProviderTest {
assertEquals(true, p.getActive());
}
@Test
public void testCreateWithClientAssignedIdAndRead() {
// Create
Patient p = new Patient();
p.setId("ABC");
p.setActive(true);
IIdType id = ourClient.update().resource(p).execute().getId();
assertEquals("ABC", id.getIdPart());
assertEquals("1", id.getVersionIdPart());
// Read
p = (Patient) ourClient.read().resource("Patient").withId(id).execute();
assertEquals(true, p.getActive());
}
@Test
public void testDelete() {
// Create

View File

@ -0,0 +1,33 @@
package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.r4.model.Bundle;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Test;
public class BundleUtilTest {
private static FhirContext ourCtx = FhirContext.forR4();
@Test
public void testGetLink() {
Bundle b = new Bundle();
b.getLinkOrCreate("prev").setUrl("http://bar");
b.getLinkOrCreate("next").setUrl("http://foo");
Assert.assertEquals("http://foo", BundleUtil.getLinkUrlOfType(ourCtx, b, "next"));
}
@Test
public void testGetLinkDoesntExist() {
Bundle b = new Bundle();
b.getLinkOrCreate("prev").setUrl("http://bar");
Assert.assertEquals(null, BundleUtil.getLinkUrlOfType(ourCtx, b, "next"));
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}