public void Given_call_get_view_model_Then_calls_appropiate_service_methods_for_main_site()
        {
            //Given
            const long id = 99;
            const long siteId = 1;
            const long companyId = 10;
            const long parentId = 2;


            var siteDto = new SiteDto {Id = id, SiteId = siteId, Parent = new SiteStructureElementDto {Id = parentId}};
      
            siteService.Setup(ss => ss.GetByIdAndCompanyId(id, companyId)).Returns(siteDto);

            _clientService.Setup(x => x.GetSite(companyId, siteId)).Returns(new SiteAddressDto(siteId, "Add 1", "Add 2",
                                                                                               "Add 3", "Add 4", "Add 5", "county", "PC",
                                                                                               "tel", null));
            var target = CreateSiteDetailsViewModelFactory();

            //When
            target
                .WithClientId(companyId)
                .WithId(id)
                .GetViewModel();

            //Then
            siteService.Verify(ss => ss.GetByIdAndCompanyId(id, companyId), Times.Once());
            _clientService.Verify(x => x.GetSite(companyId, siteId), Times.Once());
            siteService.Verify(x => x.GetByCompanyIdNotIncluding(companyId, siteId), Times.Once());
            siteGroupService.Verify(x => x.GetByCompanyIdExcludingSiteGroup(companyId,siteId), Times.Once());
        }
        public void given_valid_company_when_getviewmodel_then_result_contains_correct_sites()
         {
             //given
             var site = new SiteDto
                            {
                                Id =1L
                            };
             var sites = new List<SiteDto>
                             {
                                 site
                             };

             _siteService.Setup(x => x.Search(It.IsAny<SearchSitesRequest>())).Returns(sites);

             var factory = GetTarget();

             //when
             var result = factory
                 .WithCompanyId(1L)
                 .GetViewModel();

             //then            
             Assert.That(result.Sites.Skip(1).First().value, Is.EqualTo(site.Id.ToString()));
         }
        public void SetUp()
        {
            _siteStructureViewModelFactory = new Mock<ISiteStructureViewModelFactory>();
            _siteDetailsViewModelFactory = new Mock<ISiteDetailsViewModelFactory>();
            _siteService = new Mock<ISiteService>();
            _siteUpdateCompositionGateway = new Mock<ISiteUpdateCompositionGateway>();
            _siteService
                .Setup(ss => ss.CreateUpdate(It.IsAny<CreateUpdateSiteRequest>()))
                .Returns(10);

            _clientId = 888L;
            _siteStructureId = 111L;

            _siteDto = new SiteDto
                           {
                               Id = _siteStructureId,
                               ClientId = _clientId,
                               Name = "Hagatna"
                           };

            _siteService
                .Setup(x => x.GetByIdAndCompanyId(_siteStructureId, _clientId))
                .Returns(_siteDto);
        }
 private ResponsibilityDto CreateResponsibilityDto(SiteDto site, List<StatutoryResponsibilityTaskTemplateDto> tasks)
 {
     return new ResponsibilityDto
     {
         Site = site,
         UncreatedStatutoryResponsibilityTaskTemplates = tasks
     };
 }
 public static AutoCompleteViewModel ForSite(SiteDto sitesDto)
 {
     return new AutoCompleteViewModel(sitesDto.Name, sitesDto.Id.ToString());
 }
        public SiteDto Build()
        {
            var linkedSitesDto = new SiteDto { Id = _siteId, Name = _name };

            return linkedSitesDto;
        }
        public SiteDetailsViewModel GetViewModel()
        {
            SiteDto localSiteDto = null;
            SiteAddressDto peninsulaSiteDto = null;

            if (_id != default(long))
            {
                localSiteDto = _siteService.GetByIdAndCompanyId(_id, _clientId);
                peninsulaSiteDto = _clientService.GetSite(_clientId, localSiteDto.SiteId.Value);
            }

            else if (_siteId != default(long))
            {
                localSiteDto = new SiteDto();
                peninsulaSiteDto = _clientService.GetSite(_clientId, _siteId);
            }

            var siteDetailsViewModel = new SiteDetailsViewModel
                                           {
                                               SiteStructureId = localSiteDto.Id,
                                               ClientId = _clientId,
                                               Name = localSiteDto.Name,
                                               Reference = peninsulaSiteDto.Reference,
                                               LinkToSiteId =
                                                   localSiteDto.Parent != null &&
                                                   (localSiteDto.Parent as SiteDto != null)   //todo: replace with when proxy issue fixed
                                                   //localSiteDto.Parent.SiteStructureElementType == "Site"
                                                       ? (long?)localSiteDto.Parent.Id
                                                       : null,
                                               LinkToGroupId =
                                                   localSiteDto.Parent != null &&
                                                   (localSiteDto.Parent as SiteGroupDto != null)  //todo: replace with when proxy issue fixed
                                                   //localSiteDto.Parent.SiteStructureElementType == "SiteGroup"
                                                       ? (long?)localSiteDto.Parent.Id
                                                       : null,
                                               OriginalLinkId =
                                                   localSiteDto.Parent != null ? (long?)localSiteDto.Parent.Id : null,
                                               AddressLine1 = peninsulaSiteDto.AddressLine1,
                                               AddressLine2 = peninsulaSiteDto.AddressLine2,
                                               AddressLine3 = peninsulaSiteDto.AddressLine3,
                                               AddressLine4 = peninsulaSiteDto.AddressLine4,
                                               AddressLine5 = peninsulaSiteDto.AddressLine5,
                                               County = peninsulaSiteDto.County,
                                               Postcode = peninsulaSiteDto.Postcode,
                                               Telephone = peninsulaSiteDto.Telephone,
                                               SiteContact = localSiteDto.SiteContact,
                                               SiteClosed = localSiteDto.SiteClosed,
                                               SiteStatusCurrent = localSiteDto.SiteClosed ? SiteStatus.Close : SiteStatus.Open
                                           };

            if (_id != default(long))
            {
                siteDetailsViewModel.SiteId = localSiteDto.SiteId.HasValue ? localSiteDto.SiteId.Value : 0;
                siteDetailsViewModel.IsMainSite = localSiteDto.Parent == null;
            }

            else if (_siteId != default(long))
            {
                siteDetailsViewModel.SiteId = _siteId;
                siteDetailsViewModel.IsMainSite = false;
            }

            if (localSiteDto.Id == 0 || localSiteDto.Parent != null)
            {
                long siteId = _siteId != default(long) ? _siteId : localSiteDto.SiteId.Value;

                var sites = _siteService
                    .GetByCompanyIdNotIncluding(_clientId, siteId)
                    .OrderBy(x => x.Name);

                siteDetailsViewModel.ExistingSites = sites
                                                        .Where(x => localSiteDto.ChildIdsThatCannotBecomeParent.Contains(x.Id) == false)
                                                        .Select(AutoCompleteViewModel.ForSite)
                                                        .AddDefaultOption();

                var siteGroups = _siteGroupService
                    .GetByCompanyIdExcludingSiteGroup(_clientId, siteId)
                    .OrderBy(x => x.Name);


                siteDetailsViewModel.ExistingGroups = siteGroups
                                                        .Where(x => localSiteDto.ChildIdsThatCannotBecomeParent.Contains(x.Id) == false)
                                                        .Select(AutoCompleteViewModel.ForSiteGroup)
                                                        .AddDefaultOption();
            }
            else
            {
                siteDetailsViewModel.ExistingSites = new List<AutoCompleteViewModel>()
                                                         {
                                                             new AutoCompleteViewModel("--Select Option--","")
                                                         };
                siteDetailsViewModel.ExistingGroups = new List<AutoCompleteViewModel>() 
                                                         {
                                                             new AutoCompleteViewModel("--Select Option--","")
                                                         };
            }

            return siteDetailsViewModel;
        }
        public void Given_search_with_parameters_When_GetViewModel_return_only_allowed_sites_in_viewModel()
        {
            //Given
            const long categoryId = 123L;
            const long siteId = 1L;
            const long siteGroupId = 157473L;
            const bool deleted = true;
            var from = DateTime.Now.AddDays(-124);
            var to = DateTime.Now.AddDays(-3231);
            const string title = "title";
            IList<long> allowedSites = new List<long>() { siteId };
            var target = CreateTarget();

            var site1 = new SiteDto() { Id = siteId };
            
            _siteService
                .Setup(x => x.Search(It.IsAny<SearchSitesRequest>()))
                .Returns(new List<SiteDto>() { site1 });

            var searchSitesRequest = new SearchSitesRequest();
            _siteService
                .Setup(x => x.Search(It.IsAny<SearchSitesRequest>()))
                    .Returns(new List<SiteDto>() { site1 })
                        .Callback<SearchSitesRequest>(y => searchSitesRequest = y);

            //When
            var result = target
                .WithCategoryId(categoryId)
                .WithCreatedFrom(from)
                .WithCreatedTo(to)
                .WithTitle(title)
                .WithSiteId(siteId)
                .WithSiteGroupId(siteGroupId)
                .WithShowDeleted(deleted)
                .WithAllowedSiteIds(allowedSites)
                .GetViewModel();

            //Then
            Assert.That(result.CategoryId, Is.EqualTo(categoryId));
            Assert.That(result.SiteId, Is.EqualTo(siteId));
            Assert.That(result.SiteGroupId, Is.EqualTo(siteGroupId));
            Assert.That(result.IsShowDeleted, Is.EqualTo(deleted));
            Assert.That(DateTime.Parse(result.CreatedFrom), Is.EqualTo(from.Date));
            Assert.That(DateTime.Parse(result.CreatedTo), Is.EqualTo(to.Date));
            Assert.That(result.Title, Is.EqualTo(title));
            Assert.That(result.Sites.Count(), Is.EqualTo(2)); //One for autocomplete
            Assert.That(result.Sites.Last().value, Is.EqualTo(siteId.ToString()));
            Assert.That(searchSitesRequest.AllowedSiteIds.Count, Is.EqualTo(allowedSites.Count));
        }
        public void Given_search_for_current_user_When_GetViewModel_is_called_Then_returns_model()
        {
            //Given
            var taskId = 124124124L;
            var target = CreateTarget();

            var site = new SiteDto() {Id = 1L, Name = "the main site"};
            var taskDocument = new TaskDocumentDto()
                                   {Id = 12312, DocumentLibraryId = 13123, Description = "doc description", Filename = "the filename"};
            var responsibility = new ResponsibilityDto{Id = 1L, CompanyId = _companyId, Title = "Responsibilty Title",Description = "Responsibilty Description"};
            var responsibilityTask = new ResponsibilityTaskDto
                                       {
                                           Id = taskId,
                                           Title = "task title",
                                           Description = "task description",
                                           TaskAssignedTo = new EmployeeDto {FullName = "Test employee name", Id=Guid.NewGuid()},
                                           CreatedDate = DateTime.Now.ToShortDateString(),
                                           TaskCompletionDueDate = DateTime.Now.ToShortDateString(),
                                           TaskStatusString = string.Empty,
                                           Site = site,
                                           Responsibility = responsibility,
                                           TaskReoccurringType = TaskReoccurringType.Monthly,
                                           TaskReoccurringEndDate = DateTime.Now.AddDays(234),
                                           IsReoccurring = true,
                                           SendTaskCompletedNotification = true,
                                           SendTaskNotification = true,
                                           SendTaskOverdueNotification = true,
                                           Documents = new List<TaskDocumentDto>(){taskDocument}
                                       };

            _responsibilityTaskService
                .Setup(x=> x.GetByIdAndCompanyId(taskId, It.IsAny<long>()))
                .Returns(() => responsibilityTask );

            //When
            var result = target
                .WithCompanyId(_companyId)
                .WithResponsibilityTaskId(taskId)
                .GetViewModel();

            //Then
            Assert.That(result.CompanyId, Is.EqualTo(responsibility.CompanyId));
            Assert.That(result.ResponsibilityTaskId, Is.EqualTo(responsibilityTask.Id));
            Assert.That(result.ResponsibilitySummary.Id, Is.EqualTo(responsibility.Id));
            Assert.That(result.ResponsibilitySummary.Title, Is.EqualTo(responsibility.Title));
            Assert.That(result.ResponsibilitySummary.Description, Is.EqualTo(responsibility.Description));

            Assert.That(result.ResponsibilityTask.CompanyId, Is.EqualTo(responsibility.CompanyId));
            Assert.That(result.ResponsibilityTask.ResponsibilityTaskId, Is.EqualTo(responsibilityTask.Id));
            Assert.That(result.ResponsibilityTask.Title, Is.EqualTo(responsibilityTask.Title));
            Assert.That(result.ResponsibilityTask.Description, Is.EqualTo(responsibilityTask.Description));
            Assert.That(result.ResponsibilityTask.IsRecurring, Is.EqualTo(responsibilityTask.IsReoccurring));
            Assert.That(result.ResponsibilityTask.TaskReoccurringType, Is.EqualTo(responsibilityTask.TaskReoccurringType));
            Assert.That(result.ResponsibilityTask.TaskReoccurringTypeId, Is.EqualTo((int)responsibilityTask.TaskReoccurringType));
            Assert.That(result.ResponsibilityTask.ReoccurringStartDate, Is.EqualTo(responsibilityTask.TaskCompletionDueDate));
            Assert.That(result.ResponsibilityTask.ReoccurringEndDate, Is.EqualTo(responsibilityTask.TaskReoccurringEndDate));
            Assert.That(result.ResponsibilityTask.CompletionDueDate, Is.EqualTo(responsibilityTask.TaskCompletionDueDate));
            Assert.That(result.ResponsibilityTask.ResponsibilityTaskSite, Is.EqualTo(responsibilityTask.Site.Name));
            Assert.That(result.ResponsibilityTask.ResponsibilityTaskSiteId, Is.EqualTo(responsibilityTask.Site.Id));
            Assert.That(result.ResponsibilityTask.AssignedTo, Is.EqualTo(responsibilityTask.TaskAssignedTo.FullName));
            Assert.That(result.ResponsibilityTask.AssignedToId, Is.EqualTo(responsibilityTask.TaskAssignedTo.Id));
            Assert.That(result.ResponsibilityTask.DoNotSendTaskAssignedNotification, Is.EqualTo(!responsibilityTask.SendTaskNotification));
            Assert.That(result.ResponsibilityTask.DoNotSendTaskCompletedNotification, Is.EqualTo(!responsibilityTask.SendTaskCompletedNotification));
            Assert.That(result.ResponsibilityTask.DoNotSendTaskOverdueNotification, Is.EqualTo(!responsibilityTask.SendTaskOverdueNotification));
            Assert.That(result.ResponsibilityTask.TaskStatusId, Is.EqualTo(responsibilityTask.TaskStatusId));
            Assert.That(result.ResponsibilityTask.ExistingDocuments.CanDeleteDocuments, Is.EqualTo(false));
            Assert.That(result.ResponsibilityTask.ExistingDocuments.DocumentTypeId, Is.EqualTo((int)DocumentTypeEnum.Responsibility));

            Assert.That(result.ResponsibilityTask.ExistingDocuments.PreviouslyAddedDocuments[0].Id, Is.EqualTo(taskDocument.Id));
            Assert.That(result.ResponsibilityTask.ExistingDocuments.PreviouslyAddedDocuments[0].DocumentLibraryId, Is.EqualTo(taskDocument.DocumentLibraryId));
            Assert.That(result.ResponsibilityTask.ExistingDocuments.PreviouslyAddedDocuments[0].Filename, Is.EqualTo(taskDocument.Filename));
            Assert.That(result.ResponsibilityTask.ExistingDocuments.PreviouslyAddedDocuments[0].Description, Is.EqualTo(taskDocument.Description));
            Assert.That(result.ResponsibilityTask.ResponsibilitySummary.Id, Is.EqualTo(responsibility.Id));
            Assert.That(result.ResponsibilityTask.ResponsibilitySummary.Title, Is.EqualTo(responsibility.Title));
            Assert.That(result.ResponsibilityTask.ResponsibilitySummary.Description, Is.EqualTo(responsibility.Description));
        }
        public void Given_search_for_current_user_with_allowed_site_ids_When_GetViewModel_is_called_Then_returns_model()
        {
            //Given
            var target = CreateTarget();

            var responsibility = new ResponsibilityDto
            {
                Id = 1L,
                ResponsibilityTasks = new List<ResponsibilityTaskDto>
                                                                   {
                                                                       new ResponsibilityTaskDto
                                                                           {
                                                                               Id = 1L,
                                                                               Description = "my description",
                                                                               TaskAssignedTo = new EmployeeDto{FullName = "Test"},
                                                                               CreatedDate = DateTime.Now.ToShortDateString(),
                                                                               TaskCompletionDueDate = DateTime.Now.ToShortDateString(),
                                                                               TaskStatusString = string.Empty,
                                                                               Site = new SiteDto{Id = 1L}
                                                                           }
                                                                   }
            };

            var reasons = new List<ResponsibilityReasonDto>
                                 {
                                     new ResponsibilityReasonDto {Id = default(long)}
                                 };

            var categories = new List<ResponsibilityCategoryDto>
                                 {
                                     new ResponsibilityCategoryDto {Id = default(long)}
                                 };

            var employess = new List<EmployeeDto>
                                {
                                    new EmployeeDto{Id = new Guid()}
                                };
            var site1 = new SiteDto() { Id = 1L };
            var site2 = new SiteDto() { Id = 2L };
            var site3 = new SiteDto() { Id = 3L };

            var allowedSites = new List<long>() { 1L, 2L };

            var sites = new List<SiteDto>
                            {
                                site1, site2, site3
                            };

            _responsibilitiesService
                .Setup(x => x.GetResponsibility(It.IsAny<long>(), _companyId))
                .Returns(() => responsibility);

            _responsibilitiesService
                .Setup(x => x.GetResponsibilityReasons())
                .Returns(() => reasons);

            _responsibilitiesService
                .Setup(x => x.GetResponsibilityCategories())
                .Returns(() => categories);

            _employeeService
                .Setup(x => x.Search(It.IsAny<SearchEmployeesRequest>()))
                .Returns(() => employess);

            _employeeService
               .Setup(x => x.GetEmployeeNames(It.IsAny<long>()))
               .Returns(() => employess.Select(x => new EmployeeName() { Id = x.Id }).ToList());

            var searchSitesRequest = new SearchSitesRequest();
            _siteService
                .Setup(x => x.Search(It.IsAny<SearchSitesRequest>())).Returns(sites).Callback<SearchSitesRequest>(y => searchSitesRequest = y);

            //When
            var result = target.WithCompanyId(_companyId)
                .WithResponsibilityId(_responsibilityId)
                .WithAllowedSiteIds(allowedSites)
                .GetViewModel();

            //Then
            Assert.That(searchSitesRequest.AllowedSiteIds.Count, Is.EqualTo(allowedSites.Count));

        }
        public void When_get_view_model_Then_should_order_existing_site_group()
        {
            // Arrange
            var siteDto = new SiteDto { SiteId = 1 };

            siteService.Setup(ss => ss.GetByIdAndCompanyId(1, 1)).Returns(siteDto);

            _clientService.Setup(x => x.GetSite(1, 1)).Returns(new SiteAddressDto(1, "Add 1", "Add 2",
                                                                                               "Add 3", "Add 4", "Add 5", "county", "PC",
                                                                                               "tel", null));

            string expectedLastGroup = "Zoo bar";
            string expectedFirstGroup = "Alph bar";

            IList<SiteGroupDto> existingGroups = new List<SiteGroupDto>();
            existingGroups.Add(new SiteGroupDto{Id = 1, Name = expectedLastGroup});
            existingGroups.Add(new SiteGroupDto{Id = 1, Name = "Random"});
            existingGroups.Add(new SiteGroupDto { Id = 1, Name = expectedFirstGroup });
            siteGroupService.Setup(x => x.GetByCompanyIdExcludingSiteGroup(1, 1)).Returns(existingGroups);

            siteService.Setup(x => x.GetByCompanyId(1)).Returns(new List<SiteDto>());

            var siteGroupDto = new SiteGroupDto();
            siteGroupService.Setup(s => s.GetSiteGroup(1, 1)).Returns(siteGroupDto);

            var target = CreateSiteDetailsViewModelFactory();

            // Act
            var result = target.WithClientId(1).WithSiteId(1).GetViewModel();

            // Assert
            Assert.That(result.ExistingGroups.Skip(1).Take(1).First().label, Is.EqualTo(expectedFirstGroup));
            Assert.That(result.ExistingGroups.Last().label, Is.EqualTo(expectedLastGroup));
        }