public GuestDto CreateGuest(long businessId, GuestDto guest)
        {
            // Make sure the current user has access to this business
            CheckAccessRights(businessId);

            // Some basic checks
            // Validate if business exists
            if (Cache.Business.TryGetValue(businessId) == null)
            {
                throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30001, "PropertyManagementSystemService.CreateGuest",
                    additionalDescriptionParameters: (new object[] { businessId })));
            }

            if (businessId != guest.BusinessId)
            {
                throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30000, "PropertyManagementSystemService.CreateGuest",
                    additionalDescriptionParameters: (new object[] { businessId, guest.BusinessId }), arguments: new object[] { businessId, guest }));
            }

            // Create a model Guest to pass to the Guest manager
            Guest modelGuest = Mapper.Map<Guest>(guest);

            // Create the record
            guestManager.Create(modelGuest);

            // Change it back to a Dto Guest, now the Id should be populated. And return.
            return Mapper.Map<GuestDto>(modelGuest);
        }
        public bool ModifyGuest(long businessId, GuestDto guestDto)
        {
            // Make sure the current user has access to this business
            CheckAccessRights(businessId);

            // Validate if the business exists
            if (Cache.Business.TryGetValue(businessId) == null)
            {
                throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30001, "PropertyManagementSystemService.ModifyGuest",
                    additionalDescriptionParameters: (new object[] { businessId })));
            }

            if (businessId != guestDto.BusinessId)
            {
                throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30000, "PropertyManagementSystemService.ModifyGuest",
                    additionalDescriptionParameters: (new object[] { businessId, guestDto.BusinessId }), arguments: new object[] { businessId, guestDto }));
            }

            Guest guest = Mapper.Map<Guest>(guestDto);

            guestManager.Modify(guest);

            return true;
        }
            public void ModifyBookingWithMisMatchBusinessThrowsException()
            {
                // Arrange
                const long BUSINESS_ID = 2;
                const long MISMATCHED_BUSINESS_ID = 5;

                // Stub the BusinessCache to have the businesses to be used by our service method
                CacheHelper.StubBusinessCacheMultipleBusiness(new List<long> { MISMATCHED_BUSINESS_ID, BUSINESS_ID });

                // invalidate the cache so we make sure both our businesses are loaded into the cache
                Cache.Business.Invalidate();

                //Set up a Guest for the booking
                var guest = new GuestDto { Id = 23, BusinessId = BUSINESS_ID, Surname = "Doe" };

                var bookingDto = new BookingDto
                {
                    BusinessId = BUSINESS_ID,
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now.AddDays(1),
                    Guest = guest,
                    RoomId = 1
                };

                try
                {
                    // Act
                    PropertyManagementSystemService.ModifyBooking(MISMATCHED_BUSINESS_ID, false, bookingDto);

                    // Assert
                    Assert.Fail("An exception SRVEX30000 of type ValidationException should have been thrown");
                }
                catch (ValidationException ex)
                {
                    // Assert
                    Assert.AreEqual("SRVEX30000", ex.Code, "The Validation exception is not returning the right error code");
                }
                finally
                {
                    // Reassign the Dao on the cache to discard the stub assigned on the StubBusinessCacheSingleBusiness method
                    CacheHelper.ReAssignBusinessDaoToBusinessCache();

                }
            }
            public void ModifyBookingWithInvalidBusinessThrowsException()
            {
                // Arrange
                const long BUSINESS_ID = 123456;

                //Set up a Guest for the booking
                var guest = new GuestDto { Id = 23, BusinessId = BUSINESS_ID, Surname = "Doe" };

                var bookingDto = new BookingDto
                {
                    BusinessId = BUSINESS_ID,
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now.AddDays(1),
                    Guest = guest,
                    RoomId = 1
                };

                try
                {
                    // Act
                    PropertyManagementSystemService.ModifyBooking(BUSINESS_ID, false, bookingDto);

                    // Assert
                    Assert.Fail("An exception SRVEX30001 of type ValidationException should have been thrown");
                }
                catch (ValidationException ex)
                {
                    // Assert
                    Assert.AreEqual("SRVEX30001", ex.Code, "The Validation exception is not returning the right error code");
                }
            }
            public void ModifyBookingWithExpectedResult()
            {
                // Arrange
                const long BUSINESS_ID = 1;

                //Set up a Guest for the booking
                var guest = new GuestDto { Id = 23, BusinessId = BUSINESS_ID, Surname = "Doe" };

                var bookingDto = new BookingDto
                {
                    BusinessId = BUSINESS_ID,
                    Order = new OrderDto
                    {
                        Id = 5,
                        OfflineSourceCode = OfflineSourceEnum.Web.GetCode(),
                        CustomerCultureCode = "en-GB",
                        CustomerCurrencyCode = "GBP"
                    },
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now.AddDays(1),
                    Guest = guest,
                    RoomId = 1
                };

                var stubBookingManager = MockRepository.GenerateStub<IBookingManager>();

                PropertyManagementSystemService.BookingManager = stubBookingManager;

                stubBookingManager.Stub(c => c.ModifyBooking(Arg<bool>.Is.Equal(false), Arg<Booking>.Is.Anything)).Return(DataTransferObjectsConverter.ConvertBookingDtoToBooking(bookingDto));

                // Stub the BusinessCache to be used by our service method
                CacheHelper.StubBusinessCacheSingleBusiness(BUSINESS_ID);

                // invalidate the cache so we make sure our business is loaded into the cache
                Cache.Business.Invalidate();

                // Act
                BookingDto modifyBookingResult = PropertyManagementSystemService.ModifyBooking(BUSINESS_ID, false, bookingDto);

                // Assert
                Assert.IsNotNull(modifyBookingResult, "booking was not modified successfully");
                stubBookingManager.VerifyAllExpectations();

                // Reassign the Dao on the cache to discard the stub assigned on the StubBusinessCacheSingleBusiness method
                CacheHelper.ReAssignBusinessDaoToBusinessCache();
            }
            public void ModifyGuestWithMismatchBusinessThrowsException()
            {
                // Arrange
                const long MISMATCHED_BUSINESS_ID = 5;

                // Stub the BusinessCache to be used by our service method
                CacheHelper.StubBusinessCacheSingleBusiness(MISMATCHED_BUSINESS_ID);

                // invalidate the cache so we make sure our business is loaded into the cache
                Cache.Business.Invalidate();

                var guestDto = new GuestDto
                {
                    Id = 1,
                    BusinessId = 2,
                    Surname = "Test",
                    Email = "*****@*****.**"
                };

                try
                {
                    // Act
                    PropertyManagementSystemService.ModifyGuest(MISMATCHED_BUSINESS_ID, guestDto);

                    // Assert
                    Assert.Fail("An exception SRVEX30000 of type ValidationException should have been thrown");
                }
                catch (ValidationException ex)
                {
                    // Assert
                    Assert.AreEqual("SRVEX30000", ex.Code,
                                    "The Validation exception is not returning the right error code");
                }
                finally
                {
                    // Reassign the Dao on the cache to discard the stub assigned on the StubBusinessCacheSingleBusiness method
                    CacheHelper.ReAssignBusinessDaoToBusinessCache();
                }
            }
            public void ModifyGuestWithInvalidBusinessThrowsException()
            {
                // Arrange
                // business that we know will never exist in the cache
                const long INVALID_BUSINESS = -100;

                var guestDto = new GuestDto
                {
                    Id = 1,
                    BusinessId = INVALID_BUSINESS,
                    Surname = "Test",
                    Email = "*****@*****.**"
                };

                try
                {
                    // Act
                    PropertyManagementSystemService.ModifyGuest(INVALID_BUSINESS, guestDto);

                    // Assert
                    Assert.Fail("An exception SRVEX30001 of type ValidationException should have been thrown");
                }
                catch (ValidationException ex)
                {
                    // Assert
                    Assert.AreEqual("SRVEX30001", ex.Code,
                                    "The Validation exception is not returning the right error code");
                }
            }
            public void ModifyGuestWithExpectedResult()
            {
                // Arrange
                const long BUSINESS_ID = 1;

                var guestDto = new GuestDto
                {
                    Id = 1,
                    BusinessId = BUSINESS_ID,
                    Surname = "Test",
                    Email = "*****@*****.**"
                };

                var guestManager = MockRepository.GenerateMock<IGuestManager>();
                PropertyManagementSystemService.GuestManager = guestManager;

                guestManager.Expect(c => c.Modify(Arg<Guest>.Is.Anything));

                // Stub the BusinessCache to be used by our service method
                CacheHelper.StubBusinessCacheSingleBusiness(BUSINESS_ID);

                // invalidate the cache so we make sure our business is loaded into the cache
                Cache.Business.Invalidate();

                // Act
                PropertyManagementSystemService.ModifyGuest(BUSINESS_ID, guestDto);

                // Assert
                guestManager.VerifyAllExpectations();

                // Reassign the Dao on the cache to discard the stub assigned on the StubBusinessCacheSingleBusiness method
                CacheHelper.ReAssignBusinessDaoToBusinessCache();
            }
            public void CreateGuestForValidBusinessIsSuccessful()
            {
                //Arrange
                var guestManager = MockRepository.GenerateStub<IGuestManager>();
                const long BUSINESS_ID = 1;

                var guestPhoneDtos = new List<GuestPhoneDto>
                {
                    new GuestPhoneDto {Id = 1, Number = "07846536345", PhoneTypeCode = PhoneTypeEnumDto.Contact1, GuestId = 1}, 
                    new GuestPhoneDto {Id = 2, Number = "01254453873", PhoneTypeCode = PhoneTypeEnumDto.Contact1, GuestId = 1}
                };

                var guestToCreate = new GuestDto
                {
                    BusinessId = BUSINESS_ID,
                    Forename = "Gary",
                    Surname = "Water",
                    Email = "*****@*****.**",
                    GuestPhones = guestPhoneDtos
                };

                PropertyManagementSystemService.GuestManager = guestManager;

                // Stub the BusinessCache to be used by our service method
                CacheHelper.StubBusinessCacheSingleBusiness(BUSINESS_ID);

                // invalidate the cache so we make sure our business is loaded into the cache
                Cache.Business.Invalidate();

                //Act
                GuestDto createdGuest = PropertyManagementSystemService.CreateGuest(BUSINESS_ID, guestToCreate);

                //Assert
                Assert.AreEqual(BUSINESS_ID, createdGuest.BusinessId , "BusinessId doesn't match for guest");
                Assert.AreEqual(guestToCreate.Forename, createdGuest.Forename, "Forename doesn't match for guest");
                Assert.AreEqual(guestToCreate.Surname, createdGuest.Surname, "Surname doesn't match for guest");
                Assert.AreEqual(guestToCreate.Email, createdGuest.Email, "Email doesn't match for guest");
                Assert.AreEqual(guestToCreate.GuestPhones.Count, createdGuest.GuestPhones.Count, "Number of guest phones don't match for guest");

                // Check guest phone 1 properties
                Assert.AreEqual(guestToCreate.GuestPhones[0].Id, createdGuest.GuestPhones[0].Id, "GuestPhone 1 Id doesn't match");
                Assert.AreEqual(guestToCreate.GuestPhones[0].Number, createdGuest.GuestPhones[0].Number, "GuestPhone 1 Number doesn't match");
                Assert.AreEqual(guestToCreate.GuestPhones[0].GuestId, createdGuest.GuestPhones[0].GuestId, "GuestPhone 1 GuestId doesn't match");
                Assert.AreEqual(guestToCreate.GuestPhones[0].PhoneTypeCode, createdGuest.GuestPhones[0].PhoneTypeCode, 
                    "GuestPhone 1 PhoneTypeCode doesn't match");

                // Check guest phone 2 properties
                Assert.AreEqual(guestToCreate.GuestPhones[1].Id, createdGuest.GuestPhones[1].Id, "GuestPhone 2 Id doesn't match");
                Assert.AreEqual(guestToCreate.GuestPhones[1].Number, createdGuest.GuestPhones[1].Number, "GuestPhone 2 Number doesn't match");
                Assert.AreEqual(guestToCreate.GuestPhones[1].GuestId, createdGuest.GuestPhones[1].GuestId, "GuestPhone 2 GuestId doesn't match");
                Assert.AreEqual(guestToCreate.GuestPhones[1].PhoneTypeCode, createdGuest.GuestPhones[1].PhoneTypeCode,
                    "GuestPhone 2 PhoneTypeCode doesn't match");


                //cleanup
                CacheHelper.ReAssignBusinessDaoToBusinessCache();
                PropertyManagementSystemService.GuestManager = new GuestManager();
            }
            public void CreatGuestWithFilledInGuestIdThrowsException()
            {
                // Arrange
                const long BUSINESS_ID = 1;
                var guest = new GuestDto
                {
                    BusinessId = BUSINESS_ID,
                    Forename = "Gary",
                    Surname = "Water",
                    Email = "*****@*****.**",
                    Id = 25
                };

                // Stub the BusinessCache to be used by our service method
                CacheHelper.StubBusinessCacheSingleBusiness(BUSINESS_ID);

                // invalidate the cache so we make sure our business is loaded into the cache
                Cache.Business.Invalidate();

                // Act
                try
                {
                    PropertyManagementSystemService.CreateGuest(BUSINESS_ID, guest);
                    // Assert
                    Assert.Fail("Validation Exception SRVEX30021 was expected but none was raised");
                }
                catch (ValidationException ex)
                {
                    Assert.AreEqual("SRVEX30021", ex.Code, "Check correct error code got returned");
                }

                CacheHelper.ReAssignBusinessDaoToBusinessCache();
            }
            public void CreateGuestForInvalidBusinessThrowsException()
            {
                // arrange
                const long BUSINESS_ID = -99; // BusinessId for UNKNOWN

                var guest = new GuestDto
                {
                    BusinessId = BUSINESS_ID,
                    Forename = "Gary",
                    Surname = "Water",
                    Email = "*****@*****.**"
                };


                try
                {
                    // act
                    PropertyManagementSystemService.CreateGuest(BUSINESS_ID, guest);

                    // assert
                    Assert.Fail("Validation Exception SRVEX30001 was expected but none was raised");
                }
                catch (ValidationException ex)
                {
                    Assert.AreEqual("SRVEX30001", ex.Code, "Check correct error code got returned");
                }
            }
            public void CreateBookingWithIncorrectRateTypeCodeThrowsException()
            {
                // Arrange
                const long BUSINESS_ID = 1;

                CacheHelper.StubBusinessCacheSingleBusiness(BUSINESS_ID);
                PropertyManagementSystemService.RoomInformationManager = new RoomInformationManager();
                PropertyManagementSystemService.BookingManager = new BookingManager();

                var mockOrderManager = new Mock<IOrderManager>();

                //Set up a Guest for the booking
                var guest = new GuestDto { Id = 23, BusinessId = BUSINESS_ID, Surname = "Doe" };

                const string BAD_CODE = "ThisIsABadCode";

                var bookingDto = new BookingDto
                {
                    BusinessId = BUSINESS_ID,
                    Id = 1,
                    OrderId = 5,
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now.AddDays(1),
                    Guest = guest,
                    RoomId = 1,
                    RoomTypeId = 1,
                    RatePlanId = 1,
                    NumberOfAdults = 1,
                    NumberOfChildren = 0,
                    RateTypeCode = BAD_CODE
                };

                mockOrderManager.Setup(o => o.GetByKey(bookingDto.OrderId.Value, It.IsAny<string>()))
                                .Returns(new Order {Id = bookingDto.OrderId});

                PropertyManagementSystemService.OrderManager = mockOrderManager.Object;

                // Act
                try
                {
                    PropertyManagementSystemService.CreateBooking(BUSINESS_ID, bookingDto);

                    // Assert
                    Assert.Fail("Exception with code SRVEX30046 should have been thrown");
                }
                catch (ValidationException vex)
                {
                    Assert.AreEqual("SRVEX30046", vex.Code, "Validation exception of type SRVEX30046 was not thrown");
                    mockOrderManager.VerifyAll();
                }
                finally
                {
                    CacheHelper.ReAssignBusinessDaoToBusinessCache();
                    PropertyManagementSystemService.GuestManager = new GuestManager();
                    PropertyManagementSystemService.OrderManager = new OrderManager();
                }
            }