Example #1
0
        public void TestValidate_NotExistingProperty()
        {
            var presenceValidator = new PresenceValidator();
            var lengthValidator = new LengthValidator(2, 10);
            var account = new Account();
            var ageProperty = typeof(Customer).GetProperty("Age");

            string error;
            Assert.Throws<MissingMemberException>(() => presenceValidator.Validate(account, ageProperty, out error));
            Assert.Throws<MissingMemberException>(() => lengthValidator.Validate(account, ageProperty, out error));
        }
Example #2
0
        public void TestValidate_Indexer()
        {
            var presenceValidator = new PresenceValidator();
            var lengthValidator = new LengthValidator(2, 10);
            var account = new Account();

            string error;
            var indexerProperty = typeof(Account).GetProperty("Item");

            Assert.Throws<NotSupportedException>(() => presenceValidator.Validate(account, indexerProperty, out error));
            Assert.Throws<NotSupportedException>(() => lengthValidator.Validate(account, indexerProperty, out error));
        }
Example #3
0
        public void TestValidate_Collection()
        {
            var validator = new PresenceValidator();
            var account = new Account();

            var ownersProperty = typeof(Account).GetProperty("Owners");

            string error;
            Assert.IsTrue(validator.Validate(account, ownersProperty, out error));
            Assert.IsNull(error);

            account.Owners = null;
            Assert.IsFalse(validator.Validate(account, ownersProperty, out error));
            Assert.AreEqual("Owners can't be empty.", error);
        }
Example #4
0
        public void TestPerformValidation_Length()
        {
            var customer = new Customer
            {
                Name = "Maryika",
                IsFinraMember = true
            };
            var validationController = new ValidationController();

            validationController.PerformValidation(customer);
            Assert.IsFalse(customer.Errors.All.Any());
            CollectionAssert.IsEmpty(customer.Errors.All);

            customer.Name = "";
            validationController.PerformValidation(customer);
            Assert.IsTrue(customer.Errors.All.Any());
            Assert.AreEqual(2, customer.Errors.Count);
            CollectionAssert.AreEquivalent(Customer.NameErrors, customer.Errors[() => customer.Name]);

            customer.Name = "Some long name exceeds max";
            validationController.PerformValidation(customer);
            Assert.IsTrue(customer.Errors.All.Any());
            Assert.AreEqual(1, customer.Errors.Count);
            Assert.AreEqual("Name is too long (maximum is 12).", customer.Errors[() => customer.Name].First());

            var account = new Account { Number = "A0000000001" };
            validationController.PerformValidation(account);
            Assert.IsTrue(account.Errors.All.Any());
            Assert.AreEqual(1, account.Errors.Count);
            Assert.AreEqual("Number is too long (maximum is 10).", account.Errors[() => account.Number].First());

            account.Number = null;
            validationController.PerformValidation(account);
            Assert.IsFalse(account.Errors.All.Any());
            CollectionAssert.IsEmpty(account.Errors.All);

            var address = new Address();
            validationController.PerformValidation(address);
            Assert.IsTrue(address.Errors.All.Any());
            Assert.AreEqual(1, address.Errors.Count);
            Assert.AreEqual("StateCode should be 2 characters.", address.Errors[() => address.StateCode].First());

            address.StateCode = "NY";
            validationController.PerformValidation(address);
            Assert.IsFalse(address.Errors.All.Any());
            CollectionAssert.IsEmpty(address.Errors.All);
        }
Example #5
0
        public void TestValidate_NonStringType()
        {
            string error;

            // Value type property.
            var validator = new LengthValidator(4, 10);
            var valueTypeProperty = typeof(Customer).GetProperty("ActivationMode");
            var customer = new Customer { ActivationMode = ActivationMode.Client };
            Assert.Throws<NotSupportedException>(() => validator.Validate(customer, valueTypeProperty, out error));

            // Nullable value type property.
            validator = new LengthValidator(2);
            var countryProperty = typeof(Address).GetProperty("Country");
            var address = new Address { Country = Country.RussianFederation };
            Assert.Throws<NotSupportedException>(() => validator.Validate(address, countryProperty, out error));

            // Domain object ref property.
            var addressProperty = typeof(Customer).GetProperty("MailingAddress");
            Assert.Throws<NotSupportedException>(() => validator.Validate(customer, addressProperty, out error));

            // Collection property.
            var collectionProperty = typeof(Account).GetProperty("Owners");
            var account = new Account { Number = UnitTestHelper.ACCOUNT_NUMBER };
            Assert.Throws<NotSupportedException>(() => validator.Validate(account, collectionProperty, out error));
        }
