Esempio n. 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;

            IPostLinkService service = testServer.Host.Services.GetService(typeof(IPostLinkService)) as IPostLinkService;
            var model = new ApiPostLinkServerRequestModel();

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

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

            ActionResponse deleteResult = await client.PostLinkDeleteAsync(2);

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

            verifyResponse.Should().BeNull();
        }
Esempio n. 2
0
        public virtual async Task <IActionResult> Create([FromBody] ApiPostLinkServerRequestModel model)
        {
            CreateResponse <ApiPostLinkServerResponseModel> result = await this.PostLinkService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/PostLinks/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Esempio n. 3
0
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiPostLinkServerModelMapper();
            var model  = new ApiPostLinkServerResponseModel();

            model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, 1);
            ApiPostLinkServerRequestModel response = mapper.MapServerResponseToRequest(model);

            response.Should().NotBeNull();
            response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.LinkTypeId.Should().Be(1);
            response.PostId.Should().Be(1);
            response.RelatedPostId.Should().Be(1);
        }
Esempio n. 4
0
        private async Task <ApiPostLinkServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiPostLinkServerRequestModel> patch)
        {
            var record = await this.PostLinkService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiPostLinkServerRequestModel request = this.PostLinkModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Esempio n. 5
0
        public void CreatePatch()
        {
            var mapper = new ApiPostLinkServerModelMapper();
            var model  = new ApiPostLinkServerRequestModel();

            model.SetProperties(DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, 1);

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

            patch.ApplyTo(response);
            response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.LinkTypeId.Should().Be(1);
            response.PostId.Should().Be(1);
            response.RelatedPostId.Should().Be(1);
        }
Esempio n. 6
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <IPostLinkService, IPostLinkRepository>();
            var model = new ApiPostLinkServerRequestModel();

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

            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.PostLinkModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <PostLinkDeletedNotification>(), It.IsAny <CancellationToken>()));
        }
Esempio n. 7
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiPostLinkServerRequestModel model)
        {
            ApiPostLinkServerRequestModel request = await this.PatchModel(id, this.PostLinkModelMapper.CreatePatch(model)) as ApiPostLinkServerRequestModel;

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Esempio n. 8
0
        public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <IPostLinkService, IPostLinkRepository>();
            var model         = new ApiPostLinkServerRequestModel();
            var validatorMock = new Mock <IApiPostLinkServerRequestModelValidator>();

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

            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 <PostLinkDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
Esempio n. 9
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiPostLinkServerRequestModel> patch)
        {
            ApiPostLinkServerResponseModel record = await this.PostLinkService.Get(id);

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

                UpdateResponse <ApiPostLinkServerResponseModel> result = await this.PostLinkService.Update(id, model);

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