コード例 #1
0
        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 Patch_UpdatesCache()
        {
            // Arrange
            InternalDelivery updatedDelivery = 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);
            deliveryRepository.Setup(r => r.UpdateAsync("deliveryid", It.IsAny <InternalDelivery>()))
            .Returns(Task.CompletedTask)
            .Callback((string i, InternalDelivery d) => updatedDelivery = d);

            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,
                                                  new Mock <IDeliveryStatusEventRepository>().Object,
                                                  loggerFactory.Object);

            // Act
            await target.Patch("deliveryid", rescheduledDelivery);

            // Assert
            //unchanged values
            Assert.AreEqual("deliveryid", updatedDelivery.Id);
            Assert.AreEqual("userid", updatedDelivery.Owner.UserId);

            //updated values
            Assert.AreEqual(2, updatedDelivery.Pickup.Altitude);
            Assert.AreEqual(3, updatedDelivery.Dropoff.Altitude);
            Assert.AreEqual("newdeadline", updatedDelivery.Deadline);

            deliveryRepository.VerifyAll();
        }
        public async Task Patch_AddsRescheduledDeliveryEvent()
        {
            // Arrange
            DeliveryStatusEvent rescheduledEvent = 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 <IDeliveryStatusEventRepository>();

            deliveryStatusEventRepository.Setup(r => r.AddAsync(It.IsAny <DeliveryStatusEvent>()))
            .Returns(Task.CompletedTask)
            .Callback <DeliveryStatusEvent>(e => rescheduledEvent = 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(rescheduledEvent);
            Assert.AreEqual(DeliveryEventType.Rescheduled, rescheduledEvent.Stage);
            deliveryStatusEventRepository.VerifyAll();
        }