public void CancelNonExistingBookingThrowsValidationException()
            {
                // Arrange
                var cancelBookingDto = new CancelBookingDto
                {
                    BookingId = 1,
                    BusinessId = 11111111111,
                };
                var bookingManager = MockRepository.GenerateStub<IBookingManager>();
                bookingManager.Stub(b => b.GetByKey(BOOKING_ID)).Return(null);
                PropertyManagementSystemService.BookingManager = bookingManager;

                // 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();

                try
                {
                    // Act
                    PropertyManagementSystemService.CancelBooking(cancelBookingDto);

                    // Assert
                    Assert.Fail("An exception SRVEX30010 of type ValidationException should have been thrown");

                }
                catch (ValidationException ex)
                {
                    // Assert
                    Assert.AreEqual("SRVEX30010", ex.Code, "The exception code thrown is not the expected.");
                }

                // Reassign the Dao on the cache to discard the stub assigned on the StubBusinessCacheSingleBusiness method
                CacheHelper.ReAssignBusinessDaoToBusinessCache();
            }
            public void CancelValidBookingCancelsBookingAndUpdatesAvailability()
            {
                // Arrange

                var cancelBookingDto = new CancelBookingDto
                {
                    BookingId = 1,
                    BusinessId = 11111111111,
                };
             
                DateTime startDate = DateTime.UtcNow;
                DateTime endDate = DateTime.UtcNow.AddDays(1);
                var existingBooking = new Booking
                {
                    Id = BOOKING_ID,
                    BusinessId = BUSINESS_ID,
                    BookingScenarioType = BookingScenarioTypeEnum.OnAccountBooking,
                    BookingStatus = new EnumEntity { Code = BookingStatusType.CANCELLED },
                    Guest = new Guest
                    {
                        Id = 1,
                        Email = "*****@*****.**",
                        Surname = "Test"
                    },
                    RoomId = 1,
                    RoomTypeId = 1,
                    RatePlanId = 1,
                    Cost = new decimal(10),
                    StartDate = startDate,
                    EndDate = endDate,
                    NumberOfAdults = 1,
                    CancellationDetails = new CancellationDetail{ CancellationType = CancellationTypeEnum.NonRefundable }
                };

                var bookingManager = MockRepository.GenerateMock<IBookingManager>();
                bookingManager.Stub(b => b.GetByKey(BOOKING_ID)).Return(existingBooking);
                int? refundId;
                bool? isRefundSuccessful;
                bool? isRefundAttempted;
                bookingManager.Expect(c => c.CancelBooking(existingBooking, out refundId, out isRefundSuccessful, out isRefundAttempted)).OutRef(null, null);
                PropertyManagementSystemService.BookingManager = bookingManager;

                // 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
                var result = PropertyManagementSystemService.CancelBooking(cancelBookingDto);

                // Assert
                Assert.IsTrue(result.Result, "Booking is not cancelled successfully.");
                bookingManager.VerifyAllExpectations();

                // Reassign the Dao on the cache to discard the stub assigned on the StubBusinessCacheSingleBusiness method
                CacheHelper.ReAssignBusinessDaoToBusinessCache();
            }
        public CancellationResponseDto CancelBooking(CancelBookingDto cancelBooking)
        {
            // Cancel Booking
            var internalBooking = GetBookingAndCheckAccessForCancelBooking(cancelBooking, true);

            int? refundId;
            bool? isRefundSuccessful;
            bool? isRefundAttempted;
            bookingManager.CancelBooking(internalBooking, out refundId, out isRefundSuccessful, out isRefundAttempted);

            // cancel booking successful so send email if requested
            if (cancelBooking.SendCancellationEmail.HasValue && cancelBooking.SendCancellationEmail.Value)
            {
                var cultureCode = internalBooking.Order.CustomerCultureCode;

                if (string.IsNullOrEmpty(cultureCode))
                {
                    var business = Cache.Business.TryGetValue(internalBooking.BusinessId);

                    cultureCode = business.CultureCode;
                }

                emailManager.SendCancellationEmails(orderManager.GetOrderWithBookingsByKey(internalBooking.OrderId, cultureCode: cultureCode));
            }

            return new CancellationResponseDto{RefundId = refundId, IsRefundSuccessful = isRefundSuccessful, IsRefundAttempted = isRefundAttempted ,Result = true};
        }
            public void CancelBookingForAnInvalidBusinessThrowsValidationException()
            {
                // Arrange
                var cancelBookingDto = new CancelBookingDto
                                                        {
                                                            BookingId  = 1,
                                                            BusinessId = 11111111111,
                                                        };
                try
                {
                    // Act
                    PropertyManagementSystemService.CancelBooking(cancelBookingDto);

                    // 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");
                }
            }
        /// <summary>
        /// Check access for the cancel booking request and return the booking with cancel details set
        /// </summary>
        /// <param name="cancelBooking">cancel booking request</param>
        /// <param name="getOrder">if true will get order with the booking</param>
        /// <returns>booking with cancel details</returns>
        private Booking GetBookingAndCheckAccessForCancelBooking(CancelBookingDto cancelBooking, bool getOrder)
        {
            // Make sure the current user has access to this business
            CheckAccessRights(cancelBooking.BusinessId);

            // Validate if business exists
            if (BusinessCache.TryGetValue(cancelBooking.BusinessId) == null)
            {
                throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30001,
                                                                                "PropertyManagementSystemService.CancelBooking",
                                                                                additionalDescriptionParameters:
                                                                                    (new object[] { cancelBooking.BusinessId })));
            }

            // Retrieve latest booking information
            Booking internalBooking = bookingManager.GetByKey(cancelBooking.BookingId, getOrder: getOrder);

            // Throw validation exception if booking could not be found in the system
            if (internalBooking == null)
            {
                throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30010,
                                                                             "PropertyManagementSystemService.CancelBooking",
                                                                             descriptionParameters:
                                                                                 (new object[] { cancelBooking.BookingId, cancelBooking.BookingId }),
                                                                             additionalDescriptionParameters:
                                                                                 (new object[] { cancelBooking.BookingId, cancelBooking.BusinessId }),
                                                                             arguments:
                                                                                 new object[] { cancelBooking.BookingId, cancelBooking.BusinessId }));

            }

            internalBooking.CancellationDetails = Mapper.Map<CancellationDetail>(cancelBooking);

            return internalBooking;
        }