Example #6
0
        public void TestValidate_DomainObjectRef()
        {
            const string OWNER_CANT_BE_EMPRTY = "PrimaryOwner can't be empty.";
            string error;
            var validator = new PresenceValidator();
            var customer = new Customer();
            var account = new Account();

            // Reference to other domain object.
            var primaryOwnerProperty = typeof(Account).GetProperty("PrimaryOwner");
            var addressProperty = typeof(Customer).GetProperty("MailingAddress");

            Assert.IsFalse(validator.Validate(account, primaryOwnerProperty, out error));
            Assert.AreEqual(OWNER_CANT_BE_EMPRTY, error);

            customer.MailingAddress.Country = Country.UnitedStates;
            account.Owners.Add(customer);
            Assert.IsTrue(validator.Validate(account, primaryOwnerProperty, out error));
            Assert.IsNull(error);

            account.Owners.Clear();
            Assert.IsFalse(validator.Validate(account, primaryOwnerProperty, out error));
            Assert.AreEqual(OWNER_CANT_BE_EMPRTY, error);

            Assert.IsTrue(validator.Validate(customer, addressProperty, out error));
            Assert.IsNull(error);
        }
Example #7
0
        public void TestValidate_String()
        {
            const string NAME_CANT_BE_EMPTY = "Name can't be empty.";
            const string NUMBER_CANT_BE_EMPTY = "Number can't be empty.";
            string error;
            var validator = new PresenceValidator();
            var customer = new Customer();
            var account = new Account { Number = UnitTestHelper.ACCOUNT_NUMBER };

            var nameProperty = typeof(Customer).GetProperty("Name");
            var accountNumberProperty = typeof(Account).GetProperty("Number");

            Assert.IsFalse(validator.Validate(customer, nameProperty, out error));
            Assert.AreEqual(NAME_CANT_BE_EMPTY, error);

            customer.Name = "John Smith";
            Assert.IsTrue(validator.Validate(customer, nameProperty, out error));
            Assert.IsNull(error);

            customer.Name = UnitTestHelper.BLANK_STRING;
            Assert.IsFalse(validator.Validate(customer, nameProperty, out error));
            Assert.AreEqual(NAME_CANT_BE_EMPTY, error);

            customer.Name = String.Empty;
            Assert.IsFalse(validator.Validate(customer, nameProperty, out error));
            Assert.AreEqual(NAME_CANT_BE_EMPTY, error);

            customer.Name = UnitTestHelper.SAMPLE_NAME;
            Assert.IsTrue(validator.Validate(customer, nameProperty, out error));
            Assert.IsNull(error);

            customer.Name = null;
            Assert.IsFalse(validator.Validate(customer, nameProperty, out error));
            Assert.AreEqual(NAME_CANT_BE_EMPTY, error);

            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = String.Empty;
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(NUMBER_CANT_BE_EMPTY, error);

            account.Number = "\n\r\t ";
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(NUMBER_CANT_BE_EMPTY, error);

            account.Number = " abc ";
            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = null;
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(NUMBER_CANT_BE_EMPTY, error);
        }
        public void TestObjectNotFoundException_Type_ObjectId()
        {
            var customer = new Customer();
            var account = new Account();

            CheckObjectNotFoundException(new ObjectNotFoundException(typeof(Account), account.Id),
                OBJECT_NOT_FOUND_MESSAGE.Form(typeof(Account).Name, account.Id), null, typeof(Account), account.Id);
            CheckObjectNotFoundException(new ObjectNotFoundException(typeof(Customer), customer.Id),
                OBJECT_NOT_FOUND_MESSAGE.Form(typeof(Customer).Name, customer.Id), null, typeof(Customer), customer.Id);

            CheckObjectNotFoundException(new ObjectNotFoundException(typeof(Customer), customer.Id, null),
                OBJECT_NOT_FOUND_MESSAGE.Form(typeof(Customer).Name, customer.Id), null, typeof(Customer), customer.Id);
            CheckObjectNotFoundException(new ObjectNotFoundException(typeof(Customer), customer.Id, UnitTestHelper.SAMPLE_STRING),
                UnitTestHelper.SAMPLE_STRING, null, typeof(Customer), customer.Id);
            CheckObjectNotFoundException(new ObjectNotFoundException(typeof(Customer), customer.Id, String.Empty),
                String.Empty, null, typeof(Customer), customer.Id);
            CheckObjectNotFoundException(new ObjectNotFoundException(typeof(Customer), customer.Id, UnitTestHelper.BLANK_STRING),
                UnitTestHelper.BLANK_STRING, null, typeof(Customer), customer.Id);
        }
