Skip to content

Commit

Permalink
Fix web testing UI regression
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Sep 6, 2016
1 parent 6b9f8ec commit 202a1ea
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
*/

public enum RequestTypeEnum {
CONNECT, DELETE, GET, OPTIONS, PATCH, POST, PUT, TRACE, TRACK
CONNECT, DELETE, GET, OPTIONS, PATCH, POST, PUT, TRACE, TRACK, HEAD
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public BanUnsupportedHttpMethodsInterceptor() {
myAllowedMethods.add(RequestTypeEnum.DELETE);
myAllowedMethods.add(RequestTypeEnum.PUT);
myAllowedMethods.add(RequestTypeEnum.POST);
myAllowedMethods.add(RequestTypeEnum.HEAD);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ public abstract class BaseHapiFhirDao<T extends IBaseResource> implements IDao {

private FhirContext myContext;

// @PersistenceContext(name = "FHIR_UT", type = PersistenceContextType.TRANSACTION, unitName = "FHIR_UT")
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
protected EntityManager myEntityManager;

Expand Down Expand Up @@ -669,7 +668,7 @@ protected void notifyInterceptors(RestOperationTypeEnum theOperationType, Action
}
}

private String parseContentTextIntoWords(IBaseResource theResource) {
public String parseContentTextIntoWords(IBaseResource theResource) {
StringBuilder retVal = new StringBuilder();
@SuppressWarnings("rawtypes")
List<IPrimitiveType> childElements = getContext().newTerser().getAllPopulatedChildElementsOfType(theResource, IPrimitiveType.class);
Expand All @@ -686,6 +685,17 @@ private String parseContentTextIntoWords(IBaseResource theResource) {
return retVal.toString();
}

@Override
public void populateFullTextFields(final IBaseResource theResource, ResourceTable theEntity) {
if (theEntity.getDeleted() != null) {
theEntity.setNarrativeTextParsedIntoWords(null);
theEntity.setContentTextParsedIntoWords(null);
} else {
theEntity.setNarrativeTextParsedIntoWords(parseNarrativeTextIntoWords(theResource));
theEntity.setContentTextParsedIntoWords(parseContentTextIntoWords(theResource));
}
}

private void populateResourceId(final IBaseResource theResource, BaseHasResource theEntity) {
IIdType id = theEntity.getIdDt();
if (getContext().getVersion().getVersion().isRi()) {
Expand Down Expand Up @@ -931,10 +941,6 @@ public BaseHasResource readEntity(IIdType theValueId) {
throw new NotImplementedException("");
}

public void setConfig(DaoConfig theConfig) {
myConfig = theConfig;
}

// protected MetaDt toMetaDt(Collection<TagDefinition> tagDefinitions) {
// MetaDt retVal = new MetaDt();
// for (TagDefinition next : tagDefinitions) {
Expand All @@ -953,6 +959,10 @@ public void setConfig(DaoConfig theConfig) {
// return retVal;
// }

public void setConfig(DaoConfig theConfig) {
myConfig = theConfig;
}

@Autowired
public void setContext(FhirContext theContext) {
myContext = theContext;
Expand Down Expand Up @@ -1273,8 +1283,7 @@ protected ResourceTable updateEntity(final IBaseResource theResource, ResourceTa
theEntity.setResourceLinks(links);
theEntity.setHasLinks(links.isEmpty() == false);
theEntity.setIndexStatus(INDEX_STATUS_INDEXED);
theEntity.setNarrativeTextParsedIntoWords(parseNarrativeTextIntoWords(theResource));
theEntity.setContentTextParsedIntoWords(parseContentTextIntoWords(theResource));
populateFullTextFields(theResource, theEntity);

} else {

Expand Down Expand Up @@ -1397,53 +1406,6 @@ protected ResourceTable updateEntity(IBaseResource theResource, ResourceTable en
return updateEntity(theResource, entity, theDeletedTimestampOrNull, true, true, theUpdateTime);
}

protected void validateDeleteConflictsEmptyOrThrowException(List<DeleteConflict> theDeleteConflicts) {
if (theDeleteConflicts.isEmpty()) {
return;
}

IBaseOperationOutcome oo = OperationOutcomeUtil.newInstance(getContext());
for (DeleteConflict next : theDeleteConflicts) {
String msg = "Unable to delete " + next.getTargetId().toUnqualifiedVersionless().getValue()
+ " because at least one resource has a reference to this resource. First reference found was resource " + next.getTargetId().toUnqualifiedVersionless().getValue() + " in path "
+ next.getSourcePath();
OperationOutcomeUtil.addIssue(getContext(), oo, OO_SEVERITY_ERROR, msg, null, "processing");
}

throw new ResourceVersionConflictException("Delete failed because of constraint failure", oo);
}

/**
* This method is invoked immediately before storing a new resource, or an update to an existing resource to allow the DAO to ensure that it is valid for persistence. By default, checks for the
* "subsetted" tag and rejects resources which have it. Subclasses should call the superclass implementation to preserve this check.
*
* @param theResource
* The resource that is about to be persisted
* @param theEntityToSave
* TODO
*/
protected void validateResourceForStorage(T theResource, ResourceTable theEntityToSave) {
Object tag = null;
if (theResource instanceof IResource) {
IResource res = (IResource) theResource;
TagList tagList = ResourceMetadataKeyEnum.TAG_LIST.get(res);
if (tagList != null) {
tag = tagList.getTag(Constants.TAG_SUBSETTED_SYSTEM, Constants.TAG_SUBSETTED_CODE);
}
} else {
IAnyResource res = (IAnyResource) theResource;
tag = res.getMeta().getTag(Constants.TAG_SUBSETTED_SYSTEM, Constants.TAG_SUBSETTED_CODE);
}

if (tag != null) {
throw new UnprocessableEntityException("Resource contains the 'subsetted' tag, and must not be stored as it may contain a subset of available data");
}

String resName = getContext().getResourceDefinition(theResource).getName();
validateChildReferences(theResource, resName);

}

private void validateChildReferences(IBase theElement, String thePath) {
if (theElement == null) {
return;
Expand Down Expand Up @@ -1499,6 +1461,53 @@ private void validateChildReferences(IBase theElement, String thePath) {
}
}

protected void validateDeleteConflictsEmptyOrThrowException(List<DeleteConflict> theDeleteConflicts) {
if (theDeleteConflicts.isEmpty()) {
return;
}

IBaseOperationOutcome oo = OperationOutcomeUtil.newInstance(getContext());
for (DeleteConflict next : theDeleteConflicts) {
String msg = "Unable to delete " + next.getTargetId().toUnqualifiedVersionless().getValue()
+ " because at least one resource has a reference to this resource. First reference found was resource " + next.getTargetId().toUnqualifiedVersionless().getValue() + " in path "
+ next.getSourcePath();
OperationOutcomeUtil.addIssue(getContext(), oo, OO_SEVERITY_ERROR, msg, null, "processing");
}

throw new ResourceVersionConflictException("Delete failed because of constraint failure", oo);
}

/**
* This method is invoked immediately before storing a new resource, or an update to an existing resource to allow the DAO to ensure that it is valid for persistence. By default, checks for the
* "subsetted" tag and rejects resources which have it. Subclasses should call the superclass implementation to preserve this check.
*
* @param theResource
* The resource that is about to be persisted
* @param theEntityToSave
* TODO
*/
protected void validateResourceForStorage(T theResource, ResourceTable theEntityToSave) {
Object tag = null;
if (theResource instanceof IResource) {
IResource res = (IResource) theResource;
TagList tagList = ResourceMetadataKeyEnum.TAG_LIST.get(res);
if (tagList != null) {
tag = tagList.getTag(Constants.TAG_SUBSETTED_SYSTEM, Constants.TAG_SUBSETTED_CODE);
}
} else {
IAnyResource res = (IAnyResource) theResource;
tag = res.getMeta().getTag(Constants.TAG_SUBSETTED_SYSTEM, Constants.TAG_SUBSETTED_CODE);
}

if (tag != null) {
throw new UnprocessableEntityException("Resource contains the 'subsetted' tag, and must not be stored as it may contain a subset of available data");
}

String resName = getContext().getResourceDefinition(theResource).getName();
validateChildReferences(theResource, resName);

}

protected static boolean isValidPid(IIdType theId) {
if (theId == null || theId.getIdPart() == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.entity.BaseHasResource;
import ca.uhn.fhir.jpa.entity.ResourceTable;
import ca.uhn.fhir.jpa.search.PersistedJpaBundleProvider;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum.ResourceMetadataKeySupportingAnyResource;
Expand Down Expand Up @@ -70,4 +71,6 @@ public void put(IResource theResource, Long theObject) {
IBaseResource toResource(BaseHasResource theEntity, boolean theForHistoryOperation);

<R extends IBaseResource> R toResource(Class<R> theResourceType, BaseHasResource theEntity, boolean theForHistoryOperation);

void populateFullTextFields(IBaseResource theResource, ResourceTable theEntity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T
search = myValueSetDao.search(ValueSet.SP_URL, new UriParam(theUri));
}
} else if ("StructureDefinition".equals(resourceName)) {
if (theUri.startsWith("http://hl7.org/fhir/StructureDefinition/")) {
return null;
}
search = myStructureDefinitionDao.search(StructureDefinition.SP_URL, new UriParam(theUri));
} else if ("Questionnaire".equals(resourceName)) {
search = myQuestionnaireDao.search(IAnyResource.SP_RES_ID, new StringParam(id.getIdPart()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
@Index(name = "IDX_RES_DATE", columnList="RES_UPDATED"),
@Index(name = "IDX_RES_LANG", columnList="RES_TYPE,RES_LANGUAGE"),
@Index(name = "IDX_RES_PROFILE", columnList="RES_PROFILE"),
@Index(name = "IDX_INDEXSTATUS", columnList="SP_INDEX_STATUS")
@Index(name = "IDX_INDEXSTATUS", columnList="SP_INDEX_STATUS")
})
@AnalyzerDefs({
@AnalyzerDef(name = "autocompleteEdgeAnalyzer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ public abstract class BaseJpaDstu3Test extends BaseJpaTest {
@Autowired
@Qualifier("myValueSetDaoDstu3")
protected IFhirResourceDaoValueSet<ValueSet, Coding, CodeableConcept> myValueSetDao;
@Autowired
protected PlatformTransactionManager myTransactionMgr;
@After()
public void afterGrabCaches() {
ourValueSetDao = myValueSetDao;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.junit.AfterClass;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import ca.uhn.fhir.jpa.dao.BaseHapiFhirDao;
import ca.uhn.fhir.jpa.dao.SearchParameterMap;
Expand All @@ -49,6 +52,36 @@
public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirResourceDaoDstu3SearchNoFtTest.class);

@Test
public void testSearchWithRevIncludes() {
final String methodName = "testSearchWithRevIncludes";
TransactionTemplate txTemplate = new TransactionTemplate(myTransactionMgr);
txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
IIdType pid = txTemplate.execute(new TransactionCallback<IIdType>() {

@Override
public IIdType doInTransaction(TransactionStatus theStatus) {
org.hl7.fhir.dstu3.model.Patient p = new org.hl7.fhir.dstu3.model.Patient();
p.addName().addFamily(methodName);
IIdType pid = myPatientDao.create(p).getId().toUnqualifiedVersionless();

org.hl7.fhir.dstu3.model.Condition c = new org.hl7.fhir.dstu3.model.Condition();
c.getSubject().setReferenceElement(pid);
myConditionDao.create(c);

return pid;
}
});

SearchParameterMap map = new SearchParameterMap();
map.add(Patient.SP_RES_ID, new StringParam(pid.getIdPart()));
map.addRevInclude(Condition.INCLUDE_PATIENT);
IBundleProvider results = myPatientDao.search(map);
List<IBaseResource> foundResources = results.getResources(0, results.size());
assertEquals(Patient.class, foundResources.get(0).getClass());
assertEquals(Condition.class, foundResources.get(1).getClass());
}

@Test
public void testCodeSearch() {
Subscription subs = new Subscription();
Expand Down Expand Up @@ -276,7 +309,7 @@ public void testSearchAll() {

@Test
public void testHasParameter() {
IIdType pid0, pid1;
IIdType pid0;
{
Patient patient = new Patient();
patient.addIdentifier().setSystem("urn:system").setValue("001");
Expand All @@ -287,7 +320,7 @@ public void testHasParameter() {
Patient patient = new Patient();
patient.addIdentifier().setSystem("urn:system").setValue("001");
patient.addName().addFamily("Tester").addGiven("Joe");
pid1 = myPatientDao.create(patient, mySrd).getId().toUnqualifiedVersionless();
myPatientDao.create(patient, mySrd).getId().toUnqualifiedVersionless();
}
{
Observation obs = new Observation();
Expand Down Expand Up @@ -907,6 +940,7 @@ public void testSearchLastUpdatedParam() throws InterruptedException {
}
}

@SuppressWarnings("deprecation")
@Test
public void testSearchLastUpdatedParamWithComparator() throws InterruptedException {
IIdType id0;
Expand Down Expand Up @@ -2339,7 +2373,7 @@ public void testSearchWithUriParam() throws Exception {

ValueSet vs2 = new ValueSet();
vs2.setUrl("http://hl7.org/foo/bar");
IIdType id2 = myValueSetDao.create(vs2, mySrd).getId().toUnqualifiedVersionless();
myValueSetDao.create(vs2, mySrd).getId().toUnqualifiedVersionless();

IBundleProvider result;
result = myValueSetDao.search(ValueSet.SP_URL, new UriParam("http://hl7.org/fhir/ValueSet/basic-resource-type"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,25 @@
import static org.junit.Assert.fail;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.Observation;
import org.hl7.fhir.dstu3.model.Observation.ObservationStatus;
import org.hl7.fhir.dstu3.model.OperationOutcome;
import org.hl7.fhir.dstu3.model.Organization;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.StructureDefinition;
import org.hl7.fhir.dstu3.model.ValueSet;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.AfterClass;
import org.junit.Ignore;
import org.junit.Test;

import ca.uhn.fhir.jpa.util.StopWatch;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.api.ValidationModeEnum;
import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException;
import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
import ca.uhn.fhir.util.TestUtil;

public class FhirResourceDaoDstu3ValidateTest extends BaseJpaDstu3Test {
Expand All @@ -40,6 +34,31 @@ public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}

@Test
public void testValidateStructureDefinition() throws Exception {
String input = IOUtils.toString(getClass().getResourceAsStream("/sd-david-dhtest7.json"), StandardCharsets.UTF_8);
StructureDefinition sd = myFhirCtx.newJsonParser().parseResource(StructureDefinition.class, input);


ourLog.info("Starting validation");
try {
myStructureDefinitionDao.validate(sd, null, null, null, ValidationModeEnum.UPDATE, null, mySrd);
} catch (PreconditionFailedException e) {
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome()));
}
ourLog.info("Done validation");

StopWatch sw = new StopWatch();
ourLog.info("Starting validation");
try {
myStructureDefinitionDao.validate(sd, null, null, null, ValidationModeEnum.UPDATE, null, mySrd);
} catch (PreconditionFailedException e) {
// ok
}
ourLog.info("Done validation in {}ms", sw.getMillis());

}


@Test
@Ignore
Expand Down
Loading

0 comments on commit 202a1ea

Please sign in to comment.