refactored tests

This commit is contained in:
IrynaMatveieva 2025-02-07 17:04:01 +02:00
parent b078f93de5
commit c6f6408c22
2 changed files with 78 additions and 87 deletions

View File

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2024 The Thingsboard Authors * Copyright © 2016-2024 The Thingsboard Authors
* <p> *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* <p> *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* <p> *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

View File

@ -15,7 +15,6 @@
*/ */
package org.thingsboard.server.cf; package org.thingsboard.server.cf;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Test; import org.junit.Test;
@ -35,6 +34,8 @@ import org.thingsboard.server.common.data.cf.configuration.OutputType;
import org.thingsboard.server.common.data.cf.configuration.ReferencedEntityKey; import org.thingsboard.server.common.data.cf.configuration.ReferencedEntityKey;
import org.thingsboard.server.common.data.cf.configuration.SimpleCalculatedFieldConfiguration; import org.thingsboard.server.common.data.cf.configuration.SimpleCalculatedFieldConfiguration;
import org.thingsboard.server.common.data.debug.DebugSettings; import org.thingsboard.server.common.data.debug.DebugSettings;
import org.thingsboard.server.common.data.id.AssetProfileId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.controller.CalculatedFieldControllerTest; import org.thingsboard.server.controller.CalculatedFieldControllerTest;
import org.thingsboard.server.dao.service.DaoSqlTest; import org.thingsboard.server.dao.service.DaoSqlTest;
@ -53,12 +54,8 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes
@Test @Test
public void testSimpleCalculatedField() throws Exception { public void testSimpleCalculatedField() throws Exception {
Device testDevice = createDevice("Test device", "1234567890"); Device testDevice = createDevice("Test device", "1234567890");
doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/timeseries/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"temperature\":25}"));
JsonNode timeSeries = JacksonUtil.toJsonNode("{\"temperature\":25}"); doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"deviceTemperature\":40}"));
doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/timeseries/" + DataConstants.SERVER_SCOPE, timeSeries);
JsonNode attributes = JacksonUtil.toJsonNode("{\"deviceTemperature\":40}");
doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, attributes);
CalculatedField calculatedField = new CalculatedField(); CalculatedField calculatedField = new CalculatedField();
calculatedField.setEntityId(testDevice.getId()); calculatedField.setEntityId(testDevice.getId());
@ -72,15 +69,12 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes
Argument argument = new Argument(); Argument argument = new Argument();
ReferencedEntityKey refEntityKey = new ReferencedEntityKey("temperature", ArgumentType.TS_LATEST, null); ReferencedEntityKey refEntityKey = new ReferencedEntityKey("temperature", ArgumentType.TS_LATEST, null);
argument.setRefEntityKey(refEntityKey); argument.setRefEntityKey(refEntityKey);
config.setArguments(Map.of("T", argument)); config.setArguments(Map.of("T", argument));
config.setExpression("(T * 9/5) + 32"); config.setExpression("(T * 9/5) + 32");
Output output = new Output(); Output output = new Output();
output.setName("fahrenheitTemp"); output.setName("fahrenheitTemp");
output.setType(OutputType.TIME_SERIES); output.setType(OutputType.TIME_SERIES);
config.setOutput(output); config.setOutput(output);
calculatedField.setConfiguration(config); calculatedField.setConfiguration(config);
@ -91,19 +85,18 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes
Thread.sleep(300); Thread.sleep(300);
ObjectNode fahrenheitTemp = doGetAsync("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/values/timeseries?keys=fahrenheitTemp", ObjectNode.class); ObjectNode fahrenheitTemp = getLatestTelemetry(testDevice.getId(), "fahrenheitTemp");
assertThat(fahrenheitTemp).isNotNull(); assertThat(fahrenheitTemp).isNotNull();
assertThat(fahrenheitTemp.get("fahrenheitTemp").get(0).get("value").asText()).isEqualTo("77.0"); assertThat(fahrenheitTemp.get("fahrenheitTemp").get(0).get("value").asText()).isEqualTo("77.0");
// update telemetry -> recalculate state // update telemetry -> recalculate state
JsonNode newTelemetry = JacksonUtil.toJsonNode("{\"temperature\":30}"); doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/timeseries/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"temperature\":30}"));
doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/timeseries/" + DataConstants.SERVER_SCOPE, newTelemetry);
Thread.sleep(300); Thread.sleep(300);
ObjectNode fahrenheitTempAfterUpdate = doGetAsync("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/values/timeseries?keys=fahrenheitTemp", ObjectNode.class); fahrenheitTemp = getLatestTelemetry(testDevice.getId(), "fahrenheitTemp");
assertThat(fahrenheitTempAfterUpdate).isNotNull(); assertThat(fahrenheitTemp).isNotNull();
assertThat(fahrenheitTempAfterUpdate.get("fahrenheitTemp").get(0).get("value").asText()).isEqualTo("86.0"); assertThat(fahrenheitTemp.get("fahrenheitTemp").get(0).get("value").asText()).isEqualTo("86.0");
// update CF output -> perform calculation with updated output // update CF output -> perform calculation with updated output
Output savedOutput = savedCalculatedField.getConfiguration().getOutput(); Output savedOutput = savedCalculatedField.getConfiguration().getOutput();
@ -114,21 +107,20 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes
Thread.sleep(300); Thread.sleep(300);
ArrayNode temperatureF = doGetAsync("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=temperatureF", ArrayNode.class); ArrayNode temperatureF = getServerAttributes(testDevice.getId(), "temperatureF");
assertThat(temperatureF).isNotNull(); assertThat(temperatureF).isNotNull();
assertThat(temperatureF.get(0).get("value").asText()).isEqualTo("86.0"); assertThat(temperatureF.get(0).get("value").asText()).isEqualTo("86.0");
// update CF argument -> perform calculation with new argument // update CF argument -> perform calculation with new argument
Argument savedArgument = savedCalculatedField.getConfiguration().getArguments().get("T"); Argument savedArgument = savedCalculatedField.getConfiguration().getArguments().get("T");
savedArgument.setRefEntityKey(new ReferencedEntityKey("deviceTemperature", ArgumentType.ATTRIBUTE, AttributeScope.SERVER_SCOPE)); savedArgument.setRefEntityKey(new ReferencedEntityKey("deviceTemperature", ArgumentType.ATTRIBUTE, AttributeScope.SERVER_SCOPE));
savedCalculatedField = doPost("/api/calculatedField", savedCalculatedField, CalculatedField.class); savedCalculatedField = doPost("/api/calculatedField", savedCalculatedField, CalculatedField.class);
Thread.sleep(300); Thread.sleep(300);
ArrayNode temperatureFAfterUpdateArg = doGetAsync("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=temperatureF", ArrayNode.class); temperatureF = getServerAttributes(testDevice.getId(), "temperatureF");
assertThat(temperatureFAfterUpdateArg).isNotNull(); assertThat(temperatureF).isNotNull();
assertThat(temperatureFAfterUpdateArg.get(0).get("value").asText()).isEqualTo("104.0"); assertThat(temperatureF.get(0).get("value").asText()).isEqualTo("104.0");
// update CF expression -> perform calculation with new expression // update CF expression -> perform calculation with new expression
savedCalculatedField.getConfiguration().setExpression("1.8 * T + 32"); savedCalculatedField.getConfiguration().setExpression("1.8 * T + 32");
@ -136,36 +128,23 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes
Thread.sleep(300); Thread.sleep(300);
ArrayNode temperatureFAfterUpdateExpression = doGetAsync("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=temperatureF", ArrayNode.class); temperatureF = getServerAttributes(testDevice.getId(), "temperatureF");
assertThat(temperatureFAfterUpdateExpression).isNotNull(); assertThat(temperatureF).isNotNull();
assertThat(temperatureFAfterUpdateExpression.get(0).get("value").asText()).isEqualTo("104.0"); assertThat(temperatureF.get(0).get("value").asText()).isEqualTo("104.0");
} }
@Test @Test
public void testSimpleCalculatedFieldWhenEntityIdIsProfile() throws Exception { public void testSimpleCalculatedFieldWhenEntityIdIsProfile() throws Exception {
Device testDevice = createDevice("Test device", "1234567890"); Device testDevice = createDevice("Test device", "1234567890");
JsonNode deviceAttributes = JacksonUtil.toJsonNode("{\"x\":40}"); doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"x\":40}"));
doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, deviceAttributes);
AssetProfile assetProfile = doPost("/api/assetProfile", createAssetProfile("Test Asset Profile"), AssetProfile.class); AssetProfile assetProfile = doPost("/api/assetProfile", createAssetProfile("Test Asset Profile"), AssetProfile.class);
Asset asset1 = new Asset(); Asset asset1 = createAsset("Test asset 1", assetProfile.getId());
asset1.setName("Test asset 1"); doPost("/api/plugins/telemetry/ASSET/" + asset1.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"y\":11}"));
asset1.setAssetProfileId(assetProfile.getId());
Asset savedAsset1 = doPost("/api/asset", asset1, Asset.class); Asset asset2 = createAsset("Test asset 2", assetProfile.getId());
doPost("/api/plugins/telemetry/ASSET/" + asset2.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"y\":12}"));
JsonNode asset1Attributes = JacksonUtil.toJsonNode("{\"y\":11}");
doPost("/api/plugins/telemetry/ASSET/" + savedAsset1.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, asset1Attributes);
Asset asset2 = new Asset();
asset2.setName("Test asset 2");
asset2.setAssetProfileId(assetProfile.getId());
Asset savedAsset2 = doPost("/api/asset", asset2, Asset.class);
JsonNode asset2Attributes = JacksonUtil.toJsonNode("{\"y\":12}");
doPost("/api/plugins/telemetry/ASSET/" + savedAsset2.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, asset2Attributes);
CalculatedField calculatedField = new CalculatedField(); CalculatedField calculatedField = new CalculatedField();
calculatedField.setEntityId(assetProfile.getId()); calculatedField.setEntityId(assetProfile.getId());
@ -205,62 +184,74 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes
Thread.sleep(300); Thread.sleep(300);
// result of asset 1 // result of asset 1
ArrayNode z1 = doGetAsync("/api/plugins/telemetry/ASSET/" + savedAsset1.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=z", ArrayNode.class); ArrayNode z1 = getServerAttributes(asset1.getId(), "z");
assertThat(z1).isNotNull(); assertThat(z1).isNotNull();
assertThat(z1.get(0).get("value").asText()).isEqualTo("51.0"); assertThat(z1.get(0).get("value").asText()).isEqualTo("51.0");
// result of asset 2 // result of asset 2
ArrayNode z2 = doGetAsync("/api/plugins/telemetry/ASSET/" + savedAsset2.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=z", ArrayNode.class); ArrayNode z2 = getServerAttributes(asset2.getId(), "z");
assertThat(z2).isNotNull(); assertThat(z2).isNotNull();
assertThat(z2.get(0).get("value").asText()).isEqualTo("52.0"); assertThat(z2.get(0).get("value").asText()).isEqualTo("52.0");
// update device telemetry -> recalculate state for all assets // update device telemetry -> recalculate state for all assets
JsonNode updatedDeviceAttributes = JacksonUtil.toJsonNode("{\"x\":25}"); doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"x\":25}"));
doPost("/api/plugins/telemetry/DEVICE/" + testDevice.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, updatedDeviceAttributes);
Thread.sleep(300); Thread.sleep(300);
// result of asset 1 // result of asset 1
ArrayNode updZ1 = doGetAsync("/api/plugins/telemetry/ASSET/" + savedAsset1.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=z", ArrayNode.class); z1 = getServerAttributes(asset1.getId(), "z");
assertThat(updZ1).isNotNull(); assertThat(z1).isNotNull();
assertThat(updZ1.get(0).get("value").asText()).isEqualTo("36.0"); assertThat(z1.get(0).get("value").asText()).isEqualTo("36.0");
// result of asset 2 // result of asset 2
ArrayNode updZ2 = doGetAsync("/api/plugins/telemetry/ASSET/" + savedAsset2.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=z", ArrayNode.class); z2 = getServerAttributes(asset2.getId(), "z");
assertThat(updZ2).isNotNull(); assertThat(z2).isNotNull();
assertThat(updZ2.get(0).get("value").asText()).isEqualTo("37.0"); assertThat(z2.get(0).get("value").asText()).isEqualTo("37.0");
// // update asset 1 telemetry -> recalculate state only for asset 1 // update asset 1 telemetry -> recalculate state only for asset 1
// JsonNode updatedAsset1Attributes = JacksonUtil.toJsonNode("{\"x\":15}"); doPost("/api/plugins/telemetry/ASSET/" + asset1.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"y\":15}"));
// doPost("/api/plugins/telemetry/DEVICE/" + asset1.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, updatedAsset1Attributes);
// Thread.sleep(300);
// Thread.sleep(300);
// // result of asset 1
// // result of asset 1 z1 = getServerAttributes(asset1.getId(), "z");
// updZ1 = doGetAsync("/api/plugins/telemetry/ASSET/" + savedAsset1.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=z", ArrayNode.class); assertThat(z1).isNotNull();
// assertThat(updZ1).isNotNull(); assertThat(z1.get(0).get("value").asText()).isEqualTo("40.0");
// assertThat(updZ1.get(0).get("value").asText()).isEqualTo("40.0");
// // result of asset 2 (no changes)
// // result of asset 2 (no changes) z2 = getServerAttributes(asset2.getId(), "z");
// updZ2 = doGetAsync("/api/plugins/telemetry/ASSET/" + savedAsset2.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=z", ArrayNode.class); assertThat(z2).isNotNull();
// assertThat(updZ2).isNotNull(); assertThat(z2.get(0).get("value").asText()).isEqualTo("37.0");
// assertThat(updZ2.get(0).get("value").asText()).isEqualTo("37.0");
// // update asset 2 telemetry -> recalculate state only for asset 2
// // update asset 2 telemetry -> recalculate state only for asset 2 doPost("/api/plugins/telemetry/ASSET/" + asset2.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, JacksonUtil.toJsonNode("{\"y\":5}"));
// JsonNode updatedAsset2Attributes = JacksonUtil.toJsonNode("{\"x\":5}");
// doPost("/api/plugins/telemetry/DEVICE/" + asset2.getUuidId() + "/attributes/" + DataConstants.SERVER_SCOPE, updatedAsset2Attributes); Thread.sleep(300);
//
// Thread.sleep(300); // result of asset 1 (no changes)
// z1 = getServerAttributes(asset1.getId(), "z");
// // result of asset 1 (no changes) assertThat(z1).isNotNull();
// updZ1 = doGetAsync("/api/plugins/telemetry/ASSET/" + savedAsset1.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=z", ArrayNode.class); assertThat(z1.get(0).get("value").asText()).isEqualTo("40.0");
// assertThat(updZ1).isNotNull();
// assertThat(updZ1.get(0).get("value").asText()).isEqualTo("40.0"); // result of asset 2
// z2 = getServerAttributes(asset2.getId(), "z");
// // result of asset 2 assertThat(z2).isNotNull();
// updZ2 = doGetAsync("/api/plugins/telemetry/ASSET/" + savedAsset2.getUuidId() + "/values/attributes/SERVER_SCOPE?keys=z", ArrayNode.class); assertThat(z2.get(0).get("value").asText()).isEqualTo("30.0");
// assertThat(updZ2).isNotNull(); }
// assertThat(updZ2.get(0).get("value").asText()).isEqualTo("30.0");
private ObjectNode getLatestTelemetry(EntityId entityId, String... keys) throws Exception {
return doGetAsync("/api/plugins/telemetry/" + entityId.getEntityType() + "/" + entityId.getId() + "/values/timeseries?keys=" + String.join(",", keys), ObjectNode.class);
}
private ArrayNode getServerAttributes(EntityId entityId, String... keys) throws Exception {
return doGetAsync("/api/plugins/telemetry/" + entityId.getEntityType() + "/" + entityId.getId() + "/values/attributes/SERVER_SCOPE?keys=" + String.join(",", keys), ArrayNode.class);
}
private Asset createAsset(String name, AssetProfileId assetProfileId) {
Asset asset = new Asset();
asset.setName(name);
asset.setAssetProfileId(assetProfileId);
return doPost("/api/asset", asset, Asset.class);
} }
} }