public void ValidateSensorLogRecordsInvalidInputTest(SensorLogModel sensorLogModel, string expected) { // Arrange // Act Action actual = () => _validateSensorRecordRepository.ValidateSensorLogRecords(sensorLogModel); // Assert using (new AssertionScope()) { actual .Should() .Throw <ArgumentException>() .WithMessage(expected); _logger.Invocations.Count .Should() .Be(1); _logger.Invocations[0].Arguments[0] .Should() .Be(LogLevel.Error); _logger .Verify(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.Is <It.IsAnyType>((x, t) => string.Equals(x.ToString(), expected)), It.IsAny <Exception>(), It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true)), Times.Once()); } }
public async Task Hanlde_Should_Return_Valid_Result_Test() { // Arrange string path = @"C:\temp\file.txt"; SensorLogModel expectedModel = new SensorLogModel(); CommandResult <SensorLogModel> expectedResult = new CommandResult <SensorLogModel> { Result = expectedModel, CommandResultType = CommandResultType.Success }; ParseSensorRecordCommand command = new ParseSensorRecordCommand { Path = path }; _fileSystem .Setup(f => f.File.Exists(It.IsAny <string>())) .Returns(true); _repository .Setup(r => r.ParseInputLogFileAsync(It.IsAny <string>())) .ReturnsAsync(expectedModel); // Act CommandResult <SensorLogModel> actual = await _handler.Handle(command); // Assert actual .Should() .NotBeNull() .And .BeEquivalentTo(expectedResult); }
/// <summary> /// Parses an input file and returns the resulting sensor log model /// </summary> /// <param name="path">The path to the sensor log record to parse</param> /// <returns>A task representing the resulting sensor log model</returns> public async Task <SensorLogModel> ParseInputLogFileAsync(string path) { string input = await _fileSystem.File.ReadAllTextAsync(path); SensorLogModel model = ParseInputLogFile(input); return(model); }
public void ValidateSensorLogRecordsTest(SensorLogModel sensorLogModel, ValidateSensorLogModel expected) { // Arrange // Act ValidateSensorLogModel actual = _validateSensorRecordRepository.ValidateSensorLogRecords(sensorLogModel); // Assert actual .Should() .NotBeNull() .And .Equals(expected); }
public async Task ParseInputLogFileTest(string input, SensorLogModel expected) { // Arrange string path = @"C:\temp\file.txt"; _fileSystem .Setup(fs => fs.File.ReadAllTextAsync(It.IsAny <string>(), default)) .ReturnsAsync(input); // Act SensorLogModel actual = await _parseSensorRecordRepository.ParseInputLogFileAsync(path); // Assert actual .Should() .NotBeNull() .And .Equals(expected); }
/// <summary> /// Reads the sensors and the log records and generates the quality control evaluation for the sensors /// </summary> /// <param name="sensorLogModel">The sensor log input model</param> /// <exception cref="ArgumentNullException">sensorLogModel is null or sensorLogModel.ReverenceValues is null</exception> /// <returns>A model representing the sensor log quality control evaluation</returns> public ValidateSensorLogModel ValidateSensorLogRecords(SensorLogModel sensorLogModel) { if (sensorLogModel == null) { _logger.LogError($"{Messages.NullValue} (Parameter '{nameof(sensorLogModel)}')"); throw new ArgumentNullException(nameof(sensorLogModel), Messages.NullValue); } if (sensorLogModel.ReferenceValues == null) { _logger.LogError($"{Messages.NullValue} (Parameter '{nameof(sensorLogModel.ReferenceValues)}')"); throw new ArgumentNullException(nameof(sensorLogModel.ReferenceValues), Messages.NullValue); } ValidateSensorLogModel resultModel = new ValidateSensorLogModel { HumidityResults = EvaluateHumidityLogRecords(sensorLogModel.HumidityReadings, sensorLogModel.ReferenceValues.HumidityReferenceValue), MonoxideResults = EvaluateMonoxideLogRecords(sensorLogModel.MonoxideReadings, sensorLogModel.ReferenceValues.MonoxideReferenceValue), ThermometerResults = EvaluateThermometerLogRecords(sensorLogModel.ThermometerReadings, sensorLogModel.ReferenceValues.ThermometerReferenceValue) }; return(resultModel); }
/// <summary> /// Asynchronously sends command to handler /// </summary> /// <param name="sensorLogModel">The sensor log model to validate</param> /// <returns>The result of the command</returns> private Task <CommandResult <ValidateSensorLogModel> > SendValidateSensorRecordCommandAsync(SensorLogModel sensorLogModel) { ValidateSensorRecordCommand command = new ValidateSensorRecordCommand { SensorLogModel = sensorLogModel }; return(_mediator.Send(command));; }
/// <summary> /// Generates a sensor log model from the sensor log input text file /// </summary> /// <param name="logContentsStr">The sensor log input text file</param> /// <exception cref="ArgumentException">logContentStr is null or whitespace</exception> /// <exception cref="NotImplementedException">Invalid record type is entered</exception> /// <returns>The sensor log model representing the sensors and reading log records</returns> private SensorLogModel ParseInputLogFile(string logContentsStr) { if (string.IsNullOrWhiteSpace(logContentsStr)) { _logger.LogError($"{Messages.NullWhitespaceString} (Parameter '{nameof(logContentsStr)}')"); throw new ArgumentException(Messages.NullWhitespaceString, nameof(logContentsStr)); } SensorLogModel model = new SensorLogModel(); string[] input = logContentsStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); Regex timeValueRegex = new Regex("[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T(2[0-3]|[01][0-9]):[0-5][0-9]"); for (int i = 0; i < input.Length; i++) { string[] line = input[i].Split(' '); switch (line[0]) { case "reference": model.ReferenceValues = HandleReferenceValuesLogRecord(line[1], line[2], line[3]); break; case "thermometer": ThermometerModel thermometerModel = new ThermometerModel { Name = line[1] }; while (true) { i++; line = input[i].Split(' '); if (timeValueRegex.IsMatch(line[0])) { DecimalReadingModel readingModel = HandleDecimalReading(line[0], line[1]); thermometerModel.Readings.Add(readingModel); } else { i--; break; } if (i + 1 >= input.Length) { break; } } model.ThermometerReadings.Add(thermometerModel); break; case "humidity": HumidityModel humidityModel = new HumidityModel { Name = line[1] }; while (true) { i++; line = input[i].Split(' '); if (timeValueRegex.IsMatch(line[0])) { DecimalReadingModel readingModel = HandleDecimalReading(line[0], line[1]); humidityModel.Readings.Add(readingModel); } else { i--; break; } if (i + 1 >= input.Length) { break; } } model.HumidityReadings.Add(humidityModel); break; case "monoxide": MonoxideModel monoxideModel = new MonoxideModel { Name = line[1] }; while (true) { i++; line = input[i].Split(' '); if (timeValueRegex.IsMatch(line[0])) { IntegerReadingModel readingModel = HandleIntegerReading(line[0], line[1]); monoxideModel.Readings.Add(readingModel); } else { i--; break; } if (i + 1 >= input.Length) { break; } } model.MonoxideReadings.Add(monoxideModel); break; default: _logger.LogError($"Sensor type {line[0]} has not been implemented yet."); throw new NotImplementedException($"Sensor type {line[0]} has not been implemented yet."); } } return(model); }