コード例 #1
0
        public async Task <IActionResult> UpdateLocation(int id, [FromBody] LocationForUpdateDto locationForUpdate)
        {
            if (id != locationForUpdate.Id)
            {
                _logger.LogWarning("Bad request. Location ID mismatch: {0} != {Id}", id, locationForUpdate.Id);
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                _logger.LogWarning("Bad request. Model state invalid: {@ModelState}", ModelState);
                return(BadRequest(ModelState));
            }

            var changeTarget = (await _locationDataService.GetAllLocationsAsync())
                               .FirstOrDefault(_ => _.Id == id);

            if (changeTarget == null)
            {
                _logger.LogWarning("Location not found. Location ID = {0}", id);
                return(NotFound());
            }

            var locationToUpdate = Mapper.Map <LocationForUpdateDto, Location>(locationForUpdate);
            var updatedLocation  = await _locationDataService.RenameLocationAsync(id, locationForUpdate.Name);

            var result = Mapper.Map <Location, LocationDto>(updatedLocation);

            return(Ok(result));
        }
コード例 #2
0
        public async void UpdateLocation_ShouldReturnOk()
        {
            WebApiTestsHelper.Lock();

            // Arrange
            SetUp();
            var locationForUpdateDto = new LocationForUpdateDto
            {
                Id   = 5,
                Name = "Warsaw_Updated"
            };
            var locations = new List <Location>
            {
                new Location
                {
                    Id   = 5,
                    Name = "Warsaw"
                }
            };
            var locationDataService = Substitute.For <ILocationDataService>();

            locationDataService.GetAllLocationsAsync()
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <Location> >();
                tcs.SetResult(locations);
                return(tcs.Task);
            });
            locationDataService.RenameLocationAsync(Arg.Any <int>(), Arg.Any <string>())
            .Returns(_ =>
            {
                var locationId        = _.Arg <int>();
                var newName           = _.Arg <string>();
                var locationToUpdate  = locations.First(loc => loc.Id == locationId);
                locationToUpdate.Name = newName;

                var tcs = new TaskCompletionSource <Location>();
                tcs.SetResult(locationToUpdate);
                return(tcs.Task);
            });

            // Act
            var sit    = new LocationsController(locationDataService, Substitute.For <ILogger <LocationsController> >());
            var result = await sit.UpdateLocation(5, locationForUpdateDto);

            // Assert
            var updatedLocation = WebApiTestsHelper.ExtractObjectFromActionResult <OkObjectResult, LocationDto>(result);

            Assert.Equal(locationForUpdateDto.Name, updatedLocation.Name);

            WebApiTestsHelper.Unlock();
        }
コード例 #3
0
        public async void UpdateLocation_ShouldReturnBadRequest_IdMismatch()
        {
            // Arrange
            var locationForUpdateDto = new LocationForUpdateDto {
                Id = 5
            };

            // Act
            var sit    = new LocationsController(null, Substitute.For <ILogger <LocationsController> >());
            var result = await sit.UpdateLocation(13, locationForUpdateDto);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
コード例 #4
0
        public async void UpdateLocation_ShouldReturnBadRequest_ModelError()
        {
            // Arrange
            var locationForUpdateDto = new LocationForUpdateDto {
                Id = 1
            };

            // Act
            var sit = new LocationsController(null, Substitute.For <ILogger <LocationsController> >());

            sit.ModelState.AddModelError("PropName", "ModelError");
            var result = await sit.UpdateLocation(1, locationForUpdateDto);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
        }
コード例 #5
0
        public async void UpdateLocation_ShouldReturnNotFound()
        {
            // Arrange
            var locationForUpdateDto = new LocationForUpdateDto {
                Id = 1
            };
            var locationDataService = Substitute.For <ILocationDataService>();

            locationDataService.GetAllLocationsAsync()
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <Location> >();
                tcs.SetResult(Enumerable.Empty <Location>());
                return(tcs.Task);
            });

            // Act
            var sit    = new LocationsController(locationDataService, Substitute.For <ILogger <LocationsController> >());
            var result = await sit.UpdateLocation(1, locationForUpdateDto);

            // Assert
            Assert.IsType <NotFoundResult>(result);
        }