public void PolicyScopeMatchesCollectionNameFromContextWithMultipleCollection()
        {
            Policy p = new Policy();
            p.Scope = new[] { new CollectionScope() { CollectionNames = new[] { "DefaultCollection", "OtherCollection" } } };

            var context = Substitute.For<IRequestContext>();
            context.CollectionName.Returns("DefaultCollection");

            var notification = Substitute.For<INotification>();
            notification.ProjectUri.Returns("http://localhost:8080/tfs/defaultcollection/TestOne");

            bool result = p.Scope.All(s => s.Matches(context, notification));

            Assert.IsTrue(result);
        }
        public void PolicyScopeMatchesWildcardProjectFromContext()
        {
            Policy p = new Policy();
            p.Scope = new[] { new ProjectScope() { ProjectNames = new[] { "*" } } };

            var context = Substitute.For<IRequestContext>();
            context.GetProjectName(new Uri("http://localhost:8080/tfs/defaultcollection/TestOne")).Returns("TestOne");

            var notification = Substitute.For<INotification>();
            notification.ProjectUri.Returns("http://localhost:8080/tfs/defaultcollection/TestOne");

            bool result = p.Scope.All(s => s.Matches(context, notification).Success);

            Assert.IsTrue(result);
        }
        public void PolicyMismatchingProjectAndCollectionScopeDoesNotMatches()
        {
            Policy p = new Policy();
            p.Scope = new PolicyScope[]
                      {
                          new CollectionScope() { CollectionNames = new[] { "DefaultCollection", "OtherCollection" } },
                          new ProjectScope() { ProjectNames = new[] { "TestTwo" } }
                      };

            var context = Substitute.For<IRequestContext>();
            context.CollectionName.Returns("DefaultCollection");
            context.GetProjectName(new Uri("http://localhost:8080/tfs/defaultcollection/TestOne")).Returns("TestOne");

            var notification = Substitute.For<INotification>();
            notification.ProjectUri.Returns("http://localhost:8080/tfs/defaultcollection/TestOne");

            bool result = p.Scope.All(s => s.Matches(context, notification));

            Assert.IsFalse(result);
        }
            private List<Policy> ParsePoliciesSection(XDocument doc, Dictionary<string, Rule> rules)
            {
                var policies = new List<Policy>();
                foreach (var policyElem in doc.Root.Elements("policy"))
                {
                    var policy = new Policy()
                    {
                        Name = policyElem.Attribute("name").Value,
                    };

                    List<PolicyScope> scope = new List<PolicyScope>();

                    // trick to return string.Empty in case of missing attribute
                    var nullAttribute = new XAttribute("empty", string.Empty);

                    foreach (var element in policyElem.Elements())
                    {
                        switch (element.Name.LocalName)
                        {
                            case "collectionScope":
                                {
                                    var collections = new List<string>();
                                    collections.AddRange((element.Attribute("collections") ?? nullAttribute).Value.Split(ListSeparators));
                                    scope.Add(new CollectionScope() { CollectionNames = collections });
                                    break;
                                }

                            case "templateScope":
                                {
                                    string templateName = (element.Attribute("name") ?? nullAttribute).Value;
                                    string templateId = (element.Attribute("typeId") ?? nullAttribute).Value;
                                    string minVersion = (element.Attribute("minVersion") ?? nullAttribute).Value;
                                    string maxVersion = (element.Attribute("maxVersion") ?? nullAttribute).Value;

                                    // check for proper attribute combo (cannot be done in XSD)
                                    if (string.IsNullOrWhiteSpace(templateName) && string.IsNullOrWhiteSpace(templateId))
                                    {
                                        this.logger.TemplateScopeConfigurationRequiresAtLeastNameOrType();
                                    }
                                    else
                                    {
                                        scope.Add(new TemplateScope()
                                        {
                                            TemplateName = templateName,
                                            TemplateTypeId = templateId,
                                            MinVersion = minVersion,
                                            MaxVersion = maxVersion
                                        });
                                    }

                                    break;
                                }

                            case "projectScope":
                                {
                                    var projects = new List<string>();
                                    projects.AddRange((element.Attribute("projects") ?? nullAttribute).Value.Split(ListSeparators));
                                    scope.Add(new ProjectScope() { ProjectNames = projects });
                                    break;
                                }

                            default:
                                {
                                    // Ignore other rules for now.
                                    break;
                                }
                        }
                    }

                    policy.Scope = scope;

                    var referredRules = new List<Rule>();
                    foreach (var ruleRefElem in policyElem.Elements("ruleRef"))
                    {
                        string refName = ruleRefElem.Attribute("name").Value;
                        var rule = rules[refName];
                        referredRules.Add(rule);
                    }

                    policy.Rules = referredRules;

                    policies.Add(policy);
                }

                return policies;
            }
        public void PolicyTemplateVersionAndNameMatchTogetherOnly()
        {
            Policy p = new Policy();
            p.Scope = new[]
            {
                new TemplateScope()
                {
                    TemplateName = "Scrum",
                    MinVersion = "5.0",
                    MaxVersion = "5.5"
                }
            };

            var context = Substitute.For<IRequestContext>();
            context.CollectionName.Returns("DefaultCollection");
            context.GetProjectName(new Uri("http://localhost:8080/tfs/defaultcollection/TestOne")).Returns("TestOne");
            context.GetCurrentProjectProcessVersion(new Uri("http://localhost:8080/tfs/defaultcollection/TestOne"))
                .Returns(new ProcessTemplateVersionWrapper(Guid.Empty, 5, 3));
            context.GetProjectProperties(new Uri("http://localhost:8080/tfs/defaultcollection/TestOne"))
                .Returns(
                    new IProjectProperty[]
                                {
                                    new ProjectPropertyWrapper()
                                    {
                                        Name = "Process Template",
                                        Value = "Scrum"
                                    }
                                });

            var notification = Substitute.For<INotification>();
            notification.ProjectUri.Returns("http://localhost:8080/tfs/defaultcollection/TestOne");

            bool result = p.Scope.All(s => s.Matches(context, notification).Success);

            Assert.IsTrue(result);
        }
        public void PolicyTemplateMaxVersionDoesNotMatchesWhenCurrentIsHigher()
        {
            Policy p = new Policy();
            p.Scope = new[]
            {
                new TemplateScope() { MinVersion = "5.0" }
            };

            var context = Substitute.For<IRequestContext>();
            context.CollectionName.Returns("DefaultCollection");
            context.GetProjectName(new Uri("http://localhost:8080/tfs/defaultcollection/TestOne")).Returns("TestOne");
            context.GetCurrentProjectProcessVersion(new Uri("http://localhost:8080/tfs/defaultcollection/TestOne"))
                .Returns(new ProcessTemplateVersionWrapper(Guid.Empty, 3, 0));

            var notification = Substitute.For<INotification>();
            notification.ProjectUri.Returns("http://localhost:8080/tfs/defaultcollection/TestOne");

            bool result = p.Scope.All(s => s.Matches(context, notification).Success);

            Assert.IsFalse(result);
        }
        public void PolicyTemplateNameMatches()
        {
            Policy p = new Policy();
            p.Scope = new[]
            {
                new TemplateScope() { TemplateName = "Scrum" }
            };

            var context = Substitute.For<IRequestContext>();
            context.CollectionName.Returns("DefaultCollection");
            context.GetProjectName(new Uri("http://localhost:8080/tfs/defaultcollection/TestOne")).Returns("TestOne");
            context.GetProjectProperties(new Uri("http://localhost:8080/tfs/defaultcollection/TestOne"))
                .Returns(
                    new IProjectPropertyWrapper[]
                    {
                        new ProjectPropertyWrapper()
                        {
                            Name = "Process Template",
                            Value = "Scrum"
                        }
                    });

            var notification = Substitute.For<INotification>();
            notification.ProjectUri.Returns("http://localhost:8080/tfs/defaultcollection/TestOne");

            bool result = p.Scope.All(s => s.Matches(context, notification));

            Assert.IsTrue(result);
        }
        private static List<Policy> ParsePoliciesSection(XDocument doc, Dictionary<string, Rule> rules, Dictionary<string, bool> ruleInUse)
        {
            var policies = new List<Policy>();
            foreach (var policyElem in doc.Root.Elements("policy"))
            {
                var policy = new Policy()
                {
                    Name = policyElem.Attribute("name").Value,
                };

                List<PolicyScope> scope = new List<PolicyScope>();
                var nullAttribute = new XAttribute("empty", string.Empty);

                foreach (var element in policyElem.Elements())
                {
                    switch (element.Name.LocalName)
                    {
                        case "collectionScope":
                        {
                            var collections = new List<string>();
                            collections.AddRange((element.Attribute("collections") ?? nullAttribute).Value.Split(ListSeparators));
                            scope.Add(new CollectionScope() { CollectionNames = collections });
                            break;
                        }

                        case "templateScope":
                        {
                            string templateName = (element.Attribute("name") ?? nullAttribute).Value;
                            string templateId = (element.Attribute("typeId") ?? nullAttribute).Value;
                            string minVersion = (element.Attribute("minVersion") ?? nullAttribute).Value;
                            string maxVersion = (element.Attribute("maxVersion") ?? nullAttribute).Value;

                            scope.Add(new TemplateScope()
                            {
                                TemplateName = templateName,
                                TemplateTypeId = templateId,
                                MinVersion = minVersion,
                                MaxVersion = maxVersion
                            });
                            break;
                        }

                        case "projectScope":
                        {
                            var projects = new List<string>();
                            projects.AddRange((element.Attribute("projects") ?? nullAttribute).Value.Split(ListSeparators));
                            scope.Add(new ProjectScope() { ProjectNames = projects });
                            break;
                        }

                        default:
                        {
                            // Ignore other rules for now.
                            break;
                        }
                    }
                }

                policy.Scope = scope;

                var referredRules = new List<Rule>();
                foreach (var ruleRefElem in policyElem.Elements("ruleRef"))
                {
                    string refName = ruleRefElem.Attribute("name").Value;
                    var rule = rules[refName];
                    referredRules.Add(rule);

                    ruleInUse[refName] = true;
                }

                policy.Rules = referredRules;

                policies.Add(policy);
            }

            return policies;
        }