예제 #1
0
        public void IsSameRule_ArchitecturalRule_Success()
        {
            var rule = new ArchitecturalRule()
            {
                OriginLayer   = "Origin",
                TargetLayer   = "Target",
                RuleOperator  = RuleOperator.CanNotRelate,
                RelationTypes = new List <RelationType>()
                {
                    RelationType.Declarate, RelationType.Indirect
                }
            };

            var anotherRule = new ArchitecturalRule()
            {
                OriginLayer   = "Origin",
                TargetLayer   = "Target",
                RuleOperator  = RuleOperator.CanNotRelate,
                RelationTypes = new List <RelationType>()
                {
                    RelationType.Declarate, RelationType.Indirect
                }
            };

            Assert.True(rule.IsSameRule(anotherRule));
        }
예제 #2
0
        public void CheckIfItsValid_ArchitecturalRule_Error_InvalidRule()
        {
            var rules = new ArchitecturalRule[3]
            {
                new ArchitecturalRule(),
                new ArchitecturalRule()
                {
                    OriginLayer = "Origin"
                },
                new ArchitecturalRule()
                {
                    OriginLayer = "Origin",
                    TargetLayer = "Target"
                }
            };

            foreach (var rule in rules)
            {
                var result = Assert.Throws <ConstraintsException>(() =>
                {
                    rule.CheckIfItsValid();
                });

                Assert.Equal(ConstraintsError.InvalidRule.Key, result.Key);
            }
        }
        public void GetViolatingOccurrences_Success_CanNotRelate()
        {
            var architectureRule = new ArchitecturalRule()
            {
                OriginLayer  = "Origin",
                TargetLayer  = "Target",
                RuleOperator = RuleOperator.CanNotRelate
            };

            var targetNamespace     = "Test.Target";
            var targetStructureName = "TargetTestStructure";

            var originStructure = new Structure()
            {
                Name       = "OriginTestStructure",
                Type       = StructureType.Class,
                Namespace  = "Test.Origin",
                References = new List <string>()
                {
                    { targetNamespace }
                },
                Relations = new List <Relation>()
                {
                    new Relation(RelationType.Inheritance,
                                 targetNamespace, true, targetStructureName)
                }
            };

            var layersNamespaces = new Dictionary <string, IEnumerable <string> >()
            {
                { "Origin", new List <string>()
                  {
                      originStructure.Namespace
                  } },
                { "Target", new List <string>()
                  {
                      targetNamespace
                  } }
            };

            var structures = new List <Structure>()
            {
                originStructure,
                new Structure()
                {
                    Name      = targetStructureName,
                    Type      = StructureType.Class,
                    Namespace = targetNamespace
                }
            };

            var violatingOccurrences = ArchitecturalRuleHelper.GetViolatingOccurrences(
                architectureRule, layersNamespaces, structures);

            AssertViolatingOccurrences(violatingOccurrences, originStructure,
                                       $"{targetNamespace}.{targetStructureName}");
        }
예제 #4
0
        public void CheckIfItsValid_ArchitecturalRule_Error_NullReference()
        {
            ArchitecturalRule rule = null;

            var result = Record.Exception(() =>
            {
                rule.CheckIfItsValid();
            });

            Assert.Null(result);
        }
        public void GetViolatingOccurrences_Success_NeedToRelate()
        {
            var architectureRule = new ArchitecturalRule()
            {
                OriginLayer  = "Origin",
                TargetLayer  = "Target",
                RuleOperator = RuleOperator.NeedToRelate
            };

            var targetNamespace     = "Test.Target";
            var targetStructureName = "TargetTestStructure";

            var originStructure = new Structure()
            {
                Name      = "OriginTestStructure",
                Type      = StructureType.Class,
                Namespace = "Test.Origin"
            };

            var layersNamespaces = new Dictionary <string, IEnumerable <string> >()
            {
                { "Origin", new List <string>()
                  {
                      originStructure.Namespace
                  } },
                { "Target", new List <string>()
                  {
                      targetNamespace
                  } }
            };

            var structures = new List <Structure>()
            {
                originStructure,
                new Structure()
                {
                    Name      = targetStructureName,
                    Type      = StructureType.Class,
                    Namespace = targetNamespace
                }
            };

            var violatingOccurrences = ArchitecturalRuleHelper.GetViolatingOccurrences(
                architectureRule, layersNamespaces, structures);

            Assert.NotNull(violatingOccurrences);
            Assert.Single(violatingOccurrences);

            Assert.Equal($"{originStructure.Namespace}.{originStructure.Name}",
                         violatingOccurrences.Single().Structure);
            Assert.Empty(violatingOccurrences.Single().NonConformingRelations);
        }
