private async Task Test_PublishAsync_OK() { // Setup var opts = new TelemetryPublisherOptions() { ConnectionString = "ProjectId=test;CollectionName=test;WebApiKey=TAPIKEY;[email protected];WebApiPassword=tpass" }; var publisher = new MockTelemetryPublisherFirestore(opts); var mockHttpClient = new MockMinimalHttpClient("http://testing.com"); var authResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"idToken\": \"1\",\"expiresIn\": \"3600\"}") }; mockHttpClient.SendAsyncResponses.Add(authResponse); mockHttpClient.SendAsyncResponses.Add(new HttpResponseMessage(HttpStatusCode.OK)); publisher.SetHttpClient(mockHttpClient); publisher.SetMockData(new Dictionary <string, object> { ["testData"] = 1 }); // Run await publisher.PublishAsync(CancellationToken.None); // Assert var request = mockHttpClient.SendAsyncArgCalls[1]; Assert.Equal(HttpMethod.Post, request.Method); Assert.Equal(new Uri("http://testing.com"), request.RequestUri); Assert.Equal("{\"fields\":{\"testData\":{\"integerValue\":1}}}", request.Content.ReadAsStringAsync().GetAwaiter().GetResult()); }
/// <summary> /// Constructor for <see cref="TelemetryPublisher"/>. /// </summary> /// <param name="opts">TelemetryPublisher options, see: <see cref="TelemetryPublisherOptions"/></param> protected TelemetryPublisher(TelemetryPublisherOptions opts) { ConnectionString = opts.ConnectionString; TelemetrySource = opts.TelemetrySource; Logger = opts.Logger; RegisteredTelemeters = new List <ITelemeter>(10); }
private async Task Test_PublishAsync_Cancel() { // Setup var opts = new TelemetryPublisherOptions() { ConnectionString = "ProjectId=test;CollectionName=test" }; var publisher = new MockTelemetryPublisherFirestore(opts); var mockHttpClient = new MockMinimalHttpClient("http://testing.com"); mockHttpClient.SendAsyncResponses.Add(new HttpResponseMessage(HttpStatusCode.OK)); publisher.SetHttpClient(mockHttpClient); publisher.SetMockData(new Dictionary <string, object> { ["testData"] = 1 }); var cts = new CancellationTokenSource(); cts.Cancel(); // Run await publisher.PublishAsync(cts.Token); // Assert Assert.Empty(mockHttpClient.SendAsyncArgCalls); }
private async Task Test_PublishAsync_InvalidJson() { // Setup var opts = new TelemetryPublisherOptions() { ConnectionString = "ProjectId=test;CollectionName=test;WebApiKey=TAPIKEY;[email protected];WebApiPassword=tpass" }; var publisher = new MockTelemetryPublisherFirestore(opts); var mockHttpClient = new MockMinimalHttpClient("http://testing.com"); var authResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"idToken\": \"1\",\"expiresIn\": \"3600\"}") }; mockHttpClient.SendAsyncResponses.Add(authResponse); mockHttpClient.SendAsyncResponses.Add(new HttpResponseMessage(HttpStatusCode.OK)); publisher.SetHttpClient(mockHttpClient); publisher.SetMockData(new Dictionary <string, object> { ["testData"] = double.PositiveInfinity }); // Run await publisher.PublishAsync(CancellationToken.None); // Assert only auth request made. Assert.Single(mockHttpClient.SendAsyncArgCalls); }
private void Test_Construct_BadCollectionName() { // Setup var opts = new TelemetryPublisherOptions() { ConnectionString = "ProjectId=test;CollectionNameBAD=test" }; // Run & Assert Assert.Throws <ArgumentException>(() => { new MockTelemetryPublisherFirestore(opts); }); }
/// <summary> /// Constructs an instance of <see cref="TelemetryPublisherDisk"/>. /// <remarks> /// The connection string must contain the following options: /// Filename (optional) - The path of the filename in which to log telemetry data. /// FileExtension (optional) - The extension of the filename. Default csv /// Separator (optional) - The separator of the messages. Default , /// BufferSize (optional) - The buffer size for the async writer. Default: 4096 /// </remarks> /// </summary> /// <param name="opts"></param> public TelemetryPublisherDisk(TelemetryPublisherOptions opts) : base(opts) { var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString); var fileName = connectionStringParams.GetValueOrDefault("FileName", "telemetry"); var fileExtension = connectionStringParams.GetValueOrDefault("FileExtension", "csv"); var bufferSize = connectionStringParams.GetValueOrDefault("BufferSize", "4096"); _separator = connectionStringParams.GetValueOrDefault("Separator", ","); _fileStream = new FileStream(NormalizeFilename(fileName, fileExtension), FileMode.Append, FileAccess.Write, FileShare.Read, int.Parse(bufferSize), true); Logger?.LogDebug("Initialized the TelemetryPublisherDisk!"); }
private async Task Test_PublishAsync_Authorization_Refresh() { // Setup var opts = new TelemetryPublisherOptions() { ConnectionString = "ProjectId=test;CollectionName=test;WebApiKey=TAPIKEY;[email protected];WebApiPassword=tpass" }; var publisher = new MockTelemetryPublisherFirestore(opts); var mockHttpClient = new MockMinimalHttpClient("http://testing.com"); mockHttpClient.SendAsyncResponses.Add(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"idToken\": \"1\",\"expiresIn\": \"0\"}") }); mockHttpClient.SendAsyncResponses.Add(new HttpResponseMessage(HttpStatusCode.OK)); mockHttpClient.SendAsyncResponses.Add(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"idToken\": \"1\",\"expiresIn\": \"3600\"}") }); mockHttpClient.SendAsyncResponses.Add(new HttpResponseMessage(HttpStatusCode.OK)); publisher.SetHttpClient(mockHttpClient); publisher.SetMockData(new Dictionary <string, object> { ["testData"] = 1 }); // Run await publisher.PublishAsync(CancellationToken.None); await publisher.PublishAsync(CancellationToken.None); // Assert Assert.Equal(4, mockHttpClient.SendAsyncArgCalls.Count); // 1st request auth Assert.Equal(HttpMethod.Post, mockHttpClient.SendAsyncArgCalls[0].Method); Assert.Equal("https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=TAPIKEY", mockHttpClient.SendAsyncArgCalls[0].RequestUri.ToString()); // 2st request payload Assert.Equal(HttpMethod.Post, mockHttpClient.SendAsyncArgCalls[1].Method); // 3rd request auth Assert.Equal(HttpMethod.Post, mockHttpClient.SendAsyncArgCalls[2].Method); Assert.Equal("https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=TAPIKEY", mockHttpClient.SendAsyncArgCalls[2].RequestUri.ToString()); // 4th request payload Assert.Equal(HttpMethod.Post, mockHttpClient.SendAsyncArgCalls[1].Method); }
public TelemetryPublisherAzure(TelemetryPublisherOptions opts) : base(opts) { try { DeviceClient = DeviceClient.CreateFromConnectionString(ConnectionString, TransportType.Mqtt); } catch (FormatException) { Logger?.LogCritical("Can't start telemetry service! Malformed connection string!"); throw; } Logger?.LogDebug("Initialized the AzureTelemetryPublisher!"); }
public TelemetryPublisherFirestore(TelemetryPublisherOptions opts) : base(opts) { // Parse Options var options = ConnectionStringParser.Parse(opts.ConnectionString); if (!options.TryGetValue("ProjectId", out var firestoreProjectId)) { Logger?.LogCritical( $"Can't start {nameof(TelemetryPublisherFirestore)}! Malformed connection string! " + $"Missing ProjectId!"); throw new ArgumentException("Malformed connection string!"); } if (!options.TryGetValue("CollectionName", out var firestoreCollection)) { Logger?.LogCritical( $"Can't start {nameof(TelemetryPublisherFirestore)}! Malformed connection string! " + $"Missing CollectionName!"); throw new ArgumentException("Malformed connection string!"); } var timeout = int.Parse(options.GetValueOrDefault("Timeout", "10000") ?? "10000"); _webApiKey = options.GetValueOrDefault("WebApiKey", null); _webEmail = options.GetValueOrDefault("WebApiEmail", null); _webPassword = options.GetValueOrDefault("WebApiPassword", null); // Setup HttpClient var requestUrl = $"https://firestore.googleapis.com/v1/projects/{firestoreProjectId}/" + $"databases/(default)/documents/{firestoreCollection}/"; HttpClient = new MinimalHttpClient(requestUrl) { Timeout = timeout, Logger = Logger }; Logger?.LogInformation($"Initialized {nameof(TelemetryPublisherFirestore)}"); Logger?.LogInformation($"ProjectId: {firestoreProjectId}; CollectionName: {firestoreCollection}."); }
public MockTelemetryPublisherFirestore(TelemetryPublisherOptions opts) : base(opts) { _mockData = new Dictionary <string, object>(); }
public TelemetryPublisherConsole(TelemetryPublisherOptions opts) : base(opts) { }