public async Task <IActionResult> Patch(string id, [FromBody] RescheduledDelivery rescheduledDelivery) { logger.LogInformation("In Patch action with id: {Id} and rescheduledDelivery: {@RescheduledDelivery}", id, rescheduledDelivery.ToLogInfo()); var delivery = await deliveryRepository.GetAsync(id); if (delivery == null) { logger.LogDebug("Delivery id: {Id} not found", id); return(NotFound()); } var updatedDelivery = new InternalDelivery(delivery.Id, delivery.Owner, rescheduledDelivery.Pickup, rescheduledDelivery.Dropoff, rescheduledDelivery.Deadline, delivery.Expedited, delivery.ConfirmationRequired, delivery.DroneId); // Adds the delivery rescheduled status event var deliveryTrackingEvent = new DeliveryTrackingEvent { DeliveryId = id, Stage = DeliveryStage.Rescheduled }; await deliveryTrackingRepository.AddAsync(deliveryTrackingEvent); // Updates the inflight delivery with updated information await deliveryRepository.UpdateAsync(id, updatedDelivery); return(Ok()); }
public async Task Put_AddscreatedDeliveryEvent() { // Arrange DeliveryTrackingEvent createdDelivery = null; var deliveryStatusEventRepository = new Mock <IDeliveryTrackingEventRepository>(); deliveryStatusEventRepository.Setup(r => r.AddAsync(It.IsAny <DeliveryTrackingEvent>())) .Returns(Task.CompletedTask) .Callback <DeliveryTrackingEvent>(e => createdDelivery = e); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(new Mock <ILogger>().Object); var target = new DeliveriesController(new Mock <IDeliveryRepository>().Object, new Mock <INotifyMeRequestRepository>().Object, new Mock <INotificationService>().Object, deliveryStatusEventRepository.Object, loggerFactory.Object); // Act var result = await target.Put(new Delivery("deliveryid", new UserAccount("user", "account"), new Location(0, 0, 0), new Location(2, 2, 2), "deadline", true, ConfirmationType.FingerPrint, "drone"), "deliveryid"); var createdAtRouteResult = (CreatedAtRouteResult)result; // Assert Assert.AreEqual(201, createdAtRouteResult.StatusCode); Assert.IsNotNull(createdAtRouteResult.Value); Assert.IsNotNull(createdDelivery); Assert.AreEqual(DeliveryStage.Created, createdDelivery.Stage); deliveryStatusEventRepository.VerifyAll(); }
public async Task <IActionResult> Delete(string id) { logger.LogInformation("In Delete action with id: {Id}", id); var delivery = await deliveryRepository.GetAsync(id); if (delivery == null) { logger.LogDebug("Delivery id: {Id} not found", id); return(NotFound()); } // Adds the delivery cancelled status event var deliveryTrackingEvent = new DeliveryTrackingEvent { DeliveryId = id, Stage = DeliveryStage.Cancelled }; await deliveryTrackingRepository.AddAsync(deliveryTrackingEvent); // forwards cancelled delivery to the Delivery History var allTrackingEvents = await deliveryTrackingRepository.GetByDeliveryIdAsync(id); await deliveryHistoryService.CancelAsync(delivery, allTrackingEvents.ToArray()); // logical delivery deletion await deliveryRepository.DeleteAsync(id, delivery); return(Ok()); }
public async Task <IActionResult> Put([FromBody] Delivery delivery, string id) { logger.LogInformation("In Put action with delivery {Id}: {@DeliveryInfo}", id, delivery.ToLogInfo()); try { var internalDelivery = delivery.ToInternal(); // Adds new inflight delivery await deliveryRepository.CreateAsync(internalDelivery); // Adds the delivery created status event var deliveryTrackingEvent = new DeliveryTrackingEvent { DeliveryId = delivery.Id, Stage = DeliveryStage.Created }; await deliveryTrackingRepository.AddAsync(deliveryTrackingEvent); return(CreatedAtRoute("GetDelivery", new { id = delivery.Id }, delivery)); } catch (DuplicateResourceException) { //This method is mainly used to create deliveries. If the delivery already exists then update logger.LogInformation("Updating resource with delivery id: {DeliveryId}", id); var internalDelivery = delivery.ToInternal(); // Updates inflight delivery await deliveryRepository.UpdateAsync(id, internalDelivery); return(NoContent()); } }
public async Task Delete_SendsMessageWithCancelledTrackingEvent() { // Arrange DeliveryTrackingEvent cancelledDelivery = null; DeliveryTrackingEvent[] allTrackingEvents = null; var deliveryRepository = new Mock <IDeliveryRepository>(); deliveryRepository.Setup(r => r.GetAsync("deliveryid")).ReturnsAsync(delivery); var deliveryHistoryService = new Mock <IDeliveryHistoryService>(); deliveryHistoryService.Setup(r => r.CancelAsync(It.IsAny <InternalDelivery>(), It.IsAny <DeliveryTrackingEvent[]>())) .Returns(Task.CompletedTask) .Callback <InternalDelivery, DeliveryTrackingEvent[]>((d, es) => { allTrackingEvents = es; }); var deliveryStatusEventRepository = new Mock <IDeliveryTrackingEventRepository>(); deliveryStatusEventRepository.Setup(r => r.AddAsync(It.IsAny <DeliveryTrackingEvent>())) .Returns(Task.CompletedTask) .Callback <DeliveryTrackingEvent>(e => cancelledDelivery = e); deliveryStatusEventRepository.Setup(r => r.GetByDeliveryIdAsync("deliveryid")) .ReturnsAsync(new ReadOnlyCollection <DeliveryTrackingEvent>(new List <DeliveryTrackingEvent>() { cancelledDelivery })); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(new Mock <ILogger>().Object); var target = new DeliveriesController(deliveryRepository.Object, new Mock <INotifyMeRequestRepository>().Object, new Mock <INotificationService>().Object, deliveryHistoryService.Object, deliveryStatusEventRepository.Object, loggerFactory.Object); // Act await target.Delete("deliveryid"); // Assert Assert.IsNotNull(cancelledDelivery); Assert.AreEqual("deliveryid", cancelledDelivery.DeliveryId); Assert.AreEqual(DeliveryStage.Cancelled, cancelledDelivery.Stage); deliveryRepository.VerifyAll(); deliveryHistoryService.Verify(s => s.CancelAsync(delivery, allTrackingEvents), Times.Once); }
public async Task Confirm_DeletesDeliveryLogically() { // Arrange InternalDelivery confirmedDelivery = null; DeliveryTrackingEvent completedDelivery = null; var deliveryRepository = new Mock <IDeliveryRepository>(); deliveryRepository.Setup(r => r.GetAsync("deliveryid")).ReturnsAsync(delivery); deliveryRepository.Setup(r => r.DeleteAsync("deliveryid", It.IsAny <InternalDelivery>())) .Returns(Task.CompletedTask) .Callback <string, InternalDelivery>((i, d) => confirmedDelivery = d); var deliveryStatusEventRepository = new Mock <IDeliveryTrackingEventRepository>(); deliveryStatusEventRepository.Setup(r => r.AddAsync(It.IsAny <DeliveryTrackingEvent>())) .Returns(Task.CompletedTask) .Callback <DeliveryTrackingEvent>(e => completedDelivery = e); deliveryStatusEventRepository.Setup(r => r.GetByDeliveryIdAsync("deliveryid")) .ReturnsAsync(new ReadOnlyCollection <DeliveryTrackingEvent>(new List <DeliveryTrackingEvent>() { completedDelivery })); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(new Mock <ILogger>().Object); var target = new DeliveriesController(deliveryRepository.Object, new Mock <INotifyMeRequestRepository>().Object, new Mock <INotificationService>().Object, new Mock <IDeliveryHistoryService>().Object, deliveryStatusEventRepository.Object, loggerFactory.Object); var location = new Location(1, 2, 3); var confirmation = new Confirmation(new DateTimeStamp("datetimevalue"), location, ConfirmationType.Picture, "confirmationblob"); // Act var result = await target.Confirm("deliveryid", confirmation) as OkResult; // Assert Assert.IsNotNull(result); deliveryRepository.Verify(r => r.DeleteAsync("deliveryid", confirmedDelivery)); Assert.AreEqual(location, confirmedDelivery.Dropoff); }
public async Task Patch_AddsRescheduledDeliveryEvent() { // Arrange DeliveryTrackingEvent deliveryTrackingEvent = null; var rescheduledDelivery = new RescheduledDelivery(new Location(2, 2, 2), new Location(3, 3, 3), "newdeadline"); var deliveryRepository = new Mock <IDeliveryRepository>(); deliveryRepository.Setup(r => r.GetAsync("deliveryid")).ReturnsAsync(delivery); var deliveryStatusEventRepository = new Mock <IDeliveryTrackingEventRepository>(); deliveryStatusEventRepository.Setup(r => r.AddAsync(It.IsAny <DeliveryTrackingEvent>())) .Returns(Task.CompletedTask) .Callback <DeliveryTrackingEvent>(e => deliveryTrackingEvent = e); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(new Mock <ILogger>().Object); var target = new DeliveriesController(deliveryRepository.Object, new Mock <INotifyMeRequestRepository>().Object, new Mock <INotificationService>().Object, new Mock <IDeliveryHistoryService>().Object, deliveryStatusEventRepository.Object, loggerFactory.Object); // Act var result = await target.Patch("deliveryid", rescheduledDelivery) as OkResult; // Assert Assert.IsNotNull(result); Assert.IsNotNull(deliveryTrackingEvent); Assert.AreEqual(DeliveryStage.Rescheduled, deliveryTrackingEvent.Stage); deliveryStatusEventRepository.VerifyAll(); }
public async Task Confirm_SendsNotifications() { // Arrange DeliveryTrackingEvent completedDelivery = null; var deliveryRepository = new Mock <IDeliveryRepository>(); deliveryRepository.Setup(r => r.GetAsync("deliveryid")).ReturnsAsync(delivery); var notifyMeRequests = new List <InternalNotifyMeRequest>(); notifyMeRequests.Add(new InternalNotifyMeRequest { DeliveryId = "deliveryid", EmailAddress = "*****@*****.**", SMS = "1111111" }); notifyMeRequests.Add(new InternalNotifyMeRequest { DeliveryId = "deliveryid", EmailAddress = "*****@*****.**", SMS = "2222222" }); var notifyMeRequestRepository = new Mock <INotifyMeRequestRepository>(); notifyMeRequestRepository.Setup(r => r.GetAllByDeliveryIdAsync("deliveryid")) .ReturnsAsync(notifyMeRequests); int notificationServiceCalled = 0; var notificationService = new Mock <INotificationService>(); notificationService.Setup(s => s.SendNotificationsAsync(It.IsAny <InternalNotifyMeRequest>())) .Returns(Task.CompletedTask) .Callback(() => notificationServiceCalled++); var deliveryStatusEventRepository = new Mock <IDeliveryTrackingEventRepository>(); deliveryStatusEventRepository.Setup(r => r.GetByDeliveryIdAsync("deliveryid")) .ReturnsAsync(new ReadOnlyCollection <DeliveryTrackingEvent>(new List <DeliveryTrackingEvent>() { completedDelivery })); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(new Mock <ILogger>().Object); var target = new DeliveriesController(deliveryRepository.Object, notifyMeRequestRepository.Object, notificationService.Object, deliveryStatusEventRepository.Object, loggerFactory.Object); var confirmation = new Confirmation(new DateTimeStamp("datetimevalue"), new Location(1, 2, 3), ConfirmationType.Picture, "confirmationblob"); // Acts var result = await target.Confirm("deliveryid", confirmation) as OkResult; // Assert Assert.IsNotNull(result); Assert.AreEqual(2, notificationServiceCalled); }
public async Task AddAsync(DeliveryTrackingEvent deliveryTrackingEvent) { //FC: log with scope and timing await RedisCache <DeliveryTrackingEvent> .CreateItemAsync(deliveryTrackingEvent).ConfigureAwait(continueOnCapturedContext: false); }
public async Task Confirm_SendsMessageCompleteToDeliveryHistory() { // Arrange InternalDelivery confirmedDelivery = null; InternalConfirmation sentConfirmation = null; DeliveryTrackingEvent completedDelivery = null; DeliveryTrackingEvent[] allTrackingEvents = null; var deliveryRepository = new Mock <IDeliveryRepository>(); deliveryRepository.Setup(r => r.GetAsync("deliveryid")).ReturnsAsync(delivery); var notifyMeRequestRepository = new Mock <INotifyMeRequestRepository>(); notifyMeRequestRepository.Setup(r => r.GetAllByDeliveryIdAsync("deliveryid")) .ReturnsAsync(new List <InternalNotifyMeRequest>()); var deliveryStatusEventRepository = new Mock <IDeliveryTrackingEventRepository>(); deliveryStatusEventRepository.Setup(r => r.AddAsync(It.IsAny <DeliveryTrackingEvent>())) .Returns(Task.CompletedTask) .Callback <DeliveryTrackingEvent>(e => completedDelivery = e); deliveryStatusEventRepository.Setup(r => r.GetByDeliveryIdAsync("deliveryid")) .ReturnsAsync(new ReadOnlyCollection <DeliveryTrackingEvent>(new List <DeliveryTrackingEvent>() { completedDelivery })); var deliveryHistoryService = new Mock <IDeliveryHistoryService>(); deliveryHistoryService.Setup(r => r.CompleteAsync(It.IsAny <InternalDelivery>(), It.IsAny <InternalConfirmation>(), It.IsAny <DeliveryTrackingEvent[]>())) .Returns(Task.CompletedTask) .Callback <InternalDelivery, InternalConfirmation, DeliveryTrackingEvent[]>((d, c, es) => { confirmedDelivery = d; sentConfirmation = c; allTrackingEvents = es; }); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(new Mock <ILogger>().Object); var target = new DeliveriesController(deliveryRepository.Object, notifyMeRequestRepository.Object, new Mock <INotificationService>().Object, deliveryHistoryService.Object, deliveryStatusEventRepository.Object, loggerFactory.Object); var confirmation = new Confirmation(new DateTimeStamp("datetimevalue"), new Location(1, 2, 3), ConfirmationType.Picture, "confirmationblob"); // Act var result = await target.Confirm("deliveryid", confirmation) as OkResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("datetimevalue", sentConfirmation.DateTime.DateTimeValue); Assert.AreEqual(1, sentConfirmation.GeoCoordinates.Altitude); Assert.AreEqual(2, sentConfirmation.GeoCoordinates.Latitude); Assert.AreEqual(3, sentConfirmation.GeoCoordinates.Longitude); Assert.AreEqual(ConfirmationType.Picture, sentConfirmation.ConfirmationType); Assert.AreEqual("confirmationblob", sentConfirmation.ConfirmationBlob); deliveryHistoryService.Verify(s => s.CompleteAsync(confirmedDelivery, It.IsAny <InternalConfirmation>(), allTrackingEvents), Times.Once); }