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;

            ISelfReferenceService service = testServer.Host.Services.GetService(typeof(ISelfReferenceService)) as ISelfReferenceService;
            var model = new ApiSelfReferenceServerRequestModel();

            model.SetProperties(1, 1);
            CreateResponse <ApiSelfReferenceServerResponseModel> createdResponse = await service.Create(model);

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

            ActionResponse deleteResult = await client.SelfReferenceDeleteAsync(2);

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

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

            model.SetProperties(1, 1, 1);
            ApiSelfReferenceServerRequestModel response = mapper.MapServerResponseToRequest(model);

            response.Should().NotBeNull();
            response.SelfReferenceId.Should().Be(1);
            response.SelfReferenceId2.Should().Be(1);
        }
        public virtual async Task <IActionResult> Create([FromBody] ApiSelfReferenceServerRequestModel model)
        {
            CreateResponse <ApiSelfReferenceServerResponseModel> result = await this.SelfReferenceService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/SelfReferences/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
        public void CreatePatch()
        {
            var mapper = new ApiSelfReferenceServerModelMapper();
            var model  = new ApiSelfReferenceServerRequestModel();

            model.SetProperties(1, 1);

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

            patch.ApplyTo(response);
            response.SelfReferenceId.Should().Be(1);
            response.SelfReferenceId2.Should().Be(1);
        }
        private async Task <ApiSelfReferenceServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiSelfReferenceServerRequestModel> patch)
        {
            var record = await this.SelfReferenceService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiSelfReferenceServerRequestModel request = this.SelfReferenceModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Exemple #6
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <ISelfReferenceService, ISelfReferenceRepository>();
            var model = new ApiSelfReferenceServerRequestModel();

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

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

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Exemple #8
0
        public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <ISelfReferenceService, ISelfReferenceRepository>();
            var model         = new ApiSelfReferenceServerRequestModel();
            var validatorMock = new Mock <IApiSelfReferenceServerRequestModelValidator>();

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

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

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

                UpdateResponse <ApiSelfReferenceServerResponseModel> result = await this.SelfReferenceService.Update(id, model);

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