Example #9
0
        public void TestErrors()
        {
            var customer = new Customer();
            CheckErrorsIsEmpty(customer);

            Assert.IsFalse(customer.IsValid);
            CheckErrorsIsNotEmpty(customer);
            Assert.AreEqual(3, customer.Errors.Count);
            Assert.AreEqual(2, customer.Errors[() => customer.Name].Count());
            Assert.AreEqual(1, customer.Errors.ForProperty(() => customer.IsFinraMember).Count());
            CollectionAssert.AreEquivalent(Customer.NameErrors, customer.Errors["Name"]);
            Assert.AreEqual("IsFinraMember can't be empty.", customer.Errors["IsFinraMember"].First());

            customer.IsFinraMember = false;

            Assert.IsFalse(customer.IsValid);
            CheckErrorsIsNotEmpty(customer);
            Assert.AreEqual(2, customer.Errors.Count);
            Assert.AreEqual(2, customer.Errors[() => customer.Name].Count());
            Assert.AreEqual(0, customer.Errors.ForProperty(() => customer.IsFinraMember).Count());
            CollectionAssert.AreEquivalent(Customer.NameErrors, customer.Errors["Name"]);

            customer.Name = UnitTestHelper.SAMPLE_NAME;

            Assert.IsTrue(customer.IsValid);
            CheckErrorsIsEmpty(customer);
            Assert.AreEqual(0, customer.Errors[() => customer.Name].Count());
            Assert.AreEqual(0, customer.Errors.ForProperty(() => customer.IsFinraMember).Count());

            var account = new Account { Number = "A000123" };
            Assert.IsTrue(account.IsValid);
            CheckErrorsIsEmpty(account);

            account.Number = "A00000000123";
            Assert.IsFalse(account.IsValid);
            CheckErrorsIsNotEmpty(account);
            Assert.AreEqual("Number is too long (maximum is 10).",
                account.Errors[() => account.Number].First());
        }
Example #10
0
 public void TestForProperty_NonExistingProperty()
 {
     var customer = new Customer();
     var account = new Account();
     Assert.Throws<ArgumentException>(() => customer.Errors.ForProperty(() => account.Number));
     CheckNoErrors(customer);
 }
Example #11
0
        public void TestVersion()
        {
            var customer = CreateValidCustomer();

            Assert.AreEqual(0, customer.Version);
            customer.UpdateModificationContext(DateTime.Now, UnitTestHelper.TEST_USER);
            Assert.AreEqual(1, customer.Version);
            customer.UpdateModificationContext(DateTime.Now, UnitTestHelper.TEST_USER);
            Assert.AreEqual(2, customer.Version);

            var account = new Account();
            account.Owners.Add(CreateValidCustomer());
            Assert.AreEqual(0, account.Version);

            var manager = AppContext.Persistence.Get<IPersistenceManagerStub>();
            manager.Save(account);
            Assert.AreEqual(1, account.Version);

            account.PrimaryOwner.MailingAddress.Country = Domain.Country.UnitedStates;
            manager.Save(account);
            Assert.AreEqual(2, account.Version);

            var dbCustomer = manager.Find<Customer>(Customer.DB_ID);
            Assert.AreEqual(1, dbCustomer.Version);

            manager.Save(dbCustomer);
            Assert.AreEqual(2, dbCustomer.Version);
        }
Example #12
0
        public void TestValidate()
        {
            var customer = new Customer
            {
                Name = "Maryika",
                IsFinraMember = false
            };
            Assert.IsTrue(customer.IsValid);

            customer.Name = "maryika";
            Assert.IsFalse(customer.IsValid);
            Assert.AreEqual("Customer name must start with a capital letter.",
                customer.Errors[() => customer.Name].First());

            customer.Name = "Maryika";
            Assert.IsTrue(customer.IsValid);

            customer.Age = 10;
            customer.IsFinraMember = true;
            Assert.IsFalse(customer.IsValid);
            Assert.AreEqual("The customer (10 years old) is too youing to be FINRA member.",
                customer.Errors.ForObject().First());

            customer.Age = 21;
            Assert.IsTrue(customer.IsValid);

            var account = new Account { Number = "A00012345" };
            Assert.IsTrue(account.IsValid);

            account.Number = "00012345";
            Assert.IsFalse(account.IsValid);
            Assert.AreEqual("Account number should not start with a digit.",
                account.Errors[() => account.Number].First());
        }
