Fixes #51 - cache whether or not validation was successful when the ValidationResult object is created so that mutations to the underlying OperationOutcome do not change the validation result success status.

This commit is contained in:
Joseph Athman 2014-11-18 22:17:22 -06:00
parent 93c34fdbbe
commit 0cce1d21df
2 changed files with 21 additions and 5 deletions

View File

@ -30,13 +30,16 @@ import ca.uhn.fhir.model.base.resource.BaseOperationOutcome;
*/
public class ValidationResult {
private BaseOperationOutcome myOperationOutcome;
private boolean isSuccessful;
private ValidationResult(BaseOperationOutcome myOperationOutcome) {
private ValidationResult(BaseOperationOutcome myOperationOutcome, boolean isSuccessful) {
this.myOperationOutcome = myOperationOutcome;
this.isSuccessful = isSuccessful;
}
public static ValidationResult valueOf(BaseOperationOutcome myOperationOutcome) {
return new ValidationResult(myOperationOutcome);
boolean noIssues = myOperationOutcome == null || myOperationOutcome.getIssue().isEmpty();
return new ValidationResult(myOperationOutcome, noIssues);
}
public BaseOperationOutcome getOperationOutcome() {
@ -47,6 +50,7 @@ public class ValidationResult {
public String toString() {
return "ValidationResult{" +
"myOperationOutcome=" + myOperationOutcome +
", isSuccessful=" + isSuccessful +
", description='" + toDescription() + '\'' +
'}';
}
@ -67,6 +71,6 @@ public class ValidationResult {
* @return true if the validation was successful
*/
public boolean isSuccessful() {
return myOperationOutcome == null || myOperationOutcome.getIssue().isEmpty();
return isSuccessful;
}
}

View File

@ -2,7 +2,6 @@ package ca.uhn.fhir.validation;
import ca.uhn.fhir.model.base.resource.BaseOperationOutcome.BaseIssue;
import ca.uhn.fhir.model.dstu.resource.OperationOutcome;
import org.junit.Test;
import java.util.List;
@ -45,4 +44,17 @@ public class ValidationResultTest {
assertThat("ValidationResult#toString should contain the issue description", result.toString(), containsString(errorMessage));
}
/*
Test for https://github.com/jamesagnew/hapi-fhir/issues/51
*/
@Test
public void toString_ShouldNotCauseResultToBecomeFailure() {
OperationOutcome operationOutcome = new OperationOutcome();
ValidationResult result = ValidationResult.valueOf(operationOutcome);
assertEquals(true, result.isSuccessful());
// need to call toString to make sure any unwanted side effects are generated
@SuppressWarnings("UnusedDeclaration") String unused = result.toString();
assertEquals(true, result.isSuccessful());
}
}