diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java b/common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java index a7671f4327..7360f9b8ab 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java @@ -181,6 +181,13 @@ public class StringUtils { return org.apache.commons.lang3.StringUtils.contains(seq, searchSeq); } + /** + * Use this to prevent org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00 + **/ + public static boolean contains0x00(final String s) { + return s != null && s.contains("\u0000"); + } + public static String randomNumeric(int length) { return RandomStringUtils.randomNumeric(length); } diff --git a/common/data/src/test/java/org/thingsboard/server/common/data/StringUtilsTest.java b/common/data/src/test/java/org/thingsboard/server/common/data/StringUtilsTest.java new file mode 100644 index 0000000000..4cf21baa99 --- /dev/null +++ b/common/data/src/test/java/org/thingsboard/server/common/data/StringUtilsTest.java @@ -0,0 +1,40 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.common.data; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; + +class StringUtilsTest { + + @ParameterizedTest + @ValueSource(strings = { + "\000", "\u0000", " \000", " \000 ", "\000 ", "\000\000", "\000 \000", + "世\000界", "F0929906\000\000\000\000\000\000\000\000\000", + }) + void testContains0x00_thenTrue(String sample) { + assertThat(StringUtils.contains0x00(sample)).isTrue(); + } + + @ParameterizedTest + @ValueSource(strings = {"", " ", "abc", "世界", "\001", "\uD83D\uDC0C"}) + void testContains0x00_thenFalse(String sample) { + assertThat(StringUtils.contains0x00(sample)).isFalse(); + } + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/DataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/DataValidator.java index e4bc622354..d81c05044f 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/DataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/DataValidator.java @@ -86,6 +86,15 @@ public abstract class DataValidator> { public void validateDelete(TenantId tenantId, EntityId entityId) { } + public void validateString(String exceptionPrefix, String name) { + if (StringUtils.isEmpty(name) || name.trim().length() == 0) { + throw new DataValidationException(exceptionPrefix + " should be specified!"); + } + if (StringUtils.contains0x00(name)) { + throw new DataValidationException(exceptionPrefix + " should not contain 0x00 symbol!"); + } + } + protected boolean isSameData(D existentData, D actualData) { return actualData.getId() != null && existentData.getId().equals(actualData.getId()); } @@ -151,8 +160,8 @@ public abstract class DataValidator> { validateQueueNameOrTopic(topic, TOPIC); } - private static void validateQueueNameOrTopic(String value, String fieldName) { - if (StringUtils.isEmpty(value)) { + static void validateQueueNameOrTopic(String value, String fieldName) { + if (StringUtils.isEmpty(value) || value.trim().length() == 0 ) { throw new DataValidationException(String.format("Queue %s should be specified!", fieldName)); } if (!QUEUE_PATTERN.matcher(value).matches()) { diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/AdminSettingsDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/AdminSettingsDataValidator.java index 692690f328..9d0d53732c 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/AdminSettingsDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/AdminSettingsDataValidator.java @@ -18,7 +18,6 @@ package org.thingsboard.server.dao.service.validator; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.AdminSettings; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.service.DataValidator; @@ -49,12 +48,9 @@ public class AdminSettingsDataValidator extends DataValidator { return existentAdminSettings; } - @Override protected void validateDataImpl(TenantId tenantId, AdminSettings adminSettings) { - if (StringUtils.isEmpty(adminSettings.getKey())) { - throw new DataValidationException("Key should be specified!"); - } + validateString("Key", adminSettings.getKey()); if (adminSettings.getJsonValue() == null) { throw new DataValidationException("Json value should be specified!"); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/AlarmDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/AlarmDataValidator.java index 2c76fd2bff..6ef2eb5052 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/AlarmDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/AlarmDataValidator.java @@ -17,7 +17,6 @@ package org.thingsboard.server.dao.service.validator; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.dao.exception.DataValidationException; @@ -32,9 +31,7 @@ public class AlarmDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, Alarm alarm) { - if (StringUtils.isEmpty(alarm.getType())) { - throw new DataValidationException("Alarm type should be specified!"); - } + validateString("Alarm type", alarm.getType()); if (alarm.getOriginator() == null) { throw new DataValidationException("Alarm originator should be specified!"); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetDataValidator.java index 57ae7d3c5e..6c0d862c0d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetDataValidator.java @@ -20,7 +20,6 @@ import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.EntityType; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.TenantId; @@ -64,9 +63,7 @@ public class AssetDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, Asset asset) { - if (StringUtils.isEmpty(asset.getName())) { - throw new DataValidationException("Asset name should be specified!"); - } + validateString("Asset name", asset.getName()); if (asset.getTenantId() == null) { throw new DataValidationException("Asset should be assigned to tenant!"); } else { diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidator.java index d206dfefba..f0816621e0 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidator.java @@ -53,9 +53,7 @@ public class AssetProfileDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, AssetProfile assetProfile) { - if (StringUtils.isEmpty(assetProfile.getName())) { - throw new DataValidationException("Asset profile name should be specified!"); - } + validateString("Asset profile name", assetProfile.getName()); if (assetProfile.getTenantId() == null) { throw new DataValidationException("Asset profile should be assigned to tenant!"); } else { diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidator.java index df37676688..5befacf8ea 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidator.java @@ -15,6 +15,7 @@ */ package org.thingsboard.server.dao.service.validator; +import lombok.Getter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.thingsboard.server.common.data.BaseData; @@ -31,23 +32,27 @@ import java.util.Objects; public abstract class BaseOtaPackageDataValidator> extends DataValidator { @Autowired + @Getter @Lazy private TenantService tenantService; @Autowired + @Getter private DeviceProfileDao deviceProfileDao; protected void validateImpl(OtaPackageInfo otaPackageInfo) { + validateString("OtaPackage title", otaPackageInfo.getTitle()); + if (otaPackageInfo.getTenantId() == null) { throw new DataValidationException("OtaPackage should be assigned to tenant!"); } else { - if (!tenantService.tenantExists(otaPackageInfo.getTenantId())) { + if (!getTenantService().tenantExists(otaPackageInfo.getTenantId())) { throw new DataValidationException("OtaPackage is referencing to non-existent tenant!"); } } if (otaPackageInfo.getDeviceProfileId() != null) { - DeviceProfile deviceProfile = deviceProfileDao.findById(otaPackageInfo.getTenantId(), otaPackageInfo.getDeviceProfileId().getId()); + DeviceProfile deviceProfile = getDeviceProfileDao().findById(otaPackageInfo.getTenantId(), otaPackageInfo.getDeviceProfileId().getId()); if (deviceProfile == null) { throw new DataValidationException("OtaPackage is referencing to non-existent device profile!"); } @@ -57,10 +62,6 @@ public abstract class BaseOtaPackageDataValidator> extends throw new DataValidationException("Type should be specified!"); } - if (StringUtils.isEmpty(otaPackageInfo.getTitle())) { - throw new DataValidationException("OtaPackage title should be specified!"); - } - if (StringUtils.isEmpty(otaPackageInfo.getVersion())) { throw new DataValidationException("OtaPackage version should be specified!"); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidator.java index 831b81a6fc..446091a698 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidator.java @@ -27,15 +27,13 @@ public class ComponentDescriptorDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, Customer customer) { - if (StringUtils.isEmpty(customer.getTitle())) { - throw new DataValidationException("Customer title should be specified!"); - } + validateString("Customer title", customer.getTitle()); if (customer.getTitle().equals(CustomerServiceImpl.PUBLIC_CUSTOMER_TITLE)) { throw new DataValidationException("'Public' title for customer is system reserved!"); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DashboardDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DashboardDataValidator.java index 246944199a..60e1045f07 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DashboardDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DashboardDataValidator.java @@ -19,9 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.Dashboard; import org.thingsboard.server.common.data.EntityType; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.id.TenantId; -import org.thingsboard.server.dao.dashboard.DashboardDao; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.tenant.TenantService; @@ -29,9 +27,6 @@ import org.thingsboard.server.dao.tenant.TenantService; @Component public class DashboardDataValidator extends DataValidator { - @Autowired - private DashboardDao dashboardDao; - @Autowired private TenantService tenantService; @@ -42,9 +37,7 @@ public class DashboardDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, Dashboard dashboard) { - if (StringUtils.isEmpty(dashboard.getTitle())) { - throw new DataValidationException("Dashboard title should be specified!"); - } + validateString("Dashboard title", dashboard.getTitle()); if (dashboard.getTenantId() == null) { throw new DataValidationException("Dashboard should be assigned to tenant!"); } else { diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceDataValidator.java index 51517a4dc2..41cdcb0b2e 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceDataValidator.java @@ -20,7 +20,6 @@ import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.EntityType; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.device.data.DeviceTransportConfiguration; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.TenantId; @@ -61,9 +60,7 @@ public class DeviceDataValidator extends AbstractHasOtaPackageValidator @Override protected void validateDataImpl(TenantId tenantId, Device device) { - if (StringUtils.isEmpty(device.getName()) || device.getName().trim().length() == 0) { - throw new DataValidationException("Device name should be specified!"); - } + validateString("Device name", device.getName()); if (device.getTenantId() == null) { throw new DataValidationException("Device should be assigned to tenant!"); } else { diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java index bd4bf04c45..89510c6076 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java @@ -103,9 +103,7 @@ public class DeviceProfileDataValidator extends AbstractHasOtaPackageValidator { @Override protected void validateDataImpl(TenantId tenantId, Edge edge) { - if (StringUtils.isEmpty(edge.getType())) { - throw new DataValidationException("Edge type should be specified!"); - } - if (StringUtils.isEmpty(edge.getName())) { - throw new DataValidationException("Edge name should be specified!"); - } + validateString("Edge name", edge.getName()); + validateString("Edge type", edge.getType()); if (StringUtils.isEmpty(edge.getSecret())) { throw new DataValidationException("Edge secret should be specified!"); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/EntityViewDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/EntityViewDataValidator.java index dddae3195d..7cc72a67cf 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/EntityViewDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/EntityViewDataValidator.java @@ -19,7 +19,6 @@ import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.EntityView; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.dao.customer.CustomerDao; @@ -59,12 +58,8 @@ public class EntityViewDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, EntityView entityView) { - if (StringUtils.isEmpty(entityView.getType())) { - throw new DataValidationException("Entity View type should be specified!"); - } - if (StringUtils.isEmpty(entityView.getName())) { - throw new DataValidationException("Entity view name should be specified!"); - } + validateString("Entity view name", entityView.getName()); + validateString("Entity view type", entityView.getType()); if (entityView.getTenantId() == null) { throw new DataValidationException("Entity view should be assigned to tenant!"); } else { diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/ResourceDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/ResourceDataValidator.java index 9939d887fa..d620950628 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/ResourceDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/ResourceDataValidator.java @@ -66,9 +66,7 @@ public class ResourceDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, TbResource resource) { - if (StringUtils.isEmpty(resource.getTitle())) { - throw new DataValidationException("Resource title should be specified!"); - } + validateString("Resource title", resource.getTitle()); if (resource.getResourceType() == null) { throw new DataValidationException("Resource type should be specified!"); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/RuleChainDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/RuleChainDataValidator.java index 5450e29706..08e047904f 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/RuleChainDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/RuleChainDataValidator.java @@ -23,7 +23,6 @@ import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.EntityType; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.rule.NodeConnectionInfo; import org.thingsboard.server.common.data.rule.RuleChain; @@ -65,9 +64,7 @@ public class RuleChainDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, RuleChain ruleChain) { - if (StringUtils.isEmpty(ruleChain.getName())) { - throw new DataValidationException("Rule chain name should be specified!"); - } + validateString("Rule chain name", ruleChain.getName()); if (ruleChain.getType() == null) { ruleChain.setType(RuleChainType.CORE); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantDataValidator.java index c6e8e055a5..64dff6495a 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantDataValidator.java @@ -32,9 +32,7 @@ public class TenantDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, Tenant tenant) { - if (StringUtils.isEmpty(tenant.getTitle())) { - throw new DataValidationException("Tenant title should be specified!"); - } + validateString("Tenant title", tenant.getTitle()); if (!StringUtils.isEmpty(tenant.getEmail())) { validateEmail(tenant.getEmail()); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantProfileDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantProfileDataValidator.java index aa166ba12e..a46371de39 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantProfileDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantProfileDataValidator.java @@ -19,7 +19,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.DataConstants; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.TenantProfile; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.queue.ProcessingStrategy; @@ -48,9 +47,7 @@ public class TenantProfileDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, TenantProfile tenantProfile) { - if (StringUtils.isEmpty(tenantProfile.getName())) { - throw new DataValidationException("Tenant profile name should be specified!"); - } + validateString("Tenant profile name", tenantProfile.getName()); if (tenantProfile.getProfileData() == null) { throw new DataValidationException("Tenant profile data should be specified!"); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetTypeDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetTypeDataValidator.java index 1cec787b0f..71bc17a8fa 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetTypeDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetTypeDataValidator.java @@ -17,11 +17,9 @@ package org.thingsboard.server.dao.service.validator; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.widget.WidgetType; import org.thingsboard.server.common.data.widget.WidgetTypeDetails; -import org.thingsboard.server.common.data.widget.WidgetsBundle; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.model.ModelConstants; import org.thingsboard.server.dao.service.DataValidator; @@ -39,9 +37,7 @@ public class WidgetTypeDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, WidgetTypeDetails widgetTypeDetails) { - if (StringUtils.isEmpty(widgetTypeDetails.getName())) { - throw new DataValidationException("Widgets type name should be specified!"); - } + validateString("Widgets type name", widgetTypeDetails.getName()); if (widgetTypeDetails.getDescriptor() == null || widgetTypeDetails.getDescriptor().size() == 0) { throw new DataValidationException("Widgets type descriptor can't be empty!"); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetsBundleDataValidator.java b/dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetsBundleDataValidator.java index 8c730398ed..42b60588cb 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetsBundleDataValidator.java +++ b/dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetsBundleDataValidator.java @@ -17,7 +17,6 @@ package org.thingsboard.server.dao.service.validator; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; -import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.widget.WidgetsBundle; import org.thingsboard.server.dao.exception.DataValidationException; @@ -35,9 +34,7 @@ public class WidgetsBundleDataValidator extends DataValidator { @Override protected void validateDataImpl(TenantId tenantId, WidgetsBundle widgetsBundle) { - if (StringUtils.isEmpty(widgetsBundle.getTitle())) { - throw new DataValidationException("Widgets bundle title should be specified!"); - } + validateString("Widgets bundle title", widgetsBundle.getTitle()); if (widgetsBundle.getTenantId() == null) { widgetsBundle.setTenantId(TenantId.fromUUID(ModelConstants.NULL_UUID)); } diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java index adb97196fd..ccddcb16bf 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java @@ -85,6 +85,17 @@ public class AssetServiceTest extends AbstractServiceTest { }); } + @Test + public void testSaveDeviceWithNameContains0x00_thenDataValidationException() { + Asset asset = new Asset(); + asset.setTenantId(tenantId); + asset.setType("default"); + asset.setName("F0929906\000\000\000\000\000\000\000\000\000"); + Assertions.assertThrows(DataValidationException.class, () -> { + assetService.saveAsset(asset); + }); + } + @Test public void testSaveAssetWithEmptyTenant() { Asset asset = new Asset(); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/DataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/DataValidatorTest.java index 40262ac2b8..0eafd9aeaf 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/DataValidatorTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/DataValidatorTest.java @@ -15,62 +15,93 @@ */ package org.thingsboard.server.dao.service; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.thingsboard.server.dao.exception.DataValidationException; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.spy; + +@Slf4j public class DataValidatorTest { - @Test - public void validateEmail() { - String email = "aZ1_!#$%&'*+/=?`{|}~^.-@mail.io"; - DataValidator.validateEmail(email); + DataValidator dataValidator; + + @BeforeEach + void setUp() { + dataValidator = spy(DataValidator.class); } - @Test - public void validateInvalidEmail1() { - String email = "test:1@mail.io"; - Assertions.assertThrows(DataValidationException.class, () -> { - DataValidator.validateEmail(email); - }); + @ParameterizedTest + @ValueSource(strings = { + "coffee", "1", "big box", "世界", "!", "--", "~!@#$%^&*()_+=-/|\\[]{};:'`\"?<>,.", "\uD83D\uDC0C", "\041", + "Gdy Pomorze nie pomoże, to pomoże może morze, a gdy morze nie pomoże, to pomoże może Gdańsk", + }) + void validateName_thenOK(final String name) { + dataValidator.validateString("Device name", name); + dataValidator.validateString("Asset name", name); + dataValidator.validateString("Asset profile name", name); + dataValidator.validateString("Alarm type", name); + dataValidator.validateString("Customer name", name); + dataValidator.validateString("Tenant name", name); } - @Test - public void validateInvalidEmail2() { - String email = "test()1@mail.io"; - Assertions.assertThrows(DataValidationException.class, () -> { - DataValidator.validateEmail(email); - }); + + @ParameterizedTest + @ValueSource(strings = { + "", " ", " ", "\n", "\r\n", "\t", "\000", "\000\000", "\001", "\002", "\040", "\u0000", "\u0000\u0000", + "F0929906\000\000\000\000\000\000\000\000\000", "\000\000\000F0929906", + "\u0000F0929906", "F092\u00009906", "F0929906\u0000" + }) + void validateName_thenDataValidationException(final String name) { + DataValidationException exception; + exception = Assertions.assertThrows(DataValidationException.class, () -> dataValidator.validateString("Asset name", name)); + log.warn("Exception message Asset name: {}", exception.getMessage()); + assertThat(exception.getMessage()).as("message Asset name").containsPattern("Asset name .*"); + + exception = Assertions.assertThrows(DataValidationException.class, () -> dataValidator.validateString("Device name", name)); + log.warn("Exception message Device name: {}", exception.getMessage()); + assertThat(exception.getMessage()).as("message Device name").containsPattern("Device name .*"); } - @Test - public void validateInvalidEmail3() { - String email = "test[]1@mail.io"; - Assertions.assertThrows(DataValidationException.class, () -> { - DataValidator.validateEmail(email); - }); + @ParameterizedTest + @ValueSource(strings = { + "aZ1_!#$%&'*+/=?`{|}~^.-@mail.io", "support@thingsboard.io", + }) + public void validateEmail(String email) { + DataValidator.validateEmail(email); } - @Test - public void validateInvalidEmail4() { - String email = "test\\1@mail.io"; - Assertions.assertThrows(DataValidationException.class, () -> { - DataValidator.validateEmail(email); - }); + @ParameterizedTest + @ValueSource(strings = { + "test:1@mail.io", "test()1@mail.io", "test[]1@mail.io", + "test\\1@mail.io", "test\"1@mail.io", "test<>1@mail.io", + }) + public void validateEmailInvalid(String email) { + Assertions.assertThrows(DataValidationException.class, () -> DataValidator.validateEmail(email)); } - @Test - public void validateInvalidEmail5() { - String email = "test\"1@mail.io"; - Assertions.assertThrows(DataValidationException.class, () -> { - DataValidator.validateEmail(email); - }); + @ParameterizedTest + @ValueSource(strings = { + "azAZ09_.-", "topic", + }) + public void validateQueueNameOrTopic(String value) { + DataValidator.validateQueueNameOrTopic(value, "name"); + DataValidator.validateQueueNameOrTopic(value, "topic"); } - @Test - public void validateInvalidEmail6() { - String email = "test<>1@mail.io"; - Assertions.assertThrows(DataValidationException.class, () -> { - DataValidator.validateEmail(email); - }); + @ParameterizedTest + @ValueSource(strings = { + "", " ", " ", "\n", "\r\n", "\t", "\000", "\000\000", "\001", "\002", "\040", "\u0000", "\u0000\u0000", + "topic@home", "!", ",", "Łódź", + "\uD83D\uDC0C", "\041", + "F0929906\000\000\000\000\000\000\000\000\000", + }) + public void validateQueueNameOrTopicInvalid(String value) { + Assertions.assertThrows(DataValidationException.class, () -> DataValidator.validateQueueNameOrTopic(value, "name")); + Assertions.assertThrows(DataValidationException.class, () -> DataValidator.validateQueueNameOrTopic(value, "topic")); } + } diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/DeviceServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/DeviceServiceTest.java index a2d3062cff..1073f20be4 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/DeviceServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/DeviceServiceTest.java @@ -283,6 +283,17 @@ public class DeviceServiceTest extends AbstractServiceTest { }); } + @Test + public void testSaveDeviceWithNameContains0x00_thenDataValidationException() { + Device device = new Device(); + device.setType("default"); + device.setTenantId(tenantId); + device.setName("F0929906\000\000\000\000\000\000\000\000\000"); + Assertions.assertThrows(DataValidationException.class, () -> { + deviceService.saveDevice(device); + }); + } + @Test public void testSaveDeviceWithInvalidTenant() { Device device = new Device(); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/AdminSettingsDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AdminSettingsDataValidatorTest.java new file mode 100644 index 0000000000..1f021b16e7 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AdminSettingsDataValidatorTest.java @@ -0,0 +1,50 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.common.util.JacksonUtil; +import org.thingsboard.server.common.data.AdminSettings; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.settings.AdminSettingsService; + +import java.util.UUID; + +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = AdminSettingsDataValidator.class) +class AdminSettingsDataValidatorTest { + + @MockBean + AdminSettingsService adminSettingsService; + @SpyBean + AdminSettingsDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @Test + void testValidateNameInvocation() { + AdminSettings adminSettings = new AdminSettings(); + adminSettings.setKey("jwt"); + adminSettings.setJsonValue(JacksonUtil.toJsonNode("{}")); + + validator.validateDataImpl(tenantId, adminSettings); + verify(validator).validateString("Key", adminSettings.getKey()); + } + +} \ No newline at end of file diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/AlarmDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AlarmDataValidatorTest.java new file mode 100644 index 0000000000..fc91a099fa --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AlarmDataValidatorTest.java @@ -0,0 +1,61 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.alarm.Alarm; +import org.thingsboard.server.common.data.alarm.AlarmSeverity; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = AlarmDataValidator.class) +class AlarmDataValidatorTest { + + @MockBean + TenantService tenantService; + @SpyBean + AlarmDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + Alarm alarm = new Alarm(); + alarm.setType("overheating"); + alarm.setOriginator(tenantId); + alarm.setSeverity(AlarmSeverity.CRITICAL); + alarm.setCleared(false); + alarm.setAcknowledged(false); + alarm.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, alarm); + verify(validator).validateString("Alarm type", alarm.getType()); + } + +} \ No newline at end of file diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetDataValidatorTest.java new file mode 100644 index 0000000000..7922cdacfa --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetDataValidatorTest.java @@ -0,0 +1,85 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.asset.AssetDao; +import org.thingsboard.server.dao.customer.CustomerDao; +import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.willReturn; + +@SpringBootTest(classes = AssetDataValidator.class) +@Slf4j +class AssetDataValidatorTest { + + @MockBean + AssetDao assetDao; + @MockBean + TenantService tenantService; + @MockBean + CustomerDao customerDao; + @Autowired + AssetDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @ParameterizedTest + @ValueSource(strings = { + "coffee", "1", "big box", "世界", "!", "--", "~!@#$%^&*()_+=-/|\\[]{};:'`\"?<>,.", "\uD83D\uDC0C", "\041", + "Gdy Pomorze nie pomoże, to pomoże może morze, a gdy morze nie pomoże, to pomoże może Gdańsk", + }) + void testAssetName_thenOK(final String name) { + Asset asset = new Asset(); + asset.setTenantId(tenantId); + asset.setName(name); + validator.validateDataImpl(tenantId, asset); + } + + @ParameterizedTest + @ValueSource(strings = { + "", " ", " ", "\n", "\r\n", "\t", "\000", "\000\000", "\001", "\002", "\040", "\u0000", "\u0000\u0000", + "F0929906\000\000\000\000\000\000\000\000\000", "\000\000\000F0929906", + "\u0000F0929906", "F092\u00009906", "F0929906\u0000" + }) + void testAssetName_thenDataValidationException(final String name) { + Asset asset = new Asset(); + asset.setTenantId(tenantId); + asset.setName(name); + + DataValidationException exception = Assertions.assertThrows(DataValidationException.class, () -> validator.validateDataImpl(tenantId, asset)); + log.warn("Exception message: {}", exception.getMessage()); + assertThat(exception.getMessage()).as("message Asset name").containsPattern("Asset name .*"); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidatorTest.java new file mode 100644 index 0000000000..66fdd54c86 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidatorTest.java @@ -0,0 +1,71 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.asset.AssetProfile; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.asset.AssetProfileDao; +import org.thingsboard.server.dao.asset.AssetProfileService; +import org.thingsboard.server.dao.dashboard.DashboardService; +import org.thingsboard.server.dao.queue.QueueService; +import org.thingsboard.server.dao.rule.RuleChainService; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = AssetProfileDataValidator.class) +class AssetProfileDataValidatorTest { + + @MockBean + AssetProfileDao assetProfileDao; + @MockBean + AssetProfileService assetProfileService; + @MockBean + TenantService tenantService; + @MockBean + QueueService queueService; + @MockBean + RuleChainService ruleChainService; + @MockBean + DashboardService dashboardService; + @SpyBean + AssetProfileDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + AssetProfile assetProfile = new AssetProfile(); + assetProfile.setName("prod"); + assetProfile.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, assetProfile); + verify(validator).validateString("Asset profile name", assetProfile.getName()); + } + +} \ No newline at end of file diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidatorTest.java new file mode 100644 index 0000000000..74fac9cfb9 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidatorTest.java @@ -0,0 +1,61 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.thingsboard.server.common.data.OtaPackageInfo; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.ota.OtaPackageType; +import org.thingsboard.server.dao.device.DeviceProfileDao; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +class BaseOtaPackageDataValidatorTest { + + + DeviceProfileDao deviceProfileDao = mock(DeviceProfileDao.class); + TenantService tenantService = mock(TenantService.class); + BaseOtaPackageDataValidator validator = spy(BaseOtaPackageDataValidator.class); + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + willReturn(tenantService).given(validator).getTenantService(); + willReturn(deviceProfileDao).given(validator).getDeviceProfileDao(); + } + + @Test + void testValidateNameInvocation() { + OtaPackageInfo otaPackageInfo = new OtaPackageInfo(); + otaPackageInfo.setTitle("fw"); + otaPackageInfo.setVersion("1.0"); + otaPackageInfo.setType(OtaPackageType.FIRMWARE); + otaPackageInfo.setTenantId(tenantId); + + validator.validateImpl(otaPackageInfo); + verify(validator).validateString("OtaPackage title", otaPackageInfo.getTitle()); + } + + +} \ No newline at end of file diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidatorTest.java new file mode 100644 index 0000000000..7effbf57ba --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidatorTest.java @@ -0,0 +1,43 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.plugin.ComponentDescriptor; +import org.thingsboard.server.common.data.plugin.ComponentScope; +import org.thingsboard.server.common.data.plugin.ComponentType; + +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = ComponentDescriptorDataValidator.class) +class ComponentDescriptorDataValidatorTest { + @SpyBean + ComponentDescriptorDataValidator validator; + + @Test + void testValidateNameInvocation() { + ComponentDescriptor plugin = new ComponentDescriptor(); + plugin.setType(ComponentType.ENRICHMENT); + plugin.setScope(ComponentScope.SYSTEM); + plugin.setName("originator attributes"); + plugin.setClazz("org.thingsboard.rule.engine.metadata.TbGetAttributesNode"); + validator.validateDataImpl(TenantId.SYS_TENANT_ID, plugin); + verify(validator).validateString("Component name", plugin.getName()); + } +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/CustomerDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/CustomerDataValidatorTest.java new file mode 100644 index 0000000000..67329ba864 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/CustomerDataValidatorTest.java @@ -0,0 +1,59 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.Customer; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.customer.CustomerDao; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = CustomerDataValidator.class) +class CustomerDataValidatorTest { + + @MockBean + CustomerDao customerDao; + @MockBean + TenantService tenantService; + @SpyBean + CustomerDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + Customer customer = new Customer(); + customer.setTitle("Customer A"); + customer.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, customer); + verify(validator).validateString("Customer title", customer.getTitle()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/DashboardDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DashboardDataValidatorTest.java new file mode 100644 index 0000000000..b2953ad912 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DashboardDataValidatorTest.java @@ -0,0 +1,56 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.Dashboard; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = DashboardDataValidator.class) +class DashboardDataValidatorTest { + + @MockBean + TenantService tenantService; + @SpyBean + DashboardDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + Dashboard dashboard = new Dashboard(); + dashboard.setTitle("flight control"); + dashboard.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, dashboard); + verify(validator).validateString("Dashboard title", dashboard.getTitle()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java new file mode 100644 index 0000000000..4c4ed5a0e2 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java @@ -0,0 +1,85 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.thingsboard.server.common.data.Device; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.customer.CustomerDao; +import org.thingsboard.server.dao.device.DeviceDao; +import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.willReturn; + +@SpringBootTest(classes = DeviceDataValidator.class) +@Slf4j +class DeviceDataValidatorTest { + + @MockBean + DeviceDao deviceDao; + @MockBean + TenantService tenantService; + @MockBean + CustomerDao customerDao; + @Autowired + DeviceDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @ParameterizedTest + @ValueSource(strings = { + "coffee", "1", "big box", "世界", "!", "--", "~!@#$%^&*()_+=-/|\\[]{};:'`\"?<>,.", "\uD83D\uDC0C", "\041", + "Gdy Pomorze nie pomoże, to pomoże może morze, a gdy morze nie pomoże, to pomoże może Gdańsk", + }) + void testDeviceName_thenOK(final String name) { + Device device = new Device(); + device.setTenantId(tenantId); + device.setName(name); + validator.validateDataImpl(tenantId, device); + } + + @ParameterizedTest + @ValueSource(strings = { + "", " ", " ", "\n", "\r\n", "\t", "\000", "\000\000", "\001", "\002", "\040", "\u0000", "\u0000\u0000", + "F0929906\000\000\000\000\000\000\000\000\000", "\000\000\000F0929906", + "\u0000F0929906", "F092\u00009906", "F0929906\u0000" + }) + void testDeviceName_thenDataValidationException(final String name) { + Device device = new Device(); + device.setTenantId(tenantId); + device.setName(name); + + DataValidationException exception = Assertions.assertThrows(DataValidationException.class, () -> validator.validateDataImpl(tenantId, device)); + log.warn("Exception message: {}", exception.getMessage()); + assertThat(exception.getMessage()).as("message Device name").containsPattern("Device name .*"); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java new file mode 100644 index 0000000000..fc066c201d --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java @@ -0,0 +1,83 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.DeviceProfile; +import org.thingsboard.server.common.data.DeviceProfileType; +import org.thingsboard.server.common.data.DeviceTransportType; +import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration; +import org.thingsboard.server.common.data.device.profile.DeviceProfileData; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.dashboard.DashboardService; +import org.thingsboard.server.dao.device.DeviceDao; +import org.thingsboard.server.dao.device.DeviceProfileDao; +import org.thingsboard.server.dao.device.DeviceProfileService; +import org.thingsboard.server.dao.queue.QueueService; +import org.thingsboard.server.dao.rule.RuleChainService; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = DeviceProfileDataValidator.class) +class DeviceProfileDataValidatorTest { + + @MockBean + DeviceProfileDao deviceProfileDao; + @MockBean + DeviceProfileService deviceProfileService; + @MockBean + DeviceDao deviceDao; + @MockBean + TenantService tenantService; + @MockBean + QueueService queueService; + @MockBean + RuleChainService ruleChainService; + @MockBean + DashboardService dashboardService; + @SpyBean + DeviceProfileDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + DeviceProfile deviceProfile = new DeviceProfile(); + deviceProfile.setName("default"); + deviceProfile.setType(DeviceProfileType.DEFAULT); + deviceProfile.setTransportType(DeviceTransportType.DEFAULT); + DeviceProfileData data = new DeviceProfileData(); + data.setTransportConfiguration(new DefaultDeviceProfileTransportConfiguration()); + deviceProfile.setProfileData(data); + deviceProfile.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, deviceProfile); + verify(validator).validateString("Device profile name", deviceProfile.getName()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/EdgeDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/EdgeDataValidatorTest.java new file mode 100644 index 0000000000..d852b3680a --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/EdgeDataValidatorTest.java @@ -0,0 +1,66 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.edge.Edge; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.customer.CustomerDao; +import org.thingsboard.server.dao.edge.EdgeDao; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = EdgeDataValidator.class) +class EdgeDataValidatorTest { + + @MockBean + EdgeDao edgeDao; + @MockBean + TenantService tenantService; + @MockBean + CustomerDao customerDao; + @SpyBean + EdgeDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + Edge edge = new Edge(); + edge.setName("Edge 007"); + edge.setType("Silos"); + edge.setSecret("secret"); + edge.setRoutingKey("53c56104-d302-4d6e-97f5-a7a99c7effdc"); + edge.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, edge); + verify(validator).validateString("Edge name", edge.getName()); + verify(validator).validateString("Edge type", edge.getType()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/EntityViewDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/EntityViewDataValidatorTest.java new file mode 100644 index 0000000000..0eebb15554 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/EntityViewDataValidatorTest.java @@ -0,0 +1,64 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.customer.CustomerDao; +import org.thingsboard.server.dao.entityview.EntityViewDao; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = EntityViewDataValidator.class) +class EntityViewDataValidatorTest { + + @MockBean + EntityViewDao entityViewDao; + @MockBean + TenantService tenantService; + @MockBean + CustomerDao customerDao; + @SpyBean + EntityViewDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + EntityView entityView = new EntityView(); + entityView.setName("view"); + entityView.setType("default"); + entityView.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, entityView); + verify(validator).validateString("Entity view name", entityView.getName()); + verify(validator).validateString("Entity view type", entityView.getType()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/ResourceDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/ResourceDataValidatorTest.java new file mode 100644 index 0000000000..985b80331f --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/ResourceDataValidatorTest.java @@ -0,0 +1,69 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.ResourceType; +import org.thingsboard.server.common.data.TbResource; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.resource.TbResourceDao; +import org.thingsboard.server.dao.tenant.TbTenantProfileCache; +import org.thingsboard.server.dao.tenant.TenantService; +import org.thingsboard.server.dao.widget.WidgetTypeDao; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = ResourceDataValidator.class) +class ResourceDataValidatorTest { + + @MockBean + TbResourceDao resourceDao; + @MockBean + WidgetTypeDao widgetTypeDao; + @MockBean + TenantService tenantService; + @MockBean + TbTenantProfileCache tenantProfileCache; + @SpyBean + ResourceDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + TbResource resource = new TbResource(); + resource.setTitle("rss"); + resource.setResourceType(ResourceType.PKCS_12); + resource.setFileName("cert.pem"); + resource.setResourceKey("19_1.0"); + resource.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, resource); + verify(validator).validateString("Resource title", resource.getTitle()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/RuleChainDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/RuleChainDataValidatorTest.java new file mode 100644 index 0000000000..7f9d3e1fc2 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/RuleChainDataValidatorTest.java @@ -0,0 +1,64 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.rule.RuleChain; +import org.thingsboard.server.common.data.rule.RuleChainType; +import org.thingsboard.server.dao.rule.RuleChainDao; +import org.thingsboard.server.dao.rule.RuleChainService; +import org.thingsboard.server.dao.tenant.TenantService; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = RuleChainDataValidator.class) +class RuleChainDataValidatorTest { + + @MockBean + RuleChainDao ruleChainDao; + @MockBean + RuleChainService ruleChainService; + @MockBean + TenantService tenantService; + @SpyBean + RuleChainDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + RuleChain ruleChain = new RuleChain(); + ruleChain.setName("generate daily report"); + ruleChain.setType(RuleChainType.CORE); + ruleChain.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, ruleChain); + verify(validator).validateString("Rule chain name", ruleChain.getName()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/TenantDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/TenantDataValidatorTest.java new file mode 100644 index 0000000000..e6c6a96146 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/TenantDataValidatorTest.java @@ -0,0 +1,49 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.Tenant; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.tenant.TenantDao; + +import java.util.UUID; + +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = TenantDataValidator.class) +class TenantDataValidatorTest { + + @MockBean + TenantDao tenantDao; + @SpyBean + TenantDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @Test + void testValidateNameInvocation() { + Tenant tenant = new Tenant(); + tenant.setTitle("Monster corporation ©"); + tenant.setEmail("support@thingsboard.io"); + + validator.validateDataImpl(tenantId, tenant); + verify(validator).validateString("Tenant title", tenant.getTitle()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/TenantProfileDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/TenantProfileDataValidatorTest.java new file mode 100644 index 0000000000..e3bca21984 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/TenantProfileDataValidatorTest.java @@ -0,0 +1,56 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.TenantProfile; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; +import org.thingsboard.server.common.data.tenant.profile.TenantProfileData; +import org.thingsboard.server.dao.tenant.TenantProfileDao; +import org.thingsboard.server.dao.tenant.TenantProfileService; + +import java.util.UUID; + +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = TenantProfileDataValidator.class) +class TenantProfileDataValidatorTest { + + @MockBean + TenantProfileDao tenantProfileDao; + @MockBean + TenantProfileService tenantProfileService; + @SpyBean + TenantProfileDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @Test + void testValidateNameInvocation() { + TenantProfile tenantProfile = new TenantProfile(); + tenantProfile.setName("Sandbox"); + TenantProfileData tenantProfileData = new TenantProfileData(); + tenantProfileData.setConfiguration(new DefaultTenantProfileConfiguration()); + tenantProfile.setProfileData(tenantProfileData); + + validator.validateDataImpl(tenantId, tenantProfile); + verify(validator).validateString("Tenant profile name", tenantProfile.getName()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/WidgetTypeDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/WidgetTypeDataValidatorTest.java new file mode 100644 index 0000000000..209d2b4bb8 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/WidgetTypeDataValidatorTest.java @@ -0,0 +1,63 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.common.util.JacksonUtil; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.widget.WidgetTypeDetails; +import org.thingsboard.server.dao.tenant.TenantService; +import org.thingsboard.server.dao.widget.WidgetTypeDao; +import org.thingsboard.server.dao.widget.WidgetsBundleDao; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = WidgetTypeDataValidator.class) +class WidgetTypeDataValidatorTest { + @MockBean + WidgetTypeDao widgetTypeDao; + @MockBean + WidgetsBundleDao widgetsBundleDao; + @MockBean + TenantService tenantService; + @SpyBean + WidgetTypeDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + WidgetTypeDetails widgetTypeDetails = new WidgetTypeDetails(); + widgetTypeDetails.setName("widget type gas"); + widgetTypeDetails.setDescriptor(JacksonUtil.toJsonNode("{\"content\":\"empty\"}")); + widgetTypeDetails.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, widgetTypeDetails); + verify(validator).validateString("Widgets type name", widgetTypeDetails.getName()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/validator/WidgetsBundleDataValidatorTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/validator/WidgetsBundleDataValidatorTest.java new file mode 100644 index 0000000000..9e54f85689 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/service/validator/WidgetsBundleDataValidatorTest.java @@ -0,0 +1,59 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.service.validator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.widget.WidgetsBundle; +import org.thingsboard.server.dao.tenant.TenantService; +import org.thingsboard.server.dao.widget.WidgetsBundleDao; + +import java.util.UUID; + +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.verify; + +@SpringBootTest(classes = WidgetsBundleDataValidator.class) +class WidgetsBundleDataValidatorTest { + + @MockBean + WidgetsBundleDao widgetsBundleDao; + @MockBean + TenantService tenantService; + @SpyBean + WidgetsBundleDataValidator validator; + TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); + + @BeforeEach + void setUp() { + willReturn(true).given(tenantService).tenantExists(tenantId); + } + + @Test + void testValidateNameInvocation() { + WidgetsBundle widgetsBundle = new WidgetsBundle(); + widgetsBundle.setTitle("my fancy WB"); + widgetsBundle.setTenantId(tenantId); + + validator.validateDataImpl(tenantId, widgetsBundle); + verify(validator).validateString("Widgets bundle title", widgetsBundle.getTitle()); + } + +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/sql/asset/JpaAssetDaoTest.java b/dao/src/test/java/org/thingsboard/server/dao/sql/asset/JpaAssetDaoTest.java index ef68465256..1ffb740553 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/sql/asset/JpaAssetDaoTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/sql/asset/JpaAssetDaoTest.java @@ -44,6 +44,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -94,6 +95,12 @@ public class JpaAssetDaoTest extends AbstractJpaDaoTest { savedAssetProfiles.clear(); } + @Test + public void testSaveDeviceName0x00_thenSomeDatabaseException() { + assertThatThrownBy(() -> assets.add( + saveAsset(UUID.randomUUID(), tenantId2, customerId2, "F0929906\000\000\000\000\000\000\000\000\000"))); + } + @Test public void testFindAssetsByTenantId() { PageLink pageLink = new PageLink(20, 0, "ASSET_"); diff --git a/dao/src/test/java/org/thingsboard/server/dao/sql/device/JpaDeviceDaoTest.java b/dao/src/test/java/org/thingsboard/server/dao/sql/device/JpaDeviceDaoTest.java index dc174b7423..c8e6c2cfc5 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/sql/device/JpaDeviceDaoTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/sql/device/JpaDeviceDaoTest.java @@ -45,6 +45,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -101,6 +102,12 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest { } } + @Test + public void testSaveDeviceName0x00_thenSomeDatabaseException() { + Device device = getDevice(tenantId1, customerId1, "\u0000"); + assertThatThrownBy(() -> deviceIds.add(deviceDao.save(TenantId.fromUUID(tenantId1), device).getUuidId())); + } + @Test public void testFindDevicesByTenantId() { PageLink pageLink = new PageLink(15, 0, PREFIX_FOR_DEVICE_NAME); @@ -155,16 +162,16 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest { return savedDevicesUUID; } - private Device getDevice(UUID tenantId, UUID customerID, int number) { - return getDevice(tenantId, customerID, Uuids.timeBased(), number); + private Device getDevice(UUID tenantId, UUID customerID, Object nameSuffix) { + return getDevice(tenantId, customerID, Uuids.timeBased(), nameSuffix); } - private Device getDevice(UUID tenantId, UUID customerID, UUID deviceId, int number) { + private Device getDevice(UUID tenantId, UUID customerID, UUID deviceId, Object nameSuffix) { Device device = new Device(); device.setId(new DeviceId(deviceId)); device.setTenantId(TenantId.fromUUID(tenantId)); device.setCustomerId(new CustomerId(customerID)); - device.setName(PREFIX_FOR_DEVICE_NAME + number); + device.setName(PREFIX_FOR_DEVICE_NAME + nameSuffix); device.setDeviceProfileId(savedDeviceProfile.getId()); return device; }