Пример #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;

            IVideoService service = testServer.Host.Services.GetService(typeof(IVideoService)) as IVideoService;
            var           model   = new ApiVideoServerRequestModel();

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

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

            ActionResponse deleteResult = await client.VideoDeleteAsync(2);

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

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

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Videos/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Пример #3
0
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiVideoServerModelMapper();
            var model  = new ApiVideoServerResponseModel();

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

            response.Should().NotBeNull();
            response.Description.Should().Be("A");
            response.Title.Should().Be("A");
            response.Url.Should().Be("A");
        }
Пример #4
0
        private async Task <ApiVideoServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiVideoServerRequestModel> patch)
        {
            var record = await this.VideoService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiVideoServerRequestModel request = this.VideoModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Пример #5
0
        public void CreatePatch()
        {
            var mapper = new ApiVideoServerModelMapper();
            var model  = new ApiVideoServerRequestModel();

            model.SetProperties("A", "A", "A");

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

            patch.ApplyTo(response);
            response.Description.Should().Be("A");
            response.Title.Should().Be("A");
            response.Url.Should().Be("A");
        }
Пример #6
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <IVideoService, IVideoRepository>();
            var model = new ApiVideoServerRequestModel();

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

            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.VideoModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <VideoDeletedNotification>(), It.IsAny <CancellationToken>()));
        }
Пример #7
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiVideoServerRequestModel model)
        {
            ApiVideoServerRequestModel request = await this.PatchModel(id, this.VideoModelMapper.CreatePatch(model)) as ApiVideoServerRequestModel;

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Пример #8
0
        public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <IVideoService, IVideoRepository>();
            var model         = new ApiVideoServerRequestModel();
            var validatorMock = new Mock <IApiVideoServerRequestModelValidator>();

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

            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 <VideoDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
Пример #9
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiVideoServerRequestModel> patch)
        {
            ApiVideoServerResponseModel record = await this.VideoService.Get(id);

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

                UpdateResponse <ApiVideoServerResponseModel> result = await this.VideoService.Update(id, model);

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