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:
parent
93c34fdbbe
commit
0cce1d21df
|
@ -30,13 +30,16 @@ import ca.uhn.fhir.model.base.resource.BaseOperationOutcome;
|
||||||
*/
|
*/
|
||||||
public class ValidationResult {
|
public class ValidationResult {
|
||||||
private BaseOperationOutcome myOperationOutcome;
|
private BaseOperationOutcome myOperationOutcome;
|
||||||
|
private boolean isSuccessful;
|
||||||
|
|
||||||
private ValidationResult(BaseOperationOutcome myOperationOutcome) {
|
private ValidationResult(BaseOperationOutcome myOperationOutcome, boolean isSuccessful) {
|
||||||
this.myOperationOutcome = myOperationOutcome;
|
this.myOperationOutcome = myOperationOutcome;
|
||||||
|
this.isSuccessful = isSuccessful;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ValidationResult valueOf(BaseOperationOutcome myOperationOutcome) {
|
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() {
|
public BaseOperationOutcome getOperationOutcome() {
|
||||||
|
@ -47,6 +50,7 @@ public class ValidationResult {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ValidationResult{" +
|
return "ValidationResult{" +
|
||||||
"myOperationOutcome=" + myOperationOutcome +
|
"myOperationOutcome=" + myOperationOutcome +
|
||||||
|
", isSuccessful=" + isSuccessful +
|
||||||
", description='" + toDescription() + '\'' +
|
", description='" + toDescription() + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
@ -67,6 +71,6 @@ public class ValidationResult {
|
||||||
* @return true if the validation was successful
|
* @return true if the validation was successful
|
||||||
*/
|
*/
|
||||||
public boolean isSuccessful() {
|
public boolean isSuccessful() {
|
||||||
return myOperationOutcome == null || myOperationOutcome.getIssue().isEmpty();
|
return isSuccessful;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package ca.uhn.fhir.validation;
|
||||||
|
|
||||||
import ca.uhn.fhir.model.base.resource.BaseOperationOutcome.BaseIssue;
|
import ca.uhn.fhir.model.base.resource.BaseOperationOutcome.BaseIssue;
|
||||||
import ca.uhn.fhir.model.dstu.resource.OperationOutcome;
|
import ca.uhn.fhir.model.dstu.resource.OperationOutcome;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -45,4 +44,17 @@ public class ValidationResultTest {
|
||||||
|
|
||||||
assertThat("ValidationResult#toString should contain the issue description", result.toString(), containsString(errorMessage));
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue