示例#1
0
        public async void Update_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <IFileTypeService, IFileTypeRepository>();
            var model         = new ApiFileTypeServerRequestModel();
            var validatorMock = new Mock <IApiFileTypeServerRequestModelValidator>();

            validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiFileTypeServerRequestModel>())).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 FileType()));
            var service = new FileTypeService(mock.LoggerMock.Object,
                                              mock.MediatorMock.Object,
                                              mock.RepositoryMock.Object,
                                              validatorMock.Object,
                                              mock.DALMapperMockFactory.DALFileTypeMapperMock,
                                              mock.DALMapperMockFactory.DALFileMapperMock);

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

            response.Should().NotBeNull();
            response.Success.Should().BeFalse();
            validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiFileTypeServerRequestModel>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <FileTypeUpdatedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
        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;

            IFileTypeService service = testServer.Host.Services.GetService(typeof(IFileTypeService)) as IFileTypeService;
            var model = new ApiFileTypeServerRequestModel();

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

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

            ActionResponse deleteResult = await client.FileTypeDeleteAsync(2);

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

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

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

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

            model.SetProperties("A");

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

            patch.ApplyTo(response);
            response.Name.Should().Be("A");
        }
示例#5
0
        public virtual async Task <IActionResult> Create([FromBody] ApiFileTypeServerRequestModel model)
        {
            CreateResponse <ApiFileTypeServerResponseModel> result = await this.FileTypeService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/FileTypes/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
示例#6
0
        private async Task <ApiFileTypeServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiFileTypeServerRequestModel> patch)
        {
            var record = await this.FileTypeService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiFileTypeServerRequestModel request = this.FileTypeModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
示例#7
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <IFileTypeService, IFileTypeRepository>();
            var model = new ApiFileTypeServerRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new FileTypeService(mock.LoggerMock.Object,
                                              mock.MediatorMock.Object,
                                              mock.RepositoryMock.Object,
                                              mock.ModelValidatorMockFactory.FileTypeModelValidatorMock.Object,
                                              mock.DALMapperMockFactory.DALFileTypeMapperMock,
                                              mock.DALMapperMockFactory.DALFileMapperMock);

            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.FileTypeModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <FileTypeDeletedNotification>(), It.IsAny <CancellationToken>()));
        }
示例#8
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiFileTypeServerRequestModel model)
        {
            ApiFileTypeServerRequestModel request = await this.PatchModel(id, this.FileTypeModelMapper.CreatePatch(model)) as ApiFileTypeServerRequestModel;

            if (request == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                UpdateResponse <ApiFileTypeServerResponseModel> result = await this.FileTypeService.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 <ApiFileTypeServerRequestModel> patch)
        {
            ApiFileTypeServerResponseModel record = await this.FileTypeService.Get(id);

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

                UpdateResponse <ApiFileTypeServerResponseModel> result = await this.FileTypeService.Update(id, model);

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