Skip to content

Commit

Permalink
Fix bug and add test for SP Validating Interceptor (#3930)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadgh committed Aug 17, 2022
1 parent 8732469 commit 28b09a7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,30 @@ public void testSearchParamValidatingInterceptorAllowsResourceUpdate(){
.execute();

assertNotNull(methodOutcome);
}
@Test
public void testSearchParamValidationOnUpdateWithClientAssignedId() {
registerSearchParameterValidatingInterceptor();

SearchParameter searchParameter = createSearchParameter();
searchParameter.setId("my-custom-id");

// now, create a SearchParameter
MethodOutcome methodOutcome = myClient
.update()
.resource(searchParameter)
.execute();

assertTrue(methodOutcome.getCreated());
SearchParameter createdSearchParameter = (SearchParameter) methodOutcome.getResource();

createdSearchParameter.setUrl("newUrl");
methodOutcome = myClient
.update()
.resource(createdSearchParameter)
.execute();

assertNotNull(methodOutcome);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
import ca.uhn.fhir.jpa.api.svc.IIdHelperService;
import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
import ca.uhn.fhir.jpa.searchparam.registry.SearchParameterCanonicalizer;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId;
import ca.uhn.fhir.rest.param.TokenAndListParam;
Expand All @@ -39,7 +41,10 @@
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
Expand All @@ -55,6 +60,8 @@ public class SearchParamValidatingInterceptor {

private DaoRegistry myDaoRegistry;

private IIdHelperService myIdHelperService;

@Hook(Pointcut.STORAGE_PRESTORAGE_RESOURCE_CREATED)
public void resourcePreCreate(IBaseResource theResource, RequestDetails theRequestDetails) {
validateSearchParamOnCreate(theResource, theRequestDetails);
Expand Down Expand Up @@ -90,15 +97,15 @@ public void validateSearchParamOnUpdate(IBaseResource theResource, RequestDetail

SearchParameterMap searchParameterMap = extractSearchParameterMap(runtimeSearchParam);

List<ResourcePersistentId> persistedIdList = getDao().searchForIds(searchParameterMap, theRequestDetails);
List<ResourcePersistentId> pidList = getDao().searchForIds(searchParameterMap, theRequestDetails);

if(isNotEmpty(persistedIdList)){
String resourceId = runtimeSearchParam.getId().getIdPart();
if(isNotEmpty(pidList)){
Set<String> resolvedResourceIds = myIdHelperService.translatePidsToFhirResourceIds(new HashSet<>(pidList));
String incomingResourceId = runtimeSearchParam.getId().getIdPart();

boolean isNewSearchParam = persistedIdList
boolean isNewSearchParam = resolvedResourceIds
.stream()
.map(theResourcePersistentId -> theResourcePersistentId.getId().toString())
.noneMatch(anId -> anId.equals(resourceId));
.noneMatch(resId -> resId.equals(incomingResourceId));

if(isNewSearchParam){
throw new UnprocessableEntityException(Msg.code(2125) + "Can't process submitted SearchParameter as it is overlapping an existing one.");
Expand Down Expand Up @@ -140,6 +147,11 @@ public void setDaoRegistry(DaoRegistry theDaoRegistry) {
myDaoRegistry = theDaoRegistry;
}

@Autowired
public void setIIDHelperService(IIdHelperService theIdHelperService) {
myIdHelperService = theIdHelperService;
}

private IFhirResourceDao getDao() {
return myDaoRegistry.getResourceDao(SEARCH_PARAM);
}
Expand Down

0 comments on commit 28b09a7

Please sign in to comment.