public void GetServicesByShipper_GivenAValidShipperID_ReturnsServicesByShipperFromDatabase()
        {
            // arrange
            List <DeliveryService> data = new List <DeliveryService>();

            data.Add(new DeliveryService()
            {
                ID = 1, ShipperID = 1
            });
            data.Add(new DeliveryService()
            {
                ID = 2, ShipperID = 2
            });
            data.Add(new DeliveryService()
            {
                ID = 3, ShipperID = 1
            });
            data.Add(new DeliveryService()
            {
                ID = 4, ShipperID = 4
            });
            data.Add(new DeliveryService()
            {
                ID = 5, ShipperID = 1
            });
            List <DeliveryService> expected = new List <DeliveryService>();

            expected.Add(data[0]);
            expected.Add(data[2]);
            expected.Add(data[4]);
            Mock <DbSet <DeliveryService> > mockSet = EntityMockFactory.CreateSet(data.AsQueryable());
            Mock <ShippingDb> mockContext           = new Mock <ShippingDb>();

            mockContext.Setup(c => c.Services).Returns(mockSet.Object);
            DeliveryServiceRepository sut = new DeliveryServiceRepository(mockContext.Object);

            // act
            IEnumerable <DeliveryService> actual = sut.GetServicesByShipper(1);

            // assert
            Assert.IsTrue(Equality.AreEqual(expected, actual));
        }
        public void AddService_GivenANewService_AddsToDatabase()
        {
            // arrange
            DeliveryService service = new DeliveryService()
            {
                ID = 100
            };
            Mock <DbSet <DeliveryService> > mockSet = new Mock <DbSet <DeliveryService> >();
            Mock <ShippingDb> mockContext           = new Mock <ShippingDb>();

            mockContext.Setup(c => c.Services).Returns(mockSet.Object);
            DeliveryServiceRepository sut = new DeliveryServiceRepository(mockContext.Object);

            // act
            sut.AddService(service);

            // assert
            mockSet.Verify(s => s.Add(It.Is <DeliveryService>(a => a.ID == 100)), Times.Once());
            mockContext.Verify(c => c.SaveChanges(), Times.Once());
        }
        public void GetService_GivenAValidID_ReturnsServiceRecordFromDatabase()
        {
            // arrange
            List <DeliveryService> data = new List <DeliveryService>();

            data.Add(new DeliveryService()
            {
                ID = 1, ServiceName = "Service 1"
            });
            data.Add(new DeliveryService()
            {
                ID = 2, ServiceName = "Service 2"
            });
            data.Add(new DeliveryService()
            {
                ID = 3, ServiceName = "Service 3"
            });
            data.Add(new DeliveryService()
            {
                ID = 4, ServiceName = "Service 4"
            });
            DeliveryService expected = data[2];
            Mock <DbSet <DeliveryService> > mockSet = EntityMockFactory.CreateSet(data.AsQueryable());

            mockSet.Setup(s => s.Find(It.Is <int>(a => a == 3))).Returns(expected);
            Mock <ShippingDb> mockContext = new Mock <ShippingDb>();

            mockContext.Setup(c => c.Services).Returns(mockSet.Object);
            DeliveryServiceRepository sut = new DeliveryServiceRepository(mockContext.Object);

            // act
            DeliveryService actual = sut.GetService(3);

            // assert
            Assert.AreEqual(expected, actual);
        }
Example #4
0
        public Message Handle(Message message)
        {
            Message response = new Message();

            response.SessionKey = message.SessionKey;

            if (message.Content.Contains("delivery service plugin"))
            {
                response.Content = "Usage of the Delivery Service Plugin:\n\n[...] order: [amount] [product] [|(extras)], [amount] [product] [|(extras)], ..., to: [name], [street], [zip city]";
                return(response);
            }

            Order       order;
            ObjectCache cache = MemoryCache.Default;

            if ("submit order".Equals(message.Content))
            {
                order = (Order)cache[message.SessionKey.ToString()];
                if (order == null)
                {
                    response.Content = "No order found or order expired!";
                    return(response);
                }
                DeliveryServiceRepository dal = new DeliveryServiceRepository(new DeliveryServiceContext());
                dal.Create(order);
                response.Content = "Your order was successfully submitted!";
                return(response);
            }

            Match  match    = EXTRACT_ORDER_REGEX.Match(message.Content);
            string rawOrder = match.Groups[1].Value;

            string rawAddress = match.Groups[2].Value;

            string[] addressLines = rawAddress.Split(',');

            if (addressLines.Length != 3)
            {
                response.Content = "Error";
                return(response);
            }

            OrderAddress address = new OrderAddress();

            address.Name           = addressLines[0].Trim();
            address.AddressStreet1 = addressLines[1].Trim();
            address.AddressStreet2 = addressLines[2].Trim();

            OrderWrapper orderWrapper = new OrderWrapper(address);

            string[]      positions = rawOrder.Split(',');
            Match         position;
            OrderPosition orderPosition;

            for (int i = 0; i < positions.Length; i++)
            {
                positions[i] = positions[i].Trim();
                if (string.IsNullOrEmpty(positions[i]))
                {
                    continue;
                }
                position      = EXTRACT_POSITION_REGEX.Match(positions[i]);
                orderPosition = new OrderPosition(position.Groups[2].Value, int.Parse(position.Groups[1].Value));
                if (position.Groups[3] != null)
                {
                    orderPosition.Comment = position.Groups[3].Value;
                }
                orderWrapper.Positions.Add(orderPosition);
            }

            order            = new Order();
            order.SessionKey = message.SessionKey.Key;
            order.SetOrder(orderWrapper);

            CacheItemPolicy policy = new CacheItemPolicy();

            policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(2.0);
            cache.Set(message.SessionKey.ToString(), order, policy);

            response.Content = "DeliveryService Plugin TRIGGERED!\n*autistic screeching*REEEEEEEEEEEE!!!!\n\n++++++++++++++++++++++++++++++\n\n" + order.OrderJsonData + "\n\nTo submit your order, please type \"submit order\"!";
            return(response);
        }