public int GetDistance(Address origin, Address destination, Mode mode, Units units) {
            if (origin == null || destination == null) {
                return 0;
            }
            var request =
                (HttpWebRequest)
                    WebRequest.Create(
                        new Uri(String.Format(DistanceMatrixUrl,
                            new object[] {
                                HttpUtility.JavaScriptStringEncode(origin.PostalCode), 
                                HttpUtility.JavaScriptStringEncode(destination.PostalCode), 
                                Enum.GetName(typeof(Mode), mode),
                                Enum.GetName(typeof(Units), units)
                            })));
            var response = (HttpWebResponse) request.GetResponse();
            if (response.StatusCode != HttpStatusCode.OK) return 0;

            using (var streamReader = new StreamReader(response.GetResponseStream())) {
                var result = streamReader.ReadToEnd();
                if (string.IsNullOrEmpty(result)) return 0;
                var jsonObject = JObject.Parse(result);
                var distance = (int)jsonObject.SelectToken("rows[0].elements[0].distance.value");
                return distance / 1000;
            }
        }
        public Address AddAddress(Address address) {
            address.User = _applicationUser;

            _dbContext.Addresses.Add(address);
            _dbContext.SaveChanges();

            return address;
        }
        public Address UpdateAddress(Address address) {
            var existingAddress = _dbContext.Addresses.SingleOrDefault(t => t.User.Id == _applicationUser.Id && t.Id == address.Id);
            if (existingAddress == null) {
                throw new ObjectNotFoundException();
            }
            existingAddress.Name = address.Name;
            existingAddress.AddressLine = address.AddressLine;
            existingAddress.City = address.City;
            existingAddress.PostalCode = address.PostalCode;

            _dbContext.SaveChanges();

            return existingAddress;
        }
        public void GetDistance() {
            //Arrange
            var addressOrigin = new Address {
                Name = "Heineken",
                AddressLine = "Rietveldenweg 25",
                City = "'s-Hertogenbosch",
                PostalCode = "5222 AP"
            };
            var addressDestination = new Address {
                Name = "Grolsch",
                AddressLine = "Brouwerslaan 1",
                City = "Enschede",
                PostalCode = "7548 XA"
            };
            var distanceCalculator = new GoogleDistanceCalculatorService();

            //Act
            var distance = distanceCalculator.GetDistance(addressOrigin, addressDestination, Mode.Driving, Units.Metric);

            //Assert
            Assert.AreEqual(distance, 157);
        }
 public static Address ResolveAddress(this Trip trip, IApplicationDbContext dbContext, string userId, Address address) {
     if (address != null && address.Id > 0) {
         return dbContext.Addresses.SingleOrDefault(a => a.User.Id == userId && a.Id == address.Id);
     }
     return null;
 }
        public void DeleteAddress_CantDeleteIfInUse() {
            Exception caugthException = null;
            var address = new Address {
                Id = 4,
                User = _currentUser
            };
            _fakeApplicationDbContext.Addresses.Add(address);
            _fakeApplicationDbContext.Trips.Add(new Trip { Id = 1, User = _currentUser, AddressOrigin = address });
            var addressService = new AddressService(_fakeApplicationDbContext, _mockCurrentUserService.Object);

            // Act
            var deleted = false;
            try {
                deleted = addressService.DeleteAddress(address.Id);
            } catch (DbEntityValidationException ex) {
                caugthException = ex;
            }

            // Assert
            Assert.IsFalse(deleted);
            Assert.IsNotNull(caugthException);
        }
        public void UpdateAddress_CanUpdate() {
            // Arrange
            var address = new Address {
                Id = 1,
                User = _currentUser,
                Name = "Grolsch",
                AddressLine = "Brouwerslaan 1",
                City = "Enschede",
                PostalCode = "7548 XA"
            };
            _fakeApplicationDbContext.Addresses.Add(address);

            var addressService = new AddressService(_fakeApplicationDbContext, _mockCurrentUserService.Object);

            var updatedAddress = new Address {
                Id = 1,
                Name = "Heineken",
                AddressLine = "Rietveldenweg 25",
                City = "'s-Hertogenbosch",
                PostalCode = "5222 AP"
            };

            // Act
            address = addressService.UpdateAddress(updatedAddress);

            // Assert
            Assert.IsNotNull(updatedAddress);
            Assert.AreEqual(address.Name, "Heineken");
            Assert.AreEqual(address.AddressLine, "Rietveldenweg 25");
            Assert.AreEqual(address.City, "'s-Hertogenbosch");
            Assert.AreEqual(address.PostalCode, "5222 AP");
        }
        public void AddAddress_ShouldThrowValidationError() {
            // Arrange
            Exception caugthException = null;
            var address = new Address {
                Name = "Heineken",
                AddressLine = "Rietveldenweg 25",
                City = "'s-Hertogenbosch",
                PostalCode = null
            };
            var addressService = new AddressService(_fakeApplicationDbContext, _mockCurrentUserService.Object);

            // Act
            try {
                address = addressService.AddAddress(address);
            } catch (DbEntityValidationException ex) {
                caugthException = ex;
            }

            // Assert
            Assert.IsNull(address);
            Assert.IsNotNull(caugthException);
        }
        public void AddAddress_CanAdd() {
            // Arrange
            var address = new Address {
                Name = "Heineken",
                AddressLine = "Rietveldenweg 25",
                City = "'s-Hertogenbosch",
                PostalCode = "5222 AP"
            };
            var addressService = new AddressService(_fakeApplicationDbContext, _mockCurrentUserService.Object);

            // Act
            address = addressService.AddAddress(address);

            // Assert
            Assert.IsNotNull(address);
            Assert.AreEqual(address.Name, "Heineken");
            Assert.AreEqual(address.AddressLine, "Rietveldenweg 25");
            Assert.AreEqual(address.City, "'s-Hertogenbosch");
            Assert.AreEqual(address.PostalCode, "5222 AP");
        }
        public void AddAddress_CanAdd() {
            // Arrange
            var address = new Address {
                Id = 1
            };
            var mockAddressService = new Mock<IAddressService>();
            mockAddressService.Setup(x => x.AddAddress(address))
                .Returns(address);
            var addressController = new AddressesController(mockAddressService.Object) {
                Request = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };

            // Act
            var response = addressController.AddAddress(address);

            // Assert
            Address addedAddress;
            Assert.IsTrue(response.TryGetContentValue(out addedAddress));
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
        }
        public void UpdateTrip_CanUpdate() {
            // Arrange
            var addressOrigin = new Address { Id = 1, User = _currentUser };
            var addressDestination = new Address { Id = 2, User = _currentUser };
            var car = new Car { Id = 1, User = _currentUser };
            var otherCar = new Car { Id = 2, User = _currentUser };
            var otherAddress = new Address { Id = 3, User = _currentUser };
            var trip = new Trip {
                Id = 1,
                User = _currentUser,
                Date =  DateTime.SpecifyKind(new DateTime(2014, 08, 01), DateTimeKind.Utc),
                AddressOrigin = addressOrigin,
                AddressDestination = addressDestination,
                Car = car
            };

            _fakeApplicationDbContext.Addresses.Add(addressOrigin);
            _fakeApplicationDbContext.Addresses.Add(addressDestination);
            _fakeApplicationDbContext.Addresses.Add(otherAddress);
            _fakeApplicationDbContext.Cars.Add(car);
            _fakeApplicationDbContext.Cars.Add(otherCar);
            _fakeApplicationDbContext.Trips.Add(trip);

            var tripService = new TripService(_fakeApplicationDbContext, _mockCurrentUserService.Object, _mockIDistanceCalculatorService.Object);

            var updatedTrip = new Trip {
                Id = 1,
                User = _currentUser,
                Date = DateTime.SpecifyKind(new DateTime(2014, 08, 02), DateTimeKind.Utc),
                AddressOrigin = addressOrigin,
                AddressDestination = otherAddress,
                Car = otherCar,
                Remarks = ""
            };

            // Act
            updatedTrip = tripService.UpdateTrip(updatedTrip);

            // Assert
            Assert.IsNotNull(updatedTrip);
            Assert.IsNotNull(updatedTrip.AddressOrigin);
            Assert.IsNotNull(updatedTrip.AddressDestination);
            Assert.IsNotNull(updatedTrip.Car);
            Assert.AreEqual(new DateTime(2014, 08, 02), updatedTrip.Date);
            Assert.AreEqual(addressOrigin.Id, updatedTrip.AddressOrigin.Id);
            Assert.AreEqual(otherAddress.Id, updatedTrip.AddressDestination.Id);
            Assert.AreEqual(otherCar.Id, updatedTrip.Car.Id);
            Assert.AreEqual(updatedTrip.DistanceInKm, 42);
            Assert.AreEqual(updatedTrip.Id, 1);
        }
        public void AddTrip_CanAdd() {
            // Arrange
            var addressOrigin = new Address { Id = 1, User = _currentUser};
            var addressDestination = new Address { Id = 2, User = _currentUser };
            var car = new Car { Id = 1, User = _currentUser };

            _fakeApplicationDbContext.Addresses.Add(addressOrigin);
            _fakeApplicationDbContext.Addresses.Add(addressDestination);
            _fakeApplicationDbContext.Cars.Add(car);

            var trip = new Trip {
                User = _currentUser,
                Date = new DateTime(2014, 08, 01),
                AddressOrigin = addressOrigin,
                AddressDestination = addressDestination,
                Car = car
            };
            var tripService = new TripService(_fakeApplicationDbContext, _mockCurrentUserService.Object, _mockIDistanceCalculatorService.Object);

            // Act
            trip = tripService.AddTrip(trip);

            // Assert
            Assert.IsNotNull(trip);
            Assert.IsNotNull(trip.AddressOrigin);
            Assert.IsNotNull(trip.AddressDestination);
            Assert.IsNotNull(trip.Car);
            Assert.AreEqual(addressOrigin.Id, trip.AddressOrigin.Id);
            Assert.AreEqual(addressDestination.Id, trip.AddressDestination.Id);
            Assert.AreEqual(car.Id, trip.Car.Id);
            Assert.AreEqual(trip.DistanceInKm, 42);
        }
        public void GetNewTripTemplate_WithLatestUsedCarAndAddress() {
            // Arrange
            var latestUsedAddress = new Address { Id = 1 };
            var otherAddress = new Address { Id = 2 };
            var latestUsedCar = new Car { Id = 1 };
            var otherCar = new Car { Id = 1 };
            _fakeApplicationDbContext.Trips.Add(new Trip { Id = 1, User = _currentUser, Date = new DateTime(2014, 08, 01), AddressDestination = otherAddress, Car = otherCar });
            _fakeApplicationDbContext.Trips.Add(new Trip { Id = 2, User = _currentUser, Date = new DateTime(2014, 07, 01), AddressDestination = otherAddress, Car = otherCar });
            _fakeApplicationDbContext.Trips.Add(new Trip { Id = 3, User = _currentUser, Date = new DateTime(2014, 10, 01), AddressDestination = latestUsedAddress, Car = latestUsedCar });
            _fakeApplicationDbContext.Trips.Add(new Trip { Id = 4, User = _otherUser, Date = new DateTime(2014, 11, 01), AddressDestination = otherAddress, Car = otherCar });
            _fakeApplicationDbContext.Trips.Add(new Trip { Id = 5, User = _currentUser, Date = new DateTime(2014, 05, 01), AddressDestination = otherAddress, Car = otherCar });
            var tripService = new TripService(_fakeApplicationDbContext, _mockCurrentUserService.Object, _mockIDistanceCalculatorService.Object);

            // Act
            var trip = tripService.GetNewTripTemplate();

            // Assert
            Assert.IsNotNull(trip);
            Assert.IsNotNull(trip.AddressOrigin);
            Assert.IsNotNull(trip.Car);
            Assert.AreEqual(latestUsedAddress.Id, trip.AddressOrigin.Id);
            Assert.AreEqual(latestUsedCar.Id, trip.Car.Id);
        }