예제 #1
0
        public async Task <IActionResult> UpdateSnapshot(int id, [FromBody] LocationSnapshotForUpdateDto snapshotForUpdate)
        {
            if (id != snapshotForUpdate.Id)
            {
                _logger.LogWarning("Bad request. Location snapshot ID mismatch: {0} != {Id}", id, snapshotForUpdate.Id);
                return(BadRequest());
            }

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

            var changeTargets = await _locationSnapshotDataService.GetSnapshotsByIdsAsync(new[] { id });

            if (changeTargets.Count() != 1)
            {
                _logger.LogWarning("Location snapshot not found. Location snapshot ID = {0}", id);
                return(NotFound());
            }

            var snapshotToUpdate = Mapper.Map <LocationSnapshotForUpdateDto, LocationSnapshot>(snapshotForUpdate);
            var updatedSnapshot  = await _locationSnapshotDataService.UpdateSnapshotAsync(snapshotToUpdate);

            var result = Mapper.Map <LocationSnapshot, LocationSnapshotDto>(updatedSnapshot);

            return(Ok(result));
        }
예제 #2
0
        public async void UpdateSnapshot_ShouldReturnOk()
        {
            WebApiTestsHelper.Lock();

            // Arrange
            SetUp();
            var snapshotForUpdateDto = new LocationSnapshotForUpdateDto
            {
                Id        = 5,
                Longitude = 4,
                Latitude  = 5,
                Altitude  = 6
            };
            var snapshots = new List <LocationSnapshot>
            {
                new LocationSnapshot
                {
                    Id        = 5,
                    Longitude = 1,
                    Latitude  = 2,
                    Altitude  = 3
                }
            };
            var snapshotDataService = Substitute.For <ILocationSnapshotDataService>();

            snapshotDataService.GetSnapshotsByIdsAsync(Arg.Any <IEnumerable <int> >())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <LocationSnapshot> >();
                tcs.SetResult(snapshots);
                return(tcs.Task);
            });
            snapshotDataService.UpdateSnapshotAsync(Arg.Any <LocationSnapshot>())
            .Returns(_ =>
            {
                var snapshot               = _.Arg <LocationSnapshot>();
                var snapshotToUpdate       = snapshots.First(loc => loc.Id == snapshot.Id);
                snapshotToUpdate.Longitude = snapshot.Longitude;
                snapshotToUpdate.Latitude  = snapshot.Latitude;
                snapshotToUpdate.Altitude  = snapshot.Altitude;

                var tcs = new TaskCompletionSource <LocationSnapshot>();
                tcs.SetResult(snapshotToUpdate);
                return(tcs.Task);
            });

            // Act
            var sit    = new LocationSnapshotsController(snapshotDataService, Substitute.For <ILogger <LocationSnapshotsController> >());
            var result = await sit.UpdateSnapshot(5, snapshotForUpdateDto);

            // Assert
            var updatedLocationSnapshot = WebApiTestsHelper.ExtractObjectFromActionResult <OkObjectResult, LocationSnapshotDto>(result);

            Assert.Equal(snapshotForUpdateDto.Longitude, updatedLocationSnapshot.Longitude);
            Assert.Equal(snapshotForUpdateDto.Latitude, updatedLocationSnapshot.Latitude);
            Assert.Equal(snapshotForUpdateDto.Altitude, updatedLocationSnapshot.Altitude);

            WebApiTestsHelper.Unlock();
        }
예제 #3
0
        public async void UpdateSnapshot_ShouldReturnBadRequest_IdMismatch()
        {
            // Arrange
            var snapshotForUpdateDto = new LocationSnapshotForUpdateDto {
                Id = 5
            };

            // Act
            var sit    = new LocationSnapshotsController(null, Substitute.For <ILogger <LocationSnapshotsController> >());
            var result = await sit.UpdateSnapshot(13, snapshotForUpdateDto);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
예제 #4
0
        public async void UpdateSnapshot_ShouldReturnBadRequest_ModelError()
        {
            // Arrange
            var snapshotForUpdateDto = new LocationSnapshotForUpdateDto {
                Id = 1
            };

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

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

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
        }
예제 #5
0
        public async void UpdateSnapshot_ShouldReturnNotFound()
        {
            // Arrange
            var snapshotForUpdateDto = new LocationSnapshotForUpdateDto {
                Id = 1
            };
            var snapshotDataService = Substitute.For <ILocationSnapshotDataService>();

            snapshotDataService.GetSnapshotsByIdsAsync(Arg.Any <IEnumerable <int> >())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <LocationSnapshot> >();
                tcs.SetResult(Enumerable.Empty <LocationSnapshot>());
                return(tcs.Task);
            });

            // Act
            var sit    = new LocationSnapshotsController(snapshotDataService, Substitute.For <ILogger <LocationSnapshotsController> >());
            var result = await sit.UpdateSnapshot(1, snapshotForUpdateDto);

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