예제 #6
0
        public void IsSameRule_ArchitecturalRule_Success_DifferentOriginLayer()
        {
            var rule = new ArchitecturalRule()
            {
                OriginLayer = "Origin"
            };

            var anotherRule = new ArchitecturalRule()
            {
                OriginLayer = "Test"
            };

            Assert.False(rule.IsSameRule(anotherRule));
        }
        public static IEnumerable <ArchitecturalViolationOccurrence> GetViolatingOccurrences(
            ArchitecturalRule rule,
            IDictionary <string, IEnumerable <string> > layersNamespaces,
            IEnumerable <Structure> structures)
        {
            if (rule == null)
            {
                return(Enumerable.Empty <ArchitecturalViolationOccurrence>());
            }

            rule.CheckIfItsValid();

            var originNamespaces = layersNamespaces[rule.OriginLayer];
            var targetNamespaces = layersNamespaces[rule.TargetLayer];

            var originStructures = structures
                                   .Where(s => originNamespaces.Any(n => n.Equals(s.Namespace)));

            if (rule.RuleOperator == RuleOperator.NeedToRelate)
            {
                return(GetNeedToRelateViolatingOccurrences(
                           targetNamespaces, originStructures, rule.RelationTypes));
            }
            else if (rule.RuleOperator == RuleOperator.CanNotRelate)
            {
                return(GetCanNotRelateViolatingOccurrences(
                           targetNamespaces, originStructures, rule.RelationTypes));
            }
            else
            {
                var anotherStructures = structures
                                        .Where(s => !originNamespaces.Any(n => n.Equals(s.Namespace)));

                if (rule.RuleOperator == RuleOperator.OnlyNeedToRelate)
                {
                    return(GetOnlyNeedToRelateViolatingOccurrences(
                               targetNamespaces, originStructures,
                               anotherStructures, rule.RelationTypes));
                }
                else if (rule.RuleOperator == RuleOperator.OnlyCanRelate)
                {
                    return(GetOnlyCanRelateViolatingOccurrences(
                               targetNamespaces, anotherStructures, rule.RelationTypes));
                }
            }

            return(Enumerable.Empty <ArchitecturalViolationOccurrence>());
        }
        public void GetViolatingOccurrences_Success_OnlyCanRelate_Empty()
        {
            var architectureRule = new ArchitecturalRule()
            {
                OriginLayer  = "Origin",
                TargetLayer  = "Target",
                RuleOperator = RuleOperator.OnlyCanRelate
            };

            var targetNamespace = "Test.Target";

            var layersNamespaces = new Dictionary <string, IEnumerable <string> >()
            {
                { "Origin", new List <string>()
                  {
                      "Test.Origin"
                  } },
                { "Target", new List <string>()
                  {
                      targetNamespace
                  } }
            };

            var structures = new List <Structure>()
            {
                new Structure()
                {
                    Name       = "TargetStructure",
                    Type       = StructureType.Class,
                    Namespace  = targetNamespace,
                    References = new List <string>()
                    {
                        { targetNamespace }
                    },
                    Relations = new List <Relation>()
                    {
                        new Relation(RelationType.Inheritance,
                                     targetNamespace, true, "AnotherTargetStructure")
                    }
                }
            };

            var violatingOccurrences = ArchitecturalRuleHelper.GetViolatingOccurrences(
                architectureRule, layersNamespaces, structures);

            Assert.NotNull(violatingOccurrences);
            Assert.Empty(violatingOccurrences);
        }
예제 #9
0
        public void CheckIfItsValid_ArchitecturalRule_Success()
        {
            var rule = new ArchitecturalRule()
            {
                OriginLayer  = "Origin",
                TargetLayer  = "Target",
                RuleOperator = RuleOperator.OnlyNeedToRelate
            };

            var result = Record.Exception(() =>
            {
                rule.CheckIfItsValid();
            });

            Assert.Null(result);
        }
        public static void CheckIfItsValid(
            this ArchitecturalRule rule)
        {
            if (rule != null)
            {
                var invalidRule = string.IsNullOrEmpty(rule.OriginLayer) ||
                                  string.IsNullOrEmpty(rule.TargetLayer) ||
                                  rule.RuleOperator == default(RuleOperator);

                if (invalidRule)
                {
                    throw new ConstraintsException(
                              ConstraintsError.InvalidRule);
                }
            }
        }
