public async Task WhenSendingQueryThenTheResultsAreAsExpected()
        {
            var client = new LogAnalyticsTelemetryDataClient(this.tracerMock.Object, new TestHttpClientWrapper(), this.credentialsFactoryMock.Object, this.azureResourceManagerClientMock.Object, Workspaces, TimeSpan.FromMinutes(10));
            IList <DataTable> results = await client.RunQueryAsync(Query, DataTimeSpan, default(CancellationToken));

            VerifyDataTables(TestHttpClientWrapper.GetExpectedResults(), results);
        }
        public async Task WhenSendingQueryWithEmptyResultsThenResultsAreAsExpected()
        {
            var client = new LogAnalyticsTelemetryDataClient(this.tracerMock.Object, new TestHttpClientWrapper(emptyResults: true), this.credentialsFactoryMock.Object, WorkspaceId, WorkspaceNames, TimeSpan.FromMinutes(10));
            IList <DataTable> results = await client.RunQueryAsync(Query, default(CancellationToken));

            VerifyDataTables(new List <DataTable>(), results);
        }
        public async Task WhenSendingQueryWithInvalidTypeThenAnExceptionIsThrown()
        {
            var client = new LogAnalyticsTelemetryDataClient(this.tracerMock.Object, new TestHttpClientWrapper(invalidType: true), this.credentialsFactoryMock.Object, WorkspaceId, WorkspaceNames, TimeSpan.FromMinutes(10));
            IList <DataTable> results = await client.RunQueryAsync(Query, default(CancellationToken));

            VerifyDataTables(TestHttpClientWrapper.GetExpectedResults(), results);
        }
        public async Task WhenSomeOfTelemetryResourcesAreDeletedThenDeletedResourcesAreIgnoredAndResultsAreReturnedSuccessfully()
        {
            Dictionary <ResourceIdentifier, string> workspaceResourceIdToWorkspaceIdMapping = CreateWorkspaceResourceIdToWorkspaceIdMapping();

            Mock <IExtendedAzureResourceManagerClient> azureResourceManagerClientMock = new Mock <IExtendedAzureResourceManagerClient>();

            azureResourceManagerClientMock
            .Setup(m => m.GetLogAnalyticsWorkspaceIdAsync(It.IsAny <ResourceIdentifier>(), It.IsAny <CancellationToken>()))
            .Returns <ResourceIdentifier, CancellationToken>(
                (resource, token) =>
                workspaceResourceIdToWorkspaceIdMapping.ContainsKey(resource) ?
                Task.FromResult(workspaceResourceIdToWorkspaceIdMapping[resource]) :
                throw new AzureResourceManagerClientException(HttpStatusCode.NotFound, string.Empty, null));

            azureResourceManagerClientMock
            .Setup(m => m.GetResourcePropertiesAsync(It.IsAny <ResourceIdentifier>(), It.IsAny <CancellationToken>()))
            .Returns <ResourceIdentifier, CancellationToken>(
                (resource, token) =>
                workspaceResourceIdToWorkspaceIdMapping.ContainsKey(resource) ?
                Task.FromResult <ResourceProperties>(null) :
                throw new AzureResourceManagerClientException(HttpStatusCode.NotFound, string.Empty, null));

            var resourcesForRequest = workspaceResourceIdToWorkspaceIdMapping.Keys.Select(resource => resource.ToResourceId()).ToList();

            // Delete some workspaces
            workspaceResourceIdToWorkspaceIdMapping.Remove(new ResourceIdentifier(ResourceType.LogAnalytics, "f0b2be5e-9cfa-489e-b627-50a216a18c93", "someGroup", "workspace1"));
            workspaceResourceIdToWorkspaceIdMapping.Remove(new ResourceIdentifier(ResourceType.LogAnalytics, "f0b2be5e-9cfa-489e-b627-50a216a18c93", "someGroup", "workspace2"));
            workspaceResourceIdToWorkspaceIdMapping.Remove(new ResourceIdentifier(ResourceType.LogAnalytics, "f0b2be5e-9cfa-489e-b627-50a216a18c93", "someGroup", "workspace25"));
            workspaceResourceIdToWorkspaceIdMapping.Remove(new ResourceIdentifier(ResourceType.LogAnalytics, "f0b2be5e-9cfa-489e-b627-50a216a18c93", "someGroup", "workspace31"));

            var clientWrapper         = new CrossTelemetryResourcesTestHttpClientWrapper(workspaceResourceIdToWorkspaceIdMapping);
            var client                = new LogAnalyticsTelemetryDataClient(this.tracerMock.Object, clientWrapper, this.credentialsFactoryMock.Object, azureResourceManagerClientMock.Object, resourcesForRequest, TimeSpan.FromMinutes(10));
            IList <DataTable> results = await client.RunQueryAsync(Query, DataTimeSpan, default(CancellationToken));

            // Verify results
            DataTable expectedResult = new DataTable("PrimaryResult");

            expectedResult.Columns.Add("Category", typeof(string));
            expectedResult.Columns.Add("count_", typeof(long));

            expectedResult.Rows.Add("Administrative", 20839);
            expectedResult.Rows.Add("Recommendation", 122);
            expectedResult.Rows.Add("Alert", 64);
            expectedResult.Rows.Add("ServiceHealth", 11);
            expectedResult.Rows.Add("Administrative", 20839);
            expectedResult.Rows.Add("Benefits", 2);
            expectedResult.Rows.Add("Alert", 28);
            expectedResult.Rows.Add("Employers", 15);

            VerifyDataTables(new List <DataTable> {
                expectedResult
            }, results);

            Assert.AreEqual(3, clientWrapper.CallsToSendAsync);
        }
        public async Task WhenQueryReturnsAnErrorThenTheCorrectExceptionIsThrown()
        {
            var client = new LogAnalyticsTelemetryDataClient(this.tracerMock.Object, new TestHttpClientWrapper(error: true), this.credentialsFactoryMock.Object, this.azureResourceManagerClientMock.Object, Workspaces, TimeSpan.FromMinutes(10));

            try
            {
                await client.RunQueryAsync(Query, DataTimeSpan, default(CancellationToken));

                Assert.Fail("An exception should have been thrown");
            }
            catch (TelemetryDataClientException e)
            {
                Assert.AreEqual($"[test error code] test error message", e.Message, "Exception message mismatch");
                Assert.IsNull(e.InnerException, "Inner exception is not null");
            }
        }
        public async Task WhenCrossTelemetryResourcesQueryIsSentThenResultsAreCombinedCorrectly()
        {
            var workspaceResourceIdToWorkspaceIdMapping = CreateWorkspaceResourceIdToWorkspaceIdMapping();

            Mock <IExtendedAzureResourceManagerClient> azureResourceManagerClientMock = new Mock <IExtendedAzureResourceManagerClient>();

            azureResourceManagerClientMock
            .Setup(m => m.GetLogAnalyticsWorkspaceIdAsync(It.IsAny <ResourceIdentifier>(), It.IsAny <CancellationToken>()))
            .Returns <ResourceIdentifier, CancellationToken>(
                (resource, token) =>
                workspaceResourceIdToWorkspaceIdMapping.ContainsKey(resource) ?
                Task.FromResult(workspaceResourceIdToWorkspaceIdMapping[resource]) :
                throw new Microsoft.Rest.Azure.CloudException()
            {
                Response = new Microsoft.Rest.HttpResponseMessageWrapper(new HttpResponseMessage(HttpStatusCode.NotFound), string.Empty)
            });

            var clientWrapper         = new CrossTelemetryResourcesTestHttpClientWrapper(workspaceResourceIdToWorkspaceIdMapping);
            var client                = new LogAnalyticsTelemetryDataClient(this.tracerMock.Object, clientWrapper, this.credentialsFactoryMock.Object, azureResourceManagerClientMock.Object, workspaceResourceIdToWorkspaceIdMapping.Keys.Select(resource => resource.ToResourceId()), TimeSpan.FromMinutes(10));
            IList <DataTable> results = await client.RunQueryAsync(Query, DataTimeSpan, default(CancellationToken));

            // Verify results
            DataTable expectedResult = new DataTable("PrimaryResult");

            expectedResult.Columns.Add("Category", typeof(string));
            expectedResult.Columns.Add("count_", typeof(long));
            expectedResult.Columns.Add("Subcategory", typeof(string));

            expectedResult.Rows.Add("Administrative", 20839, null);
            expectedResult.Rows.Add("Recommendation", 122, null);
            expectedResult.Rows.Add("Alert", 64, null);
            expectedResult.Rows.Add("ServiceHealth", 11, null);
            expectedResult.Rows.Add("Administrative", 20839, null);
            expectedResult.Rows.Add("Benefits", 2, null);
            expectedResult.Rows.Add("Alert", 28, null);
            expectedResult.Rows.Add("Employers", 15, null);
            expectedResult.Rows.Add("Spendings", 15687456, "Contractors");
            expectedResult.Rows.Add("Actions", 122, null);
            expectedResult.Rows.Add("Alert", 28, "Sev1");
            expectedResult.Rows.Add("Employees", 889, null);

            VerifyDataTables(new List <DataTable> {
                expectedResult
            }, results);

            Assert.AreEqual(3, clientWrapper.CallsToSendAsync);
        }