Exemple #1
0
        private async Task RunTest(string objectType)
        {
            Debug.WriteLine("Testing monitoring for ObjectType = " + objectType);

            PlanDTO plan = null;

            try
            {
                var authToken = await Fixtures.HealthMonitor_FixtureData.CreateSalesforceAuthToken();

                plan = await CreateMonitoringPlan(authToken.Id);

                await _plansHelper.RunPlan(plan.Id);

                await HttpPostAsync <string>(GetTerminalEventsUrl(), new StringContent(string.Format(SalesforcePayload, objectType)));

                var stopwatch = new Stopwatch();
                stopwatch.Start();

                while (stopwatch.ElapsedMilliseconds < MaxAwaitTime)
                {
                    await Task.Delay(PeriodAwaitTime);

                    Debug.WriteLine("Awaiting for monitor container to run: " + stopwatch.ElapsedMilliseconds.ToString() + " msec");

                    if (IsContainerAvailable(plan.Id))
                    {
                        Debug.WriteLine("Container successfully executed");
                        return;
                    }
                }

                Assert.Fail("No container found for specified plan.");
            }
            finally
            {
                if (plan != null)
                {
                    await HttpDeleteAsync(GetHubApiBaseUrl() + "/plans?id=" + plan.Id.ToString());
                }
            }
        }
        public async Task GoogleIntoGoogleEndToEnd()
        {
            var googleAuthTokenId      = await new Fr8.Testing.Integration.Tools.Terminals.IntegrationTestTools_terminalGoogle(this).ExtractGoogleDefaultToken();
            var defaultGoogleAuthToken = GetGoogleAuthToken(googleAuthTokenId);

            //create a new plan
            var googleSheetApi             = new GoogleSheet(new GoogleIntegration(ObjectFactory.GetInstance <IRestfulServiceClient>()), new GoogleDrive());
            var sourceSpreadsheetUri       = string.Empty;
            var destinationSpreadsheetUri  = string.Empty;
            var sourceSpreadsheetName      = Guid.NewGuid().ToString();
            var destinationSpreadsheetName = Guid.NewGuid().ToString();

            try
            {
                //Save test data into test spreadsheet
                sourceSpreadsheetUri = await googleSheetApi.CreateSpreadsheet(sourceSpreadsheetName, defaultGoogleAuthToken);

                var sourceWorksheetUri = (await googleSheetApi.GetWorksheets(sourceSpreadsheetUri, defaultGoogleAuthToken)).Select(x => x.Key).First();
                await googleSheetApi.WriteData(sourceSpreadsheetUri, sourceWorksheetUri, GetTestSpreadsheetContent(), defaultGoogleAuthToken);

                var thePlan = await _plansHelper.CreateNewPlan();

                //Configure Get_Google_Sheet_Data activity to read data from this test spreadsheet
                await _googleActivityConfigurator.AddAndConfigureGetFromGoogleSheet(thePlan, 1, sourceSpreadsheetName, true);

                //Configure Build_Message activity to build message based on the data from this spreadsheet
                await _fr8ActivityConfigurator.AddAndConfigureBuildMessage(thePlan, 2, "message", "Email - [email], subject - [subject], body - [body]");

                //Configure Save_To_Google activity to save this message into new test spreadsheet
                destinationSpreadsheetUri = await googleSheetApi.CreateSpreadsheet(destinationSpreadsheetName, defaultGoogleAuthToken);

                await _googleActivityConfigurator.AddAndConfigureSaveToGoogleSheet(thePlan, 3, "Standard Payload Data", Build_Message_v1.RuntimeCrateLabel, destinationSpreadsheetName);

                //run the plan
                await _plansHelper.RunPlan(thePlan.Id);

                var googleSheets = await googleSheetApi.GetSpreadsheets(defaultGoogleAuthToken);

                Assert.IsNotNull(googleSheets.FirstOrDefault(x => x.Value == destinationSpreadsheetName), "New created spreadsheet was not found into existing google files.");
                var spreadSheeturl = googleSheets.FirstOrDefault(x => x.Value == destinationSpreadsheetName).Key;

                //Checking that new test spreadsheet contains the same message that was generated
                var worksheets = await googleSheetApi.GetWorksheets(spreadSheeturl, defaultGoogleAuthToken);

                Assert.IsNotNull(worksheets.FirstOrDefault(x => x.Value == "Sheet1"), "Worksheet was not found into newly created google excel file.");
                var worksheetUri = worksheets.FirstOrDefault(x => x.Value == "Sheet1").Key;
                var dataRows     = (await googleSheetApi.GetData(spreadSheeturl, worksheetUri, defaultGoogleAuthToken)).ToArray();

                Assert.AreEqual(1, dataRows.Length, "Only one data row is expected to be in crated spreadsheet");
                var storedData = dataRows[0].Row[0].Cell;
                Assert.AreEqual("message", storedData.Key, "Saved message header doesn't match the expected data");
                Assert.AreEqual("Email - [email protected], subject - Fake Subject, body - Fake Body", storedData.Value, "Saved message body doesn't match the expected data");
            }
            finally
            {
                if (!string.IsNullOrEmpty(sourceSpreadsheetUri))
                {
                    await googleSheetApi.DeleteSpreadSheet(sourceSpreadsheetUri, defaultGoogleAuthToken);
                }
                if (!string.IsNullOrEmpty(destinationSpreadsheetUri))
                {
                    await googleSheetApi.DeleteSpreadSheet(destinationSpreadsheetUri, defaultGoogleAuthToken);
                }
            }
        }
        public async Task Monitor_Gmail_Inbox_Test()
        {
            Fr8AccountDO         currentUser = null;
            AuthorizationTokenDO token       = null;

            //we use a separate google account for this test.
            GetDifferentGoogleAuthToken(out currentUser, out token);

            var testPlan = await _plansHelper.CreateNewPlan(PlanName);

            await AddAndAuthorizeMonitorGmailInboxActivity(token, testPlan);

            //add saveToFr8Warehouse activity
            var saveToFr8WarehouseActivity = await _fr8ActivityConfigurator.AddAndConfigureSaveToFr8Warehouse(testPlan, 2);

            SetCratesForSaving(saveToFr8WarehouseActivity);
            saveToFr8WarehouseActivity = await HttpPostAsync <ActivityDTO, ActivityDTO>(GetHubApiBaseUrl() + "activities/configure", saveToFr8WarehouseActivity);

            //run the plan
            await _plansHelper.RunPlan(testPlan.Id);

            await Task.Delay(10000);

            //sending an email
            SendAnEmailToMonitoredAccountViaGoogle();

            //testing
            var stopwatch = Stopwatch.StartNew();

            using (var uow = ObjectFactory.GetInstance <IUnitOfWork>())
            {
                var mtDataCountBefore = uow.MultiTenantObjectRepository
                                        .AsQueryable <StandardPayloadDataCM>(currentUser.Id.ToString())
                                        .Count();
                int mtDataCountAfter = mtDataCountBefore;
                while (stopwatch.ElapsedMilliseconds <= MaxAwaitPeriod)
                {
                    await Task.Delay(SingleAwaitPeriod);

                    mtDataCountAfter = uow.MultiTenantObjectRepository
                                       .AsQueryable <StandardEmailMessageCM>(currentUser.Id.ToString()).Count();

                    if (mtDataCountBefore < mtDataCountAfter)
                    {
                        break;
                    }
                }
                Assert.IsTrue(mtDataCountBefore < mtDataCountAfter,
                              $"The number of MtData: ({mtDataCountAfter}) records for user {currentUser.UserName} remained unchanged within {MaxAwaitPeriod} miliseconds.");
            }

            //left here in case the test is ran locally, so the plans don't stack
            using (var uow = ObjectFactory.GetInstance <IUnitOfWork>())
            {
                var plans = uow.PlanRepository.GetPlanQueryUncached().Where(a => a.Name == PlanName && a.Fr8AccountId == currentUser.Id).ToList();
                foreach (var plan in plans)
                {
                    await HttpDeleteAsync(GetHubApiBaseUrl() + $"/plans?id={plan.Id}");
                }
            }

            //cleaning hangfire job
            string connString = (string)ObjectFactory.GetInstance <ISqlConnectionProvider>().ConnectionInfo;

            JobStorage.Current = new Hangfire.SqlServer.SqlServerStorage(connString);
            string terminalSecret = "";

            using (var uow = ObjectFactory.GetInstance <IUnitOfWork>())
            {
                terminalSecret = uow.TerminalRepository.GetQuery().Where(a => a.Name == "terminalGoogle").FirstOrDefault().Secret;
            }
            string jobId = terminalSecret + "|" + token.ExternalAccountId;

            RecurringJob.RemoveIfExists(jobId);
        }