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;

            IFollowingService service = testServer.Host.Services.GetService(typeof(IFollowingService)) as IFollowingService;
            var model = new ApiFollowingServerRequestModel();

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

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

            ActionResponse deleteResult = await client.FollowingDeleteAsync(2);

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

            verifyResponse.Should().BeNull();
        }
Esempio n. 2
0
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiFollowingServerModelMapper();
            var model  = new ApiFollowingServerResponseModel();

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

            response.Should().NotBeNull();
            response.DateFollowed.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.Muted.Should().Be("A");
        }
Esempio n. 3
0
        public virtual async Task <IActionResult> Create([FromBody] ApiFollowingServerRequestModel model)
        {
            CreateResponse <ApiFollowingServerResponseModel> result = await this.FollowingService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Followings/{result.Record.UserId}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Esempio n. 4
0
        public void CreatePatch()
        {
            var mapper = new ApiFollowingServerModelMapper();
            var model  = new ApiFollowingServerRequestModel();

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

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

            patch.ApplyTo(response);
            response.DateFollowed.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.Muted.Should().Be("A");
        }
Esempio n. 5
0
        private async Task <ApiFollowingServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiFollowingServerRequestModel> patch)
        {
            var record = await this.FollowingService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiFollowingServerRequestModel request = this.FollowingModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Esempio n. 6
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <IFollowingService, IFollowingRepository>();
            var model = new ApiFollowingServerRequestModel();

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

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

            if (request == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                UpdateResponse <ApiFollowingServerResponseModel> result = await this.FollowingService.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 <IFollowingService, IFollowingRepository>();
            var model         = new ApiFollowingServerRequestModel();
            var validatorMock = new Mock <IApiFollowingServerRequestModelValidator>();

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

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

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

                UpdateResponse <ApiFollowingServerResponseModel> result = await this.FollowingService.Update(id, model);

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