public void GetsAgencyFieldInOrder()
        {
            var field1       = createContext.Add(new IdentifierDefinitionEntity("first field", "field-1:string")).Entity;
            var field2       = createContext.Add(new IdentifierDefinitionEntity("Second Field", "field-2:string")).Entity;
            var agencyEntity = new AgencyEntity("External Agency", "external")
            {
                Fields =
                {
                    new AgencyFieldEntity {
                        Identifier = field2, Order = 1
                    }
                }
            };

            createContext.Add(agencyEntity);
            createContext.SaveChanges();

            agencyEntity = updateContext.Set <AgencyEntity>().Find(agencyEntity.Id);
            agencyEntity.Fields.Add(
                new AgencyFieldEntity {
                Identifier = updateContext.IdentifierDefinition.Find(field1.Id), Order = 0
            });
            updateContext.SaveChanges();

            var agency = repository.GetAgency("external");

            using (new AssertionScope())
            {
                agency.Name.Should().Be("External Agency");
                agency.SystemName.Should().Be("external");
                agency.Fields.Should().ContainInOrder("field-1", "field-2");
            }
        }
Esempio n. 2
0
 // CRUD
 public async Task Create()
 {
     try
     {
         var agency = new AgencyEntity() {Name = "Hollywood Foreign Press"};
         var newPerson = new PersonEntity()
         {
             Name = Constants.Nick,
             Surname = Constants.Cage,
             Agency = agency
         };
         var createRes = await databaseService.InsertOrUpdateWithChildren(newPerson);
         if (createRes.Success)
         {
             var personEntity = await databaseService.GetWithChildren<PersonEntity>(createRes.NewEntityId);
             var person = Converter.Convert(personEntity);
             if (person != null)
             {
                 Persons.Add(person);
             }
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Get a particular agency from table storage
        /// </summary>
        /// <param name="regionId">region Id</param>
        /// <param name="agencyId">agency Id</param>
        /// <returns>agency entity</returns>
        public AgencyEntity Get(string regionId, string agencyId)
        {
            AgencyEntity partialAgency = new AgencyEntity {
                Id = agencyId, RegionId = regionId, RecordType = Enum.GetName(typeof(RecordType), RecordType.Agency)
            };

            var retrievedAgencies = (from entry in this.Table.CreateQuery <DynamicTableEntity>()
                                     where entry.PartitionKey == partialAgency.PartitionKey &&
                                     entry.RowKey == partialAgency.RowKey &&
                                     entry.Properties["RecordType"].StringValue == RecordType.Agency.ToString()
                                     select entry).Resolve(AbstractEntityAdapter.AdapterResolver <AgencyEntity>).ToList();

            // return null if no records were retrieved
            if (retrievedAgencies == null || retrievedAgencies.Count == 0)
            {
                return(null);
            }

            // throw an exception if more than 1 record was received
            if (retrievedAgencies.Count > 1)
            {
                throw new Exception("Expected 1 record but retrieved " + retrievedAgencies.Count + " records.");
            }

            return(retrievedAgencies[0]);
        }
        public void CreatesAnAgencySpecificIdWhenNoneExists()
        {
            var agencyEntity = new AgencyEntity("test", "test");

            updateContext.Add(agencyEntity);
            updateContext.SaveChanges();


            repository.GetPersonAgencyId((PersonIdentity)personOne.Id, (AgencyIdentity)agencyEntity.Id).Should()
            .NotBeNull();
        }
        /// <summary>
        /// Creates an agency entity with a random Id and name
        /// </summary>
        /// <param name="regionId">optional region Id; if not specified, one will be created</param>
        /// <returns>agency entity</returns>
        public static AgencyEntity FakeAgencyEntity(string regionId = null)
        {
            AgencyEntity agency = new AgencyEntity()
            {
                Id       = Guid.NewGuid().ToString(),
                Name     = Guid.NewGuid().ToString(),
                Url      = string.Empty,
                Phone    = string.Empty,
                RegionId = regionId == null?Guid.NewGuid().ToString() : regionId,
                               RecordType = RecordType.Agency.ToString(),
                               RowState   = DataRowState.Default.ToString(),
                               RawContent = string.Empty
            };

            return(agency);
        }
        public void GetsAnExistingPersonAgencyId()
        {
            var agencyEntity = new AgencyEntity("test", "test");

            updateContext.Add(agencyEntity);
            updateContext.SaveChanges();

            updateContext.Add(
                new PersonAgencyId {
                PersonId = personOne.Id, AgencyId = agencyEntity.Id, SpecificId = "AGENCY"
            });
            updateContext.SaveChanges();


            repository.GetPersonAgencyId((PersonIdentity)personOne.Id, (AgencyIdentity)agencyEntity.Id).Should()
            .Be("AGENCY");
        }
Esempio n. 7
0
        /// <summary>
        /// Stores a list of OBA client agencies in Azure Tables
        /// </summary>
        /// <param name="agencies">list of agencies</param>
        /// <param name="regionId">uniquely identifies the region</param>
        /// <returns>task that stores the entities</returns>
        public async Task Insert(IEnumerable <OBAClient.Model.Agency> agencies, string regionId)
        {
            // convert the input agencies into agency entities
            List <AgencyEntity> agencyEntities = new List <AgencyEntity>();

            foreach (OBAClient.Model.Agency agency in agencies)
            {
                AgencyEntity agencyEntity = new AgencyEntity()
                {
                    Id         = agency.Id,
                    Name       = agency.Name,
                    Url        = agency.Url,
                    Phone      = agency.Phone,
                    RegionId   = regionId,
                    RecordType = RecordType.Agency.ToString(),
                    RowState   = DataRowState.Default.ToString(),
                    RawContent = agency.RawContent
                };

                agencyEntities.Add(agencyEntity);
            }

            await this.Insert(agencyEntities);
        }
Esempio n. 8
0
        public async Task ResurrectEntities()
        {
            // this test messes with the publish table so it should not be run on production accounts
            Assert.IsFalse(Utils.ProdConfiguration.IsProduction(TestConstants.AzureStorageConnectionString));

            string runId = RunId.GenerateTestRunId();

            // clean the publish table so that we are working with known state
            StorageManager publishStorage = new StorageManager(TestConstants.AzureStorageConnectionString, TableNames.TableType.Publish, runId);
            await TestUtilities.CleanPublishStorage(publishStorage);

            // setup download storage
            StorageManager downloadStorage = new StorageManager(TestConstants.AzureStorageConnectionString, TableNames.TableType.Download, runId);
            await downloadStorage.CreateTables();

            // create region
            RegionEntity region = TestUtilities.FakeRegionEntity();
            await downloadStorage.RegionStore.Insert(new List <RegionEntity>() { region });

            // create agency
            AgencyEntity agency = TestUtilities.FakeAgencyEntity(region.Id);
            await downloadStorage.AgencyStore.Insert(new List <AgencyEntity>() { agency });

            // create 1 route and 1 stop in download table that also exist in the publish table as deleted entities
            RouteEntity existingRoute = TestUtilities.FakeRouteEntity(region.Id, agency.Id);
            await downloadStorage.RouteStore.Insert(existingRoute);

            existingRoute.RowState = DataRowState.Delete.ToString();
            await publishStorage.RouteStore.Insert(existingRoute);

            StopEntity existingStop = TestUtilities.FakeStopEntity(region.Id);
            await downloadStorage.StopStore.Insert(existingStop);

            existingStop.RowState = DataRowState.Delete.ToString();
            await publishStorage.StopStore.Insert(existingStop);

            // setup diff manager
            DiffManager diffManager = new DiffManager(TestConstants.AzureStorageConnectionString, runId);
            await diffManager.InitializeStorage();

            // execute diff manager
            await diffManager.DiffAndStore();

            // sleep between the insert and the query otherwise Azure sometimes returns no records
            Thread.Sleep(TestConstants.AzureTableDelay);

            // query results
            StorageManager            diffStorage = new StorageManager(TestConstants.AzureStorageConnectionString, TableNames.TableType.Diff, runId);
            IEnumerable <RouteEntity> diffRoutes  = await diffStorage.RouteStore.GetAllRoutes(existingRoute.RegionId);

            IEnumerable <StopEntity> diffStops = await diffStorage.StopStore.GetAllStops(existingStop.RegionId);

            StorageManager            diffMetadataStorage = new StorageManager(TestConstants.AzureStorageConnectionString, TableNames.TableType.DiffMetadata, runId);
            List <DiffMetadataEntity> diffMetadata        = diffMetadataStorage.DiffMetadataStore.Get(runId).ToList();

            // clean up
            await diffManager.DeleteDiff();

            await downloadStorage.DeleteDataTables();

            await publishStorage.DeleteTables();

            // check route
            Assert.IsNotNull(diffRoutes);
            Assert.AreEqual(diffRoutes.Count(), 1);
            Assert.AreEqual(diffRoutes.First().Id, existingRoute.Id);
            Assert.AreEqual(diffRoutes.First().ShortName, existingRoute.ShortName);
            Assert.AreEqual(diffRoutes.First().LongName, existingRoute.LongName);
            Assert.AreEqual(diffRoutes.First().Description, existingRoute.Description);
            Assert.AreEqual(diffRoutes.First().Url, existingRoute.Url);
            Assert.AreEqual(diffRoutes.First().AgencyId, existingRoute.AgencyId);
            Assert.AreEqual(diffRoutes.First().RegionId, existingRoute.RegionId);
            Assert.AreEqual(diffRoutes.First().RecordType, existingRoute.RecordType);
            Assert.AreEqual(diffRoutes.First().RowState, DataRowState.Resurrect.ToString());
            Assert.AreEqual(diffRoutes.First().RawContent, existingRoute.RawContent);

            // check stop
            Assert.IsNotNull(diffStops);
            Assert.AreEqual(diffStops.Count(), 1);
            Assert.AreEqual(diffStops.First().Id, existingStop.Id);
            Assert.AreEqual(diffStops.First().Lat, existingStop.Lat);
            Assert.AreEqual(diffStops.First().Lon, existingStop.Lon);
            Assert.AreEqual(diffStops.First().Direction, existingStop.Direction);
            Assert.AreEqual(diffStops.First().Name, existingStop.Name);
            Assert.AreEqual(diffStops.First().Code, existingStop.Code);
            Assert.AreEqual(diffStops.First().RegionId, existingStop.RegionId);
            Assert.AreEqual(diffStops.First().RecordType, existingStop.RecordType);
            Assert.AreEqual(diffStops.First().RowState, DataRowState.Resurrect.ToString());
            Assert.AreEqual(diffStops.First().RawContent, existingStop.RawContent);

            // check metadata
            Assert.IsNotNull(diffMetadata);
            Assert.AreEqual(diffMetadata.Count, 2);
            int routeIndex = -1;
            int stopIndex  = -1;

            if (diffMetadata[0].RecordType == RecordType.Route.ToString())
            {
                routeIndex = 0;
                stopIndex  = 1;
            }
            else
            {
                routeIndex = 1;
                stopIndex  = 0;
            }

            Assert.AreEqual(diffMetadata[routeIndex].AddedCount, 0);
            Assert.AreEqual(diffMetadata[routeIndex].AgencyId, existingRoute.AgencyId);
            Assert.AreEqual(diffMetadata[routeIndex].DeletedCount, 0);
            Assert.AreEqual(diffMetadata[routeIndex].RecordType, RecordType.Route.ToString());
            Assert.AreEqual(diffMetadata[routeIndex].RegionId, existingRoute.RegionId);
            Assert.AreEqual(diffMetadata[routeIndex].ResurrectedCount, 1);
            Assert.AreEqual(diffMetadata[routeIndex].RunId, runId);
            Assert.AreEqual(diffMetadata[routeIndex].UpdatedCount, 0);

            Assert.AreEqual(diffMetadata[stopIndex].AddedCount, 0);
            Assert.AreEqual(diffMetadata[stopIndex].AgencyId, string.Empty);
            Assert.AreEqual(diffMetadata[stopIndex].DeletedCount, 0);
            Assert.AreEqual(diffMetadata[stopIndex].RecordType, RecordType.Stop.ToString());
            Assert.AreEqual(diffMetadata[stopIndex].RegionId, existingStop.RegionId);
            Assert.AreEqual(diffMetadata[stopIndex].ResurrectedCount, 1);
            Assert.AreEqual(diffMetadata[stopIndex].RunId, runId);
            Assert.AreEqual(diffMetadata[stopIndex].UpdatedCount, 0);
        }
        public async Task SendTypicalRun()
        {
            Email email = new Email();

            email.To = new List <string>()
            {
                TestConstants.SendGridEmailAddr
            };
            string runId = RunId.GenerateTestRunId();

            email.Add(runId);
            email.Add(TestConstants.EmbeddedSocialUri);

            // create fake region & agency for records
            RegionEntity region = TestUtilities.FakeRegionEntity();
            AgencyEntity agency = TestUtilities.FakeAgencyEntity(region.Id);

            // create fake download metadata
            List <DownloadMetadataEntity> downloadMetadata = new List <DownloadMetadataEntity>();

            downloadMetadata.Add(new DownloadMetadataEntity()
            {
                RunId      = runId,
                RegionId   = region.Id,
                AgencyId   = agency.Id,
                RecordType = RecordType.Route.ToString(),
                Count      = 10
            });
            downloadMetadata.Add(new DownloadMetadataEntity()
            {
                RunId      = runId,
                RegionId   = region.Id,
                AgencyId   = agency.Id,
                RecordType = RecordType.Stop.ToString(),
                Count      = 2
            });
            email.Add(downloadMetadata);

            // create fake diff metadata entries
            List <DiffMetadataEntity> diffMetadata = new List <DiffMetadataEntity>();

            diffMetadata.Add(new DiffMetadataEntity()
            {
                RunId            = runId,
                RegionId         = region.Id,
                AgencyId         = agency.Id,
                RecordType       = RecordType.Route.ToString(),
                AddedCount       = 1,
                UpdatedCount     = 0,
                DeletedCount     = 0,
                ResurrectedCount = 0
            });
            diffMetadata.Add(new DiffMetadataEntity()
            {
                RunId            = runId,
                RegionId         = region.Id,
                AgencyId         = string.Empty,
                RecordType       = RecordType.Stop.ToString(),
                AddedCount       = 1,
                UpdatedCount     = 0,
                DeletedCount     = 0,
                ResurrectedCount = 0
            });
            email.Add(diffMetadata);

            // create fake publish metadata entries
            List <PublishMetadataEntity> publishMetadata = new List <PublishMetadataEntity>();

            publishMetadata.Add(new PublishMetadataEntity()
            {
                RunId            = runId,
                RegionId         = region.Id,
                AgencyId         = agency.Id,
                RecordType       = RecordType.Route.ToString(),
                AddedCount       = 0,
                UpdatedCount     = 1,
                DeletedCount     = 2,
                ResurrectedCount = 999
            });
            publishMetadata.Add(new PublishMetadataEntity()
            {
                RunId            = runId,
                RegionId         = region.Id,
                AgencyId         = string.Empty,
                RecordType       = RecordType.Stop.ToString(),
                AddedCount       = 1,
                UpdatedCount     = 0,
                DeletedCount     = 0,
                ResurrectedCount = 0
            });
            email.Add(publishMetadata);

            // send it
            await email.Send(TestConstants.SendGridKey);
        }