private MultiValuedProperty <BindingMetadata> ValidateExchangeBindings(IEnumerable <string> bindings, int maxCount)
        {
            base.WriteVerbose(Strings.VerboseValidatingExchangeBinding);
            MultiValuedProperty <BindingMetadata> multiValuedProperty = new MultiValuedProperty <BindingMetadata>();

            if (bindings.Any <string>())
            {
                List <string> list = (from binding in bindings
                                      where SourceValidator.IsWideScope(binding)
                                      select binding).ToList <string>();
                if (list.Any <string>())
                {
                    throw new ExCannotContainWideScopeBindingsException(string.Join(", ", list));
                }
                this.ExchangeBinding = NewCompliancePolicyBase.ValidateWideScopeBinding(this.ExchangeBinding, "All", "All", new BindingCannotCombineAllWithIndividualBindingsException("Exchange"));
                ExchangeValidator exchangeValidator = this.CreateExchangeValidator(true, "Validating ExchangeBinding");
                multiValuedProperty = exchangeValidator.ValidateRecipients(this.ExchangeBinding);
                if (this.ExchangeBinding.Count <string>() > 1000)
                {
                    throw new BindingCountExceedsLimitException("Exchange", 1000);
                }
            }
            NewCompliancePolicyBase.SetBindingsSubWorkload(multiValuedProperty, Workload.Exchange);
            return(multiValuedProperty);
        }
        private void ValidateRecipientsForRemove()
        {
            base.WriteVerbose(Strings.VerboseValidatingRemoveExchangeBinding);
            ExchangeValidator exchangeValidator = this.CreateExchangeValidator(false, "Validating RemoveExchangeBinding", 0);

            this.InternalRemoveExchangeBindings = exchangeValidator.ValidateRecipients(this.RemoveExchangeBinding);
        }
        private void ExpandGroupsAndValidateRecipientsForAdd()
        {
            base.WriteVerbose(Strings.VerboseValidatingAddExchangeBinding);
            int existingRecipientsCount         = SetCompliancePolicyBase.CalculateBindingCountAfterRemove(this.PsPolicyPresentationObject.ExchangeBinding, this.InternalRemoveExchangeBindings);
            ExchangeValidator exchangeValidator = this.CreateExchangeValidator(true, "Validating AddExchangeBinding", existingRecipientsCount);

            this.InternalAddExchangeBindings = exchangeValidator.ValidateRecipients(this.AddExchangeBinding);
        }
        public void OverlapsRangeFails()
        {
            // Assert
            var start    = new DateTime(1999, 1, 1);
            var finish   = new DateTime(2020, 12, 31);
            var validity = new DateRange(start, finish);
            var system   = new MDM.SourceSystem {
                Name = "Test"
            };
            var exchangeMapping = new PartyRoleMapping {
                System = system, MappingValue = "1", Validity = validity
            };

            var list = new List <PartyRoleMapping> {
                exchangeMapping
            };
            var repository = new Mock <IRepository>();

            repository.Setup(x => x.Queryable <PartyRoleMapping>()).Returns(list.AsQueryable());

            var systemList       = new List <MDM.SourceSystem>();
            var systemRepository = new Mock <IRepository>();

            systemRepository.Setup(x => x.Queryable <MDM.SourceSystem>()).Returns(systemList.AsQueryable());

            var overlapsRangeIdentifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate  = start.AddHours(10),
                EndDate    = start.AddHours(15)
            };

            var identifierValidator = new NexusIdValidator <PartyRoleMapping>(repository.Object);
            var validatorEngine     = new Mock <IValidatorEngine>();

            validatorEngine.Setup(x => x.IsValid(It.IsAny <EnergyTrading.Mdm.Contracts.MdmId>(), It.IsAny <IList <IRule> >()))
            .Returns((EnergyTrading.Mdm.Contracts.MdmId x, IList <IRule> y) => identifierValidator.IsValid(x, y));
            var validator = new ExchangeValidator(validatorEngine.Object, repository.Object);

            var exchange = new Exchange {
                Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList {
                    overlapsRangeIdentifier
                }
            };

            // Act
            var violations = new List <IRule>();
            var result     = validator.IsValid(exchange, violations);

            // Assert
            Assert.IsFalse(result, "Validator succeeded");
        }
        public void ValidExchangePasses()
        {
            // Assert
            var start  = new DateTime(1999, 1, 1);
            var system = new MDM.SourceSystem {
                Name = "Test"
            };

            var systemList = new List <MDM.SourceSystem> {
                system
            };
            var systemRepository = new Mock <IRepository>();
            var repository       = new StubValidatorRepository();

            systemRepository.Setup(x => x.Queryable <MDM.SourceSystem>()).Returns(systemList.AsQueryable());

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate  = start.AddHours(-10),
                EndDate    = start.AddHours(-5)
            };

            var validatorEngine = new Mock <IValidatorEngine>();
            var validator       = new ExchangeValidator(validatorEngine.Object, repository);

            var exchange = new Exchange {
                Details = new EnergyTrading.MDM.Contracts.Sample.ExchangeDetails {
                    Name = "Test"
                }, Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList {
                    identifier
                }
            };

            this.AddRelatedEntities(exchange);

            // Act
            var violations = new List <IRule>();
            var result     = validator.IsValid(exchange, violations);

            // Assert
            Assert.IsTrue(result, "Validator failed");
            Assert.AreEqual(0, violations.Count, "Violation count differs");
        }
        public void BadSystemFails()
        {
            // Assert
            var start = new DateTime(1999, 1, 1);
            var finish = new DateTime(2020, 12, 31);
            var validity = new DateRange(start, finish);
            var system = new MDM.SourceSystem { Name = "Test" };
            var exchangeMapping = new PartyRoleMapping { System = system, MappingValue = "1", Validity = validity };

            var list = new List<PartyRoleMapping> { exchangeMapping };
            var repository = new Mock<IRepository>();
            repository.Setup(x => x.Queryable<PartyRoleMapping>()).Returns(list.AsQueryable());

            var badSystemIdentifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Jim",
                Identifier = "1",
                StartDate = start.AddHours(-10),
                EndDate = start.AddHours(-5)
            };

            var identifierValidator = new NexusIdValidator<PartyRoleMapping>(repository.Object);
            var validatorEngine = new Mock<IValidatorEngine>();
            validatorEngine.Setup(x => x.IsValid(It.IsAny<EnergyTrading.Mdm.Contracts.MdmId>(), It.IsAny<IList<IRule>>()))
                           .Returns((EnergyTrading.Mdm.Contracts.MdmId x, IList<IRule> y) => identifierValidator.IsValid(x, y));
            var validator = new ExchangeValidator(validatorEngine.Object, repository.Object);

            var exchange = new Exchange { Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList { badSystemIdentifier } };

            // Act
            var violations = new List<IRule>();
            var result = validator.IsValid(exchange, violations);

            // Assert
            Assert.IsFalse(result, "Validator succeeded");
        }
        public void ValidExchangePasses()
        {
            // Assert
            var start = new DateTime(1999, 1, 1);
            var system = new MDM.SourceSystem { Name = "Test" };

            var systemList = new List<MDM.SourceSystem> { system };
            var systemRepository = new Mock<IRepository>();
            var repository = new StubValidatorRepository();

            systemRepository.Setup(x => x.Queryable<MDM.SourceSystem>()).Returns(systemList.AsQueryable());

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate = start.AddHours(-10),
                EndDate = start.AddHours(-5)
            };

            var validatorEngine = new Mock<IValidatorEngine>();
            var validator = new ExchangeValidator(validatorEngine.Object, repository);

            var exchange = new Exchange { Details = new EnergyTrading.MDM.Contracts.Sample.ExchangeDetails{Name = "Test"}, Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList { identifier } };
            this.AddRelatedEntities(exchange);

            // Act
            var violations = new List<IRule>();
            var result = validator.IsValid(exchange, violations);

            // Assert
            Assert.IsTrue(result, "Validator failed");
            Assert.AreEqual(0, violations.Count, "Violation count differs");
        }
 private ExchangeValidator CreateExchangeValidator(bool allowGroups, string logTag)
 {
     return(ExchangeValidator.Create((IConfigurationSession)base.DataSession, (RecipientIdParameter recipientId, IRecipientSession recipientSession) => base.GetDataObject <ReducedRecipient>(recipientId, recipientSession, null, new LocalizedString?(Strings.ErrorUserObjectNotFound(recipientId.RawIdentity)), new LocalizedString?(Strings.ErrorUserObjectAmbiguous(recipientId.RawIdentity))) as ReducedRecipient, new Task.TaskErrorLoggingDelegate(base.WriteError), new Action <LocalizedString>(this.WriteWarning), (LocalizedString locString) => this.Force || base.ShouldContinue(locString), allowGroups, logTag, SourceValidator.Clients.NewCompliancePolicy, 0, this.executionLogger));
 }