示例#1
0
        public void ThrowInvalidOperationException_WithMessageContaining_OwnerCannotJoin_WhenOwnerOfATripTriesToJoinTheTrip()
        {
            // Arrange
            var mockedTripRepo          = new Mock <IProjectableRepositoryEf <Trip> >();
            var mockedUserTripRepo      = new Mock <IProjectableRepositoryEf <UsersTrips> >();
            var mockedCityService       = new Mock <ICityService>();
            var mockedTagService        = new Mock <ITagService>();
            var mockedDateTimpeProvider = new Mock <IDateTimeProvider>();
            var mockedMappingProvider   = new Mock <IMappingProvider>();
            var mockedUnitOfWork        = new Mock <IUnitOfWorkEF>();

            var tripService = new TripService(
                () => mockedUnitOfWork.Object,
                mockedUserTripRepo.Object,
                mockedCityService.Object,
                mockedMappingProvider.Object,
                mockedTagService.Object,
                mockedTripRepo.Object,
                mockedDateTimpeProvider.Object);

            var tripId = 1;
            var userId = "userId";

            UsersTrips IsUserOwner = new UsersTrips();

            mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >()))
            .Returns(IsUserOwner);

            Assert.That(() => tripService.RequestToJoinTrip(tripId, userId),
                        Throws.InvalidOperationException.With.Message.Contain("owner"));
        }
示例#2
0
        public void ReturnFalse_WhenFoundTripIsNull()
        {
            // Arrange
            var mockedTripRepo          = new Mock <IProjectableRepositoryEf <Trip> >();
            var mockedUserTripRepo      = new Mock <IProjectableRepositoryEf <UsersTrips> >();
            var mockedCityService       = new Mock <ICityService>();
            var mockedTagService        = new Mock <ITagService>();
            var mockedDateTimpeProvider = new Mock <IDateTimeProvider>();
            var mockedMappingProvider   = new Mock <IMappingProvider>();
            var mockedUnitOfWork        = new Mock <IUnitOfWorkEF>();

            var tripService = new TripService(
                () => mockedUnitOfWork.Object,
                mockedUserTripRepo.Object,
                mockedCityService.Object,
                mockedMappingProvider.Object,
                mockedTagService.Object,
                mockedTripRepo.Object,
                mockedDateTimpeProvider.Object);

            int    tripId = 1;
            string userId = "driverId";

            UsersTrips userTrip = null;

            mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >()))
            .Returns(userTrip);

            // Act
            var result = tripService.SignOutOfTrip(tripId, userId);

            // Assert
            mockedUserTripRepo.Verify(x => x.Delete(It.IsAny <UsersTrips>()), Times.Never);
            Assert.False(result);
        }
示例#3
0
        public void ReturnTrue_WhenUserIsTheOwner()
        {
            // Arrange
            var mockedTripRepo          = new Mock <IProjectableRepositoryEf <Trip> >();
            var mockedUserTripRepo      = new Mock <IProjectableRepositoryEf <UsersTrips> >();
            var mockedCityService       = new Mock <ICityService>();
            var mockedTagService        = new Mock <ITagService>();
            var mockedDateTimpeProvider = new Mock <IDateTimeProvider>();
            var mockedMappingProvider   = new Mock <IMappingProvider>();
            var mockedUnitOfWork        = new Mock <IUnitOfWorkEF>();

            var tripService = new TripService(
                () => mockedUnitOfWork.Object,
                mockedUserTripRepo.Object,
                mockedCityService.Object,
                mockedMappingProvider.Object,
                mockedTagService.Object,
                mockedTripRepo.Object,
                mockedDateTimpeProvider.Object);

            string userId    = "userId";
            int    tripId    = 1;
            var    userTrips = new UsersTrips()
            {
                TripId           = tripId,
                UserId           = userId,
                UserTripStatusId = (int)UserTripStatusType.Owner
            };

            var data = new List <UsersTrips>()
            {
                userTrips
            };

            UsersTrips exp = null;

            mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >()))
            .Returns((Expression <Func <UsersTrips, bool> > predicate) =>
            {
                exp = data.Where(predicate.Compile()).FirstOrDefault();

                return(exp);
            });

            // Act
            var result = tripService.IsUserOwnerOfTrip(userId, tripId);

            // Assert
            Assert.IsTrue(result);
        }
