Exemplo n.º 1
0
        public async Task InsertAndGet()
        {
            // create tables
            string         runId          = RunId.GenerateTestRunId();
            StorageManager storageManager = new StorageManager(TestConstants.AzureStorageConnectionString, TableNames.TableType.DiffMetadata, runId);
            await storageManager.CreateTables();

            // create a bogus diff metadata entity
            DiffMetadataEntity entity = new DiffMetadataEntity()
            {
                RunId            = runId,
                RegionId         = "abcd",
                AgencyId         = "1234",
                RecordType       = RecordType.Route.ToString(),
                AddedCount       = 50,
                UpdatedCount     = 75,
                DeletedCount     = 1,
                ResurrectedCount = 5
            };

            // stick it into store
            List <DiffMetadataEntity> entities = new List <DiffMetadataEntity>();

            entities.Add(entity);
            await storageManager.DiffMetadataStore.Insert(entities);

            // get it from the store
            IEnumerable <DiffMetadataEntity> retrievedEntities1 = storageManager.DiffMetadataStore.Get(runId);

            // clean up
            await storageManager.DiffMetadataStore.Delete(runId);

            // get it from the store again
            IEnumerable <DiffMetadataEntity> retrievedEntities2 = storageManager.DiffMetadataStore.Get(runId);

            // check the retrieved records
            Assert.IsNotNull(retrievedEntities1);
            Assert.AreEqual(retrievedEntities1.Count(), entities.Count);
            Assert.IsNull(retrievedEntities2);
            Assert.AreEqual(entity.RunId, retrievedEntities1.First().RunId);
            Assert.AreEqual(entity.RegionId, retrievedEntities1.First().RegionId);
            Assert.AreEqual(entity.AgencyId, retrievedEntities1.First().AgencyId);
            Assert.AreEqual(entity.RecordType, retrievedEntities1.First().RecordType);
            Assert.AreEqual(entity.AddedCount, retrievedEntities1.First().AddedCount);
            Assert.AreEqual(entity.UpdatedCount, retrievedEntities1.First().UpdatedCount);
            Assert.AreEqual(entity.DeletedCount, retrievedEntities1.First().DeletedCount);
            Assert.AreEqual(entity.ResurrectedCount, retrievedEntities1.First().ResurrectedCount);
        }
        /// <summary>
        /// Compares downloaded OBA routes to published OBA routes for a single region and stores a diff
        /// </summary>
        /// <param name="downloadedRoutes">list of routes downloaded from OBA servers</param>
        /// <param name="publishedRoutes">list of routes published to Embedded Social</param>
        /// <param name="diffStorage">interface to diff data</param>
        /// <param name="diffMetadataStorage">inteface to diff metadata</param>
        /// <param name="regionId">uniquely identifies the region</param>
        /// <param name="agencyId">uniquely identifies the agency</param>
        /// <param name="runId">uniquely identifies a run of the service</param>
        /// <returns>task that computes and stores a diff of routes</returns>
        private static async Task DiffAndStore(IEnumerable <RouteEntity> downloadedRoutes, IEnumerable <RouteEntity> publishedRoutes, StorageManager diffStorage, StorageManager diffMetadataStorage, string regionId, string agencyId, string runId)
        {
            // create empty lists of the four different types of changes to be published
            List <RouteEntity> newRoutes         = new List <RouteEntity>();
            List <RouteEntity> updatedRoutes     = new List <RouteEntity>();
            List <RouteEntity> deletedRoutes     = new List <RouteEntity>();
            List <RouteEntity> resurrectedRoutes = new List <RouteEntity>();

            // in the common case, most route information will not change.
            // so, start by removing those items that are common to both considering their relevant content
            IEnumerable <RouteEntity> unchangedRoutes = downloadedRoutes.Intersect(publishedRoutes.Where(r => r.RowState != DataRowState.Delete.ToString()), new RouteEqualityComparerByChecksum());

            downloadedRoutes = downloadedRoutes.Except(unchangedRoutes, new RouteEqualityComparerById());
            publishedRoutes  = publishedRoutes.Except(unchangedRoutes, new RouteEqualityComparerById());

            // new routes are those in downloadedRoutes but not in publishedRoutes when solely considering route Ids
            newRoutes = downloadedRoutes.Except(publishedRoutes, new RouteEqualityComparerById()).ToList();

            // deleted routes are those in publishedRoutes but not in downloadedRoutes when solely considering routeIds,
            // and are not already marked as deleted in publishedRoutes
            deletedRoutes = publishedRoutes.Except(downloadedRoutes, new RouteEqualityComparerById()).Where(r => r.RowState != DataRowState.Delete.ToString()).ToList();

            // resurrected routes are those in publishedRoutes that are marked as deleted but are in downloadedRoutes when solely considering routeIds
            resurrectedRoutes = downloadedRoutes.Intersect(publishedRoutes.Where(r => r.RowState == DataRowState.Delete.ToString()), new RouteEqualityComparerById()).ToList();

            // updated routes : first get the routes that are in common by Id that have not been resurrected,
            // then pick those who's contents are different
            updatedRoutes = downloadedRoutes.Intersect(publishedRoutes.Where(r => r.RowState != DataRowState.Delete.ToString()), new RouteEqualityComparerById()).ToList();
            updatedRoutes = updatedRoutes.Except(publishedRoutes, new RouteEqualityComparerByChecksum()).ToList();

            // set the appropriate datarowstate values
            newRoutes.ForEach(r => r.RowState         = DataRowState.Create.ToString());
            updatedRoutes.ForEach(r => r.RowState     = DataRowState.Update.ToString());
            deletedRoutes.ForEach(r => r.RowState     = DataRowState.Delete.ToString());
            resurrectedRoutes.ForEach(r => r.RowState = DataRowState.Resurrect.ToString());

            // write to diff table
            await diffStorage.RouteStore.Insert(newRoutes);

            await diffStorage.RouteStore.Insert(updatedRoutes);

            await diffStorage.RouteStore.Insert(deletedRoutes);

            await diffStorage.RouteStore.Insert(resurrectedRoutes);

            // write to metadata table
            DiffMetadataEntity metadata = new DiffMetadataEntity
            {
                RunId            = runId,
                RegionId         = regionId,
                AgencyId         = agencyId,
                RecordType       = RecordType.Route.ToString(),
                AddedCount       = newRoutes.Count,
                UpdatedCount     = updatedRoutes.Count,
                DeletedCount     = deletedRoutes.Count,
                ResurrectedCount = resurrectedRoutes.Count
            };
            await diffMetadataStorage.DiffMetadataStore.Insert(new List <DiffMetadataEntity> {
                metadata
            });
        }