Example #13
0
        public void TestRaisePropertyChanged_Generic_MissingProperty()
        {
            var customer = CreateValidCustomer();
            var account = new Account();

            CheckPropertyChanged(() =>
                customer.RaisePropertyChanged(() => account.Number), false, false);
        }
Example #14
0
        public void TestIsValid()
        {
            var customer = new Customer();
            Assert.IsFalse(customer.IsValid);

            var validCustomer = new Customer
            {
                Name = UnitTestHelper.SAMPLE_NAME,
                Age = 43,
                IsFinraMember = true
            };
            Assert.IsTrue(validCustomer.IsValid);

            customer.Name = "James Bond";
            customer.IsFinraMember = false;
            Assert.IsTrue(customer.IsValid);

            var account = new Account();

            Assert.IsTrue(account.IsValid);
            account.Number = "A000123456";
            Assert.IsTrue(account.IsValid);
            account.Number = "A0000000012";
            Assert.IsFalse(account.IsValid);
        }
Example #15
0
        public void TestValidate_String()
        {
            string error;
            var accountNumberProperty = typeof(Account).GetProperty("Number");

            var validator = new LengthValidator(6, 20);
            var account = new Account { Number = UnitTestHelper.ACCOUNT_NUMBER };

            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = "A12345";
            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = "A0000000000";
            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = "12345678901234567890";
            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            var customerNameProperty = typeof(Customer).GetProperty("Name");
            validator = new LengthValidator(4);
            var customer = new Customer { Name = "John" };
            Assert.IsTrue(validator.Validate(customer, customerNameProperty, out error));
            Assert.IsNull(error);

            customer.Name = "    ";
            Assert.IsTrue(validator.Validate(customer, customerNameProperty, out error));
            Assert.IsNull(error);

            var addressStateCodeProperty = typeof(Address).GetProperty("StateCode");
            var stateCodeValidator = new LengthValidator(2);

            var address = new Address { StateCode = "NJ" };
            Assert.IsTrue(stateCodeValidator.Validate(address, addressStateCodeProperty, out error));
            Assert.IsNull(error);

            address.StateCode = "  ";
            Assert.IsTrue(stateCodeValidator.Validate(address, addressStateCodeProperty, out error));
            Assert.IsNull(error);
        }
Example #16
0
 public void TestIndexer_Expression_NonExistingProperty()
 {
     var customer = new Customer();
     var account = new Account();
     Assert.Throws<ArgumentException>(() => customer.Errors[() => account.Number].Any());
     CheckNoErrors(customer);
 }
Example #17
0
        public void TestValidate_String_OutOfInterval()
        {
            const string TOO_SHORT = "Number is too short (minimum is 6).";
            const string TOO_LONG = "Number is too long (maximum is 10).";
            string error;
            var accountNumberProperty = typeof(Account).GetProperty("Number");
            var validator = new LengthValidator(6, 10);

            var account = new Account();
            Assert.IsNull(account.Number);
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = String.Empty;
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = "\n\r\t ";
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = " abc ";
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = null;
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = "A60000";
            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = "A6000";
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = "12345678901";
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_LONG, error);

            var customerNameProperty = typeof(Customer).GetProperty("Name");
            validator = new LengthValidator(4, 6);
            var customer = new Customer { Name = "Joe" };
            Assert.IsFalse(validator.Validate(customer, customerNameProperty, out error));
            Assert.AreEqual("Name is too short (minimum is 4).", error);

            customer.Name = "Jillian";
            validator.TooLongMessage = "It is too long name to remember, '{{count}}' is maximum";
            Assert.IsFalse(validator.Validate(customer, customerNameProperty, out error));
            Assert.AreEqual("It is too long name to remember, '6' is maximum", error);

            customer.Name = "Bob";
            validator.TooShortMessage = "Must be {{count}} symbols.";
            Assert.IsFalse(validator.Validate(customer, customerNameProperty, out error));
            Assert.AreEqual("Must be 4 symbols.", error);
        }
Example #18
0
 public void TestToString()
 {
     var account = new Account();
     Assert.AreEqual("Account (Id: '{0}')".Form(account.Id.Value), account.ToString());
 }