Exemplo n.º 1
0
        public void CanIdentifyNullablePropWithCanBeBullSetWithConstant()
        {
            var jobs = new List <JobWithNull>
            {
                new JobWithNull {
                    Capacity = null
                }
            };
            var validationResults = new SimpleRulesEngine()
                                    .Validate <JobWithNull>(jobs);

            Assert.AreEqual(1, validationResults.Count());
            Assert.AreEqual(0, validationResults.First().Errors.Count());
            jobs = new List <JobWithNull>
            {
                new JobWithNull {
                    Capacity = 25, MaxCapacity = 15
                }
            };
            Assert.AreEqual(1, validationResults.Count());
            Assert.AreEqual(0, validationResults.First().Errors.Count());
            jobs = new List <JobWithNull>
            {
                new JobWithNull {
                    Capacity = 40, MaxCapacity = 45
                }
            };
            Assert.AreEqual(1, validationResults.Count());
            Assert.AreEqual(0, validationResults.First().Errors.Count());
        }
Exemplo n.º 2
0
        public void CanGenerateRegexRules()
        {
            var codes = new List <Code>
            {
                new Code {
                    Value = "abc"
                }
            };
            var validationResults = new SimpleRulesEngine()
                                    .Validate <Code>(codes);

            Assert.AreEqual(1, validationResults.Count());
            Assert.AreEqual(1, validationResults.First().Errors.Count());
            Assert.AreEqual("Value does not match the expected format", validationResults.First().Errors[0]);
            codes = new List <Code>
            {
                new Code {
                    Value = "123"
                }
            };
            validationResults = new SimpleRulesEngine()
                                .Validate <Code>(codes);
            Assert.AreEqual(1, validationResults.Count());
            Assert.AreEqual(0, validationResults.First().Errors.Count());
        }
Exemplo n.º 3
0
        public void CanGenerateMessagesconsideringNullAndConstantStatus()
        {
            var engine = new SimpleRulesEngine();
            var jobs   = new List <JobWithNull>
            {
                new JobWithNull {
                    MaxCapacity = 15, Capacity = 40
                }
            };
            var validationResults = engine.Validate <JobWithNull>(jobs);

            Assert.AreEqual(1, validationResults.Count());
            Assert.AreEqual("Capacity should be Less Than the Max Capacity. Or Capacity can be Less Than 30. It can also be null.", validationResults.First().Errors[0]);
            var jobsWithConst = new List <JobWithOnlyConstant>
            {
                new JobWithOnlyConstant {
                    MaxCapacity = 15, Capacity = 40
                }
            };
            var validationResultsConst = engine.Validate <JobWithOnlyConstant>(jobsWithConst);

            Assert.AreEqual(1, validationResultsConst.Count());
            Assert.AreEqual("Capacity should be Less Than the Max Capacity. Or Capacity can be Less Than 30.", validationResultsConst.First().Errors[0]);
            var jobsWithNull = new List <JobWithOnlyNull>
            {
                new JobWithOnlyNull {
                    MaxCapacity = 15, Capacity = 40
                }
            };
            var validationResultsNull = engine.Validate <JobWithOnlyNull>(jobsWithNull);

            Assert.AreEqual(1, validationResultsNull.Count());
            Assert.AreEqual("Capacity should be Less Than the Max Capacity. It can also be null.", validationResultsNull.First().Errors[0]);
        }
Exemplo n.º 4
0
 public void CanIdentifyNotNullablePropWithCanBeNullSetWithConstant()
 {
     var jobs = new List <JobWithoutNull>
     {
         new JobWithoutNull {
             Capacity = 10
         }
     };
     var validationResultsEnumerator = new SimpleRulesEngine()
                                       .Validate <JobWithoutNull>(jobs);
     var validationResults = validationResultsEnumerator.ToList();
 }
Exemplo n.º 5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            var simpleRulesEngine = new SimpleRulesEngine()
                                    .DiscoverHandlers(new [] { typeof(Startup) })
                                    .RegisterMetadata <Registration, RegistrationMetadata>()
                                    .RegisterMetadata <Activity, ActivityMetadata>();

            services.AddSingleton(typeof(SimpleRulesEngine), simpleRulesEngine);

            services.AddMvc();
        }
Exemplo n.º 6
0
        public void CanThrowExceptionForDuplicateMetdataRegistration()
        {
            var users = new List <User>
            {
                new User {
                    Id = 1001, RegistrationDate = DateTime.Now, LastLoginDate = DateTime.Now.AddDays(-1)
                }
            };
            var results = new SimpleRulesEngine()
                          .RegisterMetadata <User, UserMetadata>()
                          .RegisterMetadata <User, UserMetadata>()
                          .Validate <User>(users);

            Assert.IsNotNull(results);
            Assert.AreEqual(1, results.Count());
        }
Exemplo n.º 7
0
        public void CanIdentifyCustomHandlersUsingDiscovery()
        {
            var activities = new List <Activity>
            {
                new Activity {
                    Id = 1, StartDate = DateTime.Parse("04/01/2017"), EndDate = DateTime.Parse("03/30/2017"), Capacity = 45, Name = "Cricket"
                }
            };
            var validationResults = new SimpleRulesEngine()
                                    .DiscoverHandlers(new[] { typeof(Marker) })
                                    .Validate <Activity>(activities);

            Assert.AreEqual(1, validationResults.Count());
            var validationResult = validationResults.First();

            Assert.AreEqual(2, validationResult.Errors.Count);
            Assert.AreEqual("Capacity should be between 10 and 30", validationResult.Errors[1]);
            Assert.AreEqual("End Date should be Greater Than the Start Date.", validationResult.Errors[0]);
        }
Exemplo n.º 8
0
        public void CanGenerateSimpleRuleWithWarnings()
        {
            var users = new List <User>
            {
                new User {
                    Id = 1001, RegistrationDate = DateTime.Now, LastLoginDate = DateTime.Now.AddDays(-1)
                }
            };
            var results = new SimpleRulesEngine()
                          .RegisterMetadata <User, UserMetadataWithWarning>()
                          .Validate <User>(users);

            Assert.IsNotNull(results);
            Assert.AreEqual(1, results.Count());
            var first = results.First();

            Assert.AreEqual(1001, first.Key);
            Assert.AreEqual(1, first.Warnings.Count);
            Assert.AreEqual("Last Login Date should be Greater Than the Registration Date.", first.Warnings[0]);
        }
Exemplo n.º 9
0
        public void CanIdentifyNullableOtherProperty()
        {
            var students = new List <Student>
            {
                new Student
                {
                    StartDate   = DateTime.Now.AddDays(-5),
                    EndDate     = DateTime.Now,
                    DateOfBirth = DateTime.Now.AddDays(1)
                }
            };
            var validationResults = new SimpleRulesEngine()
                                    .Validate <Student>(students);

            Assert.AreEqual(1, validationResults.Count());
            var first = validationResults.First();

            Assert.AreEqual(1, first.Errors.Count());
            Assert.AreEqual("Date Of Birth should be Less Than the End Date.", first.Errors[0]);
        }
Exemplo n.º 10
0
 // see Startup.cs - ConfigureServices
 public HomeController(SimpleRulesEngine rulesEngine)
 {
     _rulesEngine = rulesEngine;
 }