public void Delete_Handles_Item_Not_Found()
        {
            // Arrange
            var mockLogger = new Mock<ILog>();
            var mockMessageCache = new Mock<IInventoryTakeMessageCache>();
            var mockMessageQueue = new Mock<IMessageQueueService>();
            var mockNotificationService = new Mock<INotificationService>();
            var mockRepository = new Mock<IInventoryRepository>();
            var settings = new ApplicationSettings(false);

            var item = new InventoryItem
            {
                Label = "test",
                Expires = DateTime.Now.AddDays(30)
            };

            InventoryItem output = null;

            mockRepository.Setup(r => r.Remove(item)).Returns(output);

            var controller = GetAccountControllerWithContext(mockLogger, mockMessageCache,
                mockMessageQueue, mockNotificationService, mockRepository, settings);

            // Act
            HttpResponseMessage response = null;

            Assert.DoesNotThrow(() => response = controller.Delete(item));

            // Assert
            Assert.NotNull(response);
            Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);

            mockMessageQueue.Verify(n => n.QueueInventoryItem(item), Times.Never);
        }
        public void QueueInventoryItem(InventoryItem item)
        {
            var client = QueueClient.CreateFromConnectionString(_settings.ServiceBusConnection, _settings.QueueName);
            var message = new BrokeredMessage {Label = item.Label};
            client.Send(message);

            client.Close();
        }
        public InventoryItem Remove(InventoryItem item)
        {
            try
            {
                if (_source.ContainsKey(item.Label.ToLower()))
                {
                    _source.Remove(item.Label);
                    return item;
                }

                return null;
            }
            catch (Exception ex)
            {
                throw new InventoryFatalException(ex);
            }
        }
        public InventoryItem Create(InventoryItem item)
        {
            try
            {
                item.Id = Guid.NewGuid();
                item.Created = DateTime.UtcNow;

                if (!_source.ContainsKey(item.Label.ToLower()))
                {
                    _source.Add(item.Label.ToLower(), item);
                    return item;
                }

                // Duplicate. Using null here as throwing an exception costs overhead.
                return null;
            }
            catch (Exception ex)
            {
                throw new InventoryFatalException(ex);
            }
        }
        public void Delete_Handles_DataSource_Failure_Gracefully()
        {
            // Arrange
            var mockLogger = new Mock<ILog>();
            var mockMessageCache = new Mock<IInventoryTakeMessageCache>();
            var mockMessageQueue = new Mock<IMessageQueueService>();
            var mockNotificationService = new Mock<INotificationService>();
            var mockRepository = new Mock<IInventoryRepository>();
            var settings = new ApplicationSettings(false);

            var item = new InventoryItem
            {
                Label = "test",
                Expires = DateTime.Now.AddDays(30)
            };

            mockRepository.Setup(r => r.Remove(item)).Throws(new InventoryFatalException(new Exception()));

            var controller = GetAccountControllerWithContext(mockLogger, mockMessageCache,
                mockMessageQueue, mockNotificationService, mockRepository, settings);

            // Act
            HttpResponseMessage response = null;

            Assert.DoesNotThrow(() => response = controller.Delete(item));

            // Assert
            Assert.NotNull(response);
            Assert.AreEqual(HttpStatusCode.ServiceUnavailable, response.StatusCode);

            mockMessageQueue.Verify(n => n.QueueInventoryItem(item), Times.Never);
            mockMessageQueue.Verify(n => n.FlushLocalCache(mockMessageCache.Object), Times.Never);

            mockNotificationService
                .Verify(n => n.SendEmail(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
        }
 public void CacheInventoryItem(IInventoryTakeMessageCache cache, InventoryItem item)
 {
     cache.Items.Add(item);
 }
        public void Post_Succeeds()
        {
            // Arrange
            var mockLogger = new Mock<ILog>();
            var mockMessageCache = new Mock<IInventoryTakeMessageCache>();
            var mockMessageQueue = new Mock<IMessageQueueService>();
            var mockNotificationService = new Mock<INotificationService>();
            var mockRepository = new Mock<IInventoryRepository>();
            var settings = new ApplicationSettings(false);

            var item = new InventoryItem
            {
                Label = "test",
                Expires = DateTime.Now.AddDays(30)
            };

            mockRepository.Setup(r => r.Create(item)).Returns(item);

            var controller = GetAccountControllerWithContext(mockLogger, mockMessageCache,
                mockMessageQueue, mockNotificationService, mockRepository, settings);

            // Act
            HttpResponseMessage response = null;

            Assert.DoesNotThrow(() => response = controller.Post(item));

            // Assert
            Assert.NotNull(response);
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
        }