public void ThenDelegatesToVehicleRepository()
        {
            var vehicleForm = new VehicleFormModel { VehicleId = DefaultVehicleId };

            _vehicleRepo
                .Setup(vr => vr.GetVehicle(UserId, DefaultVehicleId))
                .Returns(new Model.Vehicle());

            var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object);
            handler.Execute(UserId, vehicleForm, null);

            _vehicleRepo.Verify(r => r.Update(It.IsAny<Model.Vehicle>()), Times.Once());
        }
        public void ForOtherUser_ThenThrows()
        {
            const int anotherUserId = 87;

            var vehicleForm = new VehicleFormModel { Name = "vehicle", VehicleId = DefaultVehicleId };

            _vehicleRepo
                .Setup(vr => vr.GetVehicle(anotherUserId, DefaultVehicleId))
                .Throws(new InvalidOperationException());

            var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object);

            Assert.Throws<BusinessServicesException>(() => handler.Execute(anotherUserId, vehicleForm, null));
        }
        public void WihtNewPhoto_ThenDelegatesToPhotoRepositoryAddNewPhoto()
        {
            var vehicleForm = new VehicleFormModel { VehicleId = DefaultVehicleId };

            _vehicleRepo
                .Setup(r => r.GetVehicle(UserId, DefaultVehicleId))
                .Returns(new Model.Vehicle { VehicleId = DefaultVehicleId });
            
            var newPhotoFile = Mock.MockPhotoStream().Object;

            var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object);
            handler.Execute(UserId, vehicleForm, newPhotoFile);

            _photoRepo.Verify(r => r.Create(DefaultVehicleId, It.IsAny<Model.VehiclePhoto>()), Times.Once());
        }
        public void WithExistingPhoto_ThenDelegatesToPhotoRepositoryToDeleteOldPhoto()
        {
            const int vehiclePhotoId = 300;
            var vehicleForm = new VehicleFormModel { VehicleId = DefaultVehicleId };

            _vehicleRepo
                .Setup(vr => vr.GetVehicle(UserId, DefaultVehicleId))
                .Returns(new Model.Vehicle { VehicleId = DefaultVehicleId, PhotoId = vehiclePhotoId });

            var newPhotoFile = Mock.MockPhotoStream().Object;

            var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object);
            handler.Execute(UserId, vehicleForm, newPhotoFile);

            _photoRepo.Verify(r => r.Delete(vehiclePhotoId), Times.Once());
        }
        public void Throws_ThenWrapsException()
        {
            var vehicleForm = new VehicleFormModel { Name = "vehicle", VehicleId = DefaultVehicleId };

            _vehicleRepo
                .Setup(vr => vr.GetVehicle(UserId, DefaultVehicleId))
                .Throws(new InvalidOperationException());

            var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object);

            Exception ex = Assert.Throws<BusinessServicesException>(() => handler.Execute(UserId, vehicleForm, null));

            Assert.NotNull(ex.InnerException);
            Assert.IsType<InvalidOperationException>(ex.InnerException);
        }
        public void ThatAndVehicleDoesNotExist_ThenThrowsNonExistent_ThenThrows()
        {
            // this is the same test as FromOtherUsers_ThenThrows
            const int nonExistentVehicleId = 87;
            var vehicleForm = new VehicleFormModel { Name = "vehicle", VehicleId = nonExistentVehicleId };

            // the repo throws an exception when it can't find a match with both the user and the vehicle
            _vehicleRepo
                .Setup(vr => vr.GetVehicle(UserId, It.IsAny<int>()))
                .Throws(new InvalidOperationException());

            var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object);

            Assert.Throws<BusinessServicesException>(() => handler.Execute(UserId, vehicleForm, null));
        }
 public PutVehicleController(UpdateVehicle updateVehicle)
 {
     this.updateVehicle = updateVehicle;
 }