示例#4
0
        public void CallDeleteMethodWithFoundTrip_AndReturnTrue_WhenDeletingIsSuccesfull()
        {
            // Arrange
            var mockedTripRepo          = new Mock <IProjectableRepositoryEf <Trip> >();
            var mockedUserTripRepo      = new Mock <IProjectableRepositoryEf <UsersTrips> >();
            var mockedCityService       = new Mock <ICityService>();
            var mockedTagService        = new Mock <ITagService>();
            var mockedDateTimpeProvider = new Mock <IDateTimeProvider>();
            var mockedMappingProvider   = new Mock <IMappingProvider>();
            var mockedUnitOfWork        = new Mock <IUnitOfWorkEF>();

            mockedUnitOfWork.Setup(x => x.Commit()).Returns(true);

            var tripService = new TripService(
                () => mockedUnitOfWork.Object,
                mockedUserTripRepo.Object,
                mockedCityService.Object,
                mockedMappingProvider.Object,
                mockedTagService.Object,
                mockedTripRepo.Object,
                mockedDateTimpeProvider.Object);

            int    tripId = 1;
            string userId = "driverId";
            Trip   trip   = new Trip()
            {
                Id         = tripId,
                TakenSeats = 2
            };

            var userTrip = new UsersTrips()
            {
                TripId           = tripId,
                Trip             = trip,
                UserId           = userId,
                UserTripStatusId = (int)UserTripStatusType.Accepted
            };

            mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >()))
            .Returns(userTrip);

            // Act
            var result = tripService.SignOutOfTrip(tripId, userId);

            // Assert
            mockedUserTripRepo.Verify(x => x.Delete(userTrip), Times.Once);
            Assert.True(result);
        }
示例#5
0
        public void DecrementTakenSeats_FromTrip()
        {
            // Arrange
            var mockedTripRepo          = new Mock <IProjectableRepositoryEf <Trip> >();
            var mockedUserTripRepo      = new Mock <IProjectableRepositoryEf <UsersTrips> >();
            var mockedCityService       = new Mock <ICityService>();
            var mockedTagService        = new Mock <ITagService>();
            var mockedDateTimpeProvider = new Mock <IDateTimeProvider>();
            var mockedMappingProvider   = new Mock <IMappingProvider>();
            var mockedUnitOfWork        = new Mock <IUnitOfWorkEF>();

            var tripService = new TripService(
                () => mockedUnitOfWork.Object,
                mockedUserTripRepo.Object,
                mockedCityService.Object,
                mockedMappingProvider.Object,
                mockedTagService.Object,
                mockedTripRepo.Object,
                mockedDateTimpeProvider.Object);

            int    tripId = 1;
            string userId = "driverId";
            Trip   trip   = new Trip()
            {
                Id         = tripId,
                TakenSeats = 5
            };

            var userTrip = new UsersTrips()
            {
                TripId           = tripId,
                Trip             = trip,
                UserId           = userId,
                UserTripStatusId = (int)UserTripStatusType.Accepted
            };

            mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >()))
            .Returns(userTrip);

            var expectedSeats = trip.TakenSeats - 1;

            // Act
            tripService.SignOutOfTrip(tripId, userId);

            // Assert
            Assert.AreEqual(expectedSeats, trip.TakenSeats);
        }
示例#6
0
        public void CreateTrip(TripCreationInfo tripInfo)
        {
            var origin = this.cityService.GetCityByName(tripInfo.OriginName);

            if (origin == null)
            {
                origin = this.cityService.CreateCity(tripInfo.OriginName);
            }

            var destination = this.cityService.GetCityByName(tripInfo.DestinationName);

            if (destination == null)
            {
                destination = this.cityService.CreateCity(tripInfo.DestinationName);
            }

            var tags = this.tagService.GetTagsByIds(tripInfo.TagIds) as ICollection <Tag>;

            using (var uow = base.UnitOfWork())
            {
                var trip = this.mappingProvider.Map <TripCreationInfo, Trip>(tripInfo);

                trip.Tags        = tags;
                trip.Origin      = origin;
                trip.Destination = destination;
                trip.DateCreated = this.dateTimeProvider.Now;

                var userTrips = new UsersTrips()
                {
                    Trip             = trip,
                    UserId           = tripInfo.DriverId,
                    UserTripStatusId = (int)UserTripStatusType.Owner
                };

                trip.TripsUsers = new List <UsersTrips>()
                {
                    userTrips
                };

                this.tripRepo.Add(trip);

                uow.Commit();
            }
        }
示例#7
0
        public void AddNewTripUser_WithStatusOfPending_WhenUserRequestToJoin()
        {
            // Arrange
            var mockedTripRepo          = new Mock <IProjectableRepositoryEf <Trip> >();
            var mockedUserTripRepo      = new Mock <IProjectableRepositoryEf <UsersTrips> >();
            var mockedCityService       = new Mock <ICityService>();
            var mockedTagService        = new Mock <ITagService>();
            var mockedDateTimpeProvider = new Mock <IDateTimeProvider>();
            var mockedMappingProvider   = new Mock <IMappingProvider>();
            var mockedUnitOfWork        = new Mock <IUnitOfWorkEF>();

            var tripService = new TripService(
                () => mockedUnitOfWork.Object,
                mockedUserTripRepo.Object,
                mockedCityService.Object,
                mockedMappingProvider.Object,
                mockedTagService.Object,
                mockedTripRepo.Object,
                mockedDateTimpeProvider.Object);

            var tripId = 1;
            var userId = "userId";

            UsersTrips IsUserOwner = null;

            mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >()))
            .Returns(IsUserOwner);

            UsersTrips trip = null;

            mockedUserTripRepo.Setup(x => x.Add(It.IsAny <UsersTrips>()))
            .Callback((UsersTrips createdTrip) => trip = createdTrip);

            var expectedStatusId = (int)UserTripStatusType.Pending;

            // Act
            tripService.RequestToJoinTrip(tripId, userId);

            // Assert
            Assert.AreEqual(tripId, trip.TripId);
            Assert.AreEqual(userId, trip.UserId);
            Assert.AreEqual(expectedStatusId, trip.UserTripStatusId);
        }
