Skip to content

armgit / core

Public Imported from GitHub
0

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

Cleaned up rules tests

Christian Bauer016916c5083e
diff --git a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
index c0d6648ad..d6e96cbde 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
@@ -47,6 +47,7 @@ import org.openremote.model.util.JsonUtil;
import java.util.*;
import java.util.function.Consumer;
+import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@@ -109,86 +110,100 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
.filter(isPersistenceEventForEntityType(Asset.class))
.process(exchange -> {
PersistenceEvent persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);
- ServerAsset asset = (ServerAsset) persistenceEvent.getEntity();
+ final ServerAsset eventAsset = (ServerAsset) persistenceEvent.getEntity();
+
+ // We must load the asset from database (only when required), as the
+ // persistence event might not contain a completely loaded asset
+ Function<Attribute, AssetUpdate> buildUpdateFunction = new Function<Attribute, AssetUpdate>() {
+ final Asset loadedAsset = assetStorageService.find(eventAsset.getId(), true);
+
+ @Override
+ public AssetUpdate apply(Attribute attribute) {
+ return new AssetUpdate(loadedAsset, attribute);
+ }
+ };
switch (persistenceEvent.getCause()) {
case INSERT:
- // TODO: Need to populate asset path rather than refetch the entire asset
- ServerAsset insertedAsset = assetStorageService.find(asset.getId(), true, false);
-
// New asset has been created so get attributes that have RULES_FACT meta
- Attributes addedAttributes = new Attributes(asset.getAttributes());
+ Attributes addedAttributes = new Attributes(eventAsset.getAttributes());
Arrays.stream(addedAttributes.get())
- .filter(Attribute::isRulesFact)
- .forEach(attribute -> {
- AssetUpdate update = new AssetUpdate(insertedAsset, attribute);
- // Set the status to completed already so rules cannot interfere with this initial insert
- update.setStatus(AssetUpdate.Status.COMPLETED);
- insertFact(update, true);
- });
+ .filter(Attribute::isRulesFact)
+ .forEach(attribute -> {
+ AssetUpdate update = buildUpdateFunction.apply(attribute);
+ // Set the status to completed already so rules cannot interfere with this initial insert
+ update.setStatus(AssetUpdate.Status.COMPLETED);
+ insertFact(update, true);
+ });
break;
case UPDATE:
- // TODO: Need to populate asset path rather than refetch the entire asset
- ServerAsset updatedAsset = assetStorageService.find(asset.getId(), true, false);
int attributesIndex = Arrays.asList(persistenceEvent.getPropertyNames()).indexOf("attributes");
if (attributesIndex >= 0) {
// Attributes have possibly changed so need to compare old and new state to determine
// which facts to retract and which to insert
- Attributes oldAttributes = new Attributes((JsonObject)persistenceEvent.getPreviousState()[attributesIndex]);
- Attributes newAttributes = new Attributes((JsonObject)persistenceEvent.getCurrentState()[attributesIndex]);
+ Attributes oldAttributes = new Attributes((JsonObject) persistenceEvent.getPreviousState()[attributesIndex]);
+ Attributes newAttributes = new Attributes((JsonObject) persistenceEvent.getCurrentState()[attributesIndex]);
List<Attribute> oldFactAttributes = Arrays.stream(oldAttributes.get())
- .filter(Attribute::isRulesFact)
- .collect(Collectors.toList());
+ .filter(Attribute::isRulesFact)
+ .collect(Collectors.toList());
List<Attribute> newFactAttributes = Arrays.stream(newAttributes.get())
- .filter(Attribute::isRulesFact)
- .collect(Collectors.toList());
+ .filter(Attribute::isRulesFact)
+ .collect(Collectors.toList());
// Compare attributes by JSON value
// Retract facts for attributes that are in oldFactAttributes but not in newFactAttributes
oldFactAttributes
+ .stream()
+ .filter(oldFactAttribute -> newFactAttributes
.stream()
- .filter(oldFactAttribute -> newFactAttributes
- .stream()
- .noneMatch(newFactAttribute ->
- JsonUtil.equals(oldFactAttribute.getJsonObject(), newFactAttribute.getJsonObject(), Collections.singletonList(AbstractValueTimestampHolder.VALUE_TIMESTAMP_FIELD_NAME))
- )
+ .noneMatch(newFactAttribute -> JsonUtil.equals( // Ignore the timestamp in comparison
+ oldFactAttribute.getJsonObject(),
+ newFactAttribute.getJsonObject(),
+ Collections.singletonList(AbstractValueTimestampHolder.VALUE_TIMESTAMP_FIELD_NAME))
)
- .forEach(obsoleteFactAttribute -> {
- AssetUpdate update = new AssetUpdate(updatedAsset, obsoleteFactAttribute);
- retractFact(update);
- });
+ )
+ .forEach(obsoleteFactAttribute -> {
+ AssetUpdate update = buildUpdateFunction.apply(obsoleteFactAttribute);
+ retractFact(update);
+ });
// Insert facts for attributes that are in newFactAttributes but not in oldFactAttributes
newFactAttributes
+ .stream()
+ .filter(newFactAttribute -> oldFactAttributes
.stream()
- .filter(newFactAttribute -> oldFactAttributes
- .stream()
- .noneMatch(oldFactAttribute ->
- JsonUtil.equals(oldFactAttribute.getJsonObject(), newFactAttribute.getJsonObject(), Collections.singletonList(AbstractValueTimestampHolder.VALUE_TIMESTAMP_FIELD_NAME))
- )
+ .noneMatch(oldFactAttribute ->
+ JsonUtil.equals( // Ignore the timestamp in comparison
+ oldFactAttribute.getJsonObject(),
+ newFactAttribute.getJsonObject(),
+ Collections.singletonList(AbstractValueTimestampHolder.VALUE_TIMESTAMP_FIELD_NAME)
+ )
)
- .forEach(newFactAttribute -> {
- AssetUpdate update = new AssetUpdate(updatedAsset, newFactAttribute);
- // Set the status to completed already so rules cannot interfere with this initial insert
- update.setStatus(AssetUpdate.Status.COMPLETED);
- insertFact(update, true);
- });
+ )
+ .forEach(newFactAttribute -> {
+ AssetUpdate update = buildUpdateFunction.apply(newFactAttribute);
+ // Set the status to completed already so rules cannot interfere with this initial insert
+ update.setStatus(AssetUpdate.Status.COMPLETED);
+ insertFact(update, true);
+ });
}
break;
case DELETE:
// Retract any facts that were associated with this asset
- Attributes removedAttributes = new Attributes(asset.getAttributes());
+ Attributes removedAttributes = new Attributes(eventAsset.getAttributes());
Arrays.stream(removedAttributes.get())
- .filter(Attribute::isRulesFact)
- .forEach(attribute -> {
- AssetUpdate update = new AssetUpdate(asset, attribute);
- retractFact(update);
- });
+ .filter(Attribute::isRulesFact)
+ .forEach(attribute -> {
+ // We can't load the asset again (it was deleted), so don't use buildUpdateFunction() and
+ // hope that the path of the event asset has been loaded before deletion
+ AssetUpdate update = new AssetUpdate(eventAsset, attribute);
+ retractFact(update);
+ });
break;
}
});
@@ -211,27 +226,28 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
// Deploy asset rules - group by asset ID then tenant and check tenant is enabled
deployAssetRulesDefinitions(rulesStorageService.findEnabledAssetDefinitions());
- // Get all attributes that contain RULES_FACT = true meta and push Attribute Events for them
- // into the rule engines.
+ // Get all attributes that contain RULES_FACT = true meta and push
+ // AttributeEvents for them into the rule engines
List<ServerAsset> assets = assetStorageService.findAll(
- new AssetQuery()
- .attributeMeta(
- new AssetQuery.AttributeMetaPredicate(
- AssetMeta.RULES_FACT,
- new AssetQuery.BooleanPredicate(true))
- ));
+ new AssetQuery()
+ .select(new AssetQuery.Select(true))
+ .attributeMeta(
+ new AssetQuery.AttributeMetaPredicate(
+ AssetMeta.RULES_FACT,
+ new AssetQuery.BooleanPredicate(true))
+ ));
assets.forEach(asset -> {
Attributes attributes = new Attributes(asset.getAttributes());
Arrays
- .stream(attributes.get())
- .filter(Attribute::isRulesFact)
- .forEach(attribute -> {
- AssetUpdate update = new AssetUpdate(asset, attribute);
- // Set the status to completed already so rules cannot interfere with this initial insert
- update.setStatus(AssetUpdate.Status.COMPLETED);
- insertFact(update, true);
- });
+ .stream(attributes.get())
+ .filter(Attribute::isRulesFact)
+ .forEach(attribute -> {
+ AssetUpdate update = new AssetUpdate(asset, attribute);
+ // Set the status to completed already so rules cannot interfere with this initial insert
+ update.setStatus(AssetUpdate.Status.COMPLETED);
+ insertFact(update, true);
+ });
});
}
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy
new file mode 100644
index 000000000..86f061cda
--- /dev/null
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy
@@ -0,0 +1,351 @@
+package org.openremote.test.rules
+
+import org.openremote.manager.server.rules.RulesService
+import org.openremote.manager.server.rules.RulesStorageService
+import org.openremote.manager.server.security.ManagerIdentityService
+import org.openremote.manager.server.setup.SetupService
+import org.openremote.manager.server.setup.builtin.KeycloakDemoSetup
+import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
+import org.openremote.manager.shared.rules.AssetRulesDefinition
+import org.openremote.manager.shared.rules.GlobalRulesDefinition
+import org.openremote.manager.shared.rules.RulesDefinition.DeploymentStatus
+import org.openremote.manager.shared.rules.TenantRulesDefinition
+import org.openremote.test.ManagerContainerTrait
+import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+import static org.openremote.container.util.MapAccess.getString
+import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KEYCLOAK_ADMIN_PASSWORD
+import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KEYCLOAK_ADMIN_PASSWORD_DEFAULT
+import static org.openremote.model.Constants.*
+
+class BasicRulesDeploymentTest extends Specification implements ManagerContainerTrait {
+
+ def "Check basic rules engine deployment"() {
+
+ given: "expected conditions"
+ def conditions = new PollingConditions(timeout: 5)
+
+ and: "the container is started"
+ def serverPort = findEphemeralPort()
+ def container = startContainer(defaultConfig(serverPort), defaultServices())
+ def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
+ def keycloakDemoSetup = container.getService(SetupService.class).getTaskOfType(KeycloakDemoSetup.class)
+ def rulesService = container.getService(RulesService.class)
+ def identityService = container.getService(ManagerIdentityService.class)
+ def rulesStorageService = container.getService(RulesStorageService.class)
+
+ and: "some test rulesets have been imported"
+ def rulesImport = new BasicRulesImport(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
+
+ and: "an authenticated user"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), SETUP_KEYCLOAK_ADMIN_PASSWORD, SETUP_KEYCLOAK_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ expect: "a global rules engine should have been created and be running"
+ conditions.eventually {
+ assert rulesService.globalDeployment != null
+ assert rulesService.globalDeployment.isRunning()
+ }
+
+ and: "the global rules engine should contain the global demo rules definition"
+ conditions.eventually {
+ assert rulesService.globalDeployment.allRulesDefinitions.length == 1
+ assert rulesService.globalDeployment.allRulesDefinitions[0].enabled
+ assert rulesService.globalDeployment.allRulesDefinitions[0].name == "Some global demo rules"
+ assert rulesService.globalDeployment.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ and: "two tenant rules engines should have been created and be running"
+ conditions.eventually {
+ assert rulesService.tenantDeployments.size() == 2
+ def masterEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.masterTenant.id)
+ def customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
+ assert masterEngine != null
+ assert masterEngine.isRunning()
+ assert customerAEngine != null
+ assert customerAEngine.isRunning()
+ }
+
+ and: "the tenant rules engines should have the demo tenant rules definition"
+ conditions.eventually {
+ def masterEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.masterTenant.id)
+ def customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
+ assert masterEngine.allRulesDefinitions.length == 1
+ assert masterEngine.allRulesDefinitions[0].enabled
+ assert masterEngine.allRulesDefinitions[0].name == "Some master tenant demo rules"
+ assert masterEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ assert customerAEngine.allRulesDefinitions.length == 1
+ assert customerAEngine.allRulesDefinitions[0].enabled
+ assert customerAEngine.allRulesDefinitions[0].name == "Some customerA tenant demo rules"
+ assert customerAEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ and: "two asset rules engines should have been created and be running"
+ conditions.eventually {
+ assert rulesService.assetDeployments.size() == 2
+ def apartment1Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment1Id)
+ def apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
+ assert apartment1Engine != null
+ assert apartment1Engine.isRunning()
+ assert apartment3Engine != null
+ assert apartment3Engine.isRunning()
+ }
+
+ and: "each asset rules engine should have the demo asset rules definition"
+ conditions.eventually {
+ def apartment1Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment1Id)
+ def apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
+ assert apartment1Engine.allRulesDefinitions.length == 1
+ assert apartment1Engine.allRulesDefinitions[0].enabled
+ assert apartment1Engine.allRulesDefinitions[0].name == "Some apartment 1 demo rules"
+ assert apartment1Engine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ assert apartment3Engine.allRulesDefinitions.length == 1
+ assert apartment3Engine.allRulesDefinitions[0].enabled
+ assert apartment3Engine.allRulesDefinitions[0].name == "Some apartment 3 demo rules"
+ assert apartment3Engine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ when: "a new global rule definition is added"
+ def rulesDefinition = new GlobalRulesDefinition(
+ "Some more global rules",
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates2.drl").text
+ )
+ rulesDefinition = rulesStorageService.merge(rulesDefinition)
+
+ then: "the global rules engine should load this definition and restart successfully"
+ conditions.eventually {
+ assert rulesService.globalDeployment != null
+ assert rulesService.globalDeployment.isRunning()
+ assert rulesService.globalDeployment.allRulesDefinitions.length == 2
+ assert rulesService.globalDeployment.allRulesDefinitions[0].enabled
+ assert rulesService.globalDeployment.allRulesDefinitions[0].name == "Some global demo rules"
+ assert rulesService.globalDeployment.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ assert rulesService.globalDeployment.allRulesDefinitions[1].enabled
+ assert rulesService.globalDeployment.allRulesDefinitions[1].name == "Some more global rules"
+ assert rulesService.globalDeployment.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ when: "a new tenant rule definition is added to customer A"
+ rulesDefinition = new TenantRulesDefinition(
+ "Some more customerA tenant rules",
+ keycloakDemoSetup.customerATenant.id,
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates2.drl").text
+ )
+ rulesDefinition = rulesStorageService.merge(rulesDefinition)
+
+ then: "customer A rules engine should load this definition and restart successfully"
+ conditions.eventually {
+ def customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
+ assert customerAEngine != null
+ assert customerAEngine.isRunning()
+ assert customerAEngine.allRulesDefinitions.length == 2
+ assert customerAEngine.allRulesDefinitions[0].enabled
+ assert customerAEngine.allRulesDefinitions[0].name == "Some customerA tenant demo rules"
+ assert customerAEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ assert customerAEngine.allRulesDefinitions[1].enabled
+ assert customerAEngine.allRulesDefinitions[1].name == "Some more customerA tenant rules"
+ assert customerAEngine.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ when: "a new tenant rule definition is added to customer B"
+ rulesDefinition = new TenantRulesDefinition(
+ "Some more customerB tenant rules",
+ keycloakDemoSetup.customerBTenant.id,
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates2.drl").text
+ )
+ rulesDefinition = rulesStorageService.merge(rulesDefinition)
+
+ then: "a tenant rules engine should be created for customer B and load this definition and start successfully"
+ conditions.eventually {
+ def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
+ assert rulesService.tenantDeployments.size() == 3
+ assert customerBEngine != null
+ assert customerBEngine.isRunning()
+ assert customerBEngine.allRulesDefinitions.length == 1
+ assert customerBEngine.allRulesDefinitions[0].enabled
+ assert customerBEngine.allRulesDefinitions[0].name == "Some more customerB tenant rules"
+ assert customerBEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ when: "the disabled rule definition for customer B is enabled"
+ rulesDefinition = rulesStorageService.findById(TenantRulesDefinition.class, rulesImport.customerBRulesDefinitionId)
+ rulesDefinition.setEnabled(true)
+ rulesStorageService.merge(rulesDefinition)
+
+ then: "customer B rule engine should load this definition and restart successfully"
+ conditions.eventually {
+ def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
+ assert rulesService.tenantDeployments.size() == 3
+ assert customerBEngine != null
+ assert customerBEngine.isRunning()
+ assert customerBEngine.allRulesDefinitions.length == 2
+ assert customerBEngine.allRulesDefinitions[0].enabled
+ assert customerBEngine.allRulesDefinitions[0].name == "Some more customerB tenant rules"
+ assert customerBEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ assert customerBEngine.allRulesDefinitions[1].enabled
+ assert customerBEngine.allRulesDefinitions[1].name == "Some customerB tenant demo rules"
+ assert customerBEngine.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ when: "the enabled rule definition for customer B is disabled"
+ // TODO: Stop instances of rule definitions being passed around as rules deployment nulls the rules property
+ rulesDefinition = rulesStorageService.findById(TenantRulesDefinition.class, rulesImport.customerBRulesDefinitionId)
+ rulesDefinition.setEnabled(false)
+ rulesStorageService.merge(rulesDefinition)
+
+ then: "customer B rule engine should remove it again"
+ conditions.eventually {
+ def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
+ assert customerBEngine != null
+ assert customerBEngine.isRunning()
+ assert customerBEngine.allRulesDefinitions.length == 1
+ assert customerBEngine.allRulesDefinitions[0].enabled
+ assert customerBEngine.allRulesDefinitions[0].name == "Some more customerB tenant rules"
+ assert customerBEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ when: "the asset rule definition for apartment 1 is deleted"
+ rulesStorageService.delete(AssetRulesDefinition.class, rulesImport.apartment1RulesDefinitionId)
+
+ then: "the apartment rules engine should be removed"
+ conditions.eventually {
+ assert rulesService.assetDeployments.size() == 1
+ def apartment1Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment1Id)
+ def apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
+ assert apartment1Engine == null
+ assert apartment3Engine != null
+ assert apartment3Engine.isRunning()
+ }
+
+ when: "a broken rule definition is added to the global rules engine"
+ rulesDefinition = new GlobalRulesDefinition(
+ "Some broken global rules",
+ getClass().getResource("/org/openremote/test/rules/BrokenRules.drl").text
+ )
+ rulesDefinition = rulesStorageService.merge(rulesDefinition)
+
+ then: "the global rules engine should not run and the rule deployment status should indicate the issue"
+ conditions.eventually {
+ assert rulesService.globalDeployment.allRulesDefinitions.length == 3
+ assert rulesService.globalDeployment.running == false
+ assert rulesService.globalDeployment.error == true
+ assert rulesService.globalDeployment.allRulesDefinitions[0].enabled
+ assert rulesService.globalDeployment.allRulesDefinitions[0].name == "Some global demo rules"
+ assert rulesService.globalDeployment.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.READY
+ assert rulesService.globalDeployment.allRulesDefinitions[1].enabled
+ assert rulesService.globalDeployment.allRulesDefinitions[1].name == "Some more global rules"
+ assert rulesService.globalDeployment.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.READY
+ assert rulesService.globalDeployment.allRulesDefinitions[2].enabled
+ assert rulesService.globalDeployment.allRulesDefinitions[2].name == "Some broken global rules"
+ assert rulesService.globalDeployment.allRulesDefinitions[2].deploymentStatus == DeploymentStatus.FAILED
+ }
+
+ when: "the broken rule definition is removed from the global engine"
+ rulesStorageService.delete(GlobalRulesDefinition.class, rulesDefinition.getId())
+
+ then: "the global rules engine should restart"
+ conditions.eventually {
+ assert rulesService.globalDeployment.allRulesDefinitions.length == 2
+ assert rulesService.globalDeployment.running == true
+ assert rulesService.globalDeployment.error == false
+ assert rulesService.globalDeployment.allRulesDefinitions[0].enabled
+ assert rulesService.globalDeployment.allRulesDefinitions[0].name == "Some global demo rules"
+ assert rulesService.globalDeployment.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ assert rulesService.globalDeployment.allRulesDefinitions[1].enabled
+ assert rulesService.globalDeployment.allRulesDefinitions[1].name == "Some more global rules"
+ assert rulesService.globalDeployment.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ when: "a tenant is disabled"
+ def customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
+ def apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
+ def customerATenant = keycloakDemoSetup.customerATenant
+ customerATenant.setEnabled(false)
+ identityService.updateTenant(accessToken, customerATenant.getRealm(), customerATenant)
+
+ then: "the tenants rule engine should stop and all asset rule engines in this realm should also stop"
+ conditions.eventually {
+ assert customerAEngine.isRunning() == false
+ assert customerAEngine.allRulesDefinitions.length == 0
+ assert rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id) == null
+ assert apartment3Engine.isRunning() == false
+ assert apartment3Engine.allRulesDefinitions.length == 0
+ assert rulesService.assetDeployments.get(managerDemoSetup.apartment3Id) == null
+ }
+
+ and: "other rule engines should be unaffected"
+ conditions.eventually {
+ assert rulesService.tenantDeployments.size() == 2
+ assert rulesService.assetDeployments.size() == 0
+ def masterEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.masterTenant.id)
+ def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
+ assert masterEngine != null
+ assert masterEngine.isRunning()
+ assert customerBEngine != null
+ assert customerBEngine.isRunning()
+ }
+
+ when: "the disabled tenant is re-enabled"
+ customerATenant.setEnabled(true)
+ identityService.updateTenant(accessToken, customerATenant.getRealm(), customerATenant)
+
+ then: "the tenants rule engine should start and all asset rule engines from this realm should also start"
+ conditions.eventually {
+ customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
+ apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
+ assert rulesService.tenantDeployments.size() == 3
+ assert rulesService.assetDeployments.size() == 1
+ assert customerAEngine != null
+ assert customerAEngine.isRunning()
+ assert customerAEngine.allRulesDefinitions.length == 2
+ assert customerAEngine.allRulesDefinitions[0].enabled
+ assert customerAEngine.allRulesDefinitions[0].name == "Some customerA tenant demo rules"
+ assert customerAEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ assert customerAEngine.allRulesDefinitions[1].enabled
+ assert customerAEngine.allRulesDefinitions[1].name == "Some more customerA tenant rules"
+ assert customerAEngine.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
+ assert apartment3Engine.allRulesDefinitions.length == 1
+ assert apartment3Engine.allRulesDefinitions[0].enabled
+ assert apartment3Engine.allRulesDefinitions[0].name == "Some apartment 3 demo rules"
+ assert apartment3Engine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+//TODO: Reinstate the tenant delete test once tenant delete mechanism is finalised
+// when: "a tenant is deleted"
+// identityService.deleteTenant(accessToken, customerATenant.getRealm())
+//
+// then: "the tenants rule engine should stop and all asset rule engines in this realm should also stop"
+// conditions.eventually {
+// assert customerAEngine.isRunning() == false
+// assert customerAEngine.allRulesDefinitions.length == 0
+// assert rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id) == null
+// assert smartHomeEngine.isRunning() == false
+// assert smartHomeEngine.allRulesDefinitions.length == 0
+// assert rulesService.assetDeployments.get(managerDemoSetup.smartHomeId) == null
+// assert apartment3Engine.isRunning() == false
+// assert apartment3Engine.allRulesDefinitions.length == 0
+// assert rulesService.assetDeployments.get(managerDemoSetup.apartment3Id) == null
+// }
+//
+// and: "other rule engines should be unaffected"
+// conditions.eventually {
+// assert rulesService.tenantDeployments.size() == 2
+// assert rulesService.assetDeployments.size() == 0
+// def masterEngine = rulesService.tenantDeployments.get(Constants.MASTER_REALM)
+// def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
+// assert masterEngine != null
+// assert masterEngine.isRunning()
+// assert customerBEngine != null
+// assert customerBEngine.isRunning()
+// }
+
+ cleanup: "the server should be stopped"
+ stopContainer(container)
+ }
+}
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesImport.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesImport.groovy
new file mode 100644
index 000000000..d96732a40
--- /dev/null
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesImport.groovy
@@ -0,0 +1,77 @@
+package org.openremote.test.rules
+
+import org.openremote.manager.server.rules.RulesStorageService
+import org.openremote.manager.server.setup.builtin.KeycloakDemoSetup
+import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
+import org.openremote.manager.shared.rules.AssetRulesDefinition
+import org.openremote.manager.shared.rules.GlobalRulesDefinition
+import org.openremote.manager.shared.rules.RulesDefinition
+import org.openremote.manager.shared.rules.TenantRulesDefinition
+
+class BasicRulesImport {
+
+ def customerBRulesDefinitionId
+ def apartment1RulesDefinitionId
+
+ BasicRulesImport(RulesStorageService rulesStorageService,
+ KeycloakDemoSetup keycloakDemoSetup,
+ ManagerDemoSetup managerDemoSetup) {
+
+ RulesDefinition rulesDefinition = new GlobalRulesDefinition(
+ "Some global demo rules",
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
+ )
+ rulesStorageService.merge(rulesDefinition)
+
+ rulesDefinition = new GlobalRulesDefinition(
+ "Other global demo rules with a long name that should fill up space in UI",
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
+ )
+ rulesDefinition.setEnabled(false)
+ rulesStorageService.merge(rulesDefinition)
+
+ rulesDefinition = new TenantRulesDefinition(
+ "Some master tenant demo rules",
+ keycloakDemoSetup.masterTenant.id,
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
+ )
+ rulesStorageService.merge(rulesDefinition)
+
+ rulesDefinition = new TenantRulesDefinition(
+ "Some customerA tenant demo rules",
+ keycloakDemoSetup.customerATenant.id,
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
+ )
+ rulesStorageService.merge(rulesDefinition)
+
+ rulesDefinition = new TenantRulesDefinition(
+ "Some customerB tenant demo rules",
+ keycloakDemoSetup.customerBTenant.id,
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
+ )
+ rulesDefinition.setEnabled(false)
+ customerBRulesDefinitionId = rulesStorageService.merge(rulesDefinition).id
+
+ rulesDefinition = new AssetRulesDefinition(
+ "Some apartment 1 demo rules",
+ managerDemoSetup.apartment1Id,
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
+ )
+ apartment1RulesDefinitionId = rulesStorageService.merge(rulesDefinition).id
+
+ rulesDefinition = new AssetRulesDefinition(
+ "Some apartment 2 demo rules",
+ managerDemoSetup.apartment2Id,
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
+ )
+ rulesDefinition.setEnabled(false)
+ rulesStorageService.merge(rulesDefinition)
+
+ rulesDefinition = new AssetRulesDefinition(
+ "Some apartment 3 demo rules",
+ managerDemoSetup.apartment3Id,
+ getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
+ )
+ rulesStorageService.merge(rulesDefinition)
+ }
+}
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
new file mode 100644
index 000000000..edb978918
--- /dev/null
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
@@ -0,0 +1,427 @@
+package org.openremote.test.rules
+
+import elemental.json.Json
+import org.kie.api.event.rule.AfterMatchFiredEvent
+import org.kie.api.event.rule.DefaultAgendaEventListener
+import org.openremote.manager.server.asset.AssetProcessingService
+import org.openremote.manager.server.asset.AssetStorageService
+import org.openremote.manager.server.asset.ServerAsset
+import org.openremote.manager.server.rules.RulesDeployment
+import org.openremote.manager.server.rules.RulesService
+import org.openremote.manager.server.rules.RulesStorageService
+import org.openremote.manager.server.setup.SetupService
+import org.openremote.manager.server.setup.builtin.KeycloakDemoSetup
+import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
+import org.openremote.manager.shared.rules.AssetRulesDefinition
+import org.openremote.manager.shared.rules.GlobalRulesDefinition
+import org.openremote.manager.shared.rules.RulesDefinition.DeploymentStatus
+import org.openremote.model.*
+import org.openremote.model.asset.AssetMeta
+import org.openremote.model.asset.AssetType
+import org.openremote.test.ManagerContainerTrait
+import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+class BasicRulesProcessingTest extends Specification implements ManagerContainerTrait {
+
+ RulesDeployment globalEngine, masterEngine, customerAEngine, smartHomeEngine, apartment1Engine, apartment3Engine
+
+ List<String> globalEngineFiredRules = []
+ List<String> masterEngineFiredRules = []
+ List<String> customerAEngineFiredRules = []
+ List<String> smartHomeEngineFiredRules = []
+ List<String> apartment1EngineFiredRules = []
+ List<String> apartment3EngineFiredRules = []
+
+ def resetRuleExecutionLoggers() {
+ globalEngineFiredRules.clear()
+ customerAEngineFiredRules.clear()
+ smartHomeEngineFiredRules.clear()
+ apartment1EngineFiredRules.clear()
+ apartment3EngineFiredRules.clear()
+ }
+
+ def assertNoRulesFired = {
+ assert globalEngineFiredRules.size() == 0
+ assert masterEngineFiredRules.size() == 0
+ assert customerAEngineFiredRules.size() == 0
+ assert smartHomeEngineFiredRules.size() == 0
+ assert apartment1EngineFiredRules.size() == 0
+ assert apartment3EngineFiredRules.size() == 0
+ }
+
+ def attachRuleExecutionLogger(RulesDeployment ruleEngine, List<String> executedRules) {
+ def session = ruleEngine.getKnowledgeSession()
+ if (session == null) {
+ return
+ }
+ session.addEventListener(new DefaultAgendaEventListener() {
+ @Override
+ void afterMatchFired(AfterMatchFiredEvent event) {
+ def rule = event.getMatch().getRule()
+ def ruleName = rule.getName()
+ executedRules.add(ruleName)
+ }
+ })
+ }
+
+ def "Check firing of rules"() {
+ given: "expected conditions"
+ def conditions = new PollingConditions(timeout: 10)
+
+ and: "the container is started"
+ def serverPort = findEphemeralPort()
+ def container = startContainer(defaultConfig(serverPort), defaultServices())
+ def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
+ def keycloakDemoSetup = container.getService(SetupService.class).getTaskOfType(KeycloakDemoSetup.class)
+ def rulesService = container.getService(RulesService.class)
+ def rulesStorageService = container.getService(RulesStorageService.class)
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+
+ and: "some test rulesets have been imported"
+ new BasicRulesImport(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
+
+ expect: "the rule engines to become available and be running"
+ conditions.eventually {
+ globalEngine = rulesService.globalDeployment
+ assert globalEngine != null
+ assert globalEngine.isRunning()
+ masterEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.masterTenant.id)
+ assert masterEngine != null
+ assert masterEngine.isRunning()
+ customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
+ assert customerAEngine != null
+ assert customerAEngine.isRunning()
+ smartHomeEngine = rulesService.assetDeployments.get(managerDemoSetup.smartHomeId)
+ assert smartHomeEngine == null
+ apartment1Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment1Id)
+ assert apartment1Engine != null
+ assert apartment1Engine.isRunning()
+ apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
+ assert apartment3Engine != null
+ assert apartment3Engine.isRunning()
+ }
+
+ and: "the demo attributes marked with RULES_FACT = true meta should be inserted into the engines"
+ conditions.eventually {
+ globalEngine.facts.size() == 6
+ }
+
+ when: "rule execution loggers are attached to the engines"
+ attachRuleExecutionLogger(globalEngine, globalEngineFiredRules)
+ attachRuleExecutionLogger(masterEngine, masterEngineFiredRules)
+ attachRuleExecutionLogger(customerAEngine, customerAEngineFiredRules)
+ attachRuleExecutionLogger(apartment1Engine, apartment1EngineFiredRules)
+ attachRuleExecutionLogger(apartment3Engine, apartment3EngineFiredRules)
+
+ and: "an attribute event is pushed into the system for an attribute with RULES_FACT meta set to true"
+ def apartment1LivingRoomDemoBooleanChange = new AttributeEvent(
+ new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoBoolean"), Json.create(false)), getClass()
+ )
+ assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoBooleanChange)
+
+ then: "the rule engines in scope should fire the 'All' and 'All changed' rules"
+ conditions.eventually {
+ def expectedFiredRules = ["All", "All changed"]
+ assert globalEngineFiredRules.size() == 2
+ assert globalEngineFiredRules.containsAll(expectedFiredRules)
+ assert masterEngineFiredRules.size() == 0
+ assert customerAEngineFiredRules.size() == 2
+ assert customerAEngineFiredRules.containsAll(expectedFiredRules)
+ assert smartHomeEngineFiredRules.size() == 0
+ assert apartment1EngineFiredRules.size() == 2
+ assert apartment1EngineFiredRules.containsAll(expectedFiredRules)
+ assert apartment3EngineFiredRules.size() == 0
+ }
+
+ when: "an attribute event is pushed into the system for an attribute with RULES_FACT meta set to false"
+ resetRuleExecutionLoggers()
+ def apartment1LivingRoomDemoStringChange = new AttributeEvent(
+ new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoString"), Json.create("demo2")), getClass()
+ )
+ assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoStringChange)
+
+ then: "no rule engines should have fired after a few seconds"
+ new PollingConditions(initialDelay: 3).eventually assertNoRulesFired
+
+ when: "an attribute event is pushed into the system for an attribute with no RULES_FACT meta"
+ resetRuleExecutionLoggers()
+ def apartment1LivingRoomDemoIntegerChange = new AttributeEvent(
+ new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoInteger"), Json.create(1)), getClass()
+ )
+ assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoIntegerChange)
+
+ then: "no rule engines should have fired after a few seconds"
+ new PollingConditions(initialDelay: 3).eventually assertNoRulesFired
+
+ when: "an old (stale) attribute event is pushed into the system"
+ resetRuleExecutionLoggers()
+ assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoBooleanChange)
+
+ then: "no rule engines should have fired after a few seconds"
+ new PollingConditions(initialDelay: 3).eventually assertNoRulesFired
+
+ when: "an attribute event with the same value as current value is pushed into the system"
+ resetRuleExecutionLoggers()
+ apartment1LivingRoomDemoBooleanChange = new AttributeEvent(
+ new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoBoolean"), Json.create(false)), getClass()
+ )
+ assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoBooleanChange)
+
+ then: "the rule engines in scope should fire the 'All' rule but not the 'All changed' rule"
+ conditions.eventually {
+ assert globalEngineFiredRules.size() == 1
+ assert globalEngineFiredRules[0] == "All"
+ assert masterEngineFiredRules.size() == 0
+ assert customerAEngineFiredRules.size() == 1
+ assert customerAEngineFiredRules[0] == "All"
+ assert smartHomeEngineFiredRules.size() == 0
+ assert apartment1EngineFiredRules.size() == 1
+ assert apartment1EngineFiredRules[0] == "All"
+ assert apartment3EngineFiredRules.size() == 0
+ }
+
+ when: "a LHS filtering test rule definition is loaded into the smart home asset"
+ resetRuleExecutionLoggers()
+ def rulesDefinition = new AssetRulesDefinition(
+ "Some smart home asset rules",
+ managerDemoSetup.smartHomeId,
+ getClass().getResource("/org/openremote/test/rules/SmartHomeMatchAllAssetUpdates.drl").text
+ )
+ rulesStorageService.merge(rulesDefinition)
+
+ then: "the smart home rule engine should have ben created, loaded the new rule definition and started"
+ conditions.eventually {
+ smartHomeEngine = rulesService.assetDeployments.get(managerDemoSetup.smartHomeId)
+ assert smartHomeEngine != null
+ assert smartHomeEngine.isRunning()
+ assert smartHomeEngine.allRulesDefinitions.length == 1
+ assert smartHomeEngine.allRulesDefinitions[0].enabled
+ assert smartHomeEngine.allRulesDefinitions[0].name == "Some smart home asset rules"
+ assert smartHomeEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ when: "the engine counters are reset and the smart home engine logger is attached"
+ resetRuleExecutionLoggers()
+ attachRuleExecutionLogger(smartHomeEngine, smartHomeEngineFiredRules)
+
+ and: "an apartment 3 living room attribute event occurs"
+ def apartment3LivingRoomDemoStringChange = new AttributeEvent(
+ new AttributeState(new AttributeRef(managerDemoSetup.apartment3LivingroomId, "demoString"), Json.create("demo2")), getClass()
+ )
+ assetProcessingService.updateAttributeValue(apartment3LivingRoomDemoStringChange)
+
+ then: "the engines in scope should have fired the matched rules"
+ conditions.eventually {
+ assert globalEngineFiredRules.size() == 2
+ assert globalEngineFiredRules.containsAll(["All", "All changed"])
+ assert customerAEngineFiredRules.size() == 2
+ assert customerAEngineFiredRules.containsAll(["All", "All changed"])
+ assert smartHomeEngineFiredRules.size() == 5
+ assert smartHomeEngineFiredRules.containsAll(["Living Room All", "Current Asset Update", "Parent Type Residence", "Asset Type Room", "String Attributes"])
+ assert apartment3EngineFiredRules.size() == 2
+ assert apartment3EngineFiredRules.containsAll(["All", "All changed"])
+ assert apartment1EngineFiredRules.size() == 0
+ }
+
+ when: "an apartment 1 living room thermostat attribute event occurs"
+ resetRuleExecutionLoggers()
+ def apartment1LivingRoomTargetTempChange = new AttributeEvent(
+ new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomThermostatId, "targetTemperature"), Json.create(22.5)), getClass()
+ )
+ assetProcessingService.updateAttributeValue(apartment1LivingRoomTargetTempChange)
+
+ then: "the engines in scope should have fired the matched rules"
+ conditions.eventually {
+ assert globalEngineFiredRules.size() == 2
+ assert globalEngineFiredRules.containsAll(["All", "All changed"])
+ assert customerAEngineFiredRules.size() == 2
+ assert customerAEngineFiredRules.containsAll(["All", "All changed"])
+ assert smartHomeEngineFiredRules.size() == 5
+ assert smartHomeEngineFiredRules.containsAll(
+ [
+ "Living Room Thermostat",
+ "Living Room Target Temp",
+ "Living Room as Parent",
+ "JSON Number value types",
+ "Current Asset Update"
+ ])
+ assert apartment1EngineFiredRules.size() == 2
+ assert apartment1EngineFiredRules.containsAll(["All", "All changed"])
+ assert apartment3EngineFiredRules.size() == 0
+ }
+
+ when: "a RHS filtering test rule definition is loaded into the global rule engine"
+ rulesDefinition = new GlobalRulesDefinition(
+ "Some global test rules",
+ getClass().getResource("/org/openremote/test/rules/SmartHomePreventAssetUpdate.drl").text
+ )
+ rulesStorageService.merge(rulesDefinition)
+
+ then: "the global rule engine should have loaded the new rule definition and restarted"
+ conditions.eventually {
+ globalEngine = rulesService.globalDeployment
+ assert globalEngine != null
+ assert globalEngine.isRunning()
+ assert globalEngine.allRulesDefinitions.length == 2
+ assert globalEngine.allRulesDefinitions[1].enabled
+ assert globalEngine.allRulesDefinitions[1].name == "Some global test rules"
+ assert globalEngine.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
+ }
+
+ when: "the engine counters are reset and the global engine logger is reattached"
+ resetRuleExecutionLoggers()
+ attachRuleExecutionLogger(globalEngine, globalEngineFiredRules)
+
+ and: "an apartment 1 living room thermostat attribute event occurs"
+ apartment1LivingRoomTargetTempChange = new AttributeEvent(
+ new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomThermostatId, "targetTemperature"), Json.create(22.5)), getClass()
+ )
+ assetProcessingService.updateAttributeValue(apartment1LivingRoomTargetTempChange)
+
+ then: "after a few seconds only the global engine should have fired the All, All changed and Prevent Livingroom Thermostat Change rules"
+ conditions.eventually {
+ assert globalEngineFiredRules.size() == 3
+ assert globalEngineFiredRules.containsAll(["All", "All changed", "Prevent Livingroom Thermostat Change"])
+ assert customerAEngineFiredRules.size() == 0
+ assert smartHomeEngineFiredRules.size() == 0
+ assert apartment1EngineFiredRules.size() == 0
+ assert apartment3EngineFiredRules.size() == 0
+ }
+
+ when: "an apartment 3 living room attribute event occurs"
+ resetRuleExecutionLoggers()
+ apartment3LivingRoomDemoStringChange = new AttributeEvent(
+ new AttributeState(new AttributeRef(managerDemoSetup.apartment3LivingroomId, "demoString"), Json.create("demo3")), getClass()
+ )
+ assetProcessingService.updateAttributeValue(apartment3LivingRoomDemoStringChange)
+
+ then: "all the engines in scope should have fired the matched rules"
+ conditions.eventually {
+ assert globalEngineFiredRules.size() == 2
+ assert globalEngineFiredRules.containsAll(["All", "All changed"])
+ assert customerAEngineFiredRules.size() == 2
+ assert customerAEngineFiredRules.containsAll(["All", "All changed"])
+ assert smartHomeEngineFiredRules.size() == 5
+ assert smartHomeEngineFiredRules.containsAll(["Living Room All", "Current Asset Update", "Parent Type Residence", "Asset Type Room", "String Attributes"])
+ assert apartment3EngineFiredRules.size() == 2
+ assert apartment3EngineFiredRules.containsAll(["All", "All changed"])
+ assert apartment1EngineFiredRules.size() == 0
+ }
+
+ when: "a Kitchen room asset is inserted into apartment 1 that contains a RULES_FACT = true meta flag"
+ resetRuleExecutionLoggers()
+ def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id)
+ def asset = new ServerAsset(apartment1)
+ asset.setRealmId(keycloakDemoSetup.customerATenant.getId())
+ asset.setType(AssetType.ROOM)
+ asset.setName("Kitchen")
+ Attributes attributes = new Attributes()
+ attributes.put(
+ new Attribute("testString", AttributeType.STRING, Json.create("test"))
+ .setMeta(
+ new Meta()
+ .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
+ )
+ )
+ asset.setAttributes(attributes.getJsonObject())
+ asset = assetStorageService.merge(asset)
+
+ then: "the engines in scope should have fired the matched rules (Kitchen All should fire)"
+ conditions.eventually {
+ assert globalEngineFiredRules.size() == 2
+ assert globalEngineFiredRules.containsAll(["All", "All changed"])
+ assert customerAEngineFiredRules.size() == 2
+ assert customerAEngineFiredRules.containsAll(["All", "All changed"])
+ assert smartHomeEngineFiredRules.size() == 4
+ assert smartHomeEngineFiredRules.containsAll(["Kitchen All", "Parent Type Residence", "Asset Type Room", "String Attributes"])
+ assert apartment1EngineFiredRules.size() == 2
+ assert apartment1EngineFiredRules.containsAll(["All", "All changed"])
+ assert apartment3EngineFiredRules.size() == 0
+ }
+
+ when: "the Kitchen room asset is modified to add a new attribute but RULES_FACT = true meta is not changed"
+ resetRuleExecutionLoggers()
+ attributes = new Attributes()
+ attributes.put(
+ new Attribute("testString", AttributeType.STRING, Json.create("test"))
+ .setMeta(
+ new Meta()
+ .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
+ ),
+ new Attribute("testInteger", AttributeType.INTEGER, Json.create(0))
+ )
+ asset.setAttributes(attributes.getJsonObject())
+ def factCount = smartHomeEngine.facts.size()
+ asset = assetStorageService.merge(asset)
+
+ then: "after a few seconds the fact count shouldn't change"
+ new PollingConditions(initialDelay: 3).eventually {
+ assert smartHomeEngine.facts.size() == factCount
+ }
+
+ when: "the Kitchen room asset is modified to set the RULES_FACT to false"
+ attributes = new Attributes()
+ attributes.put(
+ new Attribute("testString", AttributeType.STRING, Json.create("test"))
+ .setMeta(
+ new Meta()
+ .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(false)))
+ ),
+ new Attribute("testInteger", AttributeType.INTEGER, Json.create(0))
+ )
+ asset.setAttributes(attributes.getJsonObject())
+ factCount = smartHomeEngine.facts.size()
+ asset = assetStorageService.merge(asset)
+
+ then: "the facts should be removed from the rule engines"
+ conditions.eventually {
+ assert smartHomeEngine.facts.size() == factCount - 1
+ }
+
+ when: "the Kitchen room asset is modified to set all attributes to RULES_FACT = true"
+ resetRuleExecutionLoggers()
+ attributes = new Attributes()
+ attributes.put(
+ new Attribute("testString", AttributeType.STRING, Json.create("test"))
+ .setMeta(
+ new Meta()
+ .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
+ ),
+ new Attribute("testInteger", AttributeType.INTEGER, Json.create(0))
+ .setMeta(
+ new Meta()
+ .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
+ )
+ )
+ asset.setAttributes(attributes.getJsonObject())
+ asset = assetStorageService.merge(asset)
+
+ then: "the engines in scope should have fired the matched rules twice (Kitchen All should fire twice Kitchen Integer should have fired once)"
+ conditions.eventually {
+ assert globalEngineFiredRules.size() == 4
+ assert globalEngineFiredRules.containsAll(["All", "All changed"])
+ assert customerAEngineFiredRules.size() == 4
+ assert customerAEngineFiredRules.containsAll(["All", "All changed"])
+ assert smartHomeEngineFiredRules.size() == 9
+ assert smartHomeEngineFiredRules.containsAll(["Kitchen All", "Parent Type Residence", "Asset Type Room", "String Attributes", "Kitchen Integer Attributes"])
+ assert apartment1EngineFiredRules.size() == 4
+ assert apartment1EngineFiredRules.containsAll(["All", "All changed"])
+ assert apartment3EngineFiredRules.size() == 0
+ }
+
+ when: "the Kitchen room asset is deleted"
+ resetRuleExecutionLoggers()
+ factCount = smartHomeEngine.facts.size()
+ assetStorageService.delete(asset.getId())
+
+ then: "the facts should be removed from the rule engines"
+ conditions.eventually {
+ assert smartHomeEngine.facts.size() == factCount - 2
+ }
+
+ cleanup: "the server should be stopped"
+ stopContainer(container)
+ }
+}
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/RulesResourceTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesResourceTest.groovy
similarity index 92%
rename from manager/test/src/test/groovy/org/openremote/test/rules/RulesResourceTest.groovy
rename to manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesResourceTest.groovy
index e46784bc3..c2d27f7a9 100644
--- a/manager/test/src/test/groovy/org/openremote/test/rules/RulesResourceTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesResourceTest.groovy
@@ -15,66 +15,7 @@ import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KE
import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KEYCLOAK_ADMIN_PASSWORD_DEFAULT
import static org.openremote.model.Constants.*
-class RulesResourceTest extends Specification implements ManagerContainerTrait {
-
- def importTestRulesets(RulesStorageService rulesStorageService, KeycloakDemoSetup keycloakDemoSetup, ManagerDemoSetup managerDemoSetup) {
- RulesDefinition rulesDefinition = new GlobalRulesDefinition(
- "Some global demo rules",
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new GlobalRulesDefinition(
- "Other global demo rules with a long name that should fill up space in UI",
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesDefinition.setEnabled(false)
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new TenantRulesDefinition(
- "Some master tenant demo rules",
- keycloakDemoSetup.masterTenant.id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new TenantRulesDefinition(
- "Some customerA tenant demo rules",
- keycloakDemoSetup.customerATenant.id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new TenantRulesDefinition(
- "Some customerB tenant demo rules",
- keycloakDemoSetup.customerBTenant.id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesDefinition.setEnabled(false)
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new AssetRulesDefinition(
- "Some apartment 1 demo rules",
- managerDemoSetup.apartment1Id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new AssetRulesDefinition(
- "Some apartment 2 demo rules",
- managerDemoSetup.apartment2Id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesDefinition.setEnabled(false)
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new AssetRulesDefinition(
- "Some apartment 3 demo rules",
- managerDemoSetup.apartment3Id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesStorageService.merge(rulesDefinition)
- }
+class BasicRulesResourceTest extends Specification implements ManagerContainerTrait {
def "Access rules as superuser"() {
given: "the server container is started"
@@ -85,7 +26,7 @@ class RulesResourceTest extends Specification implements ManagerContainerTrait {
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
and: "some test rulesets have been imported"
- importTestRulesets(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
+ new BasicRulesImport(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
and: "an authenticated admin user"
def accessToken = authenticate(
@@ -351,7 +292,7 @@ class RulesResourceTest extends Specification implements ManagerContainerTrait {
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
and: "some imported rulesets"
- importTestRulesets(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
+ new BasicRulesImport(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
and: "an authenticated test user"
def accessToken = authenticate(
@@ -577,7 +518,7 @@ class RulesResourceTest extends Specification implements ManagerContainerTrait {
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
and: "some imported rulesets"
- importTestRulesets(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
+ new BasicRulesImport(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
and: "an authenticated test user"
def accessToken = authenticate(
@@ -721,7 +662,7 @@ class RulesResourceTest extends Specification implements ManagerContainerTrait {
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
and: "some imported rulesets"
- importTestRulesets(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
+ new BasicRulesImport(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
and: "an authenticated test user"
def accessToken = authenticate(
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/RulesDeploymentTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/RulesDeploymentTest.groovy
deleted file mode 100644
index 7c09903f5..000000000
--- a/manager/test/src/test/groovy/org/openremote/test/rules/RulesDeploymentTest.groovy
+++ /dev/null
@@ -1,952 +0,0 @@
-package org.openremote.test.rules
-
-import elemental.json.Json
-import org.kie.api.event.rule.AfterMatchFiredEvent
-import org.kie.api.event.rule.DefaultAgendaEventListener
-import org.openremote.manager.server.asset.AssetProcessingService
-import org.openremote.manager.server.asset.ServerAsset
-import org.openremote.manager.server.rules.RulesDeployment
-import org.openremote.manager.server.rules.RulesService
-import org.openremote.manager.server.rules.RulesStorageService
-import org.openremote.manager.server.asset.AssetStorageService
-import org.openremote.model.Attribute
-import org.openremote.model.Attributes
-import org.openremote.model.AttributeType
-import org.openremote.model.Meta
-import org.openremote.model.MetaItem
-import org.openremote.model.asset.AssetType
-import org.openremote.model.asset.AssetMeta
-import org.openremote.manager.server.security.ManagerIdentityService
-import org.openremote.manager.server.setup.SetupService
-import org.openremote.manager.server.setup.builtin.KeycloakDemoSetup
-import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
-import org.openremote.manager.shared.rules.AssetRulesDefinition
-import org.openremote.manager.shared.rules.GlobalRulesDefinition
-import org.openremote.manager.shared.rules.RulesDefinition
-import org.openremote.manager.shared.rules.RulesDefinition.DeploymentStatus
-import org.openremote.manager.shared.rules.TenantRulesDefinition
-import org.openremote.model.AttributeEvent
-import org.openremote.model.AttributeRef
-import org.openremote.model.AttributeState
-import org.openremote.test.ManagerContainerTrait
-import spock.lang.Specification
-import spock.util.concurrent.PollingConditions
-
-import static org.openremote.container.util.MapAccess.getString
-import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KEYCLOAK_ADMIN_PASSWORD
-import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KEYCLOAK_ADMIN_PASSWORD_DEFAULT
-import static org.openremote.model.Constants.*
-
-// TODO: Add start/end processing flag to Rules Service that can be used here rather than using pollingConditions initialDelay for testing rules not fired
-class RulesDeploymentTest extends Specification implements ManagerContainerTrait {
-
- def customerBRulesDefinitionId
- def apartment1RulesDefinitionId
-
- def importTestRulesets(RulesStorageService rulesStorageService, KeycloakDemoSetup keycloakDemoSetup, ManagerDemoSetup managerDemoSetup) {
- RulesDefinition rulesDefinition = new GlobalRulesDefinition(
- "Some global demo rules",
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new GlobalRulesDefinition(
- "Other global demo rules with a long name that should fill up space in UI",
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesDefinition.setEnabled(false)
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new TenantRulesDefinition(
- "Some master tenant demo rules",
- keycloakDemoSetup.masterTenant.id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new TenantRulesDefinition(
- "Some customerA tenant demo rules",
- keycloakDemoSetup.customerATenant.id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new TenantRulesDefinition(
- "Some customerB tenant demo rules",
- keycloakDemoSetup.customerBTenant.id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesDefinition.setEnabled(false)
- customerBRulesDefinitionId = rulesStorageService.merge(rulesDefinition).id
-
- rulesDefinition = new AssetRulesDefinition(
- "Some apartment 1 demo rules",
- managerDemoSetup.apartment1Id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- apartment1RulesDefinitionId = rulesStorageService.merge(rulesDefinition).id
-
- rulesDefinition = new AssetRulesDefinition(
- "Some apartment 2 demo rules",
- managerDemoSetup.apartment2Id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesDefinition.setEnabled(false)
- rulesStorageService.merge(rulesDefinition)
-
- rulesDefinition = new AssetRulesDefinition(
- "Some apartment 3 demo rules",
- managerDemoSetup.apartment3Id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates.drl").text
- )
- rulesStorageService.merge(rulesDefinition)
- }
-
- def "Check basic rules engine deployment"() {
-
- given: "expected conditions"
- def conditions = new PollingConditions(timeout: 10)
-
- and: "the container is started"
- def serverPort = findEphemeralPort()
- def container = startContainer(defaultConfig(serverPort), defaultServices())
- def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
- def keycloakDemoSetup = container.getService(SetupService.class).getTaskOfType(KeycloakDemoSetup.class)
- def rulesService = container.getService(RulesService.class)
- def identityService = container.getService(ManagerIdentityService.class)
- def rulesStorageService = container.getService(RulesStorageService.class)
-
- and: "some test rulesets have been imported"
- importTestRulesets(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
-
- and: "an authenticated user"
- def accessToken = authenticate(
- container,
- MASTER_REALM,
- KEYCLOAK_CLIENT_ID,
- MASTER_REALM_ADMIN_USER,
- getString(container.getConfig(), SETUP_KEYCLOAK_ADMIN_PASSWORD, SETUP_KEYCLOAK_ADMIN_PASSWORD_DEFAULT)
- ).token
-
- expect: "a global rules engine should have been created and be running"
- conditions.eventually {
- assert rulesService.globalDeployment != null
- assert rulesService.globalDeployment.isRunning()
- }
-
- and: "the global rules engine should contain the global demo rules definition"
- conditions.eventually {
- assert rulesService.globalDeployment.allRulesDefinitions.length == 1
- assert rulesService.globalDeployment.allRulesDefinitions[0].enabled
- assert rulesService.globalDeployment.allRulesDefinitions[0].name == "Some global demo rules"
- assert rulesService.globalDeployment.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- and: "two tenant rules engines should have been created and be running"
- conditions.eventually {
- assert rulesService.tenantDeployments.size() == 2
- def masterEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.masterTenant.id)
- def customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
- assert masterEngine != null
- assert masterEngine.isRunning()
- assert customerAEngine != null
- assert customerAEngine.isRunning()
- }
-
- and: "the tenant rules engines should have the demo tenant rules definition"
- conditions.eventually {
- def masterEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.masterTenant.id)
- def customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
- assert masterEngine.allRulesDefinitions.length == 1
- assert masterEngine.allRulesDefinitions[0].enabled
- assert masterEngine.allRulesDefinitions[0].name == "Some master tenant demo rules"
- assert masterEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- assert customerAEngine.allRulesDefinitions.length == 1
- assert customerAEngine.allRulesDefinitions[0].enabled
- assert customerAEngine.allRulesDefinitions[0].name == "Some customerA tenant demo rules"
- assert customerAEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- and: "two asset rules engines should have been created and be running"
- conditions.eventually {
- assert rulesService.assetDeployments.size() == 2
- def apartment1Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment1Id)
- def apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
- assert apartment1Engine != null
- assert apartment1Engine.isRunning()
- assert apartment3Engine != null
- assert apartment3Engine.isRunning()
- }
-
- and: "each asset rules engine should have the demo asset rules definition"
- conditions.eventually {
- def apartment1Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment1Id)
- def apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
- assert apartment1Engine.allRulesDefinitions.length == 1
- assert apartment1Engine.allRulesDefinitions[0].enabled
- assert apartment1Engine.allRulesDefinitions[0].name == "Some apartment 1 demo rules"
- assert apartment1Engine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- assert apartment3Engine.allRulesDefinitions.length == 1
- assert apartment3Engine.allRulesDefinitions[0].enabled
- assert apartment3Engine.allRulesDefinitions[0].name == "Some apartment 3 demo rules"
- assert apartment3Engine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- when: "a new global rule definition is added"
- conditions = new PollingConditions(timeout: 10)
- def rulesDefinition = new GlobalRulesDefinition(
- "Some more global rules",
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates2.drl").text
- )
- rulesDefinition = rulesStorageService.merge(rulesDefinition)
-
- then: "the global rules engine should load this definition and restart successfully"
- conditions.eventually {
- assert rulesService.globalDeployment != null
- assert rulesService.globalDeployment.isRunning()
- assert rulesService.globalDeployment.allRulesDefinitions.length == 2
- assert rulesService.globalDeployment.allRulesDefinitions[0].enabled
- assert rulesService.globalDeployment.allRulesDefinitions[0].name == "Some global demo rules"
- assert rulesService.globalDeployment.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- assert rulesService.globalDeployment.allRulesDefinitions[1].enabled
- assert rulesService.globalDeployment.allRulesDefinitions[1].name == "Some more global rules"
- assert rulesService.globalDeployment.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- when: "a new tenant rule definition is added to customer A"
- conditions = new PollingConditions(timeout: 10)
- rulesDefinition = new TenantRulesDefinition(
- "Some more customerA tenant rules",
- keycloakDemoSetup.customerATenant.id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates2.drl").text
- )
- rulesDefinition = rulesStorageService.merge(rulesDefinition)
-
- then: "customer A rules engine should load this definition and restart successfully"
- conditions.eventually {
- def customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
- assert customerAEngine != null
- assert customerAEngine.isRunning()
- assert customerAEngine.allRulesDefinitions.length == 2
- assert customerAEngine.allRulesDefinitions[0].enabled
- assert customerAEngine.allRulesDefinitions[0].name == "Some customerA tenant demo rules"
- assert customerAEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- assert customerAEngine.allRulesDefinitions[1].enabled
- assert customerAEngine.allRulesDefinitions[1].name == "Some more customerA tenant rules"
- assert customerAEngine.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- when: "a new tenant rule definition is added to customer B"
- conditions = new PollingConditions(timeout: 10)
- rulesDefinition = new TenantRulesDefinition(
- "Some more customerB tenant rules",
- keycloakDemoSetup.customerBTenant.id,
- getClass().getResource("/org/openremote/test/rules/MatchAllAssetUpdates2.drl").text
- )
- rulesDefinition = rulesStorageService.merge(rulesDefinition)
-
- then: "a tenant rules engine should be created for customer B and load this definition and start successfully"
- conditions.eventually {
- def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
- assert rulesService.tenantDeployments.size() == 3
- assert customerBEngine != null
- assert customerBEngine.isRunning()
- assert customerBEngine.allRulesDefinitions.length == 1
- assert customerBEngine.allRulesDefinitions[0].enabled
- assert customerBEngine.allRulesDefinitions[0].name == "Some more customerB tenant rules"
- assert customerBEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- when: "the disabled rule definition for customer B is enabled"
- conditions = new PollingConditions(timeout: 10)
- rulesDefinition = rulesStorageService.findById(TenantRulesDefinition.class, customerBRulesDefinitionId)
- rulesDefinition.setEnabled(true)
- rulesDefinition = rulesStorageService.merge(rulesDefinition)
-
- then: "customer B rule engine should load this definition and restart successfully"
- conditions.eventually {
- def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
- assert rulesService.tenantDeployments.size() == 3
- assert customerBEngine != null
- assert customerBEngine.isRunning()
- assert customerBEngine.allRulesDefinitions.length == 2
- assert customerBEngine.allRulesDefinitions[0].enabled
- assert customerBEngine.allRulesDefinitions[0].name == "Some more customerB tenant rules"
- assert customerBEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- assert customerBEngine.allRulesDefinitions[1].enabled
- assert customerBEngine.allRulesDefinitions[1].name == "Some customerB tenant demo rules"
- assert customerBEngine.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- when: "the enabled rule definition for customer B is disabled"
- conditions = new PollingConditions(timeout: 10)
- // TODO: Stop instances of rule definitions being passed around as rules deployment nulls the rules property
- rulesDefinition = rulesStorageService.findById(TenantRulesDefinition.class, customerBRulesDefinitionId)
- rulesDefinition.setEnabled(false)
- rulesDefinition = rulesStorageService.merge(rulesDefinition)
-
- then: "customer B rule engine should remove it again"
- conditions.eventually {
- def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
- assert customerBEngine != null
- assert customerBEngine.isRunning()
- assert customerBEngine.allRulesDefinitions.length == 1
- assert customerBEngine.allRulesDefinitions[0].enabled
- assert customerBEngine.allRulesDefinitions[0].name == "Some more customerB tenant rules"
- assert customerBEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- when: "the asset rule definition for apartment 1 is deleted"
- conditions = new PollingConditions(timeout: 10)
- rulesStorageService.delete(AssetRulesDefinition.class, apartment1RulesDefinitionId)
-
- then: "the apartment rules engine should be removed"
- conditions.eventually {
- assert rulesService.assetDeployments.size() == 1
- def apartment1Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment1Id)
- def apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
- assert apartment1Engine == null
- assert apartment3Engine != null
- assert apartment3Engine.isRunning()
- }
-
- when: "a broken rule definition is added to the global rules engine"
- conditions = new PollingConditions(timeout: 10)
- rulesDefinition = new GlobalRulesDefinition(
- "Some broken global rules",
- getClass().getResource("/org/openremote/test/rules/BrokenRules.drl").text
- )
- rulesDefinition = rulesStorageService.merge(rulesDefinition)
-
- then: "the global rules engine should not run and the rule deployment status should indicate the issue"
- conditions.eventually {
- assert rulesService.globalDeployment.allRulesDefinitions.length == 3
- assert rulesService.globalDeployment.running == false
- assert rulesService.globalDeployment.error == true
- assert rulesService.globalDeployment.allRulesDefinitions[0].enabled
- assert rulesService.globalDeployment.allRulesDefinitions[0].name == "Some global demo rules"
- assert rulesService.globalDeployment.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.READY
- assert rulesService.globalDeployment.allRulesDefinitions[1].enabled
- assert rulesService.globalDeployment.allRulesDefinitions[1].name == "Some more global rules"
- assert rulesService.globalDeployment.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.READY
- assert rulesService.globalDeployment.allRulesDefinitions[2].enabled
- assert rulesService.globalDeployment.allRulesDefinitions[2].name == "Some broken global rules"
- assert rulesService.globalDeployment.allRulesDefinitions[2].deploymentStatus == DeploymentStatus.FAILED
- }
-
- when: "the broken rule definition is removed from the global engine"
- conditions = new PollingConditions(timeout: 10)
- rulesStorageService.delete(GlobalRulesDefinition.class, rulesDefinition.getId())
-
- then: "the global rules engine should restart"
- conditions.eventually {
- assert rulesService.globalDeployment.allRulesDefinitions.length == 2
- assert rulesService.globalDeployment.running == true
- assert rulesService.globalDeployment.error == false
- assert rulesService.globalDeployment.allRulesDefinitions[0].enabled
- assert rulesService.globalDeployment.allRulesDefinitions[0].name == "Some global demo rules"
- assert rulesService.globalDeployment.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- assert rulesService.globalDeployment.allRulesDefinitions[1].enabled
- assert rulesService.globalDeployment.allRulesDefinitions[1].name == "Some more global rules"
- assert rulesService.globalDeployment.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- when: "a tenant is disabled"
- conditions = new PollingConditions(timeout: 10)
- def customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
- def apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
- def customerATenant = keycloakDemoSetup.customerATenant
- customerATenant.setEnabled(false)
- identityService.updateTenant(accessToken, customerATenant.getRealm(), customerATenant)
-
- then: "the tenants rule engine should stop and all asset rule engines in this realm should also stop"
- conditions.eventually {
- assert customerAEngine.isRunning() == false
- assert customerAEngine.allRulesDefinitions.length == 0
- assert rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id) == null
- assert apartment3Engine.isRunning() == false
- assert apartment3Engine.allRulesDefinitions.length == 0
- assert rulesService.assetDeployments.get(managerDemoSetup.apartment3Id) == null
- }
-
- and: "other rule engines should be unaffected"
- conditions.eventually {
- assert rulesService.tenantDeployments.size() == 2
- assert rulesService.assetDeployments.size() == 0
- def masterEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.masterTenant.id)
- def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
- assert masterEngine != null
- assert masterEngine.isRunning()
- assert customerBEngine != null
- assert customerBEngine.isRunning()
- }
-
- when: "the disabled tenant is re-enabled"
- conditions = new PollingConditions(timeout: 10)
- customerATenant.setEnabled(true)
- identityService.updateTenant(accessToken, customerATenant.getRealm(), customerATenant)
-
- then: "the tenants rule engine should start and all asset rule engines from this realm should also start"
- conditions.eventually {
- customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
- apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
- assert rulesService.tenantDeployments.size() == 3
- assert rulesService.assetDeployments.size() == 1
- assert customerAEngine != null
- assert customerAEngine.isRunning()
- assert customerAEngine.allRulesDefinitions.length == 2
- assert customerAEngine.allRulesDefinitions[0].enabled
- assert customerAEngine.allRulesDefinitions[0].name == "Some customerA tenant demo rules"
- assert customerAEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- assert customerAEngine.allRulesDefinitions[1].enabled
- assert customerAEngine.allRulesDefinitions[1].name == "Some more customerA tenant rules"
- assert customerAEngine.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
- assert apartment3Engine.allRulesDefinitions.length == 1
- assert apartment3Engine.allRulesDefinitions[0].enabled
- assert apartment3Engine.allRulesDefinitions[0].name == "Some apartment 3 demo rules"
- assert apartment3Engine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- }
-
- then: "a new fact should be inserted into the engines in scope for the new RULES_FACT attribute which will cause the rules to fire"
-
-//TODO: Reinstate the tenant delete test once tenant delete mechanism is finalised
-// when: "a tenant is deleted"
-// identityService.deleteTenant(accessToken, customerATenant.getRealm())
-//
-// then: "the tenants rule engine should stop and all asset rule engines in this realm should also stop"
-// conditions.eventually {
-// assert customerAEngine.isRunning() == false
-// assert customerAEngine.allRulesDefinitions.length == 0
-// assert rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id) == null
-// assert smartHomeEngine.isRunning() == false
-// assert smartHomeEngine.allRulesDefinitions.length == 0
-// assert rulesService.assetDeployments.get(managerDemoSetup.smartHomeId) == null
-// assert apartment3Engine.isRunning() == false
-// assert apartment3Engine.allRulesDefinitions.length == 0
-// assert rulesService.assetDeployments.get(managerDemoSetup.apartment3Id) == null
-// }
-//
-// and: "other rule engines should be unaffected"
-// conditions.eventually {
-// assert rulesService.tenantDeployments.size() == 2
-// assert rulesService.assetDeployments.size() == 0
-// def masterEngine = rulesService.tenantDeployments.get(Constants.MASTER_REALM)
-// def customerBEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerBTenant.id)
-// assert masterEngine != null
-// assert masterEngine.isRunning()
-// assert customerBEngine != null
-// assert customerBEngine.isRunning()
-// }
-
- cleanup: "the server should be stopped"
- stopContainer(container)
- }
-
- def "Check firing of rules"() {
- given: "expected conditions"
- def conditions = new PollingConditions(timeout: 10)
-
- and: "the container is started"
- def serverPort = findEphemeralPort()
- def container = startContainer(defaultConfig(serverPort), defaultServices())
- def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
- def keycloakDemoSetup = container.getService(SetupService.class).getTaskOfType(KeycloakDemoSetup.class)
- def rulesService = container.getService(RulesService.class)
- def rulesStorageService = container.getService(RulesStorageService.class)
- def assetProcessingService = container.getService(AssetProcessingService.class)
- def assetStorageService = container.getService(AssetStorageService.class)
- RulesDeployment globalEngine, masterEngine, customerAEngine, smartHomeEngine, apartment1Engine, apartment3Engine
- List<String> globalEngineFiredRules = []
- List<String> masterEngineFiredRules = []
- List<String> customerAEngineFiredRules = []
- List<String> smartHomeEngineFiredRules = []
- List<String> apartment1EngineFiredRules = []
- List<String> apartment3EngineFiredRules = []
-
- and: "some test rulesets have been imported"
- importTestRulesets(rulesStorageService, keycloakDemoSetup, managerDemoSetup)
-
- //region expect: "the rule engines to become available and be running"
- expect: "the rule engines to become available and be running"
- conditions.eventually {
- globalEngine = rulesService.globalDeployment
- assert globalEngine != null
- assert globalEngine.isRunning()
- masterEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.masterTenant.id)
- assert masterEngine != null
- assert masterEngine.isRunning()
- customerAEngine = rulesService.tenantDeployments.get(keycloakDemoSetup.customerATenant.id)
- assert customerAEngine != null
- assert customerAEngine.isRunning()
- smartHomeEngine = rulesService.assetDeployments.get(managerDemoSetup.smartHomeId)
- assert smartHomeEngine == null
- apartment1Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment1Id)
- assert apartment1Engine != null
- assert apartment1Engine.isRunning()
- apartment3Engine = rulesService.assetDeployments.get(managerDemoSetup.apartment3Id)
- assert apartment3Engine != null
- assert apartment3Engine.isRunning()
- }
- //endregion
-
- and: "the demo attributes marked with RULES_FACT = true meta should be inserted into the engines"
- conditions.eventually {
- globalEngine.facts.size() == 6
- }
-
- //region when: "rule execution loggers are attached to the engines"
- when: "rule execution loggers are attached to the engines"
- attachRuleExecutionLogger(globalEngine, globalEngineFiredRules)
- attachRuleExecutionLogger(masterEngine, masterEngineFiredRules)
- attachRuleExecutionLogger(customerAEngine, customerAEngineFiredRules)
- attachRuleExecutionLogger(apartment1Engine, apartment1EngineFiredRules)
- attachRuleExecutionLogger(apartment3Engine, apartment3EngineFiredRules)
- //endregion
-
- //region and: "an attribute event is pushed into the system for an attribute with RULES_FACT meta set to true"
- and: "an attribute event is pushed into the system for an attribute with RULES_FACT meta set to true"
- def apartment1LivingRoomDemoBooleanChange = new AttributeEvent(
- new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoBoolean"), Json.create(false)), getClass()
- )
- assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoBooleanChange)
- //endregion
-
- //region then: "the rule engines in scope should fire the 'All' and 'All changed' rules"
- then: "the rule engines in scope should fire the 'All' and 'All changed' rules"
- conditions.eventually {
- def expectedFiredRules = ["All", "All changed"]
- assert globalEngineFiredRules.size() == 2
- assert globalEngineFiredRules.containsAll(expectedFiredRules)
- assert masterEngineFiredRules.size() == 0
- assert customerAEngineFiredRules.size() == 2
- assert customerAEngineFiredRules.containsAll(expectedFiredRules)
- assert smartHomeEngineFiredRules.size() == 0
- assert apartment1EngineFiredRules.size() == 2
- assert apartment1EngineFiredRules.containsAll(expectedFiredRules)
- assert apartment3EngineFiredRules.size() == 0
- }
- //endregion
-
- //region when: "an attribute event is pushed into the system for an attribute with RULES_FACT meta set to false"
- when: "an attribute event is pushed into the system for an attribute with RULES_FACT meta set to false"
- def apartment1LivingRoomDemoStringChange = new AttributeEvent(
- new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoString"), Json.create("demo2")), getClass()
- )
- assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoStringChange)
- //endregion
-
- //region then: "no rule engines should have fired after a few seconds"
- then: "no rule engines should have fired after a few seconds"
- new PollingConditions(delay: 3, timeout: 5).eventually {
- assert globalEngineFiredRules.size() == 2
- assert masterEngineFiredRules.size() == 0
- assert customerAEngineFiredRules.size() == 2
- assert smartHomeEngineFiredRules.size() == 0
- assert apartment1EngineFiredRules.size() == 2
- assert apartment3EngineFiredRules.size() == 0
- }
- //endregion
-
- //region when: "an attribute event is pushed into the system for an attribute with no RULES_FACT meta"
- when: "an attribute event is pushed into the system for an attribute with no RULES_FACT meta"
- def apartment1LivingRoomDemoIntegerChange = new AttributeEvent(
- new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoInteger"), Json.create(1)), getClass()
- )
- assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoIntegerChange)
- //endregion
-
- //region then: "no rule engines should have fired after a few seconds"
- then: "no rule engines should have fired after a few seconds"
- new PollingConditions(delay: 3, timeout: 5).eventually {
- assert globalEngineFiredRules.size() == 2
- assert masterEngineFiredRules.size() == 0
- assert customerAEngineFiredRules.size() == 2
- assert smartHomeEngineFiredRules.size() == 0
- assert apartment1EngineFiredRules.size() == 2
- assert apartment3EngineFiredRules.size() == 0
- }
- //endregion
-
- //region when: "an old (stale) attribute event is pushed into the system"
- when: "an old (stale) attribute event is pushed into the system"
- conditions = new PollingConditions(timeout: 10, initialDelay: 5)
- assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoBooleanChange)
- //endregion
-
- //region then: "no rule engines should have fired after a few seconds"
- then: "no rule engines should have fired after a few seconds"
- new PollingConditions(delay: 3, timeout: 5).eventually {
- assert globalEngineFiredRules.size() == 2
- assert masterEngineFiredRules.size() == 0
- assert customerAEngineFiredRules.size() == 2
- assert smartHomeEngineFiredRules.size() == 0
- assert apartment1EngineFiredRules.size() == 2
- assert apartment3EngineFiredRules.size() == 0
- }
- //endregion
-
- //region when: "the engine counters are reset"
- when: "the engine counters are reset"
- globalEngineFiredRules.clear();
- customerAEngineFiredRules.clear();
- smartHomeEngineFiredRules.clear();
- apartment1EngineFiredRules.clear();
- apartment3EngineFiredRules.clear();
- //endregion
-
- //region and: "an attribute event with the same value as current value is pushed into the system"
- and: "an attribute event with the same value as current value is pushed into the system"
- apartment1LivingRoomDemoBooleanChange = new AttributeEvent(
- new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomId, "demoBoolean"), Json.create(false)), getClass()
- )
- assetProcessingService.updateAttributeValue(apartment1LivingRoomDemoBooleanChange)
- //endregion
-
- //region then: "the rule engines in scope should fire the 'All' rule but not the 'All changed' rule"
- then: "the rule engines in scope should fire the 'All' rule but not the 'All changed' rule"
- conditions.eventually {
- assert globalEngineFiredRules.size() == 1
- assert globalEngineFiredRules[0] == "All"
- assert masterEngineFiredRules.size() == 0
- assert customerAEngineFiredRules.size() == 1
- assert customerAEngineFiredRules[0] == "All"
- assert smartHomeEngineFiredRules.size() == 0
- assert apartment1EngineFiredRules.size() == 1
- assert apartment1EngineFiredRules[0] == "All"
- assert apartment3EngineFiredRules.size() == 0
- }
- //endregion
-
- //region when: "a LHS filtering test rule definition is loaded into the smart home asset"
- when: "a LHS filtering test rule definition is loaded into the smart home asset"
- def rulesDefinition = new AssetRulesDefinition(
- "Some smart home asset rules",
- managerDemoSetup.smartHomeId,
- getClass().getResource("/org/openremote/test/rules/SmartHomeMatchAllAssetUpdates.drl").text
- )
- rulesDefinition = rulesStorageService.merge(rulesDefinition)
- //endregion
-
- //region then: "the smart home rule engine should have been created, loaded the new rule definition and started"
- then: "the smart home rule engine should have ben created, loaded the new rule definition and started"
- conditions.eventually {
- smartHomeEngine = rulesService.assetDeployments.get(managerDemoSetup.smartHomeId)
- assert smartHomeEngine != null
- assert smartHomeEngine.isRunning()
- assert smartHomeEngine.allRulesDefinitions.length == 1
- assert smartHomeEngine.allRulesDefinitions[0].enabled
- assert smartHomeEngine.allRulesDefinitions[0].name == "Some smart home asset rules"
- assert smartHomeEngine.allRulesDefinitions[0].deploymentStatus == DeploymentStatus.DEPLOYED
- }
- //endregion
-
- //region when: "the engine counters are reset and the smart home engine logger is attached"
- when: "the engine counters are reset and the smart home engine logger is attached"
- globalEngineFiredRules.clear();
- customerAEngineFiredRules.clear();
- smartHomeEngineFiredRules.clear();
- apartment1EngineFiredRules.clear();
- apartment3EngineFiredRules.clear();
- attachRuleExecutionLogger(smartHomeEngine, smartHomeEngineFiredRules)
- //endregion
-
- //region and: "an apartment 3 living room attribute event occurs"
- and: "an apartment 3 living room attribute event occurs"
- def apartment3LivingRoomDemoStringChange = new AttributeEvent(
- new AttributeState(new AttributeRef(managerDemoSetup.apartment3LivingroomId, "demoString"), Json.create("demo2")), getClass()
- )
- assetProcessingService.updateAttributeValue(apartment3LivingRoomDemoStringChange)
- //endregion
-
- //region then: "the engines in scope should have fired the matched rules"
- then: "the engines in scope should have fired the matched rules"
- conditions.eventually {
- assert globalEngineFiredRules.size() == 2
- assert globalEngineFiredRules.containsAll(["All", "All changed"])
- assert customerAEngineFiredRules.size() == 2
- assert customerAEngineFiredRules.containsAll(["All", "All changed"])
- assert smartHomeEngineFiredRules.size() == 5
- assert smartHomeEngineFiredRules.containsAll(["Living Room All", "Current Asset Update", "Parent Type Residence", "Asset Type Room", "String Attributes"])
- assert apartment3EngineFiredRules.size() == 2
- assert apartment3EngineFiredRules.containsAll(["All", "All changed"])
- assert apartment1EngineFiredRules.size() == 0
- }
- //endregion
-
- //region when: "the rule counters are reset"
- when: "the rule counters are reset"
- globalEngineFiredRules.clear();
- customerAEngineFiredRules.clear();
- smartHomeEngineFiredRules.clear();
- apartment1EngineFiredRules.clear();
- apartment3EngineFiredRules.clear();
- //endregion
-
- //region and: "an apartment 1 living room thermostat attribute event occurs"
- and: "an apartment 1 living room thermostat attribute event occurs"
- def apartment1LivingRoomTargetTempChange = new AttributeEvent(
- new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomThermostatId, "targetTemperature"), Json.create(22.5)), getClass()
- )
- assetProcessingService.updateAttributeValue(apartment1LivingRoomTargetTempChange)
- //endregion
-
- //region then: "the engines in scope should have fired the matched rules"
- then: "the engines in scope should have fired the matched rules"
- conditions.eventually {
- assert globalEngineFiredRules.size() == 2
- assert globalEngineFiredRules.containsAll(["All", "All changed"])
- assert customerAEngineFiredRules.size() == 2
- assert customerAEngineFiredRules.containsAll(["All", "All changed"])
- assert smartHomeEngineFiredRules.size() == 5
- assert smartHomeEngineFiredRules.containsAll(
- [
- "Living Room Thermostat",
- "Living Room Target Temp",
- "Living Room as Parent",
- "JSON Number value types",
- "Current Asset Update"
- ])
- assert apartment1EngineFiredRules.size() == 2
- assert apartment1EngineFiredRules.containsAll(["All", "All changed"])
- assert apartment3EngineFiredRules.size() == 0
- }
- //endregion
-
- //region when: "a RHS filtering test rule definition is loaded into the global rule engine"
- when: "a RHS filtering test rule definition is loaded into the global rule engine"
- rulesDefinition = new GlobalRulesDefinition(
- "Some global test rules",
- getClass().getResource("/org/openremote/test/rules/SmartHomePreventAssetUpdate.drl").text
- )
- rulesDefinition = rulesStorageService.merge(rulesDefinition)
- //endregion
-
- //region then: "the global rule engine should have loaded the new rule definition and restarted"
- then: "the global rule engine should have loaded the new rule definition and restarted"
- conditions.eventually {
- globalEngine = rulesService.globalDeployment
- assert globalEngine != null
- assert globalEngine.isRunning()
- assert globalEngine.allRulesDefinitions.length == 2
- assert globalEngine.allRulesDefinitions[1].enabled
- assert globalEngine.allRulesDefinitions[1].name == "Some global test rules"
- assert globalEngine.allRulesDefinitions[1].deploymentStatus == DeploymentStatus.DEPLOYED
- }
- //endregion
-
- //region when: "the engine counters are reset and the global engine logger is reattached"
- when: "the engine counters are reset and the global engine logger is reattached"
- globalEngineFiredRules.clear();
- customerAEngineFiredRules.clear();
- smartHomeEngineFiredRules.clear();
- apartment1EngineFiredRules.clear();
- apartment3EngineFiredRules.clear();
- attachRuleExecutionLogger(globalEngine, globalEngineFiredRules)
- //endregion
-
- //region and: "an apartment 1 living room thermostat attribute event occurs"
- and: "an apartment 1 living room thermostat attribute event occurs"
- apartment1LivingRoomTargetTempChange = new AttributeEvent(
- new AttributeState(new AttributeRef(managerDemoSetup.apartment1LivingroomThermostatId, "targetTemperature"), Json.create(22.5)), getClass()
- )
- assetProcessingService.updateAttributeValue(apartment1LivingRoomTargetTempChange)
- //endregion
-
- //region then: "only the global engine should have fired the Prevent Livingroom Thermostat Change rule"
- then: "after a few seconds only the global engine should have fired the All, All changed and Prevent Livingroom Thermostat Change rules"
- new PollingConditions(initialDelay: 5, timeout: 10).eventually {
- assert globalEngineFiredRules.size() == 3
- assert globalEngineFiredRules.containsAll(["All", "All changed", "Prevent Livingroom Thermostat Change"])
- assert customerAEngineFiredRules.size() == 0
- assert smartHomeEngineFiredRules.size() == 0
- assert apartment1EngineFiredRules.size() == 0
- assert apartment3EngineFiredRules.size() == 0
- }
- //endregion
-
- //region when: "the engine counters are reset"
- when: "the engine counters are reset and the global engine logger is reattached"
- globalEngineFiredRules.clear();
- customerAEngineFiredRules.clear();
- smartHomeEngineFiredRules.clear();
- apartment1EngineFiredRules.clear();
- apartment3EngineFiredRules.clear();
- //endregion
-
- //region and: "an apartment 3 living room attribute event occurs"
- and: "an apartment 3 living room attribute event occurs"
- apartment3LivingRoomDemoStringChange = new AttributeEvent(
- new AttributeState(new AttributeRef(managerDemoSetup.apartment3LivingroomId, "demoString"), Json.create("demo3")), getClass()
- )
- assetProcessingService.updateAttributeValue(apartment3LivingRoomDemoStringChange)
- //endregion
-
- //region then: "all the engines in scope should have fired the matched rules"
- then: "all the engines in scope should have fired the matched rules"
- conditions.eventually {
- assert globalEngineFiredRules.size() == 2
- assert globalEngineFiredRules.containsAll(["All", "All changed"])
- assert customerAEngineFiredRules.size() == 2
- assert customerAEngineFiredRules.containsAll(["All", "All changed"])
- assert smartHomeEngineFiredRules.size() == 5
- assert smartHomeEngineFiredRules.containsAll(["Living Room All", "Current Asset Update", "Parent Type Residence", "Asset Type Room", "String Attributes"])
- assert apartment3EngineFiredRules.size() == 2
- assert apartment3EngineFiredRules.containsAll(["All", "All changed"])
- assert apartment1EngineFiredRules.size() == 0
- }
- //endregion
-
- //region when: "the engine counters are reset"
- when: "the engine counters are reset"
- globalEngineFiredRules.clear();
- customerAEngineFiredRules.clear();
- smartHomeEngineFiredRules.clear();
- apartment1EngineFiredRules.clear();
- apartment3EngineFiredRules.clear();
- //endregion
-
- and: "a Kitchen room asset is inserted into apartment 1 that contains a RULES_FACT = true meta flag"
- def apartment1 = assetStorageService.find(managerDemoSetup.apartment1Id)
- def asset = new ServerAsset(apartment1)
- asset.setRealmId(keycloakDemoSetup.customerATenant.getId())
- asset.setType(AssetType.ROOM)
- asset.setName("Kitchen")
- Attributes attributes = new Attributes()
- attributes.put(
- new Attribute("testString", AttributeType.STRING, Json.create("test"))
- .setMeta(
- new Meta()
- .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
- )
- )
- asset.setAttributes(attributes.getJsonObject())
- asset = assetStorageService.merge(asset)
-
- then: "the engines in scope should have fired the matched rules (Kitchen All should fire)"
- conditions.eventually {
- assert globalEngineFiredRules.size() == 2
- assert globalEngineFiredRules.containsAll(["All", "All changed"])
- assert customerAEngineFiredRules.size() == 2
- assert customerAEngineFiredRules.containsAll(["All", "All changed"])
- assert smartHomeEngineFiredRules.size() == 4
- assert smartHomeEngineFiredRules.containsAll(["Kitchen All", "Parent Type Residence", "Asset Type Room", "String Attributes"])
- assert apartment1EngineFiredRules.size() == 2
- assert apartment1EngineFiredRules.containsAll(["All", "All changed"])
- assert apartment3EngineFiredRules.size() == 0
- }
-
- //region when: "the engine counters are reset"
- when: "the engine counters are reset"
- globalEngineFiredRules.clear();
- customerAEngineFiredRules.clear();
- smartHomeEngineFiredRules.clear();
- apartment1EngineFiredRules.clear();
- apartment3EngineFiredRules.clear();
- //endregion
-
- and: "the Kitchen room asset is modified to add a new attribute but RULES_FACT = true meta is not changed"
- attributes = new Attributes()
- attributes.put(
- new Attribute("testString", AttributeType.STRING, Json.create("test"))
- .setMeta(
- new Meta()
- .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
- ),
- new Attribute("testInteger", AttributeType.INTEGER, Json.create(0))
- );
- asset.setAttributes(attributes.getJsonObject())
- def factCount = smartHomeEngine.facts.size()
- asset = assetStorageService.merge(asset)
-
- then: "after a few seconds the fact count shouldn't change"
- new PollingConditions(initialDelay: 3, timeout: 5).eventually {
- assert smartHomeEngine.facts.size() == factCount
- }
-
- when: "the Kitchen room asset is modified to set the RULES_FACT to false"
- attributes = new Attributes()
- attributes.put(
- new Attribute("testString", AttributeType.STRING, Json.create("test"))
- .setMeta(
- new Meta()
- .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(false)))
- ),
- new Attribute("testInteger", AttributeType.INTEGER, Json.create(0))
- );
- asset.setAttributes(attributes.getJsonObject())
- factCount = smartHomeEngine.facts.size()
- asset = assetStorageService.merge(asset)
-
- then: "after a few seconds the facts should be removed from the rule engines"
- conditions.eventually {
- assert smartHomeEngine.facts.size() == factCount - 1
- }
-
- when: "the Kitchen room asset is modified to set all attributes to RULES_FACT = true"
- attributes = new Attributes()
- attributes.put(
- new Attribute("testString", AttributeType.STRING, Json.create("test"))
- .setMeta(
- new Meta()
- .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
- ),
- new Attribute("testInteger", AttributeType.INTEGER, Json.create(0))
- .setMeta(
- new Meta()
- .add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
- )
- );
- asset.setAttributes(attributes.getJsonObject())
- asset = assetStorageService.merge(asset)
-
- then: "the engines in scope should have fired the matched rules twice (Kitchen All should fire twice Kitchen Integer should have fired once)"
- conditions.eventually {
- assert globalEngineFiredRules.size() == 4
- assert globalEngineFiredRules.containsAll(["All", "All changed"])
- assert customerAEngineFiredRules.size() == 4
- assert customerAEngineFiredRules.containsAll(["All", "All changed"])
- assert smartHomeEngineFiredRules.size() == 9
- assert smartHomeEngineFiredRules.containsAll(["Kitchen All", "Parent Type Residence", "Asset Type Room", "String Attributes", "Kitchen Integer Attributes"])
- assert apartment1EngineFiredRules.size() == 4
- assert apartment1EngineFiredRules.containsAll(["All", "All changed"])
- assert apartment3EngineFiredRules.size() == 0
- }
-
- //region when: "the engine counters are reset"
- when: "the engine counters are reset"
- globalEngineFiredRules.clear();
- customerAEngineFiredRules.clear();
- smartHomeEngineFiredRules.clear();
- apartment1EngineFiredRules.clear();
- apartment3EngineFiredRules.clear();
- //endregion
-
- and: "the Kitchen room asset is deleted"
- factCount = smartHomeEngine.facts.size()
- asset = assetStorageService.delete(asset.getId())
-
- then: "after a few seconds the facts should be removed from the rule engines"
- conditions.eventually {
- assert smartHomeEngine.facts.size() == factCount - 2
- }
-
- cleanup: "the server should be stopped"
- stopContainer(container)
- }
-
- def attachRuleExecutionLogger(RulesDeployment ruleEngine, List<String> executedRules) {
- def session = ruleEngine.getKnowledgeSession()
- if (session == null) {
- return
- }
-
- session.addEventListener(new DefaultAgendaEventListener() {
- @Override
- void afterMatchFired(AfterMatchFiredEvent event) {
- def rule = event.getMatch().getRule()
- def ruleName = rule.getName()
- executedRules.add(ruleName)
- }
- })
- }
-}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetQuery.java b/model/src/main/java/org/openremote/model/asset/AssetQuery.java
index 80adc98b2..231c88ede 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetQuery.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetQuery.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016, OpenRemote Inc.
+ * Copyright 2017, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
@@ -39,6 +39,10 @@ public class AssetQuery {
public Select() {
}
+ public Select(boolean loadComplete) {
+ this.loadComplete = loadComplete;
+ }
+
public Select(boolean loadComplete, boolean filterProtected) {
this.loadComplete = loadComplete;
this.filterProtected = filterProtected;