예제 #11
0
        public void IsSameRule_ArchitecturalRule_Success_DifferentRuleOperator()
        {
            var rule = new ArchitecturalRule()
            {
                OriginLayer  = "Origin",
                TargetLayer  = "Target",
                RuleOperator = RuleOperator.CanNotRelate
            };

            var anotherRule = new ArchitecturalRule()
            {
                OriginLayer  = "Origin",
                TargetLayer  = "Target",
                RuleOperator = RuleOperator.NeedToRelate
            };

            Assert.False(rule.IsSameRule(anotherRule));
        }
예제 #12
0
        public void IsSameRule_ArchitecturalRule_Success_DifferentRelationTypesCount()
        {
            var rule = new ArchitecturalRule()
            {
                RelationTypes = new List <RelationType>()
                {
                    RelationType.Declarate
                }
            };

            var anotherRule = new ArchitecturalRule()
            {
                RelationTypes = new List <RelationType>()
                {
                    RelationType.Declarate, RelationType.Inheritance
                }
            };

            Assert.False(rule.IsSameRule(anotherRule));
        }
        public static bool IsSameRule(this ArchitecturalRule mainRule,
                                      ArchitecturalRule targetRule)
        {
            if (mainRule == null && targetRule == null)
            {
                return(true);
            }

            if (mainRule == null || targetRule == null)
            {
                return(false);
            }

            if (mainRule.RelationTypes.Count() != targetRule.RelationTypes.Count())
            {
                return(false);
            }

            return(TextEquals(mainRule.OriginLayer, targetRule.OriginLayer) &&
                   TextEquals(mainRule.TargetLayer, targetRule.TargetLayer) &&
                   mainRule.RuleOperator == targetRule.RuleOperator &&
                   !mainRule.RelationTypes
                   .Any(rt => !targetRule.RelationTypes.Any(rtt => rtt == rt)));
        }
예제 #14
0
        public void IsSameRule_ArchitecturalRule_Success_NullRule()
        {
            var rule = new ArchitecturalRule();

            Assert.False(rule.IsSameRule(null));
        }
        public void GetViolatingOccurrences_Success_Empty()
        {
            var architecturalRules = new ArchitecturalRule[]
            {
                new ArchitecturalRule()
                {
                    OriginLayer   = "X",
                    TargetLayer   = "Y",
                    RuleOperator  = RuleOperator.CanNotRelate,
                    RelationTypes = new List <RelationType>()
                    {
                        RelationType.Declarate
                    }
                },
                new ArchitecturalRule()
                {
                    OriginLayer   = "X",
                    TargetLayer   = "Z",
                    RuleOperator  = RuleOperator.OnlyCanRelate,
                    RelationTypes = new List <RelationType>()
                    {
                        RelationType.Inheritance
                    }
                },
                new ArchitecturalRule()
                {
                    OriginLayer   = "W",
                    TargetLayer   = "Z",
                    RuleOperator  = RuleOperator.OnlyNeedToRelate,
                    RelationTypes = new List <RelationType>()
                    {
                        RelationType.ReceiptByConstructorArgument
                    }
                }
            };

            var structures = new List <Structure>()
            {
                new Structure()
                {
                    Name      = "XStructure",
                    Type      = StructureType.Class,
                    Namespace = "X",
                    Relations = new List <Relation>()
                    {
                        new Relation(RelationType.Inheritance,
                                     "Y", true, "YStructure")
                    }
                },
                new Structure()
                {
                    Name      = "YStructure",
                    Type      = StructureType.Class,
                    Namespace = "Y",
                    Relations = new List <Relation>()
                    {
                        new Relation(RelationType.Indirect,
                                     "Z", true, "ZStructure")
                    }
                }
            };

            var layersNamespaces = new Dictionary <string, IEnumerable <string> >()
            {
                { "X", new List <string>()
                  {
                      "X"
                  } },
                { "Y", new List <string>()
                  {
                      "Y"
                  } },
                { "W", new List <string>()
                  {
                      "W"
                  } },
                { "Z", new List <string>()
                  {
                      "Z"
                  } }
            };

            foreach (var rule in architecturalRules)
            {
                var violatingOccurrences = ArchitecturalRuleHelper
                                           .GetViolatingOccurrences(rule, layersNamespaces, structures);

                Assert.Empty(violatingOccurrences);
            }
        }