Fix tests

This commit is contained in:
jamesagnew 2015-08-24 08:29:02 -04:00
parent 60339d6f93
commit bec43b3fdc
6 changed files with 113 additions and 39 deletions

View File

@ -107,9 +107,11 @@ public class Constants {
public static final String PARAM_PAGINGOFFSET = "_getpagesoffset";
public static final String PARAM_PRETTY = "_pretty";
public static final String PARAM_PRETTY_VALUE_TRUE = "true";
public static final String PARAM_PROFILE = "_profile";
public static final String PARAM_QUERY = "_query";
public static final String PARAM_REVINCLUDE = "_revinclude";
public static final String PARAM_SEARCH = "_search";
public static final String PARAM_SECURITY = "_security";
public static final String PARAM_SINCE = "_since";
public static final String PARAM_SORT = "_sort";
public static final String PARAM_SORT_ASC = "_sort:asc";
@ -138,10 +140,10 @@ public class Constants {
public static final int STATUS_HTTP_422_UNPROCESSABLE_ENTITY = 422;
public static final int STATUS_HTTP_500_INTERNAL_ERROR = 500;
public static final int STATUS_HTTP_501_NOT_IMPLEMENTED = 501;
public static final String TAG_SUBSETTED_CODE = "SUBSETTED";
public static final String TAG_SUBSETTED_SYSTEM = "http://hl7.org/fhir/v3/ObservationValue";
public static final String URL_TOKEN_HISTORY = "_history";
public static final String URL_TOKEN_METADATA = "metadata";
public static final String TAG_SUBSETTED_SYSTEM = "http://hl7.org/fhir/v3/ObservationValue";
public static final String TAG_SUBSETTED_CODE = "SUBSETTED";
static {
Map<String, EncodingEnum> valToEncoding = new HashMap<String, EncodingEnum>();

View File

@ -1042,13 +1042,12 @@ public abstract class BaseHapiFhirDao<T extends IBaseResource> implements IDao {
@SuppressWarnings("unchecked")
protected ResourceTable updateEntity(final IResource theResource, ResourceTable entity, boolean theUpdateHistory, Date theDeletedTimestampOrNull, boolean thePerformIndexing, boolean theUpdateVersion) {
validateResourceForStorage((T) theResource);
if (entity.getPublished() == null) {
entity.setPublished(new Date());
}
if (theResource != null) {
validateResourceForStorage((T) theResource);
String resourceType = myContext.getResourceDefinition(theResource).getName();
if (isNotBlank(entity.getResourceType()) && !entity.getResourceType().equals(resourceType)) {
throw new UnprocessableEntityException("Existing resource ID[" + entity.getIdDt().toUnqualifiedVersionless() + "] is of type[" + entity.getResourceType() + "] - Cannot update with [" + resourceType + "]");

View File

@ -118,6 +118,8 @@ import ca.uhn.fhir.rest.param.QuantityParam;
import ca.uhn.fhir.rest.param.ReferenceParam;
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.rest.param.TokenParam;
import ca.uhn.fhir.rest.param.UriParam;
import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.IBundleProvider;
import ca.uhn.fhir.rest.server.SimpleBundleProvider;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
@ -131,7 +133,7 @@ import ca.uhn.fhir.util.FhirTerser;
import ca.uhn.fhir.util.ObjectUtil;
@Transactional(propagation = Propagation.REQUIRED)
public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseHapiFhirDao<T> implements IFhirResourceDao<T> {
public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseHapiFhirDao<T>implements IFhirResourceDao<T> {
static final String OO_SEVERITY_WARN = "warning";
static final String OO_SEVERITY_INFO = "information";
@ -297,20 +299,38 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
return new HashSet<Long>(q.getResultList());
}
private Set<Long> addPredicateTag(Set<Long> thePids, List<List<? extends IQueryParameterType>> theList) {
private Set<Long> addPredicateTag(Set<Long> thePids, List<List<? extends IQueryParameterType>> theList, String theParamName) {
Set<Long> pids = thePids;
if (theList == null || theList.isEmpty()) {
return pids;
}
TagTypeEnum tagType;
if (Constants.PARAM_TAG.equals(theParamName)) {
tagType = TagTypeEnum.TAG;
} else if (Constants.PARAM_PROFILE.equals(theParamName)) {
tagType = TagTypeEnum.PROFILE;
} else if (Constants.PARAM_SECURITY.equals(theParamName)) {
tagType = TagTypeEnum.SECURITY_LABEL;
} else {
throw new IllegalArgumentException("Paramname: " + theParamName); // shouldn't happen
}
for (List<? extends IQueryParameterType> nextAndParams : theList) {
boolean haveTags = false;
for (IQueryParameterType nextParamUncasted : nextAndParams) {
TokenParam nextParam = (TokenParam) nextParamUncasted;
if (isNotBlank(nextParam.getValue())) {
haveTags = true;
} else if (isNotBlank(nextParam.getSystem())) {
throw new InvalidRequestException("Invalid _tag parameter (must supply a value/code and not just a system): " + nextParam.getValueAsQueryToken());
if (nextParamUncasted instanceof TokenParam) {
TokenParam nextParam = (TokenParam) nextParamUncasted;
if (isNotBlank(nextParam.getValue())) {
haveTags = true;
} else if (isNotBlank(nextParam.getSystem())) {
throw new InvalidRequestException("Invalid " + theParamName + " parameter (must supply a value/code and not just a system): " + nextParam.getValueAsQueryToken());
}
} else {
UriParam nextParam = (UriParam) nextParamUncasted;
if (isNotBlank(nextParam.getValue())) {
haveTags = true;
}
}
}
if (!haveTags) {
@ -327,18 +347,28 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
List<Predicate> orPredicates = new ArrayList<Predicate>();
for (IQueryParameterType nextOrParams : nextAndParams) {
TokenParam nextParam = (TokenParam) nextOrParams;
String code;
String system;
if (nextOrParams instanceof TokenParam) {
TokenParam nextParam = (TokenParam) nextOrParams;
code = nextParam.getValue();
system = nextParam.getSystem();
} else {
UriParam nextParam = (UriParam) nextOrParams;
code = nextParam.getValue();
system = null;
}
From<ResourceTag, TagDefinition> defJoin = from.join("myTag");
Predicate codePrediate = builder.equal(defJoin.get("myCode"), nextParam.getValue());
if (isBlank(nextParam.getValue())) {
Predicate typePrediate = builder.equal(defJoin.get("myTagType"), tagType);
Predicate codePrediate = builder.equal(defJoin.get("myCode"), code);
if (isBlank(code)) {
continue;
}
if (isNotBlank(nextParam.getSystem())) {
Predicate systemPrediate = builder.equal(defJoin.get("mySystem"), nextParam.getSystem());
orPredicates.add(builder.and(systemPrediate, codePrediate));
if (isNotBlank(system)) {
Predicate systemPrediate = builder.equal(defJoin.get("mySystem"), system);
orPredicates.add(builder.and(typePrediate, systemPrediate, codePrediate));
} else {
orPredicates.add(codePrediate);
orPredicates.add(builder.and(typePrediate, codePrediate));
}
}
@ -358,7 +388,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
TypedQuery<Long> q = myEntityManager.createQuery(cq);
pids = new HashSet<Long>(q.getResultList());
}
return pids;
}
@ -367,8 +397,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
return processMatchUrl(theMatchUrl, getResourceType());
}
private boolean addPredicateMissingFalseIfPresent(CriteriaBuilder theBuilder, String theParamName, Root<? extends BaseResourceIndexedSearchParam> from, List<Predicate> codePredicates,
IQueryParameterType nextOr) {
private boolean addPredicateMissingFalseIfPresent(CriteriaBuilder theBuilder, String theParamName, Root<? extends BaseResourceIndexedSearchParam> from, List<Predicate> codePredicates, IQueryParameterType nextOr) {
boolean missingFalse = false;
if (nextOr.getMissing() != null) {
if (nextOr.getMissing().booleanValue() == true) {
@ -1087,12 +1116,10 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
}
if (system != null && system.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
throw new InvalidRequestException(
"Parameter[" + theParamName + "] has system (" + system.length() + ") that is longer than maximum allowed (" + ResourceIndexedSearchParamToken.MAX_LENGTH + "): " + system);
throw new InvalidRequestException("Parameter[" + theParamName + "] has system (" + system.length() + ") that is longer than maximum allowed (" + ResourceIndexedSearchParamToken.MAX_LENGTH + "): " + system);
}
if (code != null && code.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
throw new InvalidRequestException(
"Parameter[" + theParamName + "] has code (" + code.length() + ") that is longer than maximum allowed (" + ResourceIndexedSearchParamToken.MAX_LENGTH + "): " + code);
throw new InvalidRequestException("Parameter[" + theParamName + "] has code (" + code.length() + ") that is longer than maximum allowed (" + ResourceIndexedSearchParamToken.MAX_LENGTH + "): " + code);
}
ArrayList<Predicate> singleCodePredicates = (new ArrayList<Predicate>());
@ -1252,7 +1279,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
return toMethodOutcome(entity, theResource).setCreated(false);
}
}
if (isNotBlank(theResource.getId().getIdPart())) {
if (isValidPid(theResource.getId())) {
throw new UnprocessableEntityException("This server cannot create an entity with a user-specified numeric ID - Client should not specify an ID when creating a new resource, or should include at least one letter in the ID to force a client-defined ID");
@ -1290,7 +1317,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
@Override
public TagList getAllResourceTags() {
// Notify interceptors
ActionRequestDetails requestDetails = new ActionRequestDetails(null,null);
ActionRequestDetails requestDetails = new ActionRequestDetails(null, null);
notifyInterceptors(RestOperationTypeEnum.GET_TAGS, requestDetails);
StopWatch w = new StopWatch();
@ -1305,7 +1332,6 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
return myResourceType;
}
public String getResourceName() {
return myResourceName;
}
@ -1726,8 +1752,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
if (entity == null) {
if (theId.hasVersionIdPart()) {
TypedQuery<ResourceHistoryTable> q = myEntityManager
.createQuery("SELECT t from ResourceHistoryTable t WHERE t.myResourceId = :RID AND t.myResourceType = :RTYP AND t.myResourceVersion = :RVER", ResourceHistoryTable.class);
TypedQuery<ResourceHistoryTable> q = myEntityManager.createQuery("SELECT t from ResourceHistoryTable t WHERE t.myResourceId = :RID AND t.myResourceType = :RTYP AND t.myResourceVersion = :RVER", ResourceHistoryTable.class);
q.setParameter("RID", pid);
q.setParameter("RTYP", myResourceName);
q.setParameter("RVER", Long.parseLong(theId.getVersionIdPart()));
@ -1919,7 +1944,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
// Execute the query and make sure we return distinct results
List<IBaseResource> retVal = new ArrayList<IBaseResource>();
loadResourcesByPid(pidsSubList, retVal, revIncludedPids);
/*
* Load _include resources - Note that _revincludes are handled differently than _include ones, as
* they are counted towards the total count and paged, so they are loaded outside the bundle provider
@ -2074,9 +2099,9 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
pids = addPredicateLanguage(pids, nextParamEntry.getValue());
} else if (nextParamName.equals("_tag")) {
} else if (nextParamName.equals(Constants.PARAM_TAG) || nextParamName.equals(Constants.PARAM_PROFILE) || nextParamName.equals(Constants.PARAM_SECURITY)) {
pids = addPredicateTag(pids, nextParamEntry.getValue());
pids = addPredicateTag(pids, nextParamEntry.getValue(), nextParamName);
} else {
@ -2285,7 +2310,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
if (resourceId.hasResourceType() && !resourceId.getResourceType().equals(getResourceName())) {
throw new UnprocessableEntityException("Invalid resource ID[" + entity.getIdDt().toUnqualifiedVersionless() + "] of type[" + entity.getResourceType() + "] - Does not match expected [" + getResourceName() + "]");
}
// Notify interceptors
ActionRequestDetails requestDetails = new ActionRequestDetails(resourceId, getResourceName(), theResource);
notifyInterceptors(RestOperationTypeEnum.UPDATE, requestDetails);
@ -2318,8 +2343,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IResource> extends BaseH
private void validateResourceType(BaseHasResource entity) {
if (!myResourceName.equals(entity.getResourceType())) {
throw new ResourceNotFoundException(
"Resource with ID " + entity.getIdDt().getIdPart() + " exists but it is not of type " + myResourceName + ", found resource of type " + entity.getResourceType());
throw new ResourceNotFoundException("Resource with ID " + entity.getIdDt().getIdPart() + " exists but it is not of type " + myResourceName + ", found resource of type " + entity.getResourceType());
}
}

View File

@ -84,6 +84,7 @@ import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.param.TokenOrListParam;
import ca.uhn.fhir.rest.param.TokenParam;
import ca.uhn.fhir.rest.param.UriParam;
import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.IBundleProvider;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
@ -2109,6 +2110,43 @@ public class FhirResourceDaoDstu2Test extends BaseJpaTest {
}
@Test
public void testSearchWithSecurityAndProfileParams() {
String methodName = "testSearchWithSecurityAndProfileParams";
IIdType tag1id;
{
Organization org = new Organization();
org.getNameElement().setValue("FOO");
List<BaseCodingDt> security = new ArrayList<BaseCodingDt>();
security.add(new CodingDt("urn:taglist", methodName + "1a"));
ResourceMetadataKeyEnum.SECURITY_LABELS.put(org, security);
tag1id = ourOrganizationDao.create(org).getId().toUnqualifiedVersionless();
}
IIdType tag2id;
{
Organization org = new Organization();
org.getNameElement().setValue("FOO");
List<IdDt> security = new ArrayList<IdDt>();
security.add(new IdDt("http://" + methodName));
ResourceMetadataKeyEnum.PROFILES.put(org, security);
tag2id = ourOrganizationDao.create(org).getId().toUnqualifiedVersionless();
}
{
SearchParameterMap params = new SearchParameterMap();
params.add("_security", new TokenParam("urn:taglist", methodName + "1a"));
List<IIdType> patients = toUnqualifiedVersionlessIds(ourOrganizationDao.search(params));
assertThat(patients, containsInAnyOrder(tag1id));
}
{
SearchParameterMap params = new SearchParameterMap();
params.add("_profile", new UriParam("http://" + methodName));
List<IIdType> patients = toUnqualifiedVersionlessIds(ourOrganizationDao.search(params));
assertThat(patients, containsInAnyOrder(tag2id));
}
}
@Test
public void testSearchWithIncludes() {
IIdType parentOrgId;

View File

@ -45,6 +45,7 @@ public class FhirInstanceValidatorIntegrationTest {
Observation input = new Observation();
// Has a value, but not a status (which is required)
input.getCode().addCoding().setSystem("http://loinc.org").setCode("12345");
input.setValue(new StringType("AAA"));
ValidationResult output = myVal.validateWithResult(input);

View File

@ -51,9 +51,17 @@ public class ${className}ResourceProvider extends
StringParam theResourceLanguage,
@Description(shortDefinition="Search for resources which have the given tag")
@OptionalParam(name="_tag")
@OptionalParam(name=ca.uhn.fhir.rest.server.Constants.PARAM_TAG)
TokenAndListParam theSearchForTag,
@Description(shortDefinition="Search for resources which have the given security labels")
@OptionalParam(name=ca.uhn.fhir.rest.server.Constants.PARAM_SECURITY)
TokenAndListParam theSearchForSecurity,
@Description(shortDefinition="Search for resources which have the given profile")
@OptionalParam(name=ca.uhn.fhir.rest.server.Constants.PARAM_PROFILE)
UriAndListParam theSearchForProfile,
#foreach ( $param in $searchParams ) #{if}(true) #{end}
@Description(shortDefinition="${param.description}")
@ -120,7 +128,9 @@ public class ${className}ResourceProvider extends
SearchParameterMap paramMap = new SearchParameterMap();
paramMap.add("_id", theId);
paramMap.add("_language", theResourceLanguage);
paramMap.add("_tag", theSearchForTag);
paramMap.add(ca.uhn.fhir.rest.server.Constants.PARAM_TAG, theSearchForTag);
paramMap.add(ca.uhn.fhir.rest.server.Constants.PARAM_SECURITY, theSearchForSecurity);
paramMap.add(ca.uhn.fhir.rest.server.Constants.PARAM_PROFILE, theSearchForProfile);
#foreach ( $param in $searchParams )
paramMap.add("${param.name}", the${param.nameCapitalized});
#end