public virtual void UpdateSensorTypes(IDictionary <string, SensorReadInData> availableSensorsFromAPI) { // guard IEnumerable <string> notRegistered = availableSensorsFromAPI.Keys.Except(this.dbContext.SensorTypes.Select(x => x.SensorIdICB)); if (notRegistered == null || notRegistered.Count() == 0) { return; } else { foreach (string sensorId in notRegistered) { SensorReadInData newSensorType = availableSensorsFromAPI[sensorId]; this.dbContext.SensorTypes.Add(new SensorType() { SensorIdICB = newSensorType.SensorId, MeasureType = newSensorType.MeasureType, MinPollingIntervalInSeconds = newSensorType.MinPollingIntervalInSeconds, Tag = newSensorType.Tag, IsNumericValue = newSensorType.Description.Split().Contains("between") }); } } this.dbContext.SaveChanges(); }
public async Task UpdateSensorTypesInSensorDbService() { // Arrange // arrange controller Mock <ApplicationDbContext> mockApplicationDbContext = new Mock <ApplicationDbContext>(); Mock <SensorDbService> mockSensorDbService = new Mock <SensorDbService>(mockApplicationDbContext.Object); Mock <HttpClient> mockHttpClient = new Mock <HttpClient>(); Mock <SensorApiService> mockSensorApiService = new Mock <SensorApiService>(mockHttpClient.Object); // arrange availableSensorsFromAPI SensorReadInData sensorReadInData1 = new SensorReadInData { SensorId = "SensorId", Tag = "Tag", Description = "Description", MinPollingIntervalInSeconds = 3453, MeasureType = "MeasureType" }; SensorReadInData sensorReadInData2 = new SensorReadInData { SensorId = "SensorIdSensorId", Tag = "TagTag", Description = "DescriptionDescription", MinPollingIntervalInSeconds = 3453, MeasureType = "MeasureTypeMeasureType" }; IDictionary <string, SensorReadInData> expected = new Dictionary <string, SensorReadInData>(); expected.Add(sensorReadInData1.SensorId, sensorReadInData1); expected.Add(sensorReadInData2.SensorId, sensorReadInData2); // arrange sensorApiService mockSensorApiService.Setup(x => x.ListSensorsFromAPI()) .Returns(Task.Run(() => expected)); // arrange actual controller SensorController sensorController = new SensorController(mockSensorDbService.Object, mockSensorApiService.Object); // act and assert await sensorController.RegisterNewSensor(); mockSensorDbService.Verify(x => x.UpdateSensorTypes(expected), Times.Once()); }
public void ReturnDefaultViewWithCorrectViewModel() { // Arrange // arrange controller Mock <ApplicationDbContext> mockApplicationDbContext = new Mock <ApplicationDbContext>(); Mock <SensorDbService> mockSensorDbService = new Mock <SensorDbService>(mockApplicationDbContext.Object); Mock <HttpClient> mockHttpClient = new Mock <HttpClient>(); Mock <SensorApiService> mockSensorApiService = new Mock <SensorApiService>(mockHttpClient.Object); // arrange availableSensorsFromAPI SensorReadInData sensorReadInData1 = new SensorReadInData(); SensorReadInData sensorReadInData2 = new SensorReadInData(); IDictionary <string, SensorReadInData> expected = new Dictionary <string, SensorReadInData>(); expected.Add("key1", sensorReadInData1); expected.Add("key2", sensorReadInData2); // arrange sensorApiService mockSensorApiService.Setup(x => x.ListSensorsFromAPI()) .Returns(Task.Run(() => expected)); // arrange viewModel RegisterNewSensorViewModel registerNewSensorViewModel = new RegisterNewSensorViewModel { AvailableSensors = expected.Values }; // arrange actual controller SensorController sensorController = new SensorController(mockSensorDbService.Object, mockSensorApiService.Object); // act and assert sensorController.WithCallTo(c => c.RegisterNewSensor()) .ShouldRenderDefaultView() .WithModel <RegisterNewSensorViewModel> (actual => { Assert.IsNotNull(actual.AvailableSensors); CollectionAssert.AreEquivalent(registerNewSensorViewModel.AvailableSensors.ToList(), actual.AvailableSensors.ToList()); }); }
private async Task SeedStamatSensors() { IDictionary <string, SensorReadInData> availableSensors = await this.sensorApiService.ListSensorsFromAPI(); this.sensorDbService.UpdateSensorTypes(availableSensors); IList <string> supportedSensorTypes = availableSensors.Keys.ToList(); string user = "******"; bool inverse = true; for (int i = 0; i < 10; i++) { inverse = !inverse; SensorReadInData coreSensorInfo = availableSensors[supportedSensorTypes[this.random.Next(0, supportedSensorTypes.Count - 1)]]; string descriptionByICB = coreSensorInfo.Description; string[] subDescription = descriptionByICB.Split(); //"This sensor will return values between 6 and 18" or "This sensor will return true or false value" double minValue = 0; double maxValue = 0; if (subDescription.Contains("between")) { try //try with int { int minValueI = int.Parse(subDescription[subDescription.Length - 3]); int randomMinValue = this.random.Next(minValueI, 3 * minValueI); minValue = randomMinValue; int maxValueI = int.Parse(subDescription[subDescription.Length - 1]); int randomMaxValue = this.random.Next(maxValueI, 3 * maxValueI); maxValue = randomMaxValue; } catch { try //try with double { double minValueD = Math.Truncate(double.Parse(subDescription[subDescription.Length - 3])); int randomMinValue = this.random.Next((int)minValueD, 3 * (int)minValueD); minValue = randomMinValue; double maxValueD = Math.Truncate(double.Parse(subDescription[subDescription.Length - 1])); int randomMaxValue = this.random.Next((int)maxValueD, 3 * (int)maxValueD); maxValue = randomMaxValue; } catch //ebago { } } } SensorType sensorType = new SensorType { SensorIdICB = coreSensorInfo.SensorId, Tag = coreSensorInfo.Tag, MinPollingIntervalInSeconds = coreSensorInfo.MinPollingIntervalInSeconds, MeasureType = coreSensorInfo.MeasureType }; int pollingInterval = this.random.Next(coreSensorInfo.MinPollingIntervalInSeconds, coreSensorInfo.MinPollingIntervalInSeconds * 3); Sensor newSensor = new Sensor { Name = string.Format("{0}`s {1} sensor.", user, coreSensorInfo.Tag), DescriptionGivenByTheUser = coreSensorInfo.Description, MeasurementType = coreSensorInfo.MeasureType, IsPublic = inverse, OperatingRange = coreSensorInfo.Description, MinValue = minValue, MaxValue = maxValue, LastValue = new LastValue { SensorIdICB = coreSensorInfo.SensorId, PollingInterval = pollingInterval } }; this.sensorDbService.RegisterNewSensor(user, newSensor); } }
public async Task ReturnCollectionOfSensorTypes_WhenInvoked() { // Arrange - - - - - - - - - - - //Generate mock JSON SensorReadInData mockSensor1 = new SensorReadInData { Description = "Test Description", MeasureType = "Test MeasureType", MinPollingIntervalInSeconds = 7, SensorId = "Test SensorId", Tag = "Test Tag" }; SensorReadInData mockSensor2 = new SensorReadInData { Description = "Description", MeasureType = "MeasureType", MinPollingIntervalInSeconds = 77, SensorId = "SensorId", Tag = "Tag" }; IEnumerable <SensorReadInData> sensors = new List <SensorReadInData>() { mockSensor1, mockSensor2 }; MemoryStream ms = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(IEnumerable <SensorReadInData>)); ser.WriteObject(ms, sensors); byte[] json = ms.ToArray(); ms.Close(); string expectedJSON = Encoding.UTF8.GetString(json, 0, json.Length); //mock JSON done //Generate mock HttpClient MockHttpMessageHandler mockHttp = new MockHttpMessageHandler(); string url = "http://telerikacademy.icb.bg/api/sensor/all"; mockHttp.When(url).Respond("application/json", expectedJSON); HttpClient httpClient = new HttpClient(mockHttp); //injected mock HttpClient ISensorApiService sensorApiService = new SensorApiService(httpClient); List <string> expectedResult = new List <string>(); expectedResult.Add(mockSensor1.SensorId); expectedResult.Add(mockSensor2.SensorId); // Act - - - - - - - - - - - IDictionary <string, SensorReadInData> actualSensors = await sensorApiService.ListSensorsFromAPI(); List <string> actualResult = actualSensors.Keys.ToList(); // Assert - - - - - - - - - - - Assert.IsNotNull(actualSensors); Assert.AreEqual(expectedResult.Count, actualResult.Count); CollectionAssert.AreEquivalent(expectedResult, actualResult); for (int i = 0; i <= expectedResult.Count - 1; i++) { Assert.AreEqual(expectedResult[i], actualResult[i]); } }
public async Task <ActionResult> RegisterNewSensor(RegisterNewSensorViewModel wrappedRegisterNewSensorModel) { Sensor newSensor = wrappedRegisterNewSensorModel.Sensor; //validation if (!this.ModelState.IsValid) { return(this.RedirectToAction("InvalidInput", new { message = "Invalid model state." })); } if (newSensor.LastValue.SensorIdICB == null) { return(this.RedirectToAction("InvalidInput", new { message = "Sensor ID cannot be empty." })); } if (newSensor.Name == null) { return(this.RedirectToAction("InvalidInput", new { message = "Name cannot be empty." })); } try { SensorType serverSensorType = this.sensorDbService .GetSpecificSensorType(newSensor.LastValue.SensorIdICB); if (newSensor.LastValue.PollingInterval < serverSensorType.MinPollingIntervalInSeconds) { return(this.RedirectToAction("InvalidInput", new { message = "Invalid minimal polling interval." })); } //emergencyPatch. To Fix later - - - - - //Fix here: In SensorTypes database model must be added OperationalRange property, from where to cross check the input min/max values IDictionary <string, SensorReadInData> emergencySensorsFromAPI = await this.sensorApiService.ListSensorsFromAPI(); SensorReadInData serverSensor = emergencySensorsFromAPI[newSensor.LastValue.SensorIdICB]; string operatingRange = serverSensor.Description; string[] substring = operatingRange.Split(); //"This sensor will return values between 6 and 18" or "This sensor will return true or false value" if (substring.Contains("between")) { //case sensor is integer type double serverMinVal = double.Parse(substring[substring.Length - 3]); double serverMaxVal = double.Parse(substring[substring.Length - 1]); if (newSensor.MinValue >= serverMinVal && newSensor.MinValue <= serverMaxVal) { } else { return(this.RedirectToAction("InvalidInput", new { message = "Invalid sensor minimal value." })); } if (newSensor.MaxValue <= serverMaxVal && newSensor.MaxValue >= serverMinVal) { } else { return(this.RedirectToAction("InvalidInput", new { message = "Invalid sensor maximum value." })); } } else { //case sensor is boolean type if (newSensor.MinValue != 0 || newSensor.MaxValue != 0) { return(this.RedirectToAction("InvalidInput", new { message = "Invalid sensor min / maximum value when the sensor is boolean type." })); } } newSensor.MeasurementType = serverSensor.MeasureType; //newSensor.SensorType } catch { //this is if the user tries even entering invalid sensorIdICB just to brake the application return(this.RedirectToAction("InvalidInput", new { message = "Invalid sensor ID." })); } this.sensorDbService .RegisterNewSensor(GetLoggedUsername(), newSensor); return(this.View("Success")); }