Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug and add test for SP Validating Interceptor #3930

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix bug and add test for SP Validating Interceptor
  • Loading branch information
tadgh committed Aug 17, 2022
commit 6b402d07958ba2405c99221ea4039fb8e2c6c2c8
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);
tadgh marked this conversation as resolved.
Show resolved Hide resolved
}

@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));
tadgh marked this conversation as resolved.
Show resolved Hide resolved

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