public async Task ErrorsFBUnitTest()
        {
            ConnectorTask taskInfo = new ConnectorTask();

            taskInfo.StartTime = DateTime.Parse("2018-01-09");
            taskInfo.EndTime   = DateTime.Parse("2018-01-11");
            taskInfo.JobId     = "job1";
            taskInfo.TaskId    = "task1";
            taskInfo.TenantId  = "tenant1";

            var mockDownloader = new Mock <IDownloader>();

            mockDownloader.Setup(x => x.GetWebContent <PostListFB, ErrorsFB>(It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .Throws(new ClientException <ErrorsFB>(JsonConvert.DeserializeObject <ErrorsFB>(File.ReadAllText(@"FakeData\FakeError.json"))));

            FakeUploader   uploader = new FakeUploader();
            JobProcessorFB job      = new JobProcessorFB(mockDownloader.Object, uploader);

            string sourceInfo = "{\"PageId\":\"123\",\"AccessToken\":\"Fake\",\"PageName\":\"Fake123\"}";

            try
            {
                await job.FetchData(taskInfo, sourceInfo);
            }
            catch (ClientException <ErrorsFB> error)
            {
                Assert.AreEqual(error.error.Error.ErrorMessage, "Message describing the error");
                Assert.AreEqual(error.error.Error.ErrorType, "OAuthException");
            }

            Assert.AreEqual(uploader.fakeStorage.Count, 0);
        }
        public async Task FetchUpdatedPostsUnitTest()
        {
            ConnectorTask taskInfo = new ConnectorTask();

            taskInfo.StartTime     = DateTime.Parse("2018-01-09");
            taskInfo.EndTime       = DateTime.Parse("2018-01-11");
            taskInfo.JobId         = "job1";
            taskInfo.TaskId        = "task1";
            taskInfo.TenantId      = "tenant1";
            taskInfo.DirtyEntities = new List <string>()
            {
                "a",
                "b"
            };

            var mockDownloader = new Mock <IDownloader>();

            mockDownloader.SetupSequence(x => x.GetWebContent <PostListFB, ErrorsFB>(It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync(JsonConvert.DeserializeObject <PostListFB>("{\"data\": []}"));

            mockDownloader.SetupSequence(x => x.GetWebContent <Dictionary <string, PostFB>, ErrorsFB>(It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync(JsonConvert.DeserializeObject <Dictionary <string, PostFB> >(File.ReadAllText(@"FakeData\FakeDirtyPosts.json")));

            FakeUploader   uploader = new FakeUploader();
            JobProcessorFB job      = new JobProcessorFB(mockDownloader.Object, uploader);

            string sourceInfo = "{\"PageId\":\"123\",\"AccessToken\":\"Fake\",\"PageName\":\"Fake123\"}";

            await job.FetchData(taskInfo, sourceInfo);

            Assert.AreEqual(uploader.fakeStorage.Count, 2);
        }
        public async Task DownloadDataAndTransformUnitTest()
        {
            ConnectorTask taskInfo = new ConnectorTask();

            taskInfo.StartTime = DateTime.Parse("2018-01-09");
            taskInfo.EndTime   = DateTime.Parse("2018-01-11");
            taskInfo.JobId     = "job1";
            taskInfo.TaskId    = "task1";
            taskInfo.TenantId  = "tenant1";

            var mockDownloader = new Mock <IDownloader>();

            mockDownloader.SetupSequence(x => x.GetWebContent <PostListFB, ErrorsFB>(It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync(JsonConvert.DeserializeObject <PostListFB>(File.ReadAllText(@"FakeData\FakeData.json")))
            .ReturnsAsync(JsonConvert.DeserializeObject <PostListFB>("{\"data\": []}"));

            mockDownloader.Setup(x => x.DownloadFileAsBase64EncodedString(It.IsAny <string>()))
            .ReturnsAsync(Convert.ToBase64String(File.ReadAllBytes(@"FakeData\FakeImage.jpg")));

            FakeUploader   uploader = new FakeUploader();
            JobProcessorFB job      = new JobProcessorFB(mockDownloader.Object, uploader);

            string sourceInfo = "{\"PageId\":\"123\",\"AccessToken\":\"Fake\",\"PageName\":\"Fake123\"}";

            await job.FetchData(taskInfo, sourceInfo);

            // Assert 1 item of each type - post, comment, reply
            int assertItemsCount = 0;

            foreach (var entry in uploader.fakeStorage)
            {
                if (File.Exists($@"FakeData\{entry.Key}.json"))
                {
                    assertItemsCount++;
                    string expectedJson = File.ReadAllText($@"FakeData\{entry.Key}.json");
                    Item   expectedItem = JsonConvert.DeserializeObject <Item>(expectedJson);
                    AssertItemsAreEqual(expectedItem, entry.Value);
                }
            }
            Assert.AreEqual(assertItemsCount, 3);
        }
        public async Task NoPostsFetchedUnitTest()
        {
            ConnectorTask taskInfo = new ConnectorTask();

            taskInfo.StartTime = DateTime.Parse("2018-01-09");
            taskInfo.EndTime   = DateTime.Parse("2018-01-11");
            taskInfo.JobId     = "job1";
            taskInfo.TaskId    = "task1";
            taskInfo.TenantId  = "tenant1";

            var mockDownloader = new Mock <IDownloader>();

            mockDownloader.Setup(x => x.GetWebContent <PostListFB, ErrorsFB>(It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync(JsonConvert.DeserializeObject <PostListFB>("{\"data\": []}"));

            FakeUploader   uploader = new FakeUploader();
            JobProcessorFB job      = new JobProcessorFB(mockDownloader.Object, uploader);

            string sourceInfo = "{\"PageId\":\"123\",\"AccessToken\":\"Fake\",\"PageName\":\"Fake123\"}";

            await job.FetchData(taskInfo, sourceInfo);

            Assert.AreEqual(uploader.fakeStorage.Count, 0);
        }
Exemplo n.º 5
0
        public async Task Execute(string jobMessage)
        {
            if (string.IsNullOrEmpty(Settings.AAdAppId) || string.IsNullOrEmpty(Settings.AAdAppSecret))
            {
                // Will throw exception if the connector is not configured.
                await GetConfigurationSettingFomStorge();
            }
            ConnectorTask   taskInfo       = JsonConvert.DeserializeObject <ConnectorTask>(jobMessage);
            IEventApiClient eventApiClient = new EventApiClient(new Auth(Settings.AAdAppId, Settings.AAdAppSecret), Settings.EventAPIBaseUrl);
            IUploader       uploader       = new BlobUploader(taskInfo.BlobSasUri);
            string          sourceInfo     = await GetSourceInfoFromTable(taskInfo);

            Trace.TraceInformation($"Fetched job info from PageJobEntity Table for JobId: {taskInfo.JobId} and TaskId: {taskInfo.TaskId}");
            Status status;
            List <ItemMetadata> itemMetadata = new List <ItemMetadata>();
            JobProcessorBase    jobProcessor = new JobProcessorFB(new Downloader(), uploader);

            try
            {
                itemMetadata = await jobProcessor.FetchData(taskInfo, sourceInfo);

                status = Status.Success;
                Trace.TraceInformation($"Successfully completed Job Execution, JobId:{taskInfo.JobId}, TaskId:{taskInfo.TaskId}");
            }
            catch (HttpRequestException e)
            {
                status = Status.TemporaryFailure;
                Trace.TraceError($"Connectivity Error, JobId:{taskInfo.JobId}, TaskId:{taskInfo.TaskId}, Error: {e.Message}, ErrorStackTrace: {e.StackTrace}");
            }
            catch (Exception e)
            {
                status = Status.PermanentFailure;
                Trace.TraceError($"Unknown Failure, Requires Attention, JobId:{taskInfo.JobId}, TaskId:{taskInfo.TaskId}, Error: {e.Message}, ErrorStackTrace: {e.StackTrace}");
            }
            await eventApiClient.OnDownloadCompleteAsync(taskInfo.TenantId, taskInfo.JobId, taskInfo.TaskId, status, itemMetadata);
        }