Пример #1
0
        public async void Update_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <ISpeciesService, ISpeciesRepository>();
            var model         = new ApiSpeciesServerRequestModel();
            var validatorMock = new Mock <IApiSpeciesServerRequestModelValidator>();

            validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiSpeciesServerRequestModel>())).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 Species()));
            var service = new SpeciesService(mock.LoggerMock.Object,
                                             mock.MediatorMock.Object,
                                             mock.RepositoryMock.Object,
                                             validatorMock.Object,
                                             mock.DALMapperMockFactory.DALSpeciesMapperMock,
                                             mock.DALMapperMockFactory.DALBreedMapperMock);

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

            response.Should().NotBeNull();
            response.Success.Should().BeFalse();
            validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiSpeciesServerRequestModel>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <SpeciesUpdatedNotification>(), 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;

            ISpeciesService service = testServer.Host.Services.GetService(typeof(ISpeciesService)) as ISpeciesService;
            var             model   = new ApiSpeciesServerRequestModel();

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

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

            ActionResponse deleteResult = await client.SpeciesDeleteAsync(2);

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

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

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

            response.Should().NotBeNull();
            response.Name.Should().Be("A");
        }
        public void CreatePatch()
        {
            var mapper = new ApiSpeciesServerModelMapper();
            var model  = new ApiSpeciesServerRequestModel();

            model.SetProperties("A");

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

            patch.ApplyTo(response);
            response.Name.Should().Be("A");
        }
Пример #5
0
        public virtual async Task <IActionResult> Create([FromBody] ApiSpeciesServerRequestModel model)
        {
            CreateResponse <ApiSpeciesServerResponseModel> result = await this.SpeciesService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Species/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Пример #6
0
        private async Task <ApiSpeciesServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiSpeciesServerRequestModel> patch)
        {
            var record = await this.SpeciesService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiSpeciesServerRequestModel request = this.SpeciesModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Пример #7
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <ISpeciesService, ISpeciesRepository>();
            var model = new ApiSpeciesServerRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new SpeciesService(mock.LoggerMock.Object,
                                             mock.MediatorMock.Object,
                                             mock.RepositoryMock.Object,
                                             mock.ModelValidatorMockFactory.SpeciesModelValidatorMock.Object,
                                             mock.DALMapperMockFactory.DALSpeciesMapperMock,
                                             mock.DALMapperMockFactory.DALBreedMapperMock);

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

            if (request == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                UpdateResponse <ApiSpeciesServerResponseModel> result = await this.SpeciesService.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 <ApiSpeciesServerRequestModel> patch)
        {
            ApiSpeciesServerResponseModel record = await this.SpeciesService.Get(id);

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

                UpdateResponse <ApiSpeciesServerResponseModel> result = await this.SpeciesService.Update(id, model);

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