public void ModifyDiaryNoteForValidBusinessIsSuccessful()
            {
                //Arrange
                var businessManager = MockRepository.GenerateMock<IBusinessManager>();
                const int DIARY_NOTE_ID = 1;
                const long BUSINESS_ID = 1;

                var diaryNote = new DiaryNoteDto
                {
                    Id = DIARY_NOTE_ID,
                    BusinessId = BUSINESS_ID,
                    Content = "Test Note",
                    Date = DateTime.Today
                };

                businessManager.Expect(b => b.ModifyDiaryNote(Arg<DiaryNote>.Is.Anything)).Return(true);

                PropertyManagementSystemService.BusinessManager = businessManager;

                // 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 status = PropertyManagementSystemService.ModifyDiaryNote(BUSINESS_ID, diaryNote);

                Assert.IsTrue(status, "Diary Note was not updated correctly");

                //Assert
                businessManager.VerifyAllExpectations();

                //cleanup
                CacheHelper.ReAssignBusinessDaoToBusinessCache();
            }
            public void ModifyDiaryNoteMismatchedBusinessThrowsException()
            {
                // arrange
                const int DIARY_NOTE_ID = 1;
                const long BUSINESS_ID = 1;
                const long INVALID_BUSINESS_ID = -99; // BusinessId for UNKNOWN

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

                var diaryNote = new DiaryNoteDto
                {
                    Id = DIARY_NOTE_ID,
                    BusinessId = INVALID_BUSINESS_ID,
                    Content = "Test Note",
                    Date = DateTime.Today
                };

                try
                {
                    // act
                    PropertyManagementSystemService.CreateDiaryNote(BUSINESS_ID, diaryNote);

                    // assert
                    Assert.Fail("Validation Exception SRVEX30000 was expected but none was raised");
                }
                catch (ValidationException ex)
                {
                    Assert.AreEqual("SRVEX30000", ex.Code, "Check correct error code got returned");
                }

                //cleanup
                CacheHelper.ReAssignBusinessDaoToBusinessCache();
            }
            public void CreateDiaryNoteForValidBusinessIsSuccessful()
            {
                //Arrange
                var businessManager = MockRepository.GenerateStub<IBusinessManager>();
                const long BUSINESS_ID = 1;

                var diaryNote = new DiaryNoteDto
                {
                    BusinessId = BUSINESS_ID,
                    Content = "Test Note",
                    Date = DateTime.Today
                };

                PropertyManagementSystemService.BusinessManager = businessManager;

                // 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 createDiaryNote = PropertyManagementSystemService.CreateDiaryNote(BUSINESS_ID, diaryNote);

                //Assert
                Assert.AreEqual(BUSINESS_ID, createDiaryNote.BusinessId, "BusinessId doesn't match for guest");
                Assert.AreEqual(diaryNote.Content, createDiaryNote.Content, "Content doesn't match for guest");
                Assert.AreEqual(diaryNote.Date, createDiaryNote.Date, "Content doesn't match for guest");

                //cleanup
                CacheHelper.ReAssignBusinessDaoToBusinessCache();
            }
            public void ModifyDiaryNoteForInvalidBusinessThrowsException()
            {
                // arrange
                const int DIARY_NOTE_ID = 1;
                const long BUSINESS_ID = -99; // BusinessId for UNKNOWN

                var diaryNote = new DiaryNoteDto
                {
                    Id = DIARY_NOTE_ID,
                    BusinessId = BUSINESS_ID,
                    Content = "Test Note",
                    Date = DateTime.Today
                };

                try
                {
                    // act
                    PropertyManagementSystemService.ModifyDiaryNote(BUSINESS_ID, diaryNote);

                    // 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 DiaryNoteDto CreateDiaryNote(long businessId, DiaryNoteDto diaryNoteDto)
        {
            // Make sure the current user has access to this business
            CheckAccessRights(businessId);

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

            // Validate if the business matches the business on the diary note
            if (businessId != diaryNoteDto.BusinessId)
            {
                throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30000,
                                                                             "PropertyManagementSystemService.CreateDiaryNote",
                                                                             additionalDescriptionParameters:
                                                                                 (new object[] { businessId, diaryNoteDto.BusinessId }),
                                                                             arguments:
                                                                                 new object[] { businessId, diaryNoteDto }));
            }

            DiaryNote diaryNote = Mapper.Map<DiaryNote>(diaryNoteDto);

            businessManager.CreateDiaryNote(diaryNote);

            return Mapper.Map<DiaryNoteDto>(diaryNote);
        }