Пример #1
0
        public async void Update_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <IStudentService, IStudentRepository>();
            var model         = new ApiStudentServerRequestModel();
            var validatorMock = new Mock <IApiStudentServerRequestModelValidator>();

            validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiStudentServerRequestModel>())).Returns(Task.FromResult(new ValidationResult(new List <ValidationFailure>()
            {
                new ValidationFailure("text", "test")
            })));
            mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new Student()));
            var service = new StudentService(mock.LoggerMock.Object,
                                             mock.MediatorMock.Object,
                                             mock.RepositoryMock.Object,
                                             validatorMock.Object,
                                             mock.DALMapperMockFactory.DALStudentMapperMock,
                                             mock.DALMapperMockFactory.DALEventStudentMapperMock);

            UpdateResponse <ApiStudentServerResponseModel> response = await service.Update(default(int), model);

            response.Should().NotBeNull();
            response.Success.Should().BeFalse();
            validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiStudentServerRequestModel>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <StudentUpdatedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
Пример #2
0
        public virtual async void TestDelete()
        {
            var builder = new WebHostBuilder()
                          .UseEnvironment("Production")
                          .UseStartup <TestStartup>();
            TestServer testServer = new TestServer(builder);
            var        client     = new ApiClient(testServer.CreateClient());

            client.SetBearerToken(JWTTestHelper.GenerateBearerToken());
            ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;

            IStudentService service = testServer.Host.Services.GetService(typeof(IStudentService)) as IStudentService;
            var             model   = new ApiStudentServerRequestModel();

            model.SetProperties(DateTime.Parse("1/1/1988 12:00:00 AM"), "B", true, 1, "B", true, "B", "B", true, 1);
            CreateResponse <ApiStudentServerResponseModel> createdResponse = await service.Create(model);

            createdResponse.Success.Should().BeTrue();

            ActionResponse deleteResult = await client.StudentDeleteAsync(2);

            deleteResult.Success.Should().BeTrue();
            ApiStudentServerResponseModel verifyResponse = await service.Get(2);

            verifyResponse.Should().BeNull();
        }
Пример #3
0
        public virtual async Task <IActionResult> Create([FromBody] ApiStudentServerRequestModel model)
        {
            CreateResponse <ApiStudentServerResponseModel> result = await this.StudentService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Students/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Пример #4
0
        private async Task <ApiStudentServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiStudentServerRequestModel> patch)
        {
            var record = await this.StudentService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiStudentServerRequestModel request = this.StudentModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Пример #5
0
		public void MapServerResponseToRequest()
		{
			var mapper = new ApiStudentServerModelMapper();
			var model = new ApiStudentServerResponseModel();
			model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", true, 1, "A", true, "A", "A", true, 1);
			ApiStudentServerRequestModel response = mapper.MapServerResponseToRequest(model);
			response.Should().NotBeNull();
			response.Birthday.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
			response.Email.Should().Be("A");
			response.EmailRemindersEnabled.Should().Be(true);
			response.FamilyId.Should().Be(1);
			response.FirstName.Should().Be("A");
			response.IsAdult.Should().Be(true);
			response.LastName.Should().Be("A");
			response.Phone.Should().Be("A");
			response.SmsRemindersEnabled.Should().Be(true);
			response.UserId.Should().Be(1);
		}
Пример #6
0
		public void CreatePatch()
		{
			var mapper = new ApiStudentServerModelMapper();
			var model = new ApiStudentServerRequestModel();
			model.SetProperties(DateTime.Parse("1/1/1987 12:00:00 AM"), "A", true, 1, "A", true, "A", "A", true, 1);

			JsonPatchDocument<ApiStudentServerRequestModel> patch = mapper.CreatePatch(model);
			var response = new ApiStudentServerRequestModel();
			patch.ApplyTo(response);
			response.Birthday.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
			response.Email.Should().Be("A");
			response.EmailRemindersEnabled.Should().Be(true);
			response.FamilyId.Should().Be(1);
			response.FirstName.Should().Be("A");
			response.IsAdult.Should().Be(true);
			response.LastName.Should().Be("A");
			response.Phone.Should().Be("A");
			response.SmsRemindersEnabled.Should().Be(true);
			response.UserId.Should().Be(1);
		}
Пример #7
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <IStudentService, IStudentRepository>();
            var model = new ApiStudentServerRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new StudentService(mock.LoggerMock.Object,
                                             mock.MediatorMock.Object,
                                             mock.RepositoryMock.Object,
                                             mock.ModelValidatorMockFactory.StudentModelValidatorMock.Object,
                                             mock.DALMapperMockFactory.DALStudentMapperMock,
                                             mock.DALMapperMockFactory.DALEventStudentMapperMock);

            ActionResponse response = await service.Delete(default(int));

            response.Should().NotBeNull();
            response.Success.Should().BeTrue();
            mock.RepositoryMock.Verify(x => x.Delete(It.IsAny <int>()));
            mock.ModelValidatorMockFactory.StudentModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <StudentDeletedNotification>(), It.IsAny <CancellationToken>()));
        }
Пример #8
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiStudentServerRequestModel model)
        {
            ApiStudentServerRequestModel request = await this.PatchModel(id, this.StudentModelMapper.CreatePatch(model)) as ApiStudentServerRequestModel;

            if (request == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                UpdateResponse <ApiStudentServerResponseModel> result = await this.StudentService.Update(id, request);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Пример #9
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiStudentServerRequestModel> patch)
        {
            ApiStudentServerResponseModel record = await this.StudentService.Get(id);

            if (record == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                ApiStudentServerRequestModel model = await this.PatchModel(id, patch) as ApiStudentServerRequestModel;

                UpdateResponse <ApiStudentServerResponseModel> result = await this.StudentService.Update(id, model);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }