public async Task FetchCompleteOrganisations_WithRejectedScheme_OrganisationNotReturned()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                // Arrange
                Organisation organisation = new Organisation();
                organisation.Id = new Guid("6BD77BBD-0BD8-4BAB-AA9F-A3E657D1CBB4");
                organisation.TradingName = "Test organisation";
                organisation.OrganisationType = EA.Weee.Domain.Organisation.OrganisationType.SoleTraderOrIndividual.Value;
                organisation.OrganisationStatus = EA.Weee.Domain.Organisation.OrganisationStatus.Complete.Value;

                Scheme scheme = new Scheme();
                scheme.Id = new Guid("CFD9B56F-6C3C-4E49-825C-A125ACFFEC3B");
                scheme.Organisation = organisation;
                scheme.SchemeStatus = (int)Core.Shared.SchemeStatus.Rejected;

                database.Model.Organisations.Add(organisation);
                database.Model.Schemes.Add(scheme);
                database.Model.SaveChanges();

                var dataAccess = new FetchOrganisationSearchResultsForCacheDataAccess(database.WeeeContext);

                // Act
                IList<OrganisationSearchResult> results = await dataAccess.FetchCompleteOrganisations();

                // Assert
                Assert.DoesNotContain(results,
                    r => r.OrganisationId == new Guid("6BD77BBD-0BD8-4BAB-AA9F-A3E657D1CBB4"));
            }
        }
        public async Task FetchOrganisationAsync_WithValidOrganisationId_ReturnsOrganisation()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                // Arrange
                Guid organisationId = new Guid("C826DCE8-78EB-4BE4-B419-4DE73D1AD181");
                Organisation organisation = new Organisation()
                {
                    Id = organisationId
                };
                database.Model.Organisations.Add(organisation);

                database.Model.SaveChanges();

                OrganisationDetailsDataAccess dataAccess = new OrganisationDetailsDataAccess(database.WeeeContext);

                // Act
                Domain.Organisation.Organisation result = await dataAccess.FetchOrganisationAsync(organisationId);

                // Assert
                Assert.NotNull(result);
                Assert.Equal(organisationId, result.Id);
            }
        }
        public async Task FetchCompleteOrganisations_WithCompleteCompany_UsesNameColumnToPopulateOrganisationName()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                // Arrange
                Organisation organisation = new Organisation();
                organisation.Id = new Guid("6BD77BBD-0BD8-4BAB-AA9F-A3E657D1CBB4");
                organisation.Name = "Company Name";
                organisation.OrganisationType = EA.Weee.Domain.Organisation.OrganisationType.RegisteredCompany.Value;
                organisation.OrganisationStatus = EA.Weee.Domain.Organisation.OrganisationStatus.Complete.Value;

                Scheme scheme = new Scheme();
                scheme.Id = new Guid("CFD9B56F-6C3C-4E49-825C-A125ACFFEC3B");
                scheme.Organisation = organisation;
                scheme.SchemeStatus = (int)Core.Shared.SchemeStatus.Approved;

                database.Model.Organisations.Add(organisation);
                database.Model.Schemes.Add(scheme);
                database.Model.SaveChanges();

                var dataAccess = new FetchOrganisationSearchResultsForCacheDataAccess(database.WeeeContext);

                // Act
                IList<OrganisationSearchResult> results = await dataAccess.FetchCompleteOrganisations();

                // Assert
                Assert.Contains(results,
                    r => r.OrganisationId == new Guid("6BD77BBD-0BD8-4BAB-AA9F-A3E657D1CBB4") && r.Name == "Company Name");
            }
        }
        public async Task FetchCompleteOrganisations_WithSeveralResults_ReturnsResultsOrderedByName()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                // Arrange
                Organisation organisation1 = new Organisation();
                organisation1.Id = new Guid("6BD77BBD-0BD8-4BAB-AA9F-A3E657D1CBB4");
                organisation1.Name = "Company B";
                organisation1.OrganisationType = EA.Weee.Domain.Organisation.OrganisationType.RegisteredCompany.Value;
                organisation1.OrganisationStatus = EA.Weee.Domain.Organisation.OrganisationStatus.Complete.Value;

                Scheme scheme1 = new Scheme();
                scheme1.Id = new Guid("CFD9B56F-6C3C-4E49-825C-A125ACFFEC3B");
                scheme1.Organisation = organisation1;
                scheme1.SchemeStatus = (int)Core.Shared.SchemeStatus.Approved;

                Organisation organisation2 = new Organisation();
                organisation2.Id = new Guid("659A5E1B-90F8-4E5C-8939-436189424AB6");
                organisation2.Name = "Company A";
                organisation2.OrganisationType = EA.Weee.Domain.Organisation.OrganisationType.RegisteredCompany.Value;
                organisation2.OrganisationStatus = EA.Weee.Domain.Organisation.OrganisationStatus.Complete.Value;

                Scheme scheme2 = new Scheme();
                scheme2.Id = new Guid("0F0CCEEF-849B-474A-A548-C52F99FD0C99");
                scheme2.Organisation = organisation2;
                scheme2.SchemeStatus = (int)Core.Shared.SchemeStatus.Approved;

                Organisation organisation3 = new Organisation();
                organisation3.Id = new Guid("D7C37279-C3F5-44C0-B6CF-D43A968F3F29");
                organisation3.Name = "Company C";
                organisation3.OrganisationType = EA.Weee.Domain.Organisation.OrganisationType.RegisteredCompany.Value;
                organisation3.OrganisationStatus = EA.Weee.Domain.Organisation.OrganisationStatus.Complete.Value;

                Scheme scheme3 = new Scheme();
                scheme3.Id = new Guid("52DA530C-B09C-4C52-B354-F07E72EE6111");
                scheme3.Organisation = organisation3;
                scheme3.SchemeStatus = (int)Core.Shared.SchemeStatus.Approved;

                database.Model.Organisations.Add(organisation1);
                database.Model.Organisations.Add(organisation2);
                database.Model.Organisations.Add(organisation3);
                database.Model.Schemes.Add(scheme1);
                database.Model.Schemes.Add(scheme2);
                database.Model.Schemes.Add(scheme3);
                database.Model.SaveChanges();

                var dataAccess = new FetchOrganisationSearchResultsForCacheDataAccess(database.WeeeContext);

                // Act
                IList<OrganisationSearchResult> results = await dataAccess.FetchCompleteOrganisations();

                // Assert
                int indexOfCompanyA = results.IndexOf(results.First(r => r.OrganisationId == new Guid("659A5E1B-90F8-4E5C-8939-436189424AB6")));
                int indexOfCompanyB = results.IndexOf(results.First(r => r.OrganisationId == new Guid("6BD77BBD-0BD8-4BAB-AA9F-A3E657D1CBB4")));
                int indexOfCompanyC = results.IndexOf(results.First(r => r.OrganisationId == new Guid("D7C37279-C3F5-44C0-B6CF-D43A968F3F29")));

                Assert.True(indexOfCompanyA < indexOfCompanyB, "Organisation search results must be returned ordered by organisation name.");
                Assert.True(indexOfCompanyB < indexOfCompanyC, "Organisation search results must be returned ordered by organisation name.");
            }
        }
        /// <summary>
        /// Associates a user with an organisation. The user is created if the specified username is not present. The status
        /// of the user is set to active by default.
        /// </summary>
        /// <param name="organisation"></param>
        /// <param name="username"></param>
        /// <param name="userStatus"></param>
        /// <returns></returns>
        public OrganisationUser CreateOrganisationUser(Organisation organisation, string username, int userStatus = 2)
        {
            var organisationUser = new OrganisationUser();
            organisationUser.Id = Guid.NewGuid();
            organisationUser.OrganisationId = organisation.Id;
            organisationUser.UserId = GetOrCreateUser(username).Id;
            organisationUser.UserStatus = userStatus;

            model.OrganisationUsers.Add(organisationUser);

            return organisationUser;
        }
        /// <summary>
        /// Creates and organisation
        /// </summary>
        /// <returns></returns>
        public Organisation CreateOrganisation()
        {
            int organisationId = GetNextId();
            Organisation organisation = new Organisation
            {
                Id = IntegerToGuid(organisationId),
                TradingName = string.Format("Organisation {0} Trading Name", organisationId),
            };
            model.Organisations.Add(organisation);

            return organisation;
        }
        /// <summary>
        /// Creates a scheme and associates it to the specified organisation.
        /// </summary>
        /// <returns></returns>
        public Scheme CreateScheme(Organisation organisation)
        {
            int schemeId = GetNextId();
            Scheme scheme = new Scheme
            {
                Id = IntegerToGuid(schemeId),
                Organisation = organisation,
                OrganisationId = organisation.Id,
                SchemeName = "test scheme name",
                ApprovalNumber = schemeId.ToString()
            };
            model.Schemes.Add(scheme);

            return scheme;
        }