public async Task <SimulationApiModel> PatchAsync( string id, [FromBody] SimulationPatchApiModel patch) { if (patch == null) { this.log.Warn("NULL patch provided", () => new { id }); throw new BadRequestException("No data or invalid data provided"); } SimulationPatch patchServiceModel = patch.ToServiceModel(id); if (patchServiceModel.Enabled == false) { patchServiceModel.Statistics = new SimulationStatistics { AverageMessagesPerSecond = this.rateReporter.GetThroughputForMessages(), TotalMessagesSent = this.simulationRunner.TotalMessagesCount }; } var simulation = await this.simulationsService.MergeAsync(patchServiceModel); return(SimulationApiModel.FromServiceModel( simulation, this.servicesConfig, this.deploymentConfig, this.connectionStringManager, this.simulationRunner, this.rateReporter)); }
public void ItCreatesSimulationWithValidInput() { // Arrange const string ID = "1"; var simulation = this.GetSimulationById(ID); this.simulationsService .Setup(x => x.InsertAsync(It.IsAny <Simulation>(), It.IsAny <string>())) .ReturnsAsync(simulation); // Act var fromServiceModelTask = SimulationApiModel.FromServiceModelAsync( simulation, this.servicesConfig.Object, this.deploymentConfig.Object, this.connectionStringManager.Object, this.simulationRunner.Object, this.rateReporter.Object); fromServiceModelTask.Wait(Constants.TEST_TIMEOUT); var postAsyncTask = this.target.PostAsync(fromServiceModelTask.Result); postAsyncTask.Wait(Constants.TEST_TIMEOUT); var result = postAsyncTask.Result; // Assert Assert.NotNull(result); Assert.Equal(simulation.Id, result.Id); }
public async Task <SimulationApiModel> PostAsync( [FromBody] SimulationApiModel simulationApiModel, [FromQuery(Name = "template")] string template = "") { if (simulationApiModel != null) { await simulationApiModel.ValidateInputRequestAsync(this.log, this.connectionStringManager); } else { if (string.IsNullOrEmpty(template)) { this.log.Warn("No data or invalid data provided", () => new { simulationApiModel, template }); throw new BadRequestException("No data or invalid data provided."); } // Simulation can be created with `template=default` other than created with input data simulationApiModel = new SimulationApiModel(); } var simulation = await this.simulationsService.InsertAsync(simulationApiModel.ToServiceModel(null), template); return(SimulationApiModel.FromServiceModel( simulation, this.servicesConfig, this.deploymentConfig, this.connectionStringManager, this.simulationRunner, this.rateReporter)); }
public void ItUpdatesSimulationThroughPutMethod() { // Arrange const string DEFAULT_SIMULATION_ID = "1"; var simulation = this.GetSimulationById(DEFAULT_SIMULATION_ID); this.simulationsService .Setup(x => x.UpsertAsync(It.IsAny <Simulation>())) .ReturnsAsync(simulation); // Act var fromServiceModelTask = SimulationApiModel.FromServiceModelAsync( simulation, this.servicesConfig.Object, this.deploymentConfig.Object, this.connectionStringManager.Object, this.simulationRunner.Object, this.rateReporter.Object ); fromServiceModelTask.Wait(Constants.TEST_TIMEOUT); var result = this.target.PutAsync( fromServiceModelTask.Result, DEFAULT_SIMULATION_ID ).Result; // Assert Assert.Equal(DEFAULT_SIMULATION_ID, result.Id); }
public void ItUpdatesSimulationThroughPutMethod() { // Arrange const string DEFAULT_SIMULATION_ID = "1"; var simulation = this.GetSimulationById(DEFAULT_SIMULATION_ID); this.simulationsService .Setup(x => x.UpsertAsync(It.IsAny <Simulation>(), true)) .ReturnsAsync(simulation); // Act var simulationApiModel = SimulationApiModel.FromServiceModel( simulation); var result = this.target.PutAsync( simulationApiModel, DEFAULT_SIMULATION_ID ).Result; // Assert Assert.Equal(DEFAULT_SIMULATION_ID, result.Id); // Assert - The simulation is created validating the connection string this.simulationsService.Verify( x => x.UpsertAsync(It.IsAny <Simulation>(), true), Times.Once); }
public async Task <SimulationApiModel> GetAsync(string id) { var simulation = await this.simulationsService.GetAsync(id); var simulationApiModel = SimulationApiModel.FromServiceModel( simulation, this.servicesConfig, this.deploymentConfig, this.connectionStringManager, this.simulationRunner, this.rateReporter); return(simulationApiModel); }
public void ItThrowsExceptionWhenCreateASimulationWithInValidInput() { // Arrange var simulation = new Simulation(); // Act & Assert Assert.ThrowsAsync <BadRequestException>( async() => await this.target.PostAsync( SimulationApiModel.FromServiceModel(simulation) ) ).CompleteOrTimeout(); }
public void ItReturnsSimulationApiModelFromServiceModel() { // Arrange var simulation = this.GetSimulationModel(); // Act var result = SimulationApiModel.FromServiceModel(simulation); // Assert Assert.IsType <SimulationApiModel>(result); Assert.Equal(simulation.Id, result.Id); }
public async Task <SimulationApiModel> PutAsync( [FromBody] SimulationApiModel simulation, string id = "") { if (simulation == null) { this.log.Warn("No data or invalid data provided", () => new { simulation }); throw new BadRequestException("No data or invalid data provided."); } return(new SimulationApiModel( await this.simulationsService.UpsertAsync(simulation.ToServiceModel(id)))); }
public async Task <SimulationApiModel> PatchAsync( string id, [FromBody] SimulationPatchApiModel patch) { if (patch == null) { this.log.Warn("NULL patch provided", () => new { id }); throw new BadRequestException("No data or invalid data provided"); } return(SimulationApiModel.FromServiceModel( await this.simulationsService.MergeAsync(patch.ToServiceModel(id)))); }
public async Task <SimulationApiModel> PutAsync( [FromBody] SimulationApiModel simulation, string id = "") { simulation?.ValidateInputRequest(this.log, this.connectionStringManager); if (simulation == null) { this.log.Warn("No data or invalid data provided", () => new { simulation }); throw new BadRequestException("No data or invalid data provided."); } return(SimulationApiModel.FromServiceModel( await this.simulationsService.UpsertAsync(simulation.ToServiceModel(id)))); }
public async Task <SimulationApiModel> PostAsync( [FromBody] SimulationApiModel simulation, [FromQuery(Name = "template")] string template = "") { if (simulation == null) { if (string.IsNullOrEmpty(template)) { this.log.Warn("No data or invalid data provided", () => new { simulation, template }); throw new BadRequestException("No data or invalid data provided."); } simulation = new SimulationApiModel(); } return(new SimulationApiModel( await this.simulationsService.InsertAsync(simulation.ToServiceModel(), template))); }
public async Task <SimulationApiModel> PutAsync([FromBody] SimulationApiModel simulationApiModel, string id = "") { if (simulationApiModel == null) { this.log.Warn("No data provided, request object is null"); throw new BadRequestException("No data provided, request object is empty."); } await simulationApiModel.ValidateInputRequestAsync(this.log, this.connectionStringValidation); // Load the existing resource, so that internal properties can be copied var existingSimulation = await this.GetExistingSimulationAsync(id); var simulation = await this.simulationsService.UpsertAsync(simulationApiModel.ToServiceModel(existingSimulation, this.defaultRatingConfig, id)); return(SimulationApiModel.FromServiceModel(simulation)); }
public void ItCreatesSimulationWithValidInput() { // Arrange const string ID = "1"; var simulation = this.GetSimulationById(ID); this.simulationsService .Setup(x => x.InsertAsync(It.IsAny <Simulation>(), It.IsAny <string>())) .ReturnsAsync(simulation); // Act var simulationApiModel = SimulationApiModel.FromServiceModel(simulation); var result = this.target.PostAsync(simulationApiModel).CompleteOrTimeout().Result; // Assert Assert.NotNull(result); Assert.Equal(simulation.Id, result.Id); }
public void ItThrowsExceptionWhenCreateASimulationWithInValidInput() { // Arrange var simulation = new Simulation(); // Act & Assert Assert.ThrowsAsync <BadRequestException>( async() => await this.target.PostAsync( await SimulationApiModel.FromServiceModelAsync( simulation, this.servicesConfig.Object, this.deploymentConfig.Object, this.connectionStringManager.Object, this.simulationRunner.Object, this.rateReporter.Object ) ) ) .Wait(Constants.TEST_TIMEOUT); }
public async Task <SimulationApiModel> PostAsync( [FromBody] SimulationApiModel simulation, [FromQuery(Name = "template")] string template = "") { simulation?.ValidateInputRequest(this.log, this.connectionStringManager); if (simulation == null) { if (string.IsNullOrEmpty(template)) { this.log.Warn("No data or invalid data provided", () => new { simulation, template }); throw new BadRequestException("No data or invalid data provided."); } // Simulation can be created with `template=default` other than created with input data simulation = new SimulationApiModel(); } return(SimulationApiModel.FromServiceModel( await this.simulationsService.InsertAsync(simulation.ToServiceModel(), template))); }
private SimulationApiModel GetSimulationApiModel() { var simulationApiModel = new SimulationApiModel() { Id = "id", ETag = "etag", StartTime = DateTimeOffset.UtcNow.ToString(), EndTime = DateTimeOffset.UtcNow.AddHours(1).ToString(), Enabled = false, DeviceModels = new List <SimulationDeviceModelRef>() { new SimulationDeviceModelRef() { Id = "device_id", Count = 1 } }, IotHub = new SimulationIotHub("HostName=[hubname];SharedAccessKeyName=[iothubowner];SharedAccessKey=[valid key]") }; return(simulationApiModel); }
private async Task SeedSingleTemplateAsync(string content) { Template template; try { template = JsonConvert.DeserializeObject <Template>(content); } catch (Exception ex) { throw new InvalidInputException("Failed to parse template", ex); } if (template.Groups.Select(g => g.Id).Distinct().Count() != template.Groups.Count()) { this.log.Warn("Found duplicated group ID", () => new { template.Groups }); } if (template.Rules.Select(r => r.Id).Distinct().Count() != template.Rules.Count()) { this.log.Warn("Found duplicated rule ID", () => new { template.Rules }); } var groupIds = new HashSet <string>(template.Groups.Select(g => g.Id)); var rulesWithInvalidGroupId = template.Rules.Where(r => !groupIds.Contains(r.GroupId)); if (rulesWithInvalidGroupId.Any()) { this.log.Warn("Invalid group ID found in rules", () => new { rulesWithInvalidGroupId }); } foreach (var group in template.Groups) { try { await this.storage.UpdateDeviceGroupAsync(group.Id, group, "*"); } catch (Exception ex) { this.log.Error($"Failed to seed default group {group.DisplayName}", () => new { group, ex.Message }); } } foreach (var rule in template.Rules) { try { await this.telemetryClient.UpdateRuleAsync(rule, "*"); } catch (Exception ex) { this.log.Error($"Failed to seed default rule {rule.Description}", () => new { rule, ex.Message }); } } try { var simulationModel = await this.simulationClient.GetSimulationAsync(); if (simulationModel != null) { this.log.Info("Skip seed simulation since there is already one simuation", () => new { simulationModel }); } else { simulationModel = new SimulationApiModel { Id = "1", Etag = "*" }; simulationModel.DeviceModels = template.DeviceModels.ToList(); await this.simulationClient.UpdateSimulation(simulationModel); } } catch (Exception ex) { this.log.Error("Failed to seed default simulation", () => new { ex.Message }); } }
public async Task <SimulationApiModel> GetAsync(string id) { return(SimulationApiModel.FromServiceModel(await this.simulationsService.GetAsync(id))); }
public async Task <SimulationApiModel> GetAsync(string id) { var simulation = await this.simulationsService.GetWithStatisticsAsync(id); return(SimulationApiModel.FromServiceModel(simulation)); }