Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

First steps in tests for ForecastSolarService. WIP

Michael Rademakerbf271660746e
diff --git a/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java b/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java
index 390439dd0..a80128456 100644
--- a/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java
+++ b/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java
@@ -28,10 +28,7 @@ import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -56,7 +53,7 @@ public class ForecastSolarService extends RouteBuilder implements ContainerServi
protected Result result;
}
- protected static class Result{
+ protected static class Result {
@JsonProperty
protected Map<String, Double> wattHours;
@JsonProperty
@@ -64,7 +61,7 @@ public class ForecastSolarService extends RouteBuilder implements ContainerServi
@JsonProperty
protected Map<String, Double> watts;
}
-
+
public static final String FORECAST_SOLAR_API_KEY = "FORECAST_SOLAR_API_KEY";
protected static final DateTimeFormatter ISO_LOCAL_DATE_TIME_WITHOUT_T = new DateTimeFormatterBuilder()
@@ -151,13 +148,11 @@ public class ForecastSolarService extends RouteBuilder implements ContainerServi
@Override
public void stop(Container container) throws Exception {
- calculationFutures.keySet().forEach(this::stopProcessing);
+ new ArrayList<>(calculationFutures.keySet()).forEach(this::stopProcessing);
}
protected void processAttributeEvent(AttributeEvent attributeEvent) {
- ScheduledFuture<?> calculationFuture = calculationFutures.get(attributeEvent.getAssetId());
-
- if (calculationFuture != null) {
+ if (attributeEvent.getAttributeName().equals(ElectricityProducerSolarAsset.INCLUDE_FORECAST_SOLAR_SERVICE.getName())) {
processElectricityProducerSolarAssetAttributeEvent(attributeEvent);
}
}
@@ -171,11 +166,11 @@ public class ForecastSolarService extends RouteBuilder implements ContainerServi
}
if (attributeEvent.getAttributeName().equals(ElectricityProducerSolarAsset.INCLUDE_FORECAST_SOLAR_SERVICE.getName())) {
- boolean disabled = attributeEvent.<Boolean>getValue().orElse(false);
- if (!disabled && calculationFutures.containsKey(attributeEvent.getAssetId())) {
+ boolean enabled = attributeEvent.<Boolean>getValue().orElse(false);
+ if (enabled && calculationFutures.containsKey(attributeEvent.getAssetId())) {
// Nothing to do here
return;
- } else if (disabled && !calculationFutures.containsKey(attributeEvent.getAssetId())) {
+ } else if (!enabled && !calculationFutures.containsKey(attributeEvent.getAssetId())) {
// Nothing to do here
return;
}
diff --git a/test/src/main/java/org/openremote/test/setup/ManagerTestSetup.java b/test/src/main/java/org/openremote/test/setup/ManagerTestSetup.java
index edcfdcd58..b57878610 100644
--- a/test/src/main/java/org/openremote/test/setup/ManagerTestSetup.java
+++ b/test/src/main/java/org/openremote/test/setup/ManagerTestSetup.java
@@ -85,6 +85,7 @@ public class ManagerTestSetup extends ManagerSetup {
public String masterRealm;
public String realmBuildingTenant;
public String realmCityTenant;
+ public String realmEnergyTenant;
public String smartCityServiceAgentId;
public String area1Id;
public String microphone1Id;
@@ -110,6 +111,7 @@ public class ManagerTestSetup extends ManagerSetup {
masterRealm = masterTenant.getRealm();
this.realmBuildingTenant = tenantBuilding.getRealm();
this.realmCityTenant = tenantCity.getRealm();
+ this.realmEnergyTenant = keycloakTestSetup.energyTenant.getRealm();
// ################################ Assets for 'master' realm ###################################
@@ -219,6 +221,14 @@ public class ManagerTestSetup extends ManagerSetup {
electricitySolarAsset.getAttribute(ElectricityAsset.POWER).ifPresent(attr ->
attr.addMeta(new MetaItem<>(HAS_PREDICTED_DATA_POINTS))
);
+ electricitySolarAsset.setPanelOrientation(ElectricityProducerSolarAsset.PanelOrientation.SOUTH);
+ electricitySolarAsset.setPanelAzimuth(0);
+ electricitySolarAsset.setPanelPitch(30);
+ electricitySolarAsset.setEfficiencyExport(100);
+ electricitySolarAsset.setPowerExportMax(2.5);
+ electricitySolarAsset.setLocation(new GeoJSONPoint(9.195285, 48.787418));
+ electricitySolarAsset.setSetActualValueWithForecast(true);
+ electricitySolarAsset.setIncludeForecastSolarService(true);
electricitySolarAsset = assetStorageService.merge(electricitySolarAsset);
electricitySolarAssetId = electricitySolarAsset.getId();
@@ -254,6 +264,8 @@ public class ManagerTestSetup extends ManagerSetup {
electricitySupplierAsset = assetStorageService.merge(electricitySupplierAsset);
electricitySupplierAssetId = electricitySupplierAsset.getId();
+
+
// ################################ Assets for 'building' realm ###################################
BuildingAsset smartBuilding = new BuildingAsset("Smart building");
diff --git a/test/src/test/groovy/org/openremote/test/energy/ForecastSolarServiceTest.groovy b/test/src/test/groovy/org/openremote/test/energy/ForecastSolarServiceTest.groovy
new file mode 100644
index 000000000..c5afcc6c4
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/energy/ForecastSolarServiceTest.groovy
@@ -0,0 +1,261 @@
+package org.openremote.test.energy
+
+import com.fasterxml.jackson.databind.ObjectMapper
+import org.jboss.resteasy.spi.ResteasyUriInfo
+import org.jboss.resteasy.util.BasicAuthHelper
+import org.openremote.container.web.OAuthServerResponse
+import org.openremote.manager.asset.AssetProcessingService
+import org.openremote.manager.asset.AssetStorageService
+import org.openremote.manager.energy.ForecastSolarService
+import org.openremote.manager.setup.SetupService
+import org.openremote.model.asset.impl.ElectricityProducerSolarAsset
+import org.openremote.model.attribute.AttributeEvent
+import org.openremote.model.auth.OAuthGrant
+import org.openremote.model.auth.OAuthPasswordGrant
+import org.openremote.model.auth.OAuthRefreshTokenGrant
+import org.openremote.model.geo.GeoJSONPoint
+import org.openremote.test.ManagerContainerTrait
+import org.openremote.test.setup.ManagerTestSetup
+import spock.lang.Shared
+import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+import javax.ws.rs.client.ClientRequestContext
+import javax.ws.rs.client.ClientRequestFilter
+import javax.ws.rs.core.Form
+import javax.ws.rs.core.HttpHeaders
+import javax.ws.rs.core.MediaType
+import javax.ws.rs.core.Response
+import javax.ws.rs.core.UriInfo
+import java.time.LocalDateTime
+import java.time.temporal.ChronoUnit
+
+import static org.openremote.manager.energy.ForecastSolarService.FORECAST_SOLAR_API_KEY
+
+class ForecastSolarServiceTest extends Specification implements ManagerContainerTrait {
+
+ @Shared
+ def mockServer = new ClientRequestFilter() {
+
+
+ @Override
+ void filter(ClientRequestContext requestContext) throws IOException {
+ def requestUri = requestContext.uri
+
+ switch (requestUri.host)
+ {
+ case "api.forecast.solar":
+ def now = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS)
+ def content = "\"{\\n\" +\n" +
+ " \" \\\"result\\\": {\\n\" +\n" +
+ " \" \\\"watts\\\": {\\n\" +\n" +
+ " \" \\\"${now.toLocalDate().toString()} ${now.toLocalTime().toString()}\\\": 0,\\n\" +\n" +
+ " \" \\\"2021-09-01 10:34:00\\\": 780000,\\n\" +\n" +
+ " \" \\\"2021-09-01 15:00:00\\\": 3904036,\\n\" +\n" +
+ " \" \\\"2021-09-01 15:15:00\\\": 3854598,\\n\" +\n" +
+ " \" \\\"2021-09-01 15:30:00\\\": 3746874,\\n\" +\n" +
+ " \" \\\"2021-09-01 15:45:00\\\": 3675780,\\n\" +\n" +
+ " \" \\\"2021-09-01 16:00:00\\\": 3553500,\\n\" +\n" +
+ " \" \\\"2021-09-01 16:15:00\\\": 3439679,\\n\" +\n" +
+ " \" \\\"2021-09-01 16:30:00\\\": 3278604,\\n\" +\n" +
+ " \" \\\"2021-09-01 16:45:00\\\": 3137450,\\n\" +\n" +
+ " \" \\\"2021-09-01 17:00:00\\\": 2954064,\\n\" +\n" +
+ " \" \\\"2021-09-01 17:15:00\\\": 2739056,\\n\" +\n" +
+ " \" \\\"2021-09-01 17:30:00\\\": 2493609,\\n\" +\n" +
+ " \" \\\"2021-09-01 17:45:00\\\": 2254420,\\n\" +\n" +
+ " \" \\\"2021-09-01 18:00:00\\\": 1949976,\\n\" +\n" +
+ " \" \\\"2021-09-01 18:15:00\\\": 1697104,\\n\" +\n" +
+ " \" \\\"2021-09-01 18:30:00\\\": 1452142,\\n\" +\n" +
+ " \" \\\"2021-09-01 18:45:00\\\": 1194060,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:00:00\\\": 925642,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:15:00\\\": 761949,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:30:00\\\": 594217,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:45:00\\\": 423624,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:50:00\\\": 80000,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:54:00\\\": 0,\\n\" +\n" +
+ " \" \\\"2021-09-02 06:09:00\\\": 0\\n\" +\n" +
+ " \" },\\n\" +\n" +
+ " \" \\\"watt_hours\\\": {\\n\" +\n" +
+ " \" \\\"2021-09-01 06:07:00\\\": 0,\\n\" +\n" +
+ " \" \\\"2021-09-01 10:34:00\\\": 3471000,\\n\" +\n" +
+ " \" \\\"2021-09-01 15:00:00\\\": 20778893,\\n\" +\n" +
+ " \" \\\"2021-09-01 15:15:00\\\": 21742542,\\n\" +\n" +
+ " \" \\\"2021-09-01 15:30:00\\\": 22679261,\\n\" +\n" +
+ " \" \\\"2021-09-01 15:45:00\\\": 23598206,\\n\" +\n" +
+ " \" \\\"2021-09-01 16:00:00\\\": 24486581,\\n\" +\n" +
+ " \" \\\"2021-09-01 16:15:00\\\": 25346501,\\n\" +\n" +
+ " \" \\\"2021-09-01 16:30:00\\\": 26166152,\\n\" +\n" +
+ " \" \\\"2021-09-01 16:45:00\\\": 26950514,\\n\" +\n" +
+ " \" \\\"2021-09-01 17:00:00\\\": 27689030,\\n\" +\n" +
+ " \" \\\"2021-09-01 17:15:00\\\": 28373794,\\n\" +\n" +
+ " \" \\\"2021-09-01 17:30:00\\\": 28997196,\\n\" +\n" +
+ " \" \\\"2021-09-01 17:45:00\\\": 29560801,\\n\" +\n" +
+ " \" \\\"2021-09-01 18:00:00\\\": 30048295,\\n\" +\n" +
+ " \" \\\"2021-09-01 18:15:00\\\": 30472571,\\n\" +\n" +
+ " \" \\\"2021-09-01 18:30:00\\\": 30835607,\\n\" +\n" +
+ " \" \\\"2021-09-01 18:45:00\\\": 31134122,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:00:00\\\": 31365532,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:15:00\\\": 31556020,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:30:00\\\": 31704574,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:45:00\\\": 31810480,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:50:00\\\": 31817147,\\n\" +\n" +
+ " \" \\\"2021-09-01 19:54:00\\\": 31817147,\\n\" +\n" +
+ " \" \\\"2021-09-02 06:09:00\\\": 0\\n\" +\n" +
+ " \" },\\n\" +\n" +
+ " \" \\\"watt_hours_day\\\": {\\n\" +\n" +
+ " \" \\\"2021-09-01\\\": 31817147\\n\" +\n" +
+ " \" }\\n\" +\n" +
+ " \" },\\n\" +\n" +
+ " \" \\\"message\\\": {\\n\" +\n" +
+ " \" \\\"code\\\": 0,\\n\" +\n" +
+ " \" \\\"type\\\": \\\"success\\\",\\n\" +\n" +
+ " \" \\\"text\\\": \\\"\\\",\\n\" +\n" +
+ " \" \\\"info\\\": {\\n\" +\n" +
+ " \" \\\"latitude\\\": 51.4969,\\n\" +\n" +
+ " \" \\\"longitude\\\": -0.2773,\\n\" +\n" +
+ " \" \\\"place\\\": \\\"W3 8BZ Turnham Green, Hounslow London Boro, England, GB\\\",\\n\" +\n" +
+ " \" \\\"timezone\\\": \\\"Europe/London\\\"\\n\" +\n" +
+ " \" },\\n\" +\n" +
+ " \" \\\"ratelimit\\\": {\\n\" +\n" +
+ " \" \\\"period\\\": 60,\\n\" +\n" +
+ " \" \\\"limit\\\": 5,\\n\" +\n" +
+ " \" \\\"remaining\\\": 4\\n\" +\n" +
+ " \" }\\n\" +\n" +
+ " \" }\\n\" +\n" +
+ " \"}\""
+// def response = new ObjectMapper().readValue(, ForecastSolarService.EstimateResponse.class)
+
+ // OAuth token request extract the grant info
+ def grant = ((Form)requestContext.getEntity()).asMap()
+ if (grant.getFirst(OAuthGrant.VALUE_KEY_GRANT_TYPE) == "password"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_ID ) == "client1"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_SECRET ) == "secret1"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_SCOPE ) == "scope1 scope2"
+ && grant.getFirst(OAuthPasswordGrant.VALUE_KEY_USERNAME) == "testuser"
+ && grant.getFirst(OAuthPasswordGrant.VALUE_KEY_PASSWORD) == "password1") {
+ accessToken = "accesstoken" + accessTokenCount++
+ def response = new OAuthServerResponse()
+ response.accessToken = accessToken
+ response.expiresIn = requestPath == "https://oauthserver/token" ? 5 : 100
+ response.tokenType = "Bearer"
+
+ // Include refresh token if configured to support it
+ if (supportsRefresh) {
+ refreshToken = "refreshtoken" + accessTokenCount
+ response.refreshToken = refreshToken
+ }
+
+ requestContext.abortWith(
+ Response.ok(response, MediaType.APPLICATION_JSON_TYPE).build()
+ )
+ return
+ } else if (grant.getFirst(OAuthGrant.VALUE_KEY_GRANT_TYPE) == "client_credentials"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_ID ) == "client1"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_SECRET ) == "secret1"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_SCOPE ) == "scope1 scope2") {
+ accessToken = "accesstoken" + accessTokenCount++
+ def response = new OAuthServerResponse()
+ response.accessToken = accessToken
+ response.expiresIn = requestPath == "https://oauthserver/token" ? 1 : 100
+ response.tokenType = "Bearer"
+
+ requestContext.abortWith(
+ Response.ok(response, MediaType.APPLICATION_JSON_TYPE).build()
+ )
+ return
+ } else if (supportsRefresh && grant.getFirst(OAuthGrant.VALUE_KEY_GRANT_TYPE) == "refresh_token"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_ID ) == "client1"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_SECRET ) == "secret1"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_SCOPE ) == "scope1 scope2"
+ && grant.getFirst(OAuthRefreshTokenGrant.REFRESH_TOKEN_GRANT_TYPE) == refreshToken) {
+ refreshTokenCount++
+ accessToken = "accesstoken" + accessTokenCount++
+ refreshToken = "refreshtoken" + accessTokenCount
+ def response = new OAuthServerResponse()
+ response.accessToken = accessToken
+ response.refreshToken = refreshToken
+ response.expiresIn = requestPath == "https://oauthserver/token" ? 1 : 100
+ response.tokenType = "Bearer"
+
+ requestContext.abortWith(
+ Response.ok(response, MediaType.APPLICATION_JSON_TYPE).build()
+ )
+ return
+ }
+ break
+ }
+
+ requestContext.abortWith(Response.serverError().build())
+ }
+ }
+
+ def "Test adding and removing asset with enabled attributes"() {
+ given: "the container environment is started"
+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def config = defaultConfig()
+ config << [(FORECAST_SOLAR_API_KEY) : System.getenv(FORECAST_SOLAR_API_KEY)]
+ def container = startContainer(config, defaultServices())
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ def forecastSolarService = container.getService(ForecastSolarService.class)
+
+ expect: "a Future for calculation should exist and the asset should have filled in value for power and powerForecast"
+ conditions.eventually {
+ assert !forecastSolarService.calculationFutures.isEmpty()
+ assert forecastSolarService.calculationFutures.get(managerTestSetup.electricitySolarAssetId) != null
+ def solarAsset = assetStorageService.find(managerTestSetup.electricitySolarAssetId)
+ assert solarAsset.getAttribute(ElectricityProducerSolarAsset.POWER).flatMap{it.value}.orElse(0d) != 0d
+ assert solarAsset.getAttribute(ElectricityProducerSolarAsset.POWER_FORECAST).flatMap{it.value}.orElse(0d) != 0d
+ }
+
+ when: "an asset is added with includeForecastSolarService set to true"
+ def newSolarAsset = new ElectricityProducerSolarAsset("SolarAsset")
+ newSolarAsset.setParentId(managerTestSetup.electricityOptimisationAssetId)
+ newSolarAsset.setRealm(managerTestSetup.realmEnergyTenant)
+ newSolarAsset.setPanelAzimuth(0);
+ newSolarAsset.setPanelPitch(30);
+ newSolarAsset.setEfficiencyExport(100);
+ newSolarAsset.setPowerExportMax(2.5);
+ newSolarAsset.setLocation(new GeoJSONPoint(9.195295, 48.787418));
+ newSolarAsset.setSetActualValueWithForecast(true);
+ newSolarAsset.setIncludeForecastSolarService(true);
+ newSolarAsset = assetStorageService.merge(newSolarAsset)
+
+ then: "the assetId should be present in the calculationFutures"
+ conditions.eventually {
+ assert forecastSolarService.calculationFutures.get(newSolarAsset.getId()) != null
+ newSolarAsset = assetStorageService.find(newSolarAsset.getId())
+ assert newSolarAsset.getAttribute(ElectricityProducerSolarAsset.POWER).flatMap{it.value}.orElse(0d) != 0d
+ assert newSolarAsset.getAttribute(ElectricityProducerSolarAsset.POWER_FORECAST).flatMap{it.value}.orElse(0d) != 0d
+ }
+
+ when: "an asset is added with includeForecastSolarService set to false"
+ def newSolarAsset2 = new ElectricityProducerSolarAsset("SolarAsset2")
+ newSolarAsset2.setParentId(managerTestSetup.electricityOptimisationAssetId)
+ newSolarAsset2.setRealm(managerTestSetup.realmEnergyTenant)
+ newSolarAsset2.setPanelAzimuth(0);
+ newSolarAsset2.setPanelPitch(30);
+ newSolarAsset2.setEfficiencyExport(100);
+ newSolarAsset2.setPowerExportMax(2.5);
+ newSolarAsset2.setLocation(new GeoJSONPoint(9.195275, 48.787418));
+ newSolarAsset2.setSetActualValueWithForecast(false);
+ newSolarAsset2.setIncludeForecastSolarService(false);
+ newSolarAsset2 = assetStorageService.merge(newSolarAsset2)
+
+ then: "the assetId shouldn't be present in the calculationFutures"
+ conditions.eventually {
+ assert forecastSolarService.calculationFutures.get(newSolarAsset2.getId()) == null
+ }
+
+ when: "an asset updated it's includeForecastSolarService to true"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(newSolarAsset2.getId(), ElectricityProducerSolarAsset.INCLUDE_FORECAST_SOLAR_SERVICE.name, true))
+
+ then: "it should be present present in the calculationFutures"
+ conditions.eventually {
+ assert forecastSolarService.calculationFutures.get(newSolarAsset2.getId()) != null
+ assert newSolarAsset2.getAttribute(ElectricityProducerSolarAsset.POWER).flatMap{it.value}.orElse(0d) == 0d
+ assert newSolarAsset2.getAttribute(ElectricityProducerSolarAsset.POWER_FORECAST).flatMap{it.value}.orElse(0d) != 0d
+ }
+ }
+}