示例#8
0
        public void Throw_InvalidOperationException_WhenTryingToDecrementBeyond0()
        {
            // Arrange
            var mockedTripRepo          = new Mock <IProjectableRepositoryEf <Trip> >();
            var mockedUserTripRepo      = new Mock <IProjectableRepositoryEf <UsersTrips> >();
            var mockedCityService       = new Mock <ICityService>();
            var mockedTagService        = new Mock <ITagService>();
            var mockedDateTimpeProvider = new Mock <IDateTimeProvider>();
            var mockedMappingProvider   = new Mock <IMappingProvider>();
            var mockedUnitOfWork        = new Mock <IUnitOfWorkEF>();

            var tripService = new TripService(
                () => mockedUnitOfWork.Object,
                mockedUserTripRepo.Object,
                mockedCityService.Object,
                mockedMappingProvider.Object,
                mockedTagService.Object,
                mockedTripRepo.Object,
                mockedDateTimpeProvider.Object);

            int    tripId = 1;
            string userId = "driverId";
            Trip   trip   = new Trip()
            {
                Id         = tripId,
                TakenSeats = 0
            };

            var userTrip = new UsersTrips()
            {
                TripId           = tripId,
                Trip             = trip,
                UserId           = userId,
                UserTripStatusId = (int)UserTripStatusType.Accepted
            };

            mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >()))
            .Returns(userTrip);

            // Act and Assert
            Assert.Throws <InvalidOperationException>(() => tripService.SignOutOfTrip(tripId, userId));
        }
示例#9
0
        public void Return_False_IfUserIsOwner()
        {
            // Arrange
            var mockedTripRepo          = new Mock <IProjectableRepositoryEf <Trip> >();
            var mockedUserTripRepo      = new Mock <IProjectableRepositoryEf <UsersTrips> >();
            var mockedCityService       = new Mock <ICityService>();
            var mockedTagService        = new Mock <ITagService>();
            var mockedDateTimpeProvider = new Mock <IDateTimeProvider>();
            var mockedMappingProvider   = new Mock <IMappingProvider>();
            var mockedUnitOfWork        = new Mock <IUnitOfWorkEF>();

            var tripService = new TripService(
                () => mockedUnitOfWork.Object,
                mockedUserTripRepo.Object,
                mockedCityService.Object,
                mockedMappingProvider.Object,
                mockedTagService.Object,
                mockedTripRepo.Object,
                mockedDateTimpeProvider.Object);

            int    tripId   = 1;
            string driverId = "driverId";

            var userTrip = new UsersTrips()
            {
                TripId           = tripId,
                UserId           = driverId,
                UserTripStatusId = (int)UserTripStatusType.Owner
            };

            mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >()))
            .Returns(userTrip);

            // Act
            var expected = tripService.SignOutOfTrip(tripId, driverId);

            // Assert
            Assert.False(expected);
        }
示例#10
0
        public void MarkTripAsFinished_WhenParamsAreValid()
        {
            // Arrange
            var mockedTripRepo          = new Mock <IProjectableRepositoryEf <Trip> >();
            var mockedUserTripRepo      = new Mock <IProjectableRepositoryEf <UsersTrips> >();
            var mockedCityService       = new Mock <ICityService>();
            var mockedTagService        = new Mock <ITagService>();
            var mockedDateTimpeProvider = new Mock <IDateTimeProvider>();
            var mockedMappingProvider   = new Mock <IMappingProvider>();
            var mockedUnitOfWork        = new Mock <IUnitOfWorkEF>();

            mockedUnitOfWork.Setup(x => x.Commit()).Returns(true);

            var tripService = new TripService(
                () => mockedUnitOfWork.Object,
                mockedUserTripRepo.Object,
                mockedCityService.Object,
                mockedMappingProvider.Object,
                mockedTagService.Object,
                mockedTripRepo.Object,
                mockedDateTimpeProvider.Object);

            string userId = "userId";
            int    tripId = 1;
            var    trip   = new Trip()
            {
                Id = tripId
            };

            var userTrips = new UsersTrips()
            {
                TripId           = tripId,
                UserId           = userId,
                UserTripStatusId = (int)UserTripStatusType.Owner
            };

            var data = new List <UsersTrips>()
            {
                userTrips
            };

            UsersTrips exp = null;

            mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >()))
            .Returns((Expression <Func <UsersTrips, bool> > predicate) =>
            {
                exp = data.Where(predicate.Compile()).FirstOrDefault();

                return(exp);
            });

            mockedTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <Trip, bool> > >()))
            .Returns(trip);
            // Act
            var result = tripService.MarkTripAsFinished(tripId, userId);

            // Assert
            Assert.IsTrue(trip.IsFinished);
            mockedUnitOfWork.Verify(x => x.Commit(), Times.Once);
            Assert.IsTrue(result);
        }