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(); }
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 GetAllLocations_ShouldReturnOk() { WebApiTestsHelper.Lock(); // Arrange SetUp(); var locations = new List <Location> { new Location { Id = 1, Name = "Warsaw", LocationSnapshots = new List <LocationSnapshot> { new LocationSnapshot(), new LocationSnapshot() } }, new Location { Id = 2, Name = "Lisbon", LocationSnapshots = new List <LocationSnapshot> { new LocationSnapshot() } } }; var locationDataService = Substitute.For <ILocationDataService>(); locationDataService.GetAllLocationsAsync() .Returns(_ => { var tcs = new TaskCompletionSource <IEnumerable <Location> >(); tcs.SetResult(locations); return(tcs.Task); }); // Act var sit = new LocationsController(locationDataService, Substitute.For <ILogger <LocationsController> >()); var result = await sit.GetAllLocations(); // Assert var dtos = WebApiTestsHelper.ExtractGenericCollectionFromActionResult <OkObjectResult, IEnumerable <LocationDto> >(result) .ToList(); for (var i = 0; i < dtos.Count; i++) { Assert.Equal(locations[i].Id, dtos[i].Id); Assert.Equal(locations[i].Name, dtos[i].Name); Assert.Equal(locations[i].LocationSnapshots.Count, dtos[i].SnapshotsCount); } WebApiTestsHelper.Unlock(); }
public async void GroupSnapshots_ShouldReturnOk() { WebApiTestsHelper.Lock(); // Arrange SetUp(); var snapshotGroups = new List <SnapshotGroup> { new SnapshotGroup { Name = "December 2018", SnapshotIds = new List <int> { 1, 2, 3 } }, new SnapshotGroup { Name = "January 2019", SnapshotIds = new List <int> { 4, 5, 6 } } }; Task <IEnumerable <SnapshotGroup> > dataFetchOperation() { var tcs = new TaskCompletionSource <IEnumerable <SnapshotGroup> >(); tcs.SetResult(snapshotGroups); return(tcs.Task); }; var snapshotDataService = Substitute.For <ILocationSnapshotDataService>(); snapshotDataService.ChooseGroupByOperation(Arg.Any <GroupByCriteria>()) .Returns(dataFetchOperation); // Act var sit = new SnapshotGroupsController(snapshotDataService, Substitute.For <ILogger <SnapshotGroupsController> >()); var result = await sit.GroupSnapshots(GroupByCriteria.CreatedDate); // Assert var dtos = WebApiTestsHelper.ExtractGenericCollectionFromActionResult <OkObjectResult, IEnumerable <SnapshotGroupDto> >(result) .ToList(); for (var i = 0; i < dtos.Count; i++) { Assert.Equal(snapshotGroups[i].Name, dtos[i].Name); Assert.Equal(snapshotGroups[i].SnapshotIds, dtos[i].SnapshotIds); } 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"); }
public async void GetSnapshotsByIds_ShouldReturnOk() { WebApiTestsHelper.Lock(); // Arrange SetUp(); var snapshots = new List <LocationSnapshot> { new LocationSnapshot { Id = 1, LocationId = 1, Longitude = 56, Latitude = 49, Altitude = 1024, PictureFileName = "SomePic.jpg", DateCreated = new DateTime(2018, 9, 1) } }; var snapshotDataService = Substitute.For <ILocationSnapshotDataService>(); snapshotDataService.GetSnapshotsByIdsAsync(Arg.Any <IEnumerable <int> >()) .Returns(_ => { var tcs = new TaskCompletionSource <IEnumerable <LocationSnapshot> >(); tcs.SetResult(snapshots); return(tcs.Task); }); // Act var sit = new LocationSnapshotsController(snapshotDataService, Substitute.For <ILogger <LocationSnapshotsController> >()); var result = await sit.GetSnapshotsByIds(new[] { 1, 2, 3 }); // Assert var dtos = WebApiTestsHelper.ExtractGenericCollectionFromActionResult <OkObjectResult, IEnumerable <LocationSnapshotDto> >(result) .ToList(); Assert.Equal(snapshots[0].Id, dtos[0].Id); Assert.Equal(snapshots[0].LocationId, dtos[0].LocationId); Assert.Equal(snapshots[0].Longitude, dtos[0].Longitude); Assert.Equal(snapshots[0].Latitude, dtos[0].Latitude); Assert.Equal(snapshots[0].Altitude, dtos[0].Altitude); Assert.Equal(snapshots[0].PictureFileName, dtos[0].PictureFileName); Assert.Equal(snapshots[0].DateCreated, dtos[0].DateCreated); WebApiTestsHelper.Unlock(); }
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(); }
public async void GetMiniatures_ShouldReturnOk() { // Arrange SetUp(); // Act var sit = CreateController(); var result = await sit.GetMiniatures(new List <int> { 1, 2, 3 }); // Assert var dtos = WebApiTestsHelper.ExtractGenericCollectionFromActionResult <OkObjectResult, ArrayList>(result); Assert.Equal(3, dtos.Count); await _imageService.Received().GetThumbnailAsync(@"Snapshot_Pictures\Barcelona1.jpg"); await _imageService.Received().GetThumbnailAsync(@"Snapshot_Pictures\Prague1.jpg"); await _imageService.Received().GetThumbnailAsync(@"Snapshot_Pictures\Frankfurt1.jpg"); }