public void UpdateAllowedNumberOfPeople_WhenException_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "UpdateNbPeopleException", 1000, maxNbPeople: 30); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); var mockTripRepo = new Mock <ITripRepository>(); mockTripRepo.Setup(s => s.UpdateTrip(reloadedTrip)).Throws(new Exception("ExceptionForTest")); var secondServices = new TripServices(mockTripRepo.Object, new PotRepository(), new TripParticipantRepository(), new UserTripRepository(), new PotUserRepository()); secondServices.UpdateAllowedNumberOfPeople(trip, 50); Assert.IsTrue(secondServices.HasErrors); reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(30, reloadedTrip.NumberMaxOfPeople); var potUser = new PotUserRepository().GetPotUser(reloadedTrip.TripPot.Id, 1); Assert.IsNotNull(potUser); Assert.AreEqual(reloadedTrip.Price / 30, potUser.TargetAmount); }
public void Quit_WhenError_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "QuitError"); var services = new TripServices(); var userTrip = ModelTestHelper.CreateUserTrip(2, trip.TripName); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); services.Participate(reloadedTrip, 2, "aSecondUser"); Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors)); var mockUserTripRepo = new Mock <IUserTripRepository>(); mockUserTripRepo.Setup(s => s.GetUserTrip(2, reloadedTrip.TripName)).Returns(userTrip); mockUserTripRepo.Setup(s => s.HasErrors).Returns(true); mockUserTripRepo.Setup(s => s.Errors).Returns(new List <string>()); var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(), mockUserTripRepo.Object, new PotUserRepository()); secondServices.Quit(reloadedTrip, 2, "aSecondUser"); Assert.IsTrue(secondServices.HasErrors); Assert.AreEqual(2, services.GetParticipants(reloadedTrip.Id).Count()); }
public void UpdatePrice_WhenError_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "UpdatePriceError", 1000, maxNbPeople: 30); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); var mockTripRepo = new Mock <ITripRepository>(); mockTripRepo.Setup(s => s.UpdateTrip(reloadedTrip)).Callback(() => new TripRepository().UpdateTrip(reloadedTrip)); mockTripRepo.SetupGet(s => s.HasErrors).Returns(true); mockTripRepo.SetupGet(s => s.Errors).Returns(new List <string>()); var secondServices = new TripServices(mockTripRepo.Object, new PotRepository(), new TripParticipantRepository(), new UserTripRepository(), new PotUserRepository()); secondServices.UpdatePrice(trip, 2000.26); Assert.IsTrue(secondServices.HasErrors); reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1000, reloadedTrip.Price); Assert.IsNotNull(reloadedTrip.TripPot); Assert.AreEqual(1000, reloadedTrip.TripPot.TargetAmount); var potUser = new PotUserRepository().GetPotUser(reloadedTrip.TripPot.Id, 1); Assert.IsNotNull(potUser); Assert.AreEqual(1000 / (double)reloadedTrip.NumberMaxOfPeople, potUser.TargetAmount); var userTrip = new UserTripRepository().GetUserTrip(1, trip.TripName); Assert.IsNotNull(userTrip); Assert.AreEqual(1000, userTrip.TripAmount); }
public void DeleteTrip_WhenValid_ShouldCommit() { var trip = ModelTestHelper.CreateTrip(-1, "deleteTripCommit"); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); var pot = reloadedTrip.TripPot; Assert.IsNotNull(pot); var participants = reloadedTrip.Participants; Assert.AreEqual(1, participants.Count()); var potUsers = pot.Participants; Assert.AreEqual(1, potUsers.Count()); var tripRepo = new TripRepository(); var potRepo = new PotRepository(); var userTripRepo = new UserTripRepository(); var potUserRepo = new PotUserRepository(); var tripParticipantRepo = new TripParticipantRepository(); services.DeleteTrip(reloadedTrip); Assert.IsFalse(services.HasErrors); Assert.IsNull(tripRepo.GetTrip(reloadedTrip.Id)); Assert.IsNull(potRepo.GetPot(pot.Id)); Assert.AreEqual(0, userTripRepo.GetUserTripsByTrip(reloadedTrip.TripName).Count()); Assert.AreEqual(0, potUserRepo.GetPotUsers(pot.Id).Count()); Assert.AreEqual(0, tripParticipantRepo.GetTripParticipants(reloadedTrip.Id).Count()); }
public void DeleteTrip_WhenError_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "deleteTripError"); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); var userTrip = ModelTestHelper.CreateUserTrip(1, trip.TripName); var mockUserTripRepo = new Mock <IUserTripRepository>(); mockUserTripRepo.Setup(s => s.GetUserTripsByTrip(trip.TripName)).Returns(new List <UserTrip> { userTrip }); mockUserTripRepo.SetupGet(s => s.HasErrors).Returns(true); mockUserTripRepo.SetupGet(s => s.Errors).Returns(new List <string>()); var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(), mockUserTripRepo.Object, new PotUserRepository()); secondServices.DeleteTrip(reloadedTrip); Assert.IsTrue(secondServices.HasErrors); reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); }
public TripServices GetItinerary(string tripRefNumber) { //validate triprefnumber for '-' or emptystring if (string.IsNullOrWhiteSpace(tripRefNumber)) { throw new ArgumentException("TripRefNumber cannot be empty"); } if (!tripRefNumber.Contains("-")) { throw new ArgumentException("Provider a valid trip ref number Ex.Wil5T-000100 "); } var tripId = tripRefNumber.Split('-')[1]; //Initialize db var ktdb = new KT.DAL.KTdb(); //Check if this TripId is already in db then return from db var tripIdInt = Convert.ToInt32(tripId); var tripServiceId = ktdb.ExecuteScalar <int>("select Id from TripServices where Id = ?", tripIdInt); //return from db if pk is already exists if (tripServiceId > 0) { return(ktdb.Get <TripServices>(x => x.Id == tripIdInt)); } //Call webservice to fetch data related to trip this tripId apiUri = string.Format("{0}/{1}", "trips", tripId); var tripObject = new KTApi <TripDto>().Get(apiUri); //Insert to db object var tripService = new TripServices() { Id = tripObject.TripId, Name = tripObject.TripName, ItineraryId = Convert.ToInt32(tripObject.SystemOfRecordId.Replace("KT:IT-", "")), RefNum = tripObject.TripReference, StartDate = tripObject.TripStartDate, NoOfDays = tripObject.NumberOfDays, NoOfPeople = tripObject.NumberOfPeople, GroupName = tripObject.GroupName, ImageSrc = "", IsArchived = 0 }; //insert to db var insertCount = ktdb.Insert(tripService); return(tripService); }
public void Start() { this.server = new Server { Services = { TripServices.BindService(tripServices) }, Ports = { { this.host, this.port, ServerCredentials.Insecure } } }; server.Start(); Logger.Info("Server started, listening on " + this.port); Logger.Info("Press any key to stop the server..."); Console.ReadKey(); server.ShutdownAsync().Wait(); }
public void CreateTrip_WhenExceptionIsThrown_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "createTripRollback"); var tripRepo = new TripRepository(); var mockPotRepo = new Mock <IPotRepository>(); mockPotRepo.Setup(s => s.SavePot(It.IsAny <Pot>())).Throws(new Exception("RollbackException")); var services = new TripServices(tripRepo, mockPotRepo.Object, new TripParticipantRepository(), new UserTripRepository(), new PotUserRepository()); services.CreateTrip(trip, 1); var secondServices = new TripServices(); Assert.IsNull(secondServices.GetTrip(trip.Id)); }
public void Participate_WhenValid_ShouldCommit() { var trip = ModelTestHelper.CreateTrip(-1, "participateCommit"); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.AreEqual(1, reloadedTrip.Participants.Count()); Assert.IsNotNull(reloadedTrip); services.Participate(reloadedTrip, 2, "aSecondUser"); Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors)); Assert.AreEqual(2, services.GetParticipants(reloadedTrip.Id).Count()); Assert.AreEqual(2, new UserTripRepository().GetUserTripsByTrip(reloadedTrip.TripName).Count()); Assert.AreEqual(2, new PotUserRepository().GetPotUsers(reloadedTrip.TripPot.Id).Count()); }
public void CreateTrip_WhenErrorDuringProcess_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "createTripError"); var tripRepo = new TripRepository(); var potRepo = new PotRepository(); var mockPotRepo = new Mock <IPotRepository>(); mockPotRepo.Setup(s => s.SavePot(It.IsAny <Pot>())).Callback(() => potRepo.SavePot(It.IsAny <Pot>())); mockPotRepo.SetupGet(s => s.HasErrors).Returns(true); mockPotRepo.SetupGet(s => s.Errors).Returns(new List <string>()); var services = new TripServices(tripRepo, mockPotRepo.Object, new TripParticipantRepository(), new UserTripRepository(), new PotUserRepository()); services.CreateTrip(trip, 1); var secondServices = new TripServices(); Assert.IsNull(secondServices.GetTrip(trip.Id)); }
public void UpdateAllowedNumberOfPeople_WhenValid_ShouldCommit() { var trip = ModelTestHelper.CreateTrip(-1, "UpdateNbPeopleValid", 1000, maxNbPeople: 30); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); services.UpdateAllowedNumberOfPeople(reloadedTrip, 50); reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(50, reloadedTrip.NumberMaxOfPeople); var potUser = new PotUserRepository().GetPotUser(reloadedTrip.TripPot.Id, 1); Assert.IsNotNull(potUser); Assert.AreEqual(reloadedTrip.Price / 50, potUser.TargetAmount); }
public void Quit_WhenValid_ShouldCommit() { var trip = ModelTestHelper.CreateTrip(-1, "QuitCommit", 1000); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); services.Participate(reloadedTrip, 2, "aSecondUser"); Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors)); Assert.IsNotNull(reloadedTrip.TripPot); var pot = reloadedTrip.TripPot; pot.CurrentAmount = 200; var potRepo = new PotRepository(); potRepo.UpdatePot(pot); Assert.IsFalse(potRepo.HasErrors); var potUserRepo = new PotUserRepository(); var potUser = potUserRepo.GetPotUser(pot.Id, 2); Assert.IsFalse(potUserRepo.HasErrors); Assert.IsNotNull(potUser); potUser.Amount = 200; potUserRepo.UpdatePotUser(potUser); Assert.IsFalse(potUserRepo.HasErrors); services.Quit(reloadedTrip, 2, "aSecondUser"); Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors)); reloadedTrip = services.GetTrip(reloadedTrip.Id); Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors)); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); pot = reloadedTrip.TripPot; Assert.IsNotNull(pot); Assert.AreEqual(0, pot.CurrentAmount); Assert.AreEqual(1, pot.Participants.Count()); Assert.AreEqual(1, new UserTripRepository().GetUserTripsByTrip(reloadedTrip.TripName).Count()); }
public void Participate_WhenException_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "participateException"); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.AreEqual(1, reloadedTrip.Participants.Count()); Assert.IsNotNull(reloadedTrip); var mockUserTripRepo = new Mock <IUserTripRepository>(); mockUserTripRepo.Setup(s => s.SaveUserTrip(It.IsAny <UserTrip>())).Throws(new Exception("Exception")); var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(), mockUserTripRepo.Object, new PotUserRepository()); secondServices.Participate(reloadedTrip, 2, "aSecondUser"); Assert.IsTrue(secondServices.HasErrors); Assert.AreEqual(1, services.GetParticipants(reloadedTrip.Id).Count()); }
public void DeleteTrip_WhenException_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "deleteTripException"); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); var mockUserTripRepo = new Mock <IUserTripRepository>(); mockUserTripRepo.Setup(s => s.GetUserTripsByTrip(trip.TripName)).Throws(new Exception("Exception")); var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(), mockUserTripRepo.Object, new PotUserRepository()); secondServices.DeleteTrip(reloadedTrip); Assert.IsTrue(secondServices.HasErrors); reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); }
private bool InsertTripDataFromApi(TripDto tripApiResponseObject) { //Insert to db object var tripService = new TripServices() { Id = tripApiResponseObject.TripId, Name = tripApiResponseObject.TripName, ItineraryId = Convert.ToInt32(tripApiResponseObject.SystemOfRecordId.Replace("KT:IT-", "")), RefNum = tripApiResponseObject.TripReference, StartDate = tripApiResponseObject.TripStartDate, NoOfDays = tripApiResponseObject.NumberOfDays, NoOfPeople = tripApiResponseObject.NumberOfPeople, GroupName = tripApiResponseObject.GroupName, ImageSrc = "", IsArchived = 0 }; //Initialize db var ktdb = new KT.DAL.KTdb(); //insert trip var insertCount = ktdb.Insert(tripService); return(true); }
public void Participate_WhenError_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "participateError"); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.AreEqual(1, reloadedTrip.Participants.Count()); Assert.IsNotNull(reloadedTrip); var mockUserTripRepo = new Mock <IUserTripRepository>(); mockUserTripRepo.Setup(s => s.SaveUserTrip(It.IsAny <UserTrip>())).Callback(() => new UserTripRepository().SaveUserTrip(It.IsAny <UserTrip>())); mockUserTripRepo.SetupGet(s => s.HasErrors).Returns(true); mockUserTripRepo.SetupGet(s => s.Errors).Returns(new List <string>()); var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(), mockUserTripRepo.Object, new PotUserRepository()); secondServices.Participate(reloadedTrip, 2, "aSecondUser"); Assert.IsTrue(secondServices.HasErrors); Assert.AreEqual(1, services.GetParticipants(reloadedTrip.Id).Count()); Assert.AreEqual(1, new UserTripRepository().GetUserTripsByTrip(reloadedTrip.TripName).Count()); }
public TripsController(TripServices Services) { _services = Services; }