public async Task GivenAnUnknownRoute_WhenPostingToHttp_TheServerShouldReturnAnOperationOutcome() { using var fhirException = await Assert.ThrowsAsync <FhirException>(async () => await _client.ReadAsync <OperationOutcome>("unknownRoute")); Assert.Equal(HttpStatusCode.NotFound, fhirException.StatusCode); var operationOutcome = fhirException.OperationOutcome; Assert.NotNull(operationOutcome.Id); Assert.NotEmpty(operationOutcome.Issue); Assert.Equal(OperationOutcome.IssueType.NotFound, operationOutcome.Issue[0].Code); Assert.Equal(OperationOutcome.IssueSeverity.Error, operationOutcome.Issue[0].Severity); TestHelper.AssertSecurityHeaders(fhirException.Headers); DotNetAttributeValidation.Validate(operationOutcome, true); }
private void validateErrorOrFail(object instance, bool recurse = false, string membername = null) { try { // should throw error DotNetAttributeValidation.Validate(instance, recurse); Assert.Fail(); } catch (ValidationException ve) { if (membername != null) { Assert.IsTrue(ve.ValidationResult.MemberNames.Contains(membername)); } } }
public void TestIdValidation() { Id id = new Id("az23"); DotNetAttributeValidation.Validate(id); DotNetAttributeValidation.Validate(id, true); // recursive checking shouldnt matter id = new Id("!notgood!"); validateErrorOrFail(id); id = new Id("NotGood!"); validateErrorOrFail(id); id = new Id("123456789012345678901234567890123456745290123456745290123456745290123456745290"); validateErrorOrFail(id); }
public void TestXhtmlValidation() { var p = new Patient(); p.Text = new Narrative() { Div = "<div xmlns='http://www.w3.org/1999/xhtml'><p>should be valid</p></div>", Status = Narrative.NarrativeStatus.Generated }; DotNetAttributeValidation.Validate(p, true); p.Text.Div = "<div xmlns='http://www.w3.org/1999/xhtml'><p>should not be valid<p></div>"; validateErrorOrFail(p, true); p.Text.Div = "<div xmlns='http://www.w3.org/1999/xhtml'><img onmouseover='bigImg(this)' src='smiley.gif' alt='Smiley' /></div>"; validateErrorOrFail(p, true); }
public void ContainedResourcesAreValidatedToo() { Patient p = new Patient(); // Deceased can either be boolean or dateTime, not FhirUri p.Deceased = new FhirUri(); var pr = new Patient(); pr.Contained = new List <Resource> { p }; validateErrorOrFail(pr, true); DotNetAttributeValidation.Validate(pr); }
public IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context) { EnsureArg.IsNotNull(context, nameof(context)); if (context.PropertyValue is Resource resource) { var results = new List <ValidationResult>(); if (!DotNetAttributeValidation.TryValidate(resource, results, recurse: false)) { foreach (var error in results) { yield return(new ValidationFailure(error.MemberNames?.FirstOrDefault(), error.ErrorMessage)); } } } }
public override IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var result = new List <ValidationResult>(); result.AddRange(base.Validate(validationContext)); if (Content == null) { result.Add(DotNetAttributeValidation.BuildResult(validationContext, "Entry must contain (possibly 0-length) data in Content element")); } if (ContentType == null) { result.Add(DotNetAttributeValidation.BuildResult(validationContext, "Entry must contain a ContentType")); } return(result); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var result = new List <ValidationResult>(); //if (String.IsNullOrWhiteSpace(Title)) // result.Add(new ValidationResult("Feed must contain a title", FhirValidator.SingleMemberName("Title")); //if (!UriHasValue(Id)) // result.Add(new ValidationResult("Feed must have an id")); //else // if (!Id.IsAbsoluteUri) // result.Add(new ValidationResult("Feed id must be an absolute URI")); if (Id != null && !Id.IsAbsoluteUri) { result.Add(DotNetAttributeValidation.BuildResult(validationContext, "Feed id must be an absolute URI")); } //if (LastUpdated == null) // result.Add(new ValidationResult("Feed must have a updated date")); if (Links.SearchLink != null) { result.Add(DotNetAttributeValidation.BuildResult(validationContext, "Links with rel='search' can only be used on feed entries")); } bool feedHasAuthor = !String.IsNullOrEmpty(this.AuthorName); if (Entries != null && validationContext.ValidateRecursively()) { foreach (var entry in Entries.Where(e => e != null)) { if (!feedHasAuthor && entry is ResourceEntry && String.IsNullOrEmpty(((ResourceEntry)entry).AuthorName)) { result.Add(DotNetAttributeValidation.BuildResult(validationContext, "Bundle's author and Entry author cannot both be empty")); } DotNetAttributeValidation.TryValidate(entry, result, validationContext.ValidateRecursively()); } } return(result); }
public void TestCardinality() { OperationOutcome oo = new OperationOutcome(); validateErrorOrFail(oo, true); oo.Issue = new List <OperationOutcome.OperationOutcomeIssueComponent>(); validateErrorOrFail(oo, true); var issue = new OperationOutcome.OperationOutcomeIssueComponent(); oo.Issue.Add(issue); validateErrorOrFail(oo, true); issue.Severity = OperationOutcome.IssueSeverity.Information; validateErrorOrFail(oo, true); issue.Code = new CodeableConcept("http://somesystem.org/some", "bla"); DotNetAttributeValidation.Validate(oo, true); }
public void TestCardinality() { OperationOutcome oo = new OperationOutcome(); validateErrorOrFail(oo, true); oo.Issue = new List <OperationOutcome.IssueComponent>(); validateErrorOrFail(oo, true); var issue = new OperationOutcome.IssueComponent(); oo.Issue.Add(issue); validateErrorOrFail(oo, true); issue.Severity = OperationOutcome.IssueSeverity.Information; validateErrorOrFail(oo, true); issue.Code = OperationOutcome.IssueType.Forbidden; DotNetAttributeValidation.Validate(oo, true); }
public async Task WhenPostingToHttp_GivenAMiddlewareThrowQuerystring_TheServerShouldReturnAnOperationOutcome() { if (!_fixture.IsUsingInProcTestServer) { // this test only works with the in-proc server with customized middleware pipeline return; } var fhirException = await Assert.ThrowsAsync <FhirException>(async() => await Client.ReadAsync <OperationOutcome>("?throw=middleware")); Assert.Equal(HttpStatusCode.InternalServerError, fhirException.StatusCode); var operationOutcome = fhirException.OperationOutcome; Assert.NotNull(operationOutcome.Id); Assert.NotEmpty(operationOutcome.Issue); Assert.Equal(OperationOutcome.IssueType.Exception, operationOutcome.Issue[0].Code); Assert.Equal(OperationOutcome.IssueSeverity.Fatal, operationOutcome.Issue[0].Severity); TestHelper.AssertSecurityHeaders(fhirException.Headers); DotNetAttributeValidation.Validate(operationOutcome, true); }
public async Task GivenAResource_WhenPostingToHttp_TheServerShouldRespondSuccessfully() { using FhirResponse <Observation> response = await _client.CreateAsync(Samples.GetDefaultObservation ().ToPoco <Observation>()); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.NotNull(response.Headers.ETag); Assert.NotNull(response.Headers.Location); Assert.NotNull(response.Content.Headers.LastModified); Observation observation = response.Resource; Assert.NotNull(observation.Id); Assert.NotNull(observation.Meta.VersionId); Assert.NotNull(observation.Meta.LastUpdated); Assert.Equal($@"W/""{observation.Meta.VersionId}""", response.Headers.ETag.ToString()); TestHelper.AssertLocationHeaderIsCorrect(_client, observation, response.Headers.Location); TestHelper.AssertLastUpdatedAndLastModifiedAreEqual(observation.Meta.LastUpdated, response.Content.Headers.LastModified); DotNetAttributeValidation.Validate(observation, true); }
public override IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var result = new List <ValidationResult>(); // The ID field does not need to be an abolute URI, // this should be the ResourceIdentity. // if (Id != null && !new Uri(Id,UriKind.RelativeOrAbsolute).IsAbsoluteUri) // result.Add(DotNetAttributeValidation.BuildResult(validationContext, "Entry id must be an absolute URI")); if (Meta != null) { // if (!String.IsNullOrEmpty(this.Meta.VersionId) && !new Uri(Id,UriKind.RelativeOrAbsolute).IsAbsoluteUri) // result.Add(DotNetAttributeValidation.BuildResult(validationContext, "Entry selflink must be an absolute URI")); if (Meta.Tag != null && validationContext.ValidateRecursively()) { DotNetAttributeValidation.TryValidate(Meta.Tag, result, true); } } return(result); }
public void ValidateDemoPatient() { var s = new StringReader(TestDataHelper.ReadTestData(@"TestPatient.xml")); var patient = new FhirXmlParser().Parse <Patient>(XmlReader.Create(s)); ICollection <ValidationResult> results = new List <ValidationResult>(); foreach (var contained in patient.Contained) { ((DomainResource)contained).Text = new Narrative() { Div = "<wrong />" } } ; Assert.IsFalse(DotNetAttributeValidation.TryValidate(patient, results, true)); Assert.IsTrue(results.Count > 0); results.Clear(); foreach (DomainResource contained in patient.Contained) { contained.Text = null; } // Try again Assert.IsTrue(DotNetAttributeValidation.TryValidate(patient, results, true)); patient.Identifier[0].System = "urn:oid:crap really not valid"; results = new List <ValidationResult>(); Assert.IsFalse(DotNetAttributeValidation.TryValidate(patient, results, true)); Assert.IsTrue(results.Count > 0); } }
public void TestContainedConstraints() { var pat = new Patient(); var patn = new Patient(); pat.Contained = new List <Resource> { patn }; patn.Contained = new List <Resource> { new Patient() }; // Contained resources should not themselves contain resources validateErrorOrFail(pat); patn.Contained = null; DotNetAttributeValidation.Validate(pat); patn.Text = new Narrative(); patn.Text.Div = "<div>Narrative in contained resource</div>"; // Contained resources should not contain narrative validateErrorOrFail(pat); }
public override IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var result = new List <ValidationResult>(); if (Id != null && !new Uri(Id, UriKind.RelativeOrAbsolute).IsAbsoluteUri) { result.Add(DotNetAttributeValidation.BuildResult(validationContext, "Entry id must be an absolute URI")); } if (Meta != null) { if (!String.IsNullOrEmpty(this.Meta.VersionId) && !new Uri(Id, UriKind.RelativeOrAbsolute).IsAbsoluteUri) { result.Add(DotNetAttributeValidation.BuildResult(validationContext, "Entry selflink must be an absolute URI")); } if (Meta.Tag != null && validationContext.ValidateRecursively()) { DotNetAttributeValidation.TryValidate(Meta.Tag, result, true); } } return(result); }
private static void validateElement(object value, ValidationContext validationContext, List <ValidationResult> result) { DotNetAttributeValidation.TryValidate(value, result, validationContext.ValidateRecursively()); }
public bool TryValidate(ResourceElement value, ICollection <ValidationResult> validationResults = null, bool recurse = false) { return(DotNetAttributeValidation.TryValidate(value.ToPoco(), validationResults, recurse)); }
public void ValidateInvariants(List <ValidationResult> result) => ValidateInvariants(DotNetAttributeValidation.BuildContext(null), result);
public void ValidateInvariants(OperationOutcome result) => ValidateInvariants(DotNetAttributeValidation.BuildContext(new object()), result);