Example #1
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;

            IEventStudentService service = testServer.Host.Services.GetService(typeof(IEventStudentService)) as IEventStudentService;
            var model = new ApiEventStudentServerRequestModel();

            model.SetProperties(1, 1);
            CreateResponse <ApiEventStudentServerResponseModel> createdResponse = await service.Create(model);

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

            ActionResponse deleteResult = await client.EventStudentDeleteAsync(2);

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

            verifyResponse.Should().BeNull();
        }
Example #2
0
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiEventStudentServerModelMapper();
            var model  = new ApiEventStudentServerResponseModel();

            model.SetProperties(1, 1, 1);
            ApiEventStudentServerRequestModel response = mapper.MapServerResponseToRequest(model);

            response.Should().NotBeNull();
            response.EventId.Should().Be(1);
            response.StudentId.Should().Be(1);
        }
        public virtual async Task <IActionResult> Create([FromBody] ApiEventStudentServerRequestModel model)
        {
            CreateResponse <ApiEventStudentServerResponseModel> result = await this.EventStudentService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/EventStudents/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Example #4
0
        public void CreatePatch()
        {
            var mapper = new ApiEventStudentServerModelMapper();
            var model  = new ApiEventStudentServerRequestModel();

            model.SetProperties(1, 1);

            JsonPatchDocument <ApiEventStudentServerRequestModel> patch = mapper.CreatePatch(model);
            var response = new ApiEventStudentServerRequestModel();

            patch.ApplyTo(response);
            response.EventId.Should().Be(1);
            response.StudentId.Should().Be(1);
        }
        private async Task <ApiEventStudentServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiEventStudentServerRequestModel> patch)
        {
            var record = await this.EventStudentService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiEventStudentServerRequestModel request = this.EventStudentModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <IEventStudentService, IEventStudentRepository>();
            var model = new ApiEventStudentServerRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new EventStudentService(mock.LoggerMock.Object,
                                                  mock.MediatorMock.Object,
                                                  mock.RepositoryMock.Object,
                                                  mock.ModelValidatorMockFactory.EventStudentModelValidatorMock.Object,
                                                  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.EventStudentModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <EventStudentDeletedNotification>(), It.IsAny <CancellationToken>()));
        }
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiEventStudentServerRequestModel model)
        {
            ApiEventStudentServerRequestModel request = await this.PatchModel(id, this.EventStudentModelMapper.CreatePatch(model)) as ApiEventStudentServerRequestModel;

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
        public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <IEventStudentService, IEventStudentRepository>();
            var model         = new ApiEventStudentServerRequestModel();
            var validatorMock = new Mock <IApiEventStudentServerRequestModelValidator>();

            validatorMock.Setup(x => x.ValidateDeleteAsync(It.IsAny <int>())).Returns(Task.FromResult(new FluentValidation.Results.ValidationResult(new List <ValidationFailure>()
            {
                new ValidationFailure("text", "test")
            })));
            var service = new EventStudentService(mock.LoggerMock.Object,
                                                  mock.MediatorMock.Object,
                                                  mock.RepositoryMock.Object,
                                                  validatorMock.Object,
                                                  mock.DALMapperMockFactory.DALEventStudentMapperMock);

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

            response.Should().NotBeNull();
            response.Success.Should().BeFalse();
            validatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <EventStudentDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiEventStudentServerRequestModel> patch)
        {
            ApiEventStudentServerResponseModel record = await this.EventStudentService.Get(id);

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

                UpdateResponse <ApiEventStudentServerResponseModel> result = await this.EventStudentService.Update(id, model);

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