コード例 #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;

            ICustomerService service = testServer.Host.Services.GetService(typeof(ICustomerService)) as ICustomerService;
            var model = new ApiCustomerServerRequestModel();

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

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

            ActionResponse deleteResult = await client.CustomerDeleteAsync(2);

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

            verifyResponse.Should().BeNull();
        }
コード例 #2
0
        public async void Update_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <ICustomerService, ICustomerRepository>();
            var model         = new ApiCustomerServerRequestModel();
            var validatorMock = new Mock <IApiCustomerServerRequestModelValidator>();

            validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiCustomerServerRequestModel>())).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 Customer()));
            var service = new CustomerService(mock.LoggerMock.Object,
                                              mock.MediatorMock.Object,
                                              mock.RepositoryMock.Object,
                                              validatorMock.Object,
                                              mock.DALMapperMockFactory.DALCustomerMapperMock,
                                              mock.DALMapperMockFactory.DALCustomerCommunicationMapperMock);

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

            response.Should().NotBeNull();
            response.Success.Should().BeFalse();
            validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiCustomerServerRequestModel>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <CustomerUpdatedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
コード例 #3
0
        public virtual async Task <IActionResult> Create([FromBody] ApiCustomerServerRequestModel model)
        {
            CreateResponse <ApiCustomerServerResponseModel> result = await this.CustomerService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Customers/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
コード例 #4
0
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiCustomerServerModelMapper();
            var model  = new ApiCustomerServerResponseModel();

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

            response.Should().NotBeNull();
            response.Email.Should().Be("A");
            response.FirstName.Should().Be("A");
            response.LastName.Should().Be("A");
            response.Phone.Should().Be("A");
        }
コード例 #5
0
        private async Task <ApiCustomerServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiCustomerServerRequestModel> patch)
        {
            var record = await this.CustomerService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiCustomerServerRequestModel request = this.CustomerModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
コード例 #6
0
        public void CreatePatch()
        {
            var mapper = new ApiCustomerServerModelMapper();
            var model  = new ApiCustomerServerRequestModel();

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

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

            patch.ApplyTo(response);
            response.Email.Should().Be("A");
            response.FirstName.Should().Be("A");
            response.LastName.Should().Be("A");
            response.Phone.Should().Be("A");
        }
コード例 #7
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <ICustomerService, ICustomerRepository>();
            var model = new ApiCustomerServerRequestModel();

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

            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.CustomerModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <CustomerDeletedNotification>(), It.IsAny <CancellationToken>()));
        }
コード例 #8
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiCustomerServerRequestModel model)
        {
            ApiCustomerServerRequestModel request = await this.PatchModel(id, this.CustomerModelMapper.CreatePatch(model)) as ApiCustomerServerRequestModel;

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
コード例 #9
0
        public async void Create_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock = new ServiceMockFacade <ICustomerService, ICustomerRepository>();

            var model = new ApiCustomerServerRequestModel();

            mock.RepositoryMock.Setup(x => x.Create(It.IsAny <Customer>())).Returns(Task.FromResult(new Customer()));
            var service = new CustomerService(mock.LoggerMock.Object,
                                              mock.MediatorMock.Object,
                                              mock.RepositoryMock.Object,
                                              mock.ModelValidatorMockFactory.CustomerModelValidatorMock.Object,
                                              mock.DALMapperMockFactory.DALCustomerMapperMock,
                                              mock.DALMapperMockFactory.DALCustomerCommunicationMapperMock);

            CreateResponse <ApiCustomerServerResponseModel> response = await service.Create(model);

            response.Should().NotBeNull();
            response.Success.Should().BeTrue();
            mock.ModelValidatorMockFactory.CustomerModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiCustomerServerRequestModel>()));
            mock.RepositoryMock.Verify(x => x.Create(It.IsAny <Customer>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <CustomerCreatedNotification>(), It.IsAny <CancellationToken>()));
        }
コード例 #10
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiCustomerServerRequestModel> patch)
        {
            ApiCustomerServerResponseModel record = await this.CustomerService.Get(id);

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

                UpdateResponse <ApiCustomerServerResponseModel> result = await this.CustomerService.Update(id, model);

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