Пример #1
0
        public async Task DeleteByIdUnauthorizedExceptionHappenedExpect401()
        {
            const string expectedId = "expectedId";

            _mockDeliveryService.Setup(service => service.Delete(_mockExecutionContext.Object, expectedId))
            .Throws <UnauthorizedException>();

            var result = await _deliveriesController.Delete(expectedId);

            Assert.IsType <StatusCodeResult>(result);

            var statusCodeResult = (StatusCodeResult)result;

            Assert.Equal(401, statusCodeResult.StatusCode);
        }
        public async Task Delete_SendsMessageWithCancelledEvent()
        {
            // Arrange
            InternalDelivery    cancelledDelivery = null;
            DeliveryStatusEvent cancelEvent       = null;

            DeliveryStatusEvent[] allEvents = 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 <DeliveryStatusEvent[]>()))
            .Returns(Task.CompletedTask)
            .Callback <InternalDelivery, DeliveryStatusEvent[]>((d, es) =>
            {
                cancelledDelivery = d;
                allEvents         = es;
            });

            var deliveryStatusEventRepository = new Mock <IDeliveryStatusEventRepository>();

            deliveryStatusEventRepository.Setup(r => r.AddAsync(It.IsAny <DeliveryStatusEvent>()))
            .Returns(Task.CompletedTask)
            .Callback <DeliveryStatusEvent>(e => cancelEvent = e);

            deliveryStatusEventRepository.Setup(r => r.GetByDeliveryIdAsync("deliveryid"))
            .ReturnsAsync(new ReadOnlyCollection <DeliveryStatusEvent>(new List <DeliveryStatusEvent>()
            {
                cancelEvent
            }));

            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(cancelEvent);
            Assert.AreEqual("deliveryid", cancelEvent.DeliveryId);
            Assert.AreEqual(DeliveryEventType.Cancelled, cancelEvent.Stage);
            deliveryRepository.VerifyAll();
            deliveryHistoryService.Verify(s => s.CancelAsync(delivery, allEvents), Times.Once);
        }
Пример #3
0
        public async Task Delete_Returns404_IfDeliveryIdNotValid()
        {
            // Arrange
            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,
                                                  new Mock <IDeliveryTrackingEventRepository>().Object,
                                                  loggerFactory.Object);

            // Act
            var result = await target.Delete("invaliddeliveryid") as NotFoundResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(404, result.StatusCode);
        }
Пример #4
0
        public async Task Delete_SendsMessageWithCancelledTrackingEvent()
        {
            // Arrange
            DeliveryTrackingEvent cancelledDelivery = null;

            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 => 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,
                                                  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();
        }