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;

            IAirlineService service = testServer.Host.Services.GetService(typeof(IAirlineService)) as IAirlineService;
            var             model   = new ApiAirlineServerRequestModel();

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

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

            ActionResponse deleteResult = await client.AirlineDeleteAsync(2);

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

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

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

            response.Should().NotBeNull();
            response.Name.Should().Be("A");
        }
Esempio n. 3
0
        public virtual async Task <IActionResult> Create([FromBody] ApiAirlineServerRequestModel model)
        {
            CreateResponse <ApiAirlineServerResponseModel> result = await this.AirlineService.Create(model);

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

            model.SetProperties("A");

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

            patch.ApplyTo(response);
            response.Name.Should().Be("A");
        }
Esempio n. 5
0
        private async Task <ApiAirlineServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiAirlineServerRequestModel> patch)
        {
            var record = await this.AirlineService.Get(id);

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

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

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

            if (request == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                UpdateResponse <ApiAirlineServerResponseModel> result = await this.AirlineService.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 <IAirlineService, IAirlineRepository>();
            var model         = new ApiAirlineServerRequestModel();
            var validatorMock = new Mock <IApiAirlineServerRequestModelValidator>();

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

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

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

                UpdateResponse <ApiAirlineServerResponseModel> result = await this.AirlineService.Update(id, model);

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