Example #1
0
        public async Task WhenCallingExecuteArmQueryAsyncThenTheFlowIsCorrect()
        {
            ResourceIdentifier sqlResource = new ResourceIdentifier(ResourceType.SqlServer, "subscription", "resourceGroup", "server1");

            string url       = "https://management.azure.com/subscriptions/subscription/resourceGroups/resourceGroup/providers/Microsoft.Sql/servers/server1/databases?api-version=3.3";
            string nextLink1 = "https://management.azure.com/next1";
            string nextLink2 = "https://management.azure.com/next2";

            this.SetupHttpClientWrapper(url, nextLink1, "db1", "db2");
            this.SetupHttpClientWrapper(nextLink1, nextLink2, "db3", "db4", "db5");
            this.SetupHttpClientWrapper(nextLink2, string.Empty, "db6");

            IAzureResourceManagerClient azureResourceManagerClient = new ExtendedAzureResourceManagerClient(this.httpClientWrapperMock.Object, this.credentialsFactoryMock.Object, this.tracerMock.Object);
            List <JObject> databases = await azureResourceManagerClient.ExecuteArmQueryAsync(sqlResource, "/databases", "api-version=3.3", CancellationToken.None);

            this.httpClientWrapperMock.Verify(x => x.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <TimeSpan?>(), It.IsAny <CancellationToken>()), Times.Exactly(3));
            this.httpClientWrapperMock.Verify(x => x.SendAsync(It.Is <HttpRequestMessage>(m => m.RequestUri.ToString() == url), It.IsAny <TimeSpan?>(), It.IsAny <CancellationToken>()), Times.Once);
            this.httpClientWrapperMock.Verify(x => x.SendAsync(It.Is <HttpRequestMessage>(m => m.RequestUri.ToString() == nextLink1), It.IsAny <TimeSpan?>(), It.IsAny <CancellationToken>()), Times.Once);
            this.httpClientWrapperMock.Verify(x => x.SendAsync(It.Is <HttpRequestMessage>(m => m.RequestUri.ToString() == nextLink2), It.IsAny <TimeSpan?>(), It.IsAny <CancellationToken>()), Times.Once);
            Assert.AreEqual(6, databases.Count);
            Assert.AreEqual("db1", databases[0]["DBName"]);
            Assert.AreEqual("db2", databases[1]["DBName"]);
            Assert.AreEqual("db3", databases[2]["DBName"]);
            Assert.AreEqual("db4", databases[3]["DBName"]);
            Assert.AreEqual("db5", databases[4]["DBName"]);
            Assert.AreEqual("db6", databases[5]["DBName"]);
        }
        /// <summary>
        /// Raises the <see cref="Application.Startup" /> event.
        /// </summary>
        /// <param name="e">A <see cref="StartupEventArgs" /> that contains the event data.</param>
        protected override void OnStartup(StartupEventArgs e)
        {
            // Cleanup previous temp folders (that are at least 2 days old), and create a new temp folder
            FileSystemExtensions.CleanupTempFolders(TempSubFolderName, 48);
            tempFolder = FileSystemExtensions.CreateTempFolder(TempSubFolderName);

            NotificationService notificationService = new NotificationService();
            ITracer             consoleTracer       = new ConsoleTracer(string.Empty);
            var smartDetectorLoader = new SmartDetectorLoader(tempFolder, consoleTracer);

            // *Temporary*: if package file path wasn't accepted, raise file selection window to allow package file selection.
            // This option should be removed before launching version for customers (bug for tracking: 1177247)
            string smartDetectorPackagePath = e.Args.Length != 1 ?
                                              GetSmartDetectorPackagePath() :
                                              Diagnostics.EnsureStringNotNullOrWhiteSpace(() => e.Args[0]);

            SmartDetectorPackage smartDetectorPackage;

            using (var fileStream = new FileStream(smartDetectorPackagePath, FileMode.Open))
            {
                smartDetectorPackage = SmartDetectorPackage.CreateFromStream(fileStream);
            }

            try
            {
                SmartDetectorManifest smartDetectorManifest = smartDetectorPackage.Manifest;
                ISmartDetector        detector = smartDetectorLoader.LoadSmartDetector(smartDetectorPackage);

                // Authenticate the user to Active Directory
                IAuthenticationServices authenticationServices = new AuthenticationServices();
                authenticationServices.AuthenticateUserAsync().Wait();
                ICredentialsFactory credentialsFactory = new ActiveDirectoryCredentialsFactory(authenticationServices);
                IHttpClientWrapper  httpClientWrapper  = new HttpClientWrapper();
                IExtendedAzureResourceManagerClient azureResourceManagerClient = new ExtendedAzureResourceManagerClient(httpClientWrapper, credentialsFactory, consoleTracer);

                // Create analysis service factory
                IInternalAnalysisServicesFactory analysisServicesFactory = new AnalysisServicesFactory(consoleTracer, httpClientWrapper, credentialsFactory, azureResourceManagerClient);

                // Create state repository factory
                IStateRepositoryFactory stateRepositoryFactory = new EmulationStateRepositoryFactory();

                // Load user settings
                var userSettings = UserSettings.LoadUserSettings();

                // Create the detector runner
                IPageableLogArchive           logArchive          = new PageableLogArchive(smartDetectorManifest.Name);
                IEmulationSmartDetectorRunner smartDetectorRunner = new SmartDetectorRunner(
                    detector,
                    analysisServicesFactory,
                    smartDetectorManifest,
                    stateRepositoryFactory,
                    azureResourceManagerClient,
                    logArchive);

                // Create a Unity container with all the required models and view models registrations
                Container = new UnityContainer();
                Container
                .RegisterInstance(notificationService)
                .RegisterInstance <ITracer>(consoleTracer)
                .RegisterInstance(new AlertsRepository())
                .RegisterInstance(authenticationServices)
                .RegisterInstance(azureResourceManagerClient)
                .RegisterInstance(detector)
                .RegisterInstance(smartDetectorManifest)
                .RegisterInstance(analysisServicesFactory)
                .RegisterInstance(logArchive)
                .RegisterInstance(smartDetectorRunner)
                .RegisterInstance(stateRepositoryFactory)
                .RegisterInstance(userSettings);
            }
            catch (Exception exception)
            {
                var message = $"{exception.Message}. {Environment.NewLine}{exception.InnerException?.Message}";
                MessageBox.Show(message);
                System.Diagnostics.Trace.WriteLine(message);
                Environment.Exit(1);
            }
        }