public async Task TestSensorPollingService() { // Create a client, which in turn creates the host with all hosted services // and the digitalstrom polling services will start running _factory.CreateClient(); // Init the mock for the database var db = await _factory.InitDb(); // We should see that the database is empty at first, with no recorded data var sensorDataSet = db.DsSensorDataSet.Where(x => x.ZoneId == DigitalstromDssMockExtensions.ZoneIdKitchen).FirstOrDefault(); Assert.Null(sensorDataSet?.TemperatureCurve); Assert.Null(sensorDataSet?.HumidityCurve); // After 100ms (see configured TimerInterval, plus buffer) the polling hosted service // should have written all values to the database that we have mocked with the test setup for (int i = 0; i < 100; i++) { sensorDataSet = db.DsSensorDataSet.Where(x => x.ZoneId == DigitalstromDssMockExtensions.ZoneIdKitchen).FirstOrDefault(); await Task.Delay(100); if (sensorDataSet?.TemperatureCurve != null && sensorDataSet?.HumidityCurve != null) { break; } } Assert.NotNull(sensorDataSet); Assert.NotNull(sensorDataSet?.TemperatureCurve); Assert.NotNull(sensorDataSet?.HumidityCurve); // for both temperature und humidity values that we are mocking (see DigitalstromDssMockExtensions.AddSensorMocks) Assert.NotEmpty(sensorDataSet?.TemperatureSeries.Trimmed()); Assert.NotEmpty(sensorDataSet?.HumiditySeries.Trimmed()); }
public async Task TestEnergyPollingService() { // Create a client, which in turn creates the host with all hosted services // and the digitalstrom polling services will start running _factory.CreateClient(); // Init the mock for the database var db = await _factory.InitDb(); // We should see that the database is empty at first, with no recorded data Assert.Null(db.DsEnergyHighresDataSet.FirstOrDefault()?.EnergyCurvesEveryMeter); // After 100ms (see configured TimerInterval, plus buffer) the polling hosted service // should have written all values to the database that we have mocked with the test setup for (int i = 0; i < 100; i++) { await Task.Delay(100); if (db.DsEnergyHighresDataSet.FirstOrDefault()?.EnergyCurvesEveryMeter != null) { break; } } Assert.NotNull(db.DsEnergyHighresDataSet.FirstOrDefault()?.EnergyCurvesEveryMeter); // We should have at least one polled dataset of 600 values (i.e. 10 min in a 1 sec interval) // for both metering circuits that we are mocking (see DigitalstromDssMockExtensions.AddEnergyMeteringMocks) var storedValues = db.DsEnergyHighresDataSet.FirstOrDefault()?.EnergySeriesEveryMeter; Assert.Equal(2, storedValues.Count()); Assert.True(600 <= storedValues.First().Value.Trimmed().Count); Assert.True(600 <= storedValues.Last().Value.Trimmed().Count); }
public async Task TestDsSceneEventProcessorHostedService() { // Create a client, which in turn creates the host with all hosted services // and the digitalstrom event processor service will start running _factory.CreateClient(); // Init the mock for http requests to the digitalstrom server and mock for the database var mockHttp = _factory.Server.Host.Services.GetRequiredService <MockHttpMessageHandler>(); var db = await _factory.InitDb(); // We should see that the database is empty at first, with no recorded digitalstrom events Assert.Null(db.DsSceneEventDataSet.FirstOrDefault()?.EventStreamEncoded); // After 100ms (see configured ItemCollectionInterval, plus buffer) the event processor hosted service // should have written all ds events to the database that we have mocked with the test setup for (int i = 0; i < 100; i++) { await Task.Delay(100); if (db.DsSceneEventDataSet.FirstOrDefault()?.EventStreamEncoded != null) { break; } } Assert.NotNull(db.DsSceneEventDataSet.FirstOrDefault()?.EventStreamEncoded); var storedEvents = db.DsSceneEventDataSet.FirstOrDefault()?.EventStream; Assert.True(storedEvents.Count() > 1); Assert.Equal(32027, (int)storedEvents.Last().Properties.ZoneID); Assert.Equal((int)SceneCommand.Preset0, (int)storedEvents.First().Properties.SceneID); Assert.Equal((int)SystemEvent.CallScene, (int)storedEvents.First().SystemEvent.Type); // Turn off event sending mockHttp.AutoFlush = false; mockHttp.Flush(); // Change the returned event content and reset the database _factory.MockedEventResponse.Respond("application/json", SceneCommand.Alarm1.ToMockedSceneEvent()); await _factory.InitDb(); await Task.Delay(200); mockHttp.Flush(); await Task.Delay(200); storedEvents = db.DsSceneEventDataSet.FirstOrDefault()?.EventStream; Assert.NotNull(storedEvents); Assert.Equal((int)SceneCommand.Preset0, (int)storedEvents.Last().Properties.SceneID); }
public async Task TestDsSceneEventProcessorHostedService() { // Create a client, which in turn creates the host with all hosted services // and the digitalstrom event processor service will start running _factory.CreateClient(); // Init the mock for http requests to the digitalstrom server and mock for the database var mockHttp = _factory.Server.Host.Services.GetRequiredService <MockHttpMessageHandler>(); var db = await _factory.InitDb(); Assert.NotNull(mockHttp); // We should see that the database is empty at first, with no recorded digitalstrom events Assert.Null(db.DsSceneEventDataSet.FirstOrDefault()?.EventStreamEncoded); async Task <IEnumerable <DssEvent> > DelayUntilDataWritten(IntegrationTestDbContext db, int expected) { for (int i = 0; i < 200; i++) { await Task.Delay(100); if (mockHttp is null) { continue; } mockHttp.Flush(); if (db.DsSceneEventDataSet.AsNoTracking().FirstOrDefault()?.EventStreamEncoded is null) { continue; } var expectedItems = db.DsSceneEventDataSet.AsNoTracking().FirstOrDefault()?.EventStream .Where(x => x.Properties.SceneID == expected).ToList(); if (expectedItems is not null && expectedItems.Count > 0) { return(expectedItems); } } return(new List <DssEvent>()); } // After 100ms (see configured ItemCollectionInterval, plus buffer) the event processor hosted service // should have written all ds events to the database that we have mocked with the test setup var storedEvents1 = await DelayUntilDataWritten(db, (int)SceneCommand.Preset0); Assert.NotNull(db.DsSceneEventDataSet.AsNoTracking().FirstOrDefault()?.EventStreamEncoded); Assert.NotNull(storedEvents1); if (storedEvents1 is null) { return; } Assert.NotEmpty(storedEvents1); Assert.Equal(32027, (int)storedEvents1.Last().Properties.ZoneID); Assert.Equal((int)SceneCommand.Preset0, (int)storedEvents1.First().Properties.SceneID); Assert.Equal((int)SystemEvent.CallScene, (int)storedEvents1.First().SystemEvent.Type); // Turn off event sending mockHttp.AutoFlush = false; mockHttp.Flush(); // Change the returned event content and reset the database db.DsSceneEventDataSet.RemoveRange(db.DsSceneEventDataSet); await db.SaveChangesAsync(); await _factory.InitDb(); await Task.Delay(100); _factory.MockedEventResponse.Respond("application/json", SceneCommand.Alarm1.ToMockedSceneEvent()); var storedEvents2 = await DelayUntilDataWritten(db, (int)SceneCommand.Alarm1); Assert.NotNull(storedEvents2); if (storedEvents2 is null) { return; } Assert.NotEmpty(storedEvents2); Assert.NotEmpty(storedEvents2.Where(x => x.Properties.SceneID == (int)SceneCommand.Alarm1)); }