Exemplo n.º 1
0
        public void TestConstructor_NullErrors()
        {
            var isValid  = true;
            var instance = new SimpleValidationResult(isValid, null);

            Assert.AreEqual(isValid, instance.IsValid);
            Assert.IsNotNull(instance.Errors);
            Assert.AreEqual(0, instance.Errors.Count());
        }
Exemplo n.º 2
0
        public void TestConstructor_ValidationResult_IsValid()
        {
            var validationFailures = new List <ValidationFailure>();
            var validationResult   = new ValidationResult(validationFailures);

            Assert.IsTrue(validationResult.IsValid);

            var instance = new SimpleValidationResult(validationResult);

            Assert.IsTrue(instance.IsValid);
            Assert.AreEqual(0, instance.Errors.Count());
        }
Exemplo n.º 3
0
        public void TestConstructor()
        {
            var isValid = true;
            var errors  = new List <SimpleValidationFailure>();

            errors.Add(new SimpleValidationFailure(new ErrorPath(), "C", "Property"));
            errors.Add(new SimpleValidationFailure(new ErrorPath(), "A", "Property"));
            errors.Add(new SimpleValidationFailure(new ErrorPath(), "B", "Property"));
            var instance = new SimpleValidationResult(isValid, errors);

            Assert.AreEqual(isValid, instance.IsValid);
            Assert.AreEqual(errors.Count, instance.Errors.Count());
            CollectionAssert.AreEqual(errors.OrderBy(x => x.ErrorMessage).ToList(), instance.Errors.ToList());
            CollectionAssert.AreNotEqual(errors.ToList(), instance.Errors.ToList());
        }
        private string GetSevisValidationResultAsJson(ValidationResult result)
        {
            SimpleValidationResult simpleValidationResult = null;

            if (result.IsValid)
            {
                simpleValidationResult = new SuccessfulValidationResult();
            }
            else
            {
                simpleValidationResult = new SimpleValidationResult(result);
            }
            return(JsonConvert.SerializeObject(
                       simpleValidationResult,
                       new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }));
        }
Exemplo n.º 5
0
        public void TestConstructor_ValidationResult_HasErrorsWithoutErrorPath()
        {
            var validationFailure = new ValidationFailure("property", "error");

            validationFailure.CustomState = null;

            var validationFailures = new List <ValidationFailure>();

            validationFailures.Add(validationFailure);
            var validationResult = new ValidationResult(validationFailures);

            Assert.IsFalse(validationResult.IsValid);

            var instance = new SimpleValidationResult(validationResult);

            Assert.IsFalse(instance.IsValid);
            Assert.AreEqual(1, instance.Errors.Count());
            var firstError = instance.Errors.First();

            Assert.IsNull(firstError.CustomState);
        }
Exemplo n.º 6
0
        public void TestConstructor_ValidationResult_HasErrorsWithErrorPath()
        {
            var validationFailure = new ValidationFailure("property", "error");

            validationFailure.CustomState = new FundingErrorPath();

            var validationFailures = new List <ValidationFailure>();

            validationFailures.Add(validationFailure);
            var validationResult = new ValidationResult(validationFailures);

            Assert.IsFalse(validationResult.IsValid);

            var instance = new SimpleValidationResult(validationResult);

            Assert.IsFalse(instance.IsValid);
            Assert.AreEqual(1, instance.Errors.Count());
            var firstError = instance.Errors.First();

            Assert.AreEqual(validationFailure.PropertyName, firstError.PropertyName);
            Assert.AreEqual(validationFailure.ErrorMessage, firstError.ErrorMessage);
            Assert.IsTrue(Object.ReferenceEquals(validationFailure.CustomState, firstError.CustomState));
        }
Exemplo n.º 7
0
        public ActionResult Details(int id)
        {
            Election election = db.Elections.Find(id);

            if (election == null)
            {
                return(HttpNotFound());
            }

            DetailsData data = new DetailsData
            {
                Common         = election,
                NomineeEntries = new NomineeFetcher(new TimetableUserRepository()).Fetch(election.Positions)
            };

            if (election.Type == ElectionType.StudentCouncil)
            {
                data.CouncilElectionData = db.CouncilElectionData
                                           .First(councilData => councilData.ElectionId == id);
            }

            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (election.State)
            {
            case ElectionState.Disabled:
            {
                DetailsData.Action action = new DetailsData.Action()
                {
                    Title   = "Activate",
                    Url     = Url.Action("Activate", new { id }),
                    CssType = "success"
                };

                SimpleValidationResult validationResult = ElectionLifecycleInfo.CanActivate(election);

                if (!validationResult.IsNoErrors())
                {
                    action.Enabled = false;
                    action.Tooltip = validationResult.Violations[0].HumanError;
                }
                else
                {
                    action.Enabled = true;
                }

                data.Actions.Add(action);
                break;
            }

            case ElectionState.PreNominations:
            {
                DetailsData.Action action = new DetailsData.Action()
                {
                    Title   = "Deactivate",
                    Url     = Url.Action("Deactivate", new { id }),
                    CssType = "warning",
                    Enabled = true,
                    Tooltip = "Go back to deactivated state"
                };

                data.Actions.Add(action);
                break;
            }

            case ElectionState.Closed:
            {
                DetailsData.Action action = new DetailsData.Action()
                {
                    Title   = "Publish results",
                    Url     = Url.Action("PublishResults", new { id }),
                    CssType = "success",
                    Enabled = true
                };

                data.Actions.Add(action);
                break;
            }
            }

            if (election.State == ElectionState.Aborted)
            {
                data.Actions.Add(new DetailsData.Action()
                {
                    Title   = "Remove from system",
                    Enabled = true,
                    Url     = Url.Action("Delete", new { id }),
                    CssType = "danger",
                });
            }
            else
            {
                if (ElectionLifecycleInfo.CanEdit(election))
                {
                    data.Actions.Add(new DetailsData.Action()
                    {
                        Title   = "Edit",
                        Enabled = true,
                        Url     = Url.Action("Edit", new { id }),
                        CssType = "primary"
                    });
                }

                // Should be last
                data.Actions.Add(new DetailsData.Action()
                {
                    Title   = "Abort",
                    Enabled = true,
                    Url     = Url.Action("Abort", new { id }),
                    CssType = "danger"
                });
            }

            return(View(data));
        }