private Mock <ITelimenaHttpClient> GetMockClientForCheckForUpdates(Guid propertiesTelemetryKey, object programUpdatesResponse, object updaterResponse) { Mock <ITelimenaHttpClient> client = new Mock <ITelimenaHttpClient>(); client.Setup(x => x.GetAsync(It.IsRegex("^" + Regex.Escape(ApiRoutes.GetProgramUpdaterName(propertiesTelemetryKey))))).Returns((string uri) => { HttpResponseMessage response = new HttpResponseMessage(); response.Content = new StringContent("MyUpdater.exe"); return(Task.FromResult(response)); }); client.Setup(x => x.PostAsync("api/v1/telemetry/initialize", It.IsAny <HttpContent>())).Returns((string uri, HttpContent requestContent) => { HttpResponseMessage response = new HttpResponseMessage(); TelemetryInitializeResponse telemetryInitializeResponse = new TelemetryInitializeResponse { UserId = Guid.NewGuid() }; response.Content = new StringContent(JsonConvert.SerializeObject(telemetryInitializeResponse)); return(Task.FromResult(response)); }); client.Setup(x => x.PostAsync(It.IsRegex(".*" + Regex.Escape(ApiRoutes.ProgramUpdateCheck)), It.IsAny <HttpContent>())).Returns((string uri, HttpContent cnt) => { HttpResponseMessage response = new HttpResponseMessage(); response.Content = new StringContent(JsonConvert.SerializeObject(programUpdatesResponse)); return(Task.FromResult(response)); }); client.Setup(x => x.PostAsync(It.IsRegex(".*" + Regex.Escape(ApiRoutes.UpdaterUpdateCheck)), It.IsAny <HttpContent>())).Returns((string uri, HttpContent cnt) => { HttpResponseMessage response = new HttpResponseMessage(); response.Content = new StringContent(JsonConvert.SerializeObject(updaterResponse)); return(Task.FromResult(response)); }); return(client); }
public static IMessenger GetMessenger_InitializeAndAcceptTelemetry(Guid key) { Mock <ITelimenaHttpClient> client = new Mock <ITelimenaHttpClient>(); client.Setup(x => x.GetAsync(It.IsRegex("^" + Regex.Escape(ApiRoutes.GetProgramUpdaterName(key))))).Returns((string uri) => { HttpResponseMessage response = new HttpResponseMessage(); response.Content = new StringContent("Updater.exe"); return(Task.FromResult(response)); }); client.Setup(x => x.PostAsync(ApiRoutes.Initialize, It.IsAny <HttpContent>())).Returns((string uri, HttpContent requestContent) => { HttpResponseMessage response = new HttpResponseMessage(); TelemetryInitializeResponse telemetryInitializeResponse = new TelemetryInitializeResponse { UserId = Guid.NewGuid() }; response.Content = new StringContent(JsonConvert.SerializeObject(telemetryInitializeResponse)); return(Task.FromResult(response)); }); client.Setup(x => x.PostAsync(It.IsIn(ApiRoutes.PostTelemetryData), It.IsAny <HttpContent>())).Returns( (string uri, HttpContent requestContent) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.Accepted))); return(new Messenger(new TelimenaSerializer(), client.Object)); ; }
private void HandleInitialize(ITelimena telimena) { Console.WriteLine("Sending Initialize request"); TelemetryInitializeResponse result = (telimena as Telimena).Initialize().GetAwaiter().GetResult(); Console.WriteLine("Received Initialize response"); Console.WriteLine(JsonConvert.SerializeObject(result)); }
private string PresentResponse(TelemetryInitializeResponse response) { if (response.Exception != null) { return(response.Exception.ToString()); } return(new JavaScriptSerializer().Serialize(response)); }
public async Task TestMissingProgram() { TelemetryInitializeRequest request = new TelemetryInitializeRequest(Guid.NewGuid()) { }; TelemetryUnitOfWork unit = new TelemetryUnitOfWork(this.TelemetryContext, this.PortalContext, new AssemblyStreamVersionReader()); TelemetryController sut = new TelemetryController(unit); TelemetryInitializeResponse response = await sut.Initialize(request).ConfigureAwait(false); Assert.IsTrue(response.Exception.Message.Contains($"Program [{request.TelemetryKey}] is null")); }
public async Task <TelemetryInitializeResponse> Initialize(TelemetryInitializeRequest request) { try { Stopwatch sw = Stopwatch.StartNew(); if (!ApiRequestsValidator.IsRequestValid(request)) { return(new TelemetryInitializeResponse() { Exception = new BadRequestException("Request is not valid") }); } TelemetryRootObject program = await work.GetMonitoredProgram(request.TelemetryKey).ConfigureAwait(false); if (program == null) { { return(new TelemetryInitializeResponse { Exception = new InvalidOperationException($"Program [{request.TelemetryKey}] is null") }); } } string ip = "0.0.0.0"; ClientAppUser clientAppUser = await TelemetryControllerHelpers.GetUserOrAddIfMissing(this.work, request.UserInfo, ip).ConfigureAwait(false); await this.work.CompleteAsync().ConfigureAwait(false); TelemetryInitializeResponse response = new TelemetryInitializeResponse { UserId = clientAppUser.PublicId }; sw.Stop(); this.telemetryClient.TrackEvent("Initialize", new Dictionary <string, string>() { { $"ProgramName", request.ProgramInfo?.Name }, { $"ExecutionTime", sw.ElapsedMilliseconds.ToString() }, { $"ProgramId", program.ProgramId.ToString() }, { $"TelemetryKey", program.TelemetryKey.ToString() }, }); return(response); } catch (Exception ex) { return(new TelemetryInitializeResponse { Exception = new InvalidOperationException("Error while processing telemetry initialize request", ex) }); } }
public void Test_RegistrationFunc(ITelimena telimena, Func <TelemetryInitializeResponse> func, bool skipFlagExpectedValue) { try { TelemetryInitializeResponse result = func(); Assert.Fail("Exception expected"); } catch (Exception e) { TelimenaException ex = e as TelimenaException; Assert.AreEqual(1, ex.InnerExceptions.Count); StringAssert.Contains("An error occurred while posting to [api/v1/telemetry/initialize]", ex.InnerExceptions[0].Message); TelemetryInitializeRequest jObj = ex.RequestObjects[0].Value as TelemetryInitializeRequest; ((Telimena)telimena).Properties.StaticProgramInfo.ThrowIfPublicPropertiesNotEqual(jObj.ProgramInfo, true); } }
public async Task <TelemetryInitializeResponse> Initialize(TelemetryInitializeRequest request) { try { if (!ApiRequestsValidator.IsRequestValid(request)) { return(new TelemetryInitializeResponse() { Exception = new BadRequestException("Request is not valid") }); } TelemetryRootObject program = await work.GetMonitoredProgram(request.TelemetryKey).ConfigureAwait(false); if (program == null) { { return(new TelemetryInitializeResponse { Exception = new InvalidOperationException($"Program [{request.TelemetryKey}] is null") }); } } string ip = this.Request.GetClientIp(); ClientAppUser clientAppUser = await TelemetryControllerHelpers.GetUserOrAddIfMissing(this.work, request.UserInfo, ip).ConfigureAwait(false); await this.work.CompleteAsync().ConfigureAwait(false); TelemetryInitializeResponse response = new TelemetryInitializeResponse { UserId = clientAppUser.PublicId }; return(response); } catch (Exception ex) { return(new TelemetryInitializeResponse { Exception = new InvalidOperationException("Error while processing telemetry initialize request", ex) }); } }