Beispiel #1
0
        public void WriteData_Insert_True()
        {
            ///Arrange
            var mockCon    = new Mock <Container>();
            var myItems    = new List <FoodFacility>();
            var mockLogger = new Mock <ILogger>();

            var item = new FoodFacility();

            item.objectid  = "0";
            item.latitude  = "0";
            item.longitude = "0";

            myItems.Add(item);

            var responseMock = new Mock <ItemResponse <FoodFacility> >();

            mockCon.Setup(c => c.ReadItemAsync <FoodFacility>(It.IsAny <string>(), It.IsAny <PartitionKey>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>())).Throws(new Exception());

            try
            {
                ///Act
                CosmosDAL cDAL = new CosmosDAL(null, null, mockCon.Object, mockLogger.Object);
                cDAL.WriteData(myItems).Wait();
            }
            catch
            {
                ///Assert
                Assert.True(false);
            }
        }
Beispiel #2
0
        public void QueryData_ValidResults_True()
        {
            ///Arrange
            const string QUERY   = "Test Query";
            var          mockCon = new Mock <Container>();
            var          myItems = new List <FoodFacilityResponse>();

            var item = new FoodFacilityResponse();

            myItems.Add(item);

            var feedResponseMock = new Mock <FeedResponse <FoodFacilityResponse> >();

            feedResponseMock.Setup(x => x.GetEnumerator()).Returns(myItems.GetEnumerator());

            var feedIteratorMock = new Mock <FeedIterator <FoodFacilityResponse> >();

            feedIteratorMock.Setup(f => f.HasMoreResults).Returns(true);
            feedIteratorMock
            .Setup(f => f.ReadNextAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(feedResponseMock.Object)
            .Callback(() => feedIteratorMock
                      .Setup(f => f.HasMoreResults)
                      .Returns(false));
            var qd = new QueryDefinition(QUERY);

            mockCon.Setup(c => c.GetItemQueryIterator <FoodFacilityResponse>(It.IsAny <QueryDefinition>(), It.IsAny <string>(), It.IsAny <QueryRequestOptions>())).Returns(feedIteratorMock.Object);

            ///Act
            CosmosDAL cDAL   = new CosmosDAL(null, null, mockCon.Object, null);
            var       result = cDAL.QueryData(qd).Result;

            ///Assert
            Assert.Equal(myItems.Count, result.Count);
        }
        /// <summary>
        /// Orchstrates the data ingestion
        /// </summary>
        /// <param name="wHelper">Web helper</param>
        /// <param name="cDAL">Cosmos DAL</param>
        /// <param name="config">Config</param>
        /// <param name="log">Logger</param>
        public async Task SynchronizeData(WebRequestHelper wHelper, CosmosDAL cDAL, IConfiguration config, ILogger log)
        {
            // Extract URL from config
            string url = config[ConstantStrings.DATA_API_URL];

            //Get Web response
            var jsonResponse = await wHelper.GetResponse(url);

            //Load list
            var foodtrucks = JsonConvert.DeserializeObject <List <FoodFacility> >(jsonResponse);

            //Write to Cosmos
            await cDAL.WriteData(foodtrucks);
        }
 /// <summary>
 /// Only constructor
 /// </summary>
 /// <param name="logger">logger</param>
 /// <param name="configuration">configuration</param>
 /// <param name="cDAL">DAL for Cosmos</param>
 public FoodTruckController(ILogger <FoodTruckController> logger, IConfiguration configuration, CosmosDAL cDAL = null)
 {
     this.logger        = logger;
     this.configuration = configuration;
     this.cosmosDAL     = cDAL;
     if (cosmosDAL == null)
     {
         cosmosDAL = new CosmosDAL(configuration[ConstantStrings.COSMOS_ENDPOINT_URL],
                                   configuration[ConstantStrings.COSMOS_PRIMARY_KEY],
                                   configuration[ConstantStrings.COSMOS_DATABASE_NAME],
                                   configuration[ConstantStrings.COSMOS_CONTAINER_NAME],
                                   logger);
     }
 }
Beispiel #5
0
        public static void Run(
            [TimerTrigger("0 0 */12 * * *")] TimerInfo myTimer, ILogger log)
        {
            try
            {
                log.LogInformation($"Starting scheduled task {DateTime.Now}");

                //Set Configuration Object
                var config = new ConfigurationBuilder()
                             .SetBasePath(Directory.GetCurrentDirectory())
                             .AddEnvironmentVariables()
                             .AddJsonFile("./local.settings.json", true)
                             .Build();

                //Get Web response
                WebRequestHelper wHelper = new WebRequestHelper(log);

                //DAL
                CosmosDAL cDAL = new CosmosDAL(config[ConstantStrings.COSMOS_ENDPOINT_URL],
                                               config[ConstantStrings.COSMOS_PRIMARY_KEY],
                                               config[ConstantStrings.COSMOS_DATABASE_NAME],
                                               config[ConstantStrings.COSMOS_CONTAINER_NAME],
                                               log);

                //Instantiate Orchestrator
                Orchestrator orc = new Orchestrator();

                //Perform synchronization
                orc.SynchronizeData(wHelper, cDAL, config, log);

                log.LogInformation($"Completed scheduled task {DateTime.Now}");
            }
            catch (Exception e)
            {
                log.LogError(e, $"There was an error executing the task");
                throw;
            }
        }