Пример #1
0
        public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <IChainService, IChainRepository>();
            var model         = new ApiChainServerRequestModel();
            var validatorMock = new Mock <IApiChainServerRequestModelValidator>();

            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 ChainService(mock.LoggerMock.Object,
                                           mock.MediatorMock.Object,
                                           mock.RepositoryMock.Object,
                                           validatorMock.Object,
                                           mock.DALMapperMockFactory.DALChainMapperMock,
                                           mock.DALMapperMockFactory.DALClaspMapperMock,
                                           mock.DALMapperMockFactory.DALLinkMapperMock);

            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 <ChainDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
Пример #2
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;

            IChainService service = testServer.Host.Services.GetService(typeof(IChainService)) as IChainService;
            var           model   = new ApiChainServerRequestModel();

            model.SetProperties(1, Guid.Parse("3842cac4-b9a0-8223-0dcc-509a6f75849b"), "B", 1);
            CreateResponse <ApiChainServerResponseModel> createdResponse = await service.Create(model);

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

            ActionResponse deleteResult = await client.ChainDeleteAsync(2);

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

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

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

            model.SetProperties(1, 1, Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"), "A", 1);
            ApiChainServerRequestModel response = mapper.MapServerResponseToRequest(model);

            response.Should().NotBeNull();
            response.ChainStatusId.Should().Be(1);
            response.ExternalId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"));
            response.Name.Should().Be("A");
            response.TeamId.Should().Be(1);
        }
Пример #5
0
        private async Task <ApiChainServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiChainServerRequestModel> patch)
        {
            var record = await this.ChainService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiChainServerRequestModel request = this.ChainModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Пример #6
0
        public void CreatePatch()
        {
            var mapper = new ApiChainServerModelMapper();
            var model  = new ApiChainServerRequestModel();

            model.SetProperties(1, Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"), "A", 1);

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

            patch.ApplyTo(response);
            response.ChainStatusId.Should().Be(1);
            response.ExternalId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"));
            response.Name.Should().Be("A");
            response.TeamId.Should().Be(1);
        }
Пример #7
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <IChainService, IChainRepository>();
            var model = new ApiChainServerRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new ChainService(mock.LoggerMock.Object,
                                           mock.MediatorMock.Object,
                                           mock.RepositoryMock.Object,
                                           mock.ModelValidatorMockFactory.ChainModelValidatorMock.Object,
                                           mock.DALMapperMockFactory.DALChainMapperMock,
                                           mock.DALMapperMockFactory.DALClaspMapperMock,
                                           mock.DALMapperMockFactory.DALLinkMapperMock);

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

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Пример #9
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiChainServerRequestModel> patch)
        {
            ApiChainServerResponseModel record = await this.ChainService.Get(id);

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

                UpdateResponse <ApiChainServerResponseModel> result = await this.ChainService.Update(id, model);

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