예제 #1
0
        public async Task GivenValidAndInvalidTagValues_WhenValidate_ThenReturnedValidTagsAndStoredFailure()
        {
            DicomTag     tag1     = DicomTag.DeviceSerialNumber;
            DicomTag     tag2     = DicomTag.DeviceID;
            DicomDataset ds       = Samples.CreateRandomInstanceDataset();
            DicomElement element1 = new DicomLongString(tag1, "testvalue1");
            DicomElement element2 = new DicomLongString(tag2, "testvalue2");

            ds.Add(element1);
            ds.Add(element2);
            QueryTag queryTag1 = new QueryTag(new ExtendedQueryTagStoreEntry(1, tag1.GetPath(), tag1.GetDefaultVR().Code, null, QueryTagLevel.Instance, ExtendedQueryTagStatus.Ready, QueryStatus.Enabled, 0));
            QueryTag queryTag2 = new QueryTag(new ExtendedQueryTagStoreEntry(2, tag2.GetPath(), tag2.GetDefaultVR().Code, null, QueryTagLevel.Instance, ExtendedQueryTagStatus.Ready, QueryStatus.Enabled, 0));

            // Throw exception when validate element1
            var ex = ElementValidationExceptionFactory.CreateDateIsInvalidException("testname", "testvalue");

            _minimumValidator.When(x => x.Validate(element1))
            .Throw(ex);

            // only return querytag2
            long watermark      = 1;
            var  validQueryTags = await _datasetValidator.ValidateAsync(ds, watermark, new[] { queryTag1, queryTag2 });

            Assert.Single(validQueryTags);
            Assert.Same(queryTag2, validQueryTags.First());

            // error for querytag1 is logged
            await _tagErrorsService.Received(1)
            .AddExtendedQueryTagErrorAsync(queryTag1.ExtendedQueryTagStoreEntry.Key, ex.ErrorCode, 1, Arg.Any <CancellationToken>());
        }
 private void ValidateByteBufferLength(DicomVR dicomVR, string name, IByteBuffer value)
 {
     if (value?.Size != ExpectedLength)
     {
         throw ElementValidationExceptionFactory.CreateUnexpectedLengthException(name, dicomVR, ExpectedLength);
     }
 }
 private void ValidateStringLength(DicomVR dicomVR, string name, string value)
 {
     value = value ?? "";
     if (value.Length != ExpectedLength)
     {
         throw ElementValidationExceptionFactory.CreateUnexpectedLengthException(name, dicomVR, value, ExpectedLength);
     }
 }
        public void GivenTimeIsInvalidException_WhenGetMessage_ShouldReturnExpected()
        {
            var name      = "tagname";
            var value     = "tagvalue";
            var exception = ElementValidationExceptionFactory.CreateTimeIsInvalidException(name, value);

            Assert.Equal($"Dicom element '{name}' with value '{value}' failed validation for VR 'TM': Value cannot be parsed as a valid Time.", exception.Message);
        }
        public void GivenMultiValuesException_WhenGetMessage_ShouldReturnExpected()
        {
            var name      = "tagname";
            var vr        = DicomVR.DA;
            var exception = ElementValidationExceptionFactory.CreateMultiValuesException(name, vr);

            Assert.Equal($"Dicom element '{name}' failed validation for VR '{vr}': Dicom element has multiple values. Indexing is only supported on single value element.", exception.Message);
        }
        public void GivenPersonNameGroupExceedMaxLengthException_WhenGetMessage_ShouldReturnExpected()
        {
            var name      = "tagname";
            var value     = "tagvalue";
            var exception = ElementValidationExceptionFactory.CreatePersonNameGroupExceedMaxLengthException(name, value);

            Assert.Equal($"Dicom element '{name}' with value '{value}' failed validation for VR 'PN': One or more group of person name exceeds maxium length of 64.", exception.Message);
        }
        public void GivenPersonNameExceedMaxGroupsException_WhenGetMessage_ShouldReturnExpected()
        {
            var name      = "tagname";
            var value     = "tagvalue";
            var exception = ElementValidationExceptionFactory.CreatePersonNameExceedMaxGroupsException(name, value);

            Assert.Equal($"Dicom element '{name}' with value '{value}' failed validation for VR 'PN': Value contains more than 3 groups.", exception.Message);
        }
예제 #8
0
 public static void Validate(string value, int maxLength, string name, DicomVR vr)
 {
     EnsureArg.IsNotNullOrEmpty(name, nameof(name));
     EnsureArg.IsNotNull(vr, nameof(vr));
     if (value?.Length > maxLength)
     {
         throw ElementValidationExceptionFactory.CreateExceedMaxLengthException(name, vr, value, maxLength);
     }
 }
        public void GivenUnexpectedLengthExceptionWithoutValue_WhenGetMessage_ShouldReturnExpected()
        {
            var name           = "tagname";
            var vr             = DicomVR.DA;
            var expectedLength = 8;
            var exception      = ElementValidationExceptionFactory.CreateUnexpectedLengthException(name, vr, expectedLength);

            Assert.Equal($"Dicom element '{name}' failed validation for VR '{vr}': Value length is not {expectedLength}.", exception.Message);
        }
