Exemple #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;

            ITableService service = testServer.Host.Services.GetService(typeof(ITableService)) as ITableService;
            var           model   = new ApiTableServerRequestModel();

            model.SetProperties("B");
            CreateResponse <ApiTableServerResponseModel> createdResponse = await service.Create(model);

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

            ActionResponse deleteResult = await client.TableDeleteAsync(2);

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

            verifyResponse.Should().BeNull();
        }
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiTableServerModelMapper();
            var model  = new ApiTableServerResponseModel();

            model.SetProperties(1, "A");
            ApiTableServerRequestModel response = mapper.MapServerResponseToRequest(model);

            response.Should().NotBeNull();
            response.Name.Should().Be("A");
        }
        public void CreatePatch()
        {
            var mapper = new ApiTableServerModelMapper();
            var model  = new ApiTableServerRequestModel();

            model.SetProperties("A");

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

            patch.ApplyTo(response);
            response.Name.Should().Be("A");
        }
        public virtual async Task <IActionResult> Create([FromBody] ApiTableServerRequestModel model)
        {
            CreateResponse <ApiTableServerResponseModel> result = await this.TableService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Tables/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
        private async Task <ApiTableServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiTableServerRequestModel> patch)
        {
            var record = await this.TableService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiTableServerRequestModel request = this.TableModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <ITableService, ITableRepository>();
            var model = new ApiTableServerRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new TableService(mock.LoggerMock.Object,
                                           mock.MediatorMock.Object,
                                           mock.RepositoryMock.Object,
                                           mock.ModelValidatorMockFactory.TableModelValidatorMock.Object,
                                           mock.DALMapperMockFactory.DALTableMapperMock);

            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.TableModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <TableDeletedNotification>(), It.IsAny <CancellationToken>()));
        }
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiTableServerRequestModel model)
        {
            ApiTableServerRequestModel request = await this.PatchModel(id, this.TableModelMapper.CreatePatch(model)) as ApiTableServerRequestModel;

            if (request == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                UpdateResponse <ApiTableServerResponseModel> result = await this.TableService.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 <ITableService, ITableRepository>();
            var model         = new ApiTableServerRequestModel();
            var validatorMock = new Mock <IApiTableServerRequestModelValidator>();

            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 TableService(mock.LoggerMock.Object,
                                           mock.MediatorMock.Object,
                                           mock.RepositoryMock.Object,
                                           validatorMock.Object,
                                           mock.DALMapperMockFactory.DALTableMapperMock);

            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 <TableDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiTableServerRequestModel> patch)
        {
            ApiTableServerResponseModel record = await this.TableService.Get(id);

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

                UpdateResponse <ApiTableServerResponseModel> result = await this.TableService.Update(id, model);

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