public TrailerDto Get(int id) { //Repo dependencies IRepo <Trailer> trailerRepo = this.unitOfWork.TrailerRepo; IRepo <Location> locationRepo = this.unitOfWork.LocationRepo; //Get trailer from repo Trailer trailer = trailerRepo.Get(id); //Ensure trailer exists if (trailer == null) { throw new DoesNotExistException(); } //Get location from repo Location location = locationRepo.Get(trailer.LocationId); //Create dto TrailerDto dto = Mapper.Map <TrailerDto>(trailer); if (location != null) { dto.Location = Mapper.Map <LocationDto>(location); } //Return dto return(dto); }
public static void Compare( TrailerDto expected, TrailerDto actual, bool idEqual = true, bool deletedEqual = true, bool locationIdEqual = true, bool locationDeletedEqual = true ) { Compare( expected.Location, actual.Location, idEqual: locationIdEqual, deletedEqual: locationDeletedEqual ); if (idEqual) { Assert.AreEqual(expected.Id, actual.Id); } else { Assert.AreNotEqual(expected.Id, actual.Id); } if (deletedEqual) { Assert.AreEqual(expected.Deleted, actual.Deleted); } else { Assert.AreNotEqual(expected.Deleted, actual.Deleted); } }
public HttpResponseMessage Update([FromBody] TrailerDto dto) { TrailerDto resDtos = this.trailerService.Update(dto); this.unitOfWork.Save(); return(Request.CreateResponse(HttpStatusCode.OK, resDtos)); }
public void GetNonexistentTrailer() { //Mock context Mock <FortuneDbContext> mockContext = new Mock <FortuneDbContext>(); //Mock repos Mock <IRepo <Location> > mockLocationRepo = new Mock <IRepo <Location> >(); Mock <IRepo <Trailer> > mockTrailerRepo = new Mock <IRepo <Trailer> >(); Mock <IRepo <Load> > mockLoadRepo = new Mock <IRepo <Load> >(); //Test id int testId = 111111; //Mock call mockTrailerRepo.Setup(x => x.Get(testId)).Returns <Trailer>(null); //Unit of work Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>(); mockUnitOfWork.SetupGet(x => x.LocationRepo).Returns(mockLocationRepo.Object); mockUnitOfWork.SetupGet(x => x.TrailerRepo).Returns(mockTrailerRepo.Object); //Trailer service ITrailerService trailerService = new TrailerService(mockUnitOfWork.Object); //Test TrailerDto dto = trailerService.Get(testId); }
public void UpdateTrailer() { //Automapper AutoMapperConfig.RegisterMappings(); //Mock context Mock <FortuneDbContext> mockContext = new Mock <FortuneDbContext>(); //Mock repos Mock <IRepo <Location> > mockLocationRepo = new Mock <IRepo <Location> >(); Mock <IRepo <Trailer> > mockTrailerRepo = new Mock <IRepo <Trailer> >(); Mock <IRepo <Load> > mockLoadRepo = new Mock <IRepo <Load> >(); //Test location Location testLocation = new Location { Id = Guid.NewGuid(), Deleted = false, Name = "TEST" }; //Test trailer Trailer testTrailer = new Trailer { Id = 111111, Deleted = false, LocationId = testLocation.Id }; TrailerDto testDto = Mapper.Map <TrailerDto>(testTrailer); testDto.Location = Mapper.Map <LocationDto>(testLocation); //Mock calls mockLocationRepo.Setup(x => x.Exists(It.Is <Guid>(y => y == testLocation.Id))).Returns(true); mockLocationRepo.Setup(x => x.Get(It.Is <Guid>(y => y == testLocation.Id))).Returns(testLocation); mockTrailerRepo.Setup(x => x.Get(testTrailer.Id)).Returns(testTrailer); //Unit of work Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>(); mockUnitOfWork.SetupGet(x => x.LocationRepo).Returns(mockLocationRepo.Object); mockUnitOfWork.SetupGet(x => x.TrailerRepo).Returns(mockTrailerRepo.Object); //Trailer service ITrailerService trailerService = new TrailerService(mockUnitOfWork.Object); //Test TrailerDto dto = trailerService.Update(testDto); TestUtil.Compare(testDto, dto); }
public TrailerDto Add(TrailerDto dto) { //Repo dependencies IRepo <Location> locationRepo = this.unitOfWork.LocationRepo; IRepo <Trailer> trailerRepo = this.unitOfWork.TrailerRepo; //Get location Location location = locationRepo.Get(dto.Location.Id); //Ensure location exists if (location == null) { throw new DoesNotExistException(); } //Check if trailer trailer exists // If it does, check if it has been soft-deleted // If so, restore it // Else throw exception // Else add new trailer Trailer trailer = trailerRepo.Get(dto.Id); if (trailer == null) { trailer = Mapper.Map <Trailer>(dto); trailerRepo.Insert(trailer); } else if (trailer.Deleted) { trailer.Deleted = false; trailer.LocationId = dto.Location.Id; trailerRepo.Update(trailer); } else { throw new AlreadyExistsException(); } //Create dto dto = Mapper.Map <TrailerDto>(trailer); dto.Location = Mapper.Map <LocationDto>(location); //Return dto return(dto); }
public void DeleteNonexistentTrailer() { //Automapper AutoMapperConfig.RegisterMappings(); //Mock context Mock <FortuneDbContext> mockContext = new Mock <FortuneDbContext>(); //Mock repos Mock <IRepo <Location> > mockLocationRepo = new Mock <IRepo <Location> >(); Mock <IRepo <Trailer> > mockTrailerRepo = new Mock <IRepo <Trailer> >(); Mock <IRepo <Load> > mockLoadRepo = new Mock <IRepo <Load> >(); //Test location Location testLocation = new Location { Id = Guid.NewGuid(), Deleted = false, Name = "TEST" }; //Test trailer Trailer testTrailer = new Trailer { Id = 111111, Deleted = false, LocationId = testLocation.Id }; TrailerDto testDto = Mapper.Map <TrailerDto>(testTrailer); //Mock call mockTrailerRepo.Setup(x => x.Get(testTrailer.Id)).Returns <Trailer>(null); //Unit of work Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>(); mockUnitOfWork.SetupGet(x => x.LocationRepo).Returns(mockLocationRepo.Object); mockUnitOfWork.SetupGet(x => x.TrailerRepo).Returns(mockTrailerRepo.Object); //Trailer service ITrailerService trailerService = new TrailerService(mockUnitOfWork.Object); //Test trailerService.Delete(testTrailer.Id); }
public TrailerDto Update(TrailerDto dto) { //Repo dependencies IRepo <Location> locationRepo = this.unitOfWork.LocationRepo; IRepo <Trailer> trailerRepo = this.unitOfWork.TrailerRepo; //Get location Location location = locationRepo.Get(dto.Location.Id); //Ensure location exists if (location == null) { throw new DoesNotExistException(); } //Get trailer from repo Trailer trailer = trailerRepo.Get(dto.Id); //Ensure trailer exists if (trailer == null) { throw new DoesNotExistException(); } //Update trailer trailer.LocationId = dto.Location.Id; trailer.Deleted = dto.Deleted; trailer.RowVersion = dto.RowVersion; trailerRepo.Update(trailer); //Create dto dto = Mapper.Map <TrailerDto>(trailer); dto.Location = Mapper.Map <LocationDto>(location); //Return dto return(dto); }
public HttpResponseMessage Get(int id) { TrailerDto resDto = this.trailerService.Get(id); return(Request.CreateResponse(HttpStatusCode.OK, resDto)); }
public void UpdateNonexistantLoad() { //Automapper AutoMapperConfig.RegisterMappings(); //Mock context Mock <FortuneDbContext> mockContext = new Mock <FortuneDbContext>(); //Mock repos Mock <IRepo <Location> > mockLocationRepo = new Mock <IRepo <Location> >(); Mock <IRepo <Trailer> > mockTrailerRepo = new Mock <IRepo <Trailer> >(); Mock <IRepo <Load> > mockLoadRepo = new Mock <IRepo <Load> >(); //Test location Location testLocation = new Location { Deleted = false, Id = Guid.NewGuid(), Name = "TEST LOCATION" }; LocationDto testLocationDto = Mapper.Map <LocationDto>(testLocation); //Test Trailer Trailer testTrailer = new Trailer { Deleted = false, Id = 1, LocationId = testLocation.Id }; TrailerDto testTrailerDto = Mapper.Map <TrailerDto>(testTrailer); testTrailerDto.Location = testLocationDto; //Test Load Load testLoad = new Load { Appointment = DateTime.UtcNow, ArrivalTime = DateTime.UtcNow, CfNum = 1, Deleted = false, DepartureTime = DateTime.UtcNow, DestinationId = testLocation.Id, Id = Guid.NewGuid(), OriginId = testLocation.Id, PuNum = 1, Status = LoadStatus.InTransit, TrailerId = testTrailer.Id, Type = LoadType.Inbound }; LoadDto testLoadDto = Mapper.Map <LoadDto>(testLoad); testLoadDto.Trailer = testTrailerDto; testLoadDto.Origin = testLocationDto; testLoadDto.Destination = testLocationDto; //Mock Calls mockLoadRepo.Setup(x => x.Get(It.Is <Guid>(y => y == testLoad.Id))).Returns <Load>(null); mockTrailerRepo.Setup(x => x.Get(testTrailer.Id)).Returns(testTrailer); mockLocationRepo.Setup(x => x.Get(It.Is <Guid>(y => y == testLocation.Id))).Returns(testLocation); //Mock unit of work Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>(); mockUnitOfWork.SetupGet(x => x.LocationRepo).Returns(mockLocationRepo.Object); mockUnitOfWork.SetupGet(x => x.TrailerRepo).Returns(mockTrailerRepo.Object); mockUnitOfWork.SetupGet(x => x.LoadRepo).Returns(mockLoadRepo.Object); //Load service LoadService loadService = new LoadService(mockUnitOfWork.Object); //Test LoadDto dto = loadService.Update(testLoadDto); }