예제 #10
0
        public void GivenInvalidCharactersException_WhenGetMessage_ShouldReturnExpected()
        {
            var name      = "tagname";
            var value     = "tagvalue";
            var vr        = DicomVR.DA;
            var exception = ElementValidationExceptionFactory.CreateInvalidCharactersException(name, vr, value);

            Assert.Equal($"Dicom element '{name}' with value '{value}' failed validation for VR '{vr}': Value contains invalid character.", exception.Message);
        }
예제 #11
0
        public void GivenUnexpectedVRException_WhenGetMessage_ShouldReturnExpected()
        {
            var name       = "tagname";
            var actualVR   = DicomVR.DA;
            var expectedVR = DicomVR.DT;
            var exception  = ElementValidationExceptionFactory.CreateUnexpectedVRException(name, actualVR, expectedVR);

            Assert.Equal($"Dicom element '{name}' failed validation for VR '{actualVR}': The extended query tag '{name}' is expected to have VR '{expectedVR}' but has '{actualVR}' in file.", exception.Message);
        }
예제 #12
0
        public void GivenExceedMaxLengthException_WhenGetMessage_ShouldReturnExpected()
        {
            var name      = "tagname";
            var value     = "tagvalue";
            var vr        = DicomVR.DA;
            int maxLength = 8;
            var exception = ElementValidationExceptionFactory.CreateExceedMaxLengthException(name, vr, value, maxLength);

            Assert.Equal($"Dicom element '{name}' with value '{value}' failed validation for VR '{vr}': Value length exceeds maximum length of {maxLength}.", exception.Message);
        }
예제 #13
0
        public static void Validate(string value, string name)
        {
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            ElementMaxLengthValidation.Validate(value, 64, name, DicomVR.LO);

            if (value.Contains('\\', StringComparison.OrdinalIgnoreCase) || ContainsControlExceptEsc(value))
            {
                throw ElementValidationExceptionFactory.CreateInvalidCharactersException(name, DicomVR.LO, value);
            }
        }
예제 #14
0
        public override void Validate(DicomElement dicomElement)
        {
            base.Validate(dicomElement);

            string value = dicomElement.Get <string>();
            string name  = dicomElement.Tag.GetFriendlyName();

            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            if (!DateTime.TryParseExact(value, DateFormatDA, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out _))
            {
                throw ElementValidationExceptionFactory.CreateDateIsInvalidException(name, value);
            }
        }
예제 #15
0
        public override void Validate(DicomElement dicomElement)
        {
            base.Validate(dicomElement);

            string  value = dicomElement.Get <string>();
            string  name  = dicomElement.Tag.GetFriendlyName();
            DicomVR vr    = dicomElement.ValueRepresentation;

            if (string.IsNullOrEmpty(value))
            {
                // empty values allowed
                return;
            }

            var groups = value.Split('=');

            if (groups.Length > 3)
            {
                throw ElementValidationExceptionFactory.CreatePersonNameExceedMaxGroupsException(name, value);
            }

            foreach (var group in groups)
            {
                try
                {
                    ElementMaxLengthValidation.Validate(group, 64, name, dicomElement.ValueRepresentation);
                }
                catch (ElementValidationException ex) when(ex.ErrorCode == ValidationErrorCode.ExceedMaxLength)
                {
                    // Reprocess the exception to make more meaningful message
                    throw ElementValidationExceptionFactory.CreatePersonNameGroupExceedMaxLengthException(name, value);
                }

                if (ContainsControlExceptEsc(group))
                {
                    throw ElementValidationExceptionFactory.CreateInvalidCharactersException(name, vr, value);
                }
            }

            var groupcomponents = groups.Select(group => group.Split('^').Length);

            if (groupcomponents.Any(l => l > 5))
            {
                throw ElementValidationExceptionFactory.CreatePersonNameExceedMaxComponentsException(name, value);
            }
        }
예제 #16
0
        public virtual void Validate(DicomElement dicomElement)
        {
            EnsureArg.IsNotNull(dicomElement, nameof(dicomElement));
            DicomVR vr = dicomElement.ValueRepresentation;

            if (ExtendedQueryTagEntryValidator.SupportedVRCodes.Contains(vr.Code))
            {
                // only works for single value dicom element ( Since we accept empty/null value, Count = 0 is accepted).
                if (dicomElement.Count > 1)
                {
                    throw ElementValidationExceptionFactory.CreateMultiValuesException(dicomElement.Tag.GetFriendlyName(), vr);
                }
            }
            else
            {
                Debug.Fail($"Validating VR {vr.Code} is not supported.");
            }
        }