public async void AddLocation_ShouldReturnOk()
        {
            WebApiTestsHelper.Lock();

            // Arrange
            SetUp();
            var locationForCreationDto = new LocationForCreationDto
            {
                Name = "Warsaw"
            };
            var locationDataService = Substitute.For <ILocationDataService>();

            locationDataService.AddLocationAsync(Arg.Any <Location>())
            .Returns(_ =>
            {
                var locationToAdd = _.Arg <Location>();
                locationToAdd.Id  = 55;
                var tcs           = new TaskCompletionSource <Location>();
                tcs.SetResult(locationToAdd);
                return(tcs.Task);
            });

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

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

            Assert.Equal(locationForCreationDto.Name, addedLocation.Name);
            Assert.Equal(55, addedLocation.Id);

            WebApiTestsHelper.Unlock();
        }
Beispiel #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();
        }
        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();
        }
        public async void GetMiniature_ShouldReturnOk()
        {
            // Arrange
            SetUp();

            // Act
            var sit    = CreateController();
            var result = await sit.GetPicture("Frankfurt1.jpg", true);

            // Assert
            var encodedMiniature = WebApiTestsHelper.ExtractObjectFromActionResult <OkObjectResult, string>(result);

            Assert.Equal(Convert.ToBase64String(_miniatureData), encodedMiniature);
            await _imageService.Received().GetThumbnailAsync(@"Snapshot_Pictures\Frankfurt1.jpg");
        }
        public async void GetPicture_ShouldReturnOk()
        {
            // Arrange
            SetUp();

            // Act
            var sit    = CreateController();
            var result = await sit.GetPicture("Barcelona1.jpg");

            // Assert
            var encodedPicture = WebApiTestsHelper.ExtractObjectFromActionResult <OkObjectResult, string>(result);

            Assert.Equal(Convert.ToBase64String(_pictureData), encodedPicture);
            await _fileSystemService.Received().ReadAllBytesAsync(@"Snapshot_Pictures\Barcelona1.jpg");
        }
Beispiel #6
0
        public async void AddSnapshot_ShouldReturnOk()
        {
            WebApiTestsHelper.Lock();

            // Arrange
            SetUp();
            var snapshotForCreationDto = new LocationSnapshotForCreationDto
            {
                LocationId      = 1,
                Longitude       = 56,
                Latitude        = 49,
                Altitude        = 1024,
                PictureFileName = "SomePic.jpg",
                DateCreated     = new DateTime(2018, 9, 1)
            };
            var snapshotDataService = Substitute.For <ILocationSnapshotDataService>();

            snapshotDataService.AddSnapshotAsync(Arg.Any <LocationSnapshot>())
            .Returns(_ =>
            {
                var snapshotToAdd = _.Arg <LocationSnapshot>();
                snapshotToAdd.Id  = 55;
                var tcs           = new TaskCompletionSource <LocationSnapshot>();
                tcs.SetResult(snapshotToAdd);
                return(tcs.Task);
            });

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

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

            Assert.Equal(55, addedSnapshot.Id);
            Assert.Equal(snapshotForCreationDto.LocationId, addedSnapshot.LocationId);
            Assert.Equal(snapshotForCreationDto.Longitude, addedSnapshot.Longitude);
            Assert.Equal(snapshotForCreationDto.Latitude, addedSnapshot.Latitude);
            Assert.Equal(snapshotForCreationDto.Altitude, addedSnapshot.Altitude);
            Assert.Equal(snapshotForCreationDto.PictureFileName, addedSnapshot.PictureFileName);
            Assert.Equal(snapshotForCreationDto.DateCreated, addedSnapshot.DateCreated);

            WebApiTestsHelper.Unlock();
        }