예제 #1
0
        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);
        }
예제 #2
0
 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));
         }
     }
 }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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));
                    }
                }
            }
        }
예제 #7
0
        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);
        }
예제 #8
0
        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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }
예제 #12
0
        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);
        }
예제 #13
0
        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);
        }
예제 #14
0
        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);
        }
    }
예제 #15
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);
        }
예제 #16
0
        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);
        }
예제 #17
0
 private static void validateElement(object value, ValidationContext validationContext, List <ValidationResult> result)
 {
     DotNetAttributeValidation.TryValidate(value, result, validationContext.ValidateRecursively());
 }
예제 #18
0
 public bool TryValidate(ResourceElement value, ICollection <ValidationResult> validationResults = null, bool recurse = false)
 {
     return(DotNetAttributeValidation.TryValidate(value.ToPoco(), validationResults, recurse));
 }
예제 #19
0
 public void ValidateInvariants(List <ValidationResult> result)
 => ValidateInvariants(DotNetAttributeValidation.BuildContext(null), result);
예제 #20
0
 public void ValidateInvariants(OperationOutcome result)
 => ValidateInvariants(DotNetAttributeValidation.BuildContext(new object()), result);