public async Task Handler_return_true_if_reading_is_for_portable_sensor() { //Arrange var apiKey = CryptoHelper.GenerateApiKey(); var fakePortableSensor = new PortableSensor() { Id = 1, ApiKey = apiKey }; var fakeReading = new SensorReadingDTO(); _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(new List <Sensor> { fakePortableSensor }); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); //Act var cancellationToken = new CancellationToken(); var command = new CreateReadingCommand(fakeReading, apiKey); var handler = new CreateReadingCommandHandler(_mediatorMock.Object, _dataContextFactoryMock.Object, _mapperMock.Object); var result = await handler.Handle(command, cancellationToken); //Assert Assert.True(result); }
public async Task Handler_should_throw_exception_if_sensor_not_found() { //Arrange var apiKey = CryptoHelper.GenerateApiKey(); var notExistingApiKey = apiKey.Reverse().ToString(); var fakeStaticSensor = new StaticSensor { Id = 1, ApiKey = apiKey }; var fakeReading = new SensorReadingDTO(); _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(new List <Sensor> { fakeStaticSensor }); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); var cancellationToken = new CancellationToken(); var command = new CreateReadingCommand(fakeReading, notExistingApiKey); var handler = new CreateReadingCommandHandler(_mediatorMock.Object, _dataContextFactoryMock.Object, _mapperMock.Object); //Act Task Act() => handler.Handle(command, cancellationToken); //Assert await Assert.ThrowsAsync <SensorNotFoundException>(Act); }
public async Task Handler_publish_notification_if_reading_is_for_static_sensor() { //Arrange var apiKey = CryptoHelper.GenerateApiKey(); var fakeStaticSensor = new StaticSensor { Id = 1, ApiKey = apiKey }; var fakeStaticSensorReading = new StaticSensorReading(); var fakeReadingDTO = new SensorReadingDTO(); var fakeReadingsDbSet = new List <StaticSensorReading>(); _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(new List <Sensor> { fakeStaticSensor }); _dataContextMock.Setup(x => x.StaticSensorReadings).ReturnsDbSet(fakeReadingsDbSet); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); _mapperMock.Setup(x => x.Map <SensorReadingDTO, StaticSensorReading>(fakeReadingDTO)) .Returns(fakeStaticSensorReading); //Act var cancellationToken = new CancellationToken(); var command = new CreateReadingCommand(fakeReadingDTO, apiKey); var handler = new CreateReadingCommandHandler(_mediatorMock.Object, _dataContextFactoryMock.Object, _mapperMock.Object); var result = await handler.Handle(command, cancellationToken); //Assert _mediatorMock.Verify( x => x.Publish( It.Is <StaticSensorReadingCreatedNotification>(it => it.SensorId == fakeStaticSensor.Id && it.Reading == fakeStaticSensorReading), It.Is <CancellationToken>(it => it == cancellationToken)), Times.Once); }
public async Task Handler_return_true_if_reading_is_for_static_sensor() { //Arrange var apiKey = ApiKeyHelper.Generate(); var fakeStaticSensor = new StaticSensor { Id = 1, ApiKey = apiKey }; var fakeStaticSensorReading = new StaticSensorReading(); var fakeReadingDTO = new SensorReadingDTO(); var fakeReadingsDbSet = new List <StaticSensorReading>(); _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(new List <Sensor> { fakeStaticSensor }); _dataContextMock.Setup(x => x.StaticSensorReadings).ReturnsDbSet(fakeReadingsDbSet); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); _mapperMock.Setup(x => x.Map <SensorReadingDTO, StaticSensorReading>(fakeReadingDTO)) .Returns(fakeStaticSensorReading); //Act var cancellationToken = new CancellationToken(); var command = new CreateReadingCommand(fakeReadingDTO, apiKey); var handler = new CreateReadingCommandHandler(_mediatorMock.Object, _dataContextFactoryMock.Object, _mapperMock.Object); var result = await handler.Handle(command, cancellationToken); //Assert Assert.True(result); }
public SensorReadingDTO GetTowerSensorCurrentReading(string SensorId) { var con = new SqlConnection(connectionString); try { con.Open(); string strSql = "select TOP 1 CONVERT(DATETIME, CONVERT(NCHAR(10), [Date], 112) + ' ' + CONVERT(NCHAR(10), [Time], 108)) as ReadingDate , * from SWS200Table where SensorID='" + SensorId + "' ORDER BY ReadingDate DESC"; SqlDataAdapter dadapter = new SqlDataAdapter { SelectCommand = new SqlCommand(strSql, con) }; DataSet dset = new DataSet(); dadapter.Fill(dset); con.Close(); var table = dset.Tables[0]; SensorReadingDTO returnMsg = new SensorReadingDTO(); foreach (DataRow dataRow in table.Rows) { returnMsg = new SensorReadingDTO { Code = dataRow["Code"].ToString(), Date = dataRow["Date"].ToString(), Header = dataRow["Header"].ToString(), InstantMOR = dataRow["InstantMOR"].ToString(), MOR = dataRow["MOR"].ToString(), Period = dataRow["Period"].ToString(), Precipitation = dataRow["Precipitation"].ToString(), SelfTest = dataRow["SelfTest"].ToString(), SensorID = dataRow["SensorID"].ToString(), Temperature = dataRow["Temperature"].ToString(), Time = dataRow["Time"].ToString() }; } return(returnMsg); } catch (Exception e) { return(null); } finally { con.Close(); } }
public CreateReadingCommand(SensorReadingDTO reading, string apiKey) { Reading = reading; ApiKey = apiKey; }