Exemplo n.º 1
0
 /// <summary>
 /// Initialises a new instance of <see cref="ValidatedValueFactory"/>.
 /// </summary>
 /// <param name="valueFromBasisFactory">A factory that gets the logic from a <see cref="ValidatedValueBasis"/>.</param>
 /// <param name="valueProvider">A service to get the value to be validated.</param>
 /// <param name="enumerableProvider">A service that gets the items of an enumerable object.</param>
 /// <exception cref="ArgumentNullException">If any parameter value is <see langword="null" />.</exception>
 public ValidatedValueFactory(IGetsValidatedValueFromBasis valueFromBasisFactory,
                              IGetsValueToBeValidated valueProvider,
                              IGetsEnumerableItemsToBeValidated enumerableProvider)
 {
     this.valueFromBasisFactory = valueFromBasisFactory ?? throw new ArgumentNullException(nameof(valueFromBasisFactory));
     this.valueProvider         = valueProvider ?? throw new ArgumentNullException(nameof(valueProvider));
     this.enumerableProvider    = enumerableProvider ?? throw new ArgumentNullException(nameof(enumerableProvider));
 }
        public void GetValidatedValueShouldBeAbleToProcessACollectionWithinACollection([Frozen] IGetsValidatedValueFromBasis valueFromBasisFactory,
                                                                                       [Frozen] IGetsValueToBeValidated valueProvider,
                                                                                       [Frozen] IGetsEnumerableItemsToBeValidated enumerableProvider,
                                                                                       ValidatedValueFactory sut,
                                                                                       [NoAutoProperties] ComplexObject validatedValue,
                                                                                       ResolvedValidationOptions validationOptions,
                                                                                       [ExecutableModel] ValidatedValue val,
                                                                                       [ExecutableModel] ValidatedValue firstCollection,
                                                                                       [ExecutableModel] ValidatedValue secondCollection,
                                                                                       [ExecutableModel] ValidatedValue item)
        {
            var manifestValue = new ManifestValue
            {
                ValidatedType = typeof(ComplexObject),
                Children      = new [] {
                    new ManifestValue
                    {
                        ValidatedType       = typeof(ICollection <List <ComplexObject> >),
                        MemberName          = nameof(ComplexObject.DoubleCollection),
                        AccessorFromParent  = obj => ((ComplexObject)obj).DoubleCollection,
                        CollectionItemValue = new ManifestCollectionItem
                        {
                            ValidatedType       = typeof(List <ComplexObject>),
                            CollectionItemValue = new ManifestCollectionItem
                            {
                                ValidatedType = typeof(ComplexObject),
                            }
                        },
                    },
                },
            };
            var child = (ManifestValue)manifestValue.Children.Single();

            child.Parent = manifestValue;
            child.CollectionItemValue.Parent = manifestValue;
            child.CollectionItemValue.CollectionItemValue.Parent = manifestValue;
            validatedValue.DoubleCollection = new List <List <ComplexObject> > {
                new List <ComplexObject> {
                    new ComplexObject()
                }
            };
            Mock.Get(valueFromBasisFactory)
            .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue)))
            .Returns(val);
            val.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue);
            Mock.Get(valueFromBasisFactory)
            .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == child)))
            .Returns(firstCollection);
            firstCollection.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue.DoubleCollection);
            Mock.Get(valueFromBasisFactory)
            .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == child.CollectionItemValue)))
            .Returns(secondCollection);
            secondCollection.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue.DoubleCollection.First());
            Mock.Get(valueFromBasisFactory)
            .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == child.CollectionItemValue.CollectionItemValue)))
            .Returns(item);
            item.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue.DoubleCollection.First().First());
            object validatedVal = validatedValue;

            Mock.Get(valueProvider)
            .Setup(x => x.GetValueToBeValidated(manifestValue, validatedValue, validationOptions))
            .Returns(new SuccessfulGetValueToBeValidatedResponse(validatedValue));
            object doubleCollection = firstCollection.ValueResponse;

            Mock.Get(valueProvider)
            .Setup(x => x.GetValueToBeValidated(child, validatedValue, validationOptions))
            .Returns(new SuccessfulGetValueToBeValidatedResponse(doubleCollection));
            Mock.Get(enumerableProvider)
            .Setup(x => x.GetEnumerableItems(validatedValue.DoubleCollection, child.CollectionItemValue.ValidatedType))
            .Returns(validatedValue.DoubleCollection);
            Mock.Get(enumerableProvider)
            .Setup(x => x.GetEnumerableItems(validatedValue.DoubleCollection.First(), child.CollectionItemValue.CollectionItemValue.ValidatedType))
            .Returns(validatedValue.DoubleCollection.First());

            var result = sut.GetValidatedValue(manifestValue, validatedValue, validationOptions);

            Mock.Get(valueFromBasisFactory)
            .Verify(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == child.CollectionItemValue.CollectionItemValue)), Times.Once);
        }
        public void GetValidatedValueShouldBeAbleToProcessAGrandchildManifestValue([Frozen] IGetsValidatedValueFromBasis valueFromBasisFactory,
                                                                                   [Frozen] IGetsValueToBeValidated valueProvider,
                                                                                   ValidatedValueFactory sut,
                                                                                   [NoAutoProperties] ComplexObject validatedValue,
                                                                                   [NoAutoProperties] ComplexObject childValue,
                                                                                   string grandchildValue,
                                                                                   ResolvedValidationOptions validationOptions,
                                                                                   [ExecutableModel] ValidatedValue val,
                                                                                   [ExecutableModel] ValidatedValue childVal,
                                                                                   [ExecutableModel] ValidatedValue grandchildVal)
        {
            var manifestValue = new ManifestValue
            {
                ValidatedType = typeof(ComplexObject),
            };
            var childManifest = new ManifestValue
            {
                Parent        = manifestValue,
                ValidatedType = typeof(ComplexObject),
            };
            var grandchildManifest = new ManifestValue
            {
                Parent        = childManifest,
                ValidatedType = typeof(string),
            };

            manifestValue.Children.Add(childManifest);
            childManifest.Children.Add(grandchildManifest);
            Mock.Get(valueFromBasisFactory)
            .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue)))
            .Returns(val);
            val.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(validatedValue);
            val.ManifestValue = manifestValue;
            Mock.Get(valueFromBasisFactory)
            .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == childManifest)))
            .Returns(childVal);
            childVal.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(childValue);
            childVal.ManifestValue = childManifest;;
            Mock.Get(valueFromBasisFactory)
            .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == grandchildManifest)))
            .Returns(grandchildVal);
            grandchildVal.ValueResponse = new SuccessfulGetValueToBeValidatedResponse(grandchildValue);
            grandchildVal.ManifestValue = grandchildManifest;
            object child = childValue;

            Mock.Get(valueProvider)
            .Setup(x => x.GetValueToBeValidated(childManifest, validatedValue, validationOptions))
            .Returns(new SuccessfulGetValueToBeValidatedResponse(child));
            object grandchild = grandchildValue;

            Mock.Get(valueProvider)
            .Setup(x => x.GetValueToBeValidated(grandchildManifest, childValue, validationOptions))
            .Returns(new SuccessfulGetValueToBeValidatedResponse(grandchild));

            var result = sut.GetValidatedValue(manifestValue, validatedValue, validationOptions);

            Mock.Get(valueFromBasisFactory)
            .Verify(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == childManifest && b.GetActualValue() == child)), Times.Once);
            Mock.Get(valueFromBasisFactory)
            .Verify(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == grandchildManifest && b.GetActualValue() == grandchild)), Times.Once);
        }
        public void GetValidatedValueShouldNotProcessAChildManifestValueIfTheActualValueCannotBeRetrieved([Frozen] IGetsValidatedValueFromBasis valueFromBasisFactory,
                                                                                                          [Frozen] IGetsValueToBeValidated valueProvider,
                                                                                                          ValidatedValueFactory sut,
                                                                                                          [NoAutoProperties] ComplexObject validatedValue,
                                                                                                          string childValue,
                                                                                                          ResolvedValidationOptions validationOptions,
                                                                                                          [ExecutableModel] ValidatedValue val,
                                                                                                          [ExecutableModel] ValidatedValue childVal)
        {
            var manifestValue = new ManifestValue
            {
                ValidatedType = typeof(ComplexObject),
            };
            var childManifest = new ManifestValue
            {
                Parent        = manifestValue,
                ValidatedType = typeof(string),
            };

            manifestValue.Children.Add(childManifest);
            Mock.Get(valueFromBasisFactory)
            .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == manifestValue)))
            .Returns(val);
            Mock.Get(valueFromBasisFactory)
            .Setup(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == childManifest)))
            .Returns(childVal);
            object child = childValue;

            Mock.Get(valueProvider)
            .Setup(x => x.GetValueToBeValidated(childManifest, validatedValue, validationOptions))
            .Returns(new IgnoredGetValueToBeValidatedResponse());

            var result = sut.GetValidatedValue(manifestValue, validatedValue, validationOptions);

            Mock.Get(valueFromBasisFactory)
            .Verify(x => x.GetValidatedValue(It.Is <ValidatedValueBasis>(b => b.ManifestValue == childManifest && b.ValidatedValueResponse == child)), Times.Never);
        }