Ejemplo n.º 1
0
        public async Task <ActionResult <StudentDto> > UpdateAsync(StudentDto dto)
        {
            var student = await _studentCrudService.GetByIdAsync(dto.Id);

            if (student is null)
            {
                return(NotFound());
            }

            var validator = new DefaultStudentValidator(_studentValidationService);

            student.SelectValidator(validator);

            student.SetFirstname(dto.Firstname);
            student.SetMiddlename(dto.Middlename);
            student.SetLastname(dto.Lastname);
            await student.SetCallsignAsync(dto.Callsign);

            student.SetSexId(dto.SexId);

            if (student.HasErrors)
            {
                return(BadRequest(student.ErrorsStrings));
            }

            await _studentCrudService.UpdateAsync(student);

            return(Ok(student.ToDto()));
        }
Ejemplo n.º 2
0
        public async Task SetFirstnameTestAsync()
        {
            // No valid value
            {
                var repository = new FakeStudentRepository();
                var validator  = new DefaultStudentValidator(new StudentValidationService(repository));
                var student    = await Student.BuildAsync(validator,
                                                          ValidSexId,
                                                          NotValidFirstname,
                                                          ValidMiddlename,
                                                          ValidLastname,
                                                          ValidCallsign);

                Assert.AreEqual(null, student.Firstname);
            }

            // Valid value
            {
                var repository = new FakeStudentRepository();
                var validator  = new DefaultStudentValidator(new StudentValidationService(repository));
                var student    = await Student.BuildAsync(validator,
                                                          ValidSexId,
                                                          ValidFirstname,
                                                          ValidMiddlename,
                                                          ValidLastname,
                                                          ValidCallsign);

                Assert.AreEqual(ValidFirstname, student.Firstname);
            }
        }
Ejemplo n.º 3
0
        public void SexIdTest()
        {
            const string propertyName = nameof(StudentValidator.SexId);
            const int    min          = SexIdMin;
            const int    max          = SexIdMax;

            var repository = new FakeStudentRepository();

            // Check less than min
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = min - 1;
                validator.SexId(value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                var error  = ValidationsTestHelper.CheckErrorType <ComparisonValidationError <int> >(errors);
                Assert.AreEqual(ComparisonResultType.Less, error.ComparisonResult);
                Assert.AreEqual(min, error.ComparisonValue);
            }

            // Check is min
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = min;
                validator.SexId(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Check is max
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = max;
                validator.SexId(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Check bigger than max
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = max + 1;
                validator.SexId(value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                var error  = ValidationsTestHelper.CheckErrorType <ComparisonValidationError <int> >(errors);
                Assert.AreEqual(ComparisonResultType.More, error.ComparisonResult);
                Assert.AreEqual(max, error.ComparisonValue);
            }
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <StudentDto> > AddAsync(StudentDto dto)
        {
            var validator = new DefaultStudentValidator(_studentValidationService);
            var entry     = await Student.BuildAsync(validator, dto.SexId, dto.Firstname, dto.Middlename, dto.Lastname, dto.Callsign);

            if (entry.HasErrors)
            {
                return(BadRequest(entry.ErrorsStrings));
            }

            entry = await _studentCrudService.AddAsync(entry);

            return(Ok(entry));
        }
Ejemplo n.º 5
0
        public static async Task <Student> BuildAsync(IAsyncRepository <Student, Guid> repository, int index)
        {
            var validator = new DefaultStudentValidator(new StudentValidationService(repository));
            var student   = await Student.BuildAsync(validator,
                                                     (index % 2) + 1,
                                                     $"Firstname {index}",
                                                     $"Middlename {index}",
                                                     $"Lastname {index}",
                                                     $"Callsign {index}");

            student = await repository.AddAsync(student);

            return(student);
        }
Ejemplo n.º 6
0
        public void FirstnameTest()
        {
            const string propertyName  = nameof(StudentValidator.Firstname);
            const int    upperBoundary = FirstnameUpperBoundary;

            var repository = new FakeStudentRepository();

            // Check for null
            {
                var    validator = new DefaultStudentValidator(new StudentValidationService(repository));
                string value     = null;
                validator.Firstname(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <NullValidationError>(errors);
            }

            // Check is empty
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = "";
                validator.Firstname(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <EmptyValidationError <IEnumerable <char> > >(errors);
            }

            // Whitespaces check without boundary crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => ' ').ToArray());
                validator.Firstname(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Whitespaces check with boundary crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => ' ').ToArray());
                validator.Firstname(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Upper boundary check without crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => 'x').ToArray());
                validator.Firstname(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Upper boundary check with crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => 'x').ToArray());
                validator.Firstname(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <LengthComparisonValidationError>(errors);
                ValidationsTestHelper.CheckUpperBoundaryCross(
                    (LengthComparisonValidationError)validator.Errors[propertyName][0],
                    upperBoundary);
            }
        }
Ejemplo n.º 7
0
        public async Task CallsignTestAsync()
        {
            const string propertyName  = nameof(StudentValidator.Callsign);
            const int    lowerBoundary = CallsignLowerBoundary;
            const int    upperBoundary = CallsignUpperBoundary;

            var repository = new FakeStudentRepository();

            // Check for null
            {
                var    validator = new DefaultStudentValidator(new StudentValidationService(repository));
                string value     = null;
                validator.Callsign(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Check is empty
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = "";
                validator.Callsign(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Whitespaces check without boundary crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => ' ').ToArray());
                validator.Callsign(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Whitespaces check with boundary crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => ' ').ToArray());
                validator.Callsign(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Lower boundary check without crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, lowerBoundary).Select(x => 'x').ToArray());
                validator.Callsign(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Lower boundary check with crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, lowerBoundary - 1).Select(x => 'x').ToArray());
                validator.Callsign(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <LengthComparisonValidationError>(errors);
                ValidationsTestHelper.CheckLowerBoundaryCross(
                    (LengthComparisonValidationError)validator.Errors[propertyName][0],
                    lowerBoundary);
            }

            // Upper boundary check without crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => 'x').ToArray());
                validator.Callsign(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Upper boundary check with crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => 'x').ToArray());
                validator.Callsign(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <LengthComparisonValidationError>(errors);
                ValidationsTestHelper.CheckUpperBoundaryCross(
                    (LengthComparisonValidationError)validator.Errors[propertyName][0],
                    upperBoundary);
            }

            // Check for uniqueness
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));

                string value = null;
                await validator.CallsignUniqueness(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);

                value = new string(Enumerable.Range(1, lowerBoundary).Select(x => 'x').ToArray());
                await validator.CallsignUniqueness(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);

                var student = await Student.BuildAsync(validator, 1, "firstname", "middlename", "lastname", value);

                await repository.AddAsync(student);

                await validator.CallsignUniqueness(value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <UniquenessValidationError <string> >(errors);

                value = null;
                await validator.CallsignUniqueness(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <UniquenessValidationError <string> >(errors);
            }
        }