Exemplo n.º 1
0
        public void IsResolverInformationValid_Flat_IsValid(Boolean shouldBeValid)
        {
            Random random = new Random();

            CreateScopeResolverInformation inputModel = new CreateScopeResolverInformation();
            Int32 propertyAmount = random.Next(3, 30);

            for (int i = 0; i < propertyAmount; i++)
            {
                inputModel.PropertiesAndValues.Add($"property-{i + 1}", $"{random.Next()}");
            }

            Mock <ISerializer>         serializerMock = new Mock <ISerializer>(MockBehavior.Strict);
            DHCPv6ScopeResolverManager scopeManager   = new DHCPv6ScopeResolverManager(serializerMock.Object, Mock.Of <IDeviceService>(MockBehavior.Strict), Mock.Of <ILogger <DHCPv6ScopeResolverManager> >());

            Mock <IScopeResolver <DHCPv6Packet, IPv6Address> > resolverMock = new Mock <IScopeResolver <DHCPv6Packet, IPv6Address> >(MockBehavior.Strict);

            resolverMock.Setup(x => x.ArePropertiesAndValuesValid(inputModel.PropertiesAndValues, serializerMock.Object)).Returns(shouldBeValid);

            String typeName = $"typname-{random.Next()}";

            scopeManager.AddOrUpdateScopeResolver(typeName, () => resolverMock.Object);
            inputModel.Typename = typeName;

            Boolean actual = scopeManager.IsResolverInformationValid(inputModel);

            Assert.Equal(shouldBeValid, actual);

            resolverMock.Verify(x => x.ArePropertiesAndValuesValid(inputModel.PropertiesAndValues, serializerMock.Object), Times.Once());
        }
Exemplo n.º 2
0
        public void IsResolverInformationValid_ResolverTypeNotFound()
        {
            Random random = new Random();
            CreateScopeResolverInformation inputModel = new CreateScopeResolverInformation();

            DHCPv6ScopeResolverManager scopeManager = new DHCPv6ScopeResolverManager(Mock.Of <ISerializer>(MockBehavior.Strict), Mock.Of <IDeviceService>(MockBehavior.Strict), Mock.Of <ILogger <DHCPv6ScopeResolverManager> >());

            String typeName = $"typname-{random.Next()}";

            inputModel.Typename = typeName;

            Boolean actual = scopeManager.IsResolverInformationValid(inputModel);

            Assert.False(actual);
        }
Exemplo n.º 3
0
        public void IsResolverInformationValid_InvalidModel()
        {
            List <CreateScopeResolverInformation> invalidModels = new List <CreateScopeResolverInformation>
            {
                null,
                new CreateScopeResolverInformation(),
                new CreateScopeResolverInformation {
                    Typename = ""
                },
                new CreateScopeResolverInformation {
                    Typename = String.Empty
                },
            };

            DHCPv6ScopeResolverManager scopeManager = new DHCPv6ScopeResolverManager(Mock.Of <ISerializer>(MockBehavior.Strict), Mock.Of <IDeviceService>(MockBehavior.Strict), Mock.Of <ILogger <DHCPv6ScopeResolverManager> >());

            foreach (CreateScopeResolverInformation item in invalidModels)
            {
                Boolean actual = scopeManager.IsResolverInformationValid(item);
                Assert.False(actual);
            }
        }
Exemplo n.º 4
0
        public void IsResolverInformationValid_ComplexStructure(Boolean shouldBeValid)
        {
            for (int seed = 1; seed <= 10; seed++)
            {
                Random random = new Random(seed);

                CreateScopeResolverInformation inputModel = new CreateScopeResolverInformation
                {
                    Typename = $"typename-{random.Next()}",
                };

                Int32 propertyAmount = random.Next(3, 30);
                for (int i = 0; i < propertyAmount; i++)
                {
                    inputModel.PropertiesAndValues.Add($"property-{i + 1}", $"{random.Next()}");
                }

                Mock <ISerializer>         serializerMock = new Mock <ISerializer>(MockBehavior.Strict);
                DHCPv6ScopeResolverManager scopeManager   = new DHCPv6ScopeResolverManager(serializerMock.Object, Mock.Of <IDeviceService>(MockBehavior.Strict), Mock.Of <ILogger <DHCPv6ScopeResolverManager> >());

                Mock <IScopeResolverContainingOtherResolvers <DHCPv6Packet, IPv6Address> > resolverMock = new Mock <IScopeResolverContainingOtherResolvers <DHCPv6Packet, IPv6Address> >(MockBehavior.Strict);
                resolverMock.Setup(x => x.ArePropertiesAndValuesValid(inputModel.PropertiesAndValues, serializerMock.Object)).Returns(true);

                Dictionary <String, Tuple <Mock <IScopeResolverContainingOtherResolvers <DHCPv6Packet, IPv6Address> >, CreateScopeResolverInformation> > mocksWithChildren =
                    new Dictionary <string, Tuple <Mock <IScopeResolverContainingOtherResolvers <DHCPv6Packet, IPv6Address> >, CreateScopeResolverInformation> >
                {
                    {
                        inputModel.Typename,
                        new Tuple <Mock <IScopeResolverContainingOtherResolvers <DHCPv6Packet, IPv6Address> >, CreateScopeResolverInformation>(
                            resolverMock, inputModel)
                    }
                };

                Dictionary <String, Tuple <Mock <IScopeResolver <DHCPv6Packet, IPv6Address> >, CreateScopeResolverInformation> > mocksWithoutChildren = new Dictionary <string, Tuple <Mock <IScopeResolver <DHCPv6Packet, IPv6Address> >, CreateScopeResolverInformation> >
                                                                                                                                                            ();

                GenerateResolverTreeForValidation(serializerMock, resolverMock, inputModel, mocksWithChildren, mocksWithoutChildren, random, random.NextDouble());

                foreach (var item in mocksWithChildren)
                {
                    scopeManager.AddOrUpdateScopeResolver(item.Key, () => item.Value.Item1.Object);
                }

                foreach (var item in mocksWithoutChildren)
                {
                    scopeManager.AddOrUpdateScopeResolver(item.Key, () => item.Value.Item1.Object);
                }

                if (shouldBeValid == false)
                {
                    if (random.NextDouble() > 0.5)
                    {
                        var item = mocksWithChildren.ElementAt(random.Next(0, mocksWithChildren.Count));
                        item.Value.Item1.Setup(x => x.ArePropertiesAndValuesValid(item.Value.Item2.PropertiesAndValues, serializerMock.Object)).Returns(false);
                    }
                    else
                    {
                        var item = mocksWithoutChildren.ElementAt(random.Next(0, mocksWithoutChildren.Count));
                        item.Value.Item1.Setup(x => x.ArePropertiesAndValuesValid(item.Value.Item2.PropertiesAndValues, serializerMock.Object)).Returns(false);
                    }
                }

                Boolean actual = scopeManager.IsResolverInformationValid(inputModel);
                Assert.Equal(shouldBeValid, actual);

                var times = Times.AtMost(1);
                if (shouldBeValid == true)
                {
                    times = Times.Exactly(1);
                }

                foreach (var item in mocksWithChildren)
                {
                    item.Value.Item1.Verify(x => x.ArePropertiesAndValuesValid(item.Value.Item2.PropertiesAndValues, serializerMock.Object), times);
                    item.Value.Item1.Verify(x => x.ExtractResolverCreateModels(item.Value.Item2, serializerMock.Object), times);
                }

                foreach (var item in mocksWithoutChildren)
                {
                    item.Value.Item1.Verify(x => x.ArePropertiesAndValuesValid(item.Value.Item2.PropertiesAndValues, serializerMock.Object), times);
                }
            }
        }