Browse Source

Merge pull request #9163 from smatvienko-tb/feature/validate_contains0x00

[3.5.2] Device Asset name - Validate contains0x00 to prevent PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00
pull/9198/head
Andrew Shvayka 3 years ago
committed by GitHub
parent
commit
333f41b94a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java
  2. 40
      common/data/src/test/java/org/thingsboard/server/common/data/StringUtilsTest.java
  3. 13
      dao/src/main/java/org/thingsboard/server/dao/service/DataValidator.java
  4. 6
      dao/src/main/java/org/thingsboard/server/dao/service/validator/AdminSettingsDataValidator.java
  5. 5
      dao/src/main/java/org/thingsboard/server/dao/service/validator/AlarmDataValidator.java
  6. 5
      dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetDataValidator.java
  7. 4
      dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidator.java
  8. 13
      dao/src/main/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidator.java
  9. 4
      dao/src/main/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidator.java
  10. 4
      dao/src/main/java/org/thingsboard/server/dao/service/validator/CustomerDataValidator.java
  11. 9
      dao/src/main/java/org/thingsboard/server/dao/service/validator/DashboardDataValidator.java
  12. 5
      dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceDataValidator.java
  13. 4
      dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java
  14. 8
      dao/src/main/java/org/thingsboard/server/dao/service/validator/EdgeDataValidator.java
  15. 9
      dao/src/main/java/org/thingsboard/server/dao/service/validator/EntityViewDataValidator.java
  16. 4
      dao/src/main/java/org/thingsboard/server/dao/service/validator/ResourceDataValidator.java
  17. 5
      dao/src/main/java/org/thingsboard/server/dao/service/validator/RuleChainDataValidator.java
  18. 4
      dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantDataValidator.java
  19. 5
      dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantProfileDataValidator.java
  20. 6
      dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetTypeDataValidator.java
  21. 5
      dao/src/main/java/org/thingsboard/server/dao/service/validator/WidgetsBundleDataValidator.java
  22. 11
      dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java
  23. 113
      dao/src/test/java/org/thingsboard/server/dao/service/DataValidatorTest.java
  24. 11
      dao/src/test/java/org/thingsboard/server/dao/service/DeviceServiceTest.java
  25. 50
      dao/src/test/java/org/thingsboard/server/dao/service/validator/AdminSettingsDataValidatorTest.java
  26. 61
      dao/src/test/java/org/thingsboard/server/dao/service/validator/AlarmDataValidatorTest.java
  27. 85
      dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetDataValidatorTest.java
  28. 71
      dao/src/test/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidatorTest.java
  29. 61
      dao/src/test/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidatorTest.java
  30. 43
      dao/src/test/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidatorTest.java
  31. 59
      dao/src/test/java/org/thingsboard/server/dao/service/validator/CustomerDataValidatorTest.java
  32. 56
      dao/src/test/java/org/thingsboard/server/dao/service/validator/DashboardDataValidatorTest.java
  33. 85
      dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java
  34. 83
      dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidatorTest.java
  35. 66
      dao/src/test/java/org/thingsboard/server/dao/service/validator/EdgeDataValidatorTest.java
  36. 64
      dao/src/test/java/org/thingsboard/server/dao/service/validator/EntityViewDataValidatorTest.java
  37. 69
      dao/src/test/java/org/thingsboard/server/dao/service/validator/ResourceDataValidatorTest.java
  38. 64
      dao/src/test/java/org/thingsboard/server/dao/service/validator/RuleChainDataValidatorTest.java
  39. 49
      dao/src/test/java/org/thingsboard/server/dao/service/validator/TenantDataValidatorTest.java
  40. 56
      dao/src/test/java/org/thingsboard/server/dao/service/validator/TenantProfileDataValidatorTest.java
  41. 63
      dao/src/test/java/org/thingsboard/server/dao/service/validator/WidgetTypeDataValidatorTest.java
  42. 59
      dao/src/test/java/org/thingsboard/server/dao/service/validator/WidgetsBundleDataValidatorTest.java
  43. 7
      dao/src/test/java/org/thingsboard/server/dao/sql/asset/JpaAssetDaoTest.java
  44. 15
      dao/src/test/java/org/thingsboard/server/dao/sql/device/JpaDeviceDaoTest.java

7
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); 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) { public static String randomNumeric(int length) {
return RandomStringUtils.randomNumeric(length); return RandomStringUtils.randomNumeric(length);
} }

40
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();
}
}

13
dao/src/main/java/org/thingsboard/server/dao/service/DataValidator.java

@ -86,6 +86,15 @@ public abstract class DataValidator<D extends BaseData<?>> {
public void validateDelete(TenantId tenantId, EntityId entityId) { 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) { protected boolean isSameData(D existentData, D actualData) {
return actualData.getId() != null && existentData.getId().equals(actualData.getId()); return actualData.getId() != null && existentData.getId().equals(actualData.getId());
} }
@ -151,8 +160,8 @@ public abstract class DataValidator<D extends BaseData<?>> {
validateQueueNameOrTopic(topic, TOPIC); validateQueueNameOrTopic(topic, TOPIC);
} }
private static void validateQueueNameOrTopic(String value, String fieldName) { static void validateQueueNameOrTopic(String value, String fieldName) {
if (StringUtils.isEmpty(value)) { if (StringUtils.isEmpty(value) || value.trim().length() == 0 ) {
throw new DataValidationException(String.format("Queue %s should be specified!", fieldName)); throw new DataValidationException(String.format("Queue %s should be specified!", fieldName));
} }
if (!QUEUE_PATTERN.matcher(value).matches()) { if (!QUEUE_PATTERN.matcher(value).matches()) {

6
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 lombok.AllArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.AdminSettings; 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.common.data.id.TenantId;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.DataValidator;
@ -49,12 +48,9 @@ public class AdminSettingsDataValidator extends DataValidator<AdminSettings> {
return existentAdminSettings; return existentAdminSettings;
} }
@Override @Override
protected void validateDataImpl(TenantId tenantId, AdminSettings adminSettings) { protected void validateDataImpl(TenantId tenantId, AdminSettings adminSettings) {
if (StringUtils.isEmpty(adminSettings.getKey())) { validateString("Key", adminSettings.getKey());
throw new DataValidationException("Key should be specified!");
}
if (adminSettings.getJsonValue() == null) { if (adminSettings.getJsonValue() == null) {
throw new DataValidationException("Json value should be specified!"); throw new DataValidationException("Json value should be specified!");
} }

5
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 lombok.AllArgsConstructor;
import org.springframework.stereotype.Component; 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.alarm.Alarm;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
@ -32,9 +31,7 @@ public class AlarmDataValidator extends DataValidator<Alarm> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, Alarm alarm) { protected void validateDataImpl(TenantId tenantId, Alarm alarm) {
if (StringUtils.isEmpty(alarm.getType())) { validateString("Alarm type", alarm.getType());
throw new DataValidationException("Alarm type should be specified!");
}
if (alarm.getOriginator() == null) { if (alarm.getOriginator() == null) {
throw new DataValidationException("Alarm originator should be specified!"); throw new DataValidationException("Alarm originator should be specified!");
} }

5
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.springframework.stereotype.Component;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.EntityType; 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.asset.Asset;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
@ -64,9 +63,7 @@ public class AssetDataValidator extends DataValidator<Asset> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, Asset asset) { protected void validateDataImpl(TenantId tenantId, Asset asset) {
if (StringUtils.isEmpty(asset.getName())) { validateString("Asset name", asset.getName());
throw new DataValidationException("Asset name should be specified!");
}
if (asset.getTenantId() == null) { if (asset.getTenantId() == null) {
throw new DataValidationException("Asset should be assigned to tenant!"); throw new DataValidationException("Asset should be assigned to tenant!");
} else { } else {

4
dao/src/main/java/org/thingsboard/server/dao/service/validator/AssetProfileDataValidator.java

@ -53,9 +53,7 @@ public class AssetProfileDataValidator extends DataValidator<AssetProfile> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, AssetProfile assetProfile) { protected void validateDataImpl(TenantId tenantId, AssetProfile assetProfile) {
if (StringUtils.isEmpty(assetProfile.getName())) { validateString("Asset profile name", assetProfile.getName());
throw new DataValidationException("Asset profile name should be specified!");
}
if (assetProfile.getTenantId() == null) { if (assetProfile.getTenantId() == null) {
throw new DataValidationException("Asset profile should be assigned to tenant!"); throw new DataValidationException("Asset profile should be assigned to tenant!");
} else { } else {

13
dao/src/main/java/org/thingsboard/server/dao/service/validator/BaseOtaPackageDataValidator.java

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.server.dao.service.validator; package org.thingsboard.server.dao.service.validator;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.thingsboard.server.common.data.BaseData; import org.thingsboard.server.common.data.BaseData;
@ -31,23 +32,27 @@ import java.util.Objects;
public abstract class BaseOtaPackageDataValidator<D extends BaseData<?>> extends DataValidator<D> { public abstract class BaseOtaPackageDataValidator<D extends BaseData<?>> extends DataValidator<D> {
@Autowired @Autowired
@Getter
@Lazy @Lazy
private TenantService tenantService; private TenantService tenantService;
@Autowired @Autowired
@Getter
private DeviceProfileDao deviceProfileDao; private DeviceProfileDao deviceProfileDao;
protected void validateImpl(OtaPackageInfo otaPackageInfo) { protected void validateImpl(OtaPackageInfo otaPackageInfo) {
validateString("OtaPackage title", otaPackageInfo.getTitle());
if (otaPackageInfo.getTenantId() == null) { if (otaPackageInfo.getTenantId() == null) {
throw new DataValidationException("OtaPackage should be assigned to tenant!"); throw new DataValidationException("OtaPackage should be assigned to tenant!");
} else { } else {
if (!tenantService.tenantExists(otaPackageInfo.getTenantId())) { if (!getTenantService().tenantExists(otaPackageInfo.getTenantId())) {
throw new DataValidationException("OtaPackage is referencing to non-existent tenant!"); throw new DataValidationException("OtaPackage is referencing to non-existent tenant!");
} }
} }
if (otaPackageInfo.getDeviceProfileId() != null) { 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) { if (deviceProfile == null) {
throw new DataValidationException("OtaPackage is referencing to non-existent device profile!"); throw new DataValidationException("OtaPackage is referencing to non-existent device profile!");
} }
@ -57,10 +62,6 @@ public abstract class BaseOtaPackageDataValidator<D extends BaseData<?>> extends
throw new DataValidationException("Type should be specified!"); 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())) { if (StringUtils.isEmpty(otaPackageInfo.getVersion())) {
throw new DataValidationException("OtaPackage version should be specified!"); throw new DataValidationException("OtaPackage version should be specified!");
} }

4
dao/src/main/java/org/thingsboard/server/dao/service/validator/ComponentDescriptorDataValidator.java

@ -27,15 +27,13 @@ public class ComponentDescriptorDataValidator extends DataValidator<ComponentDes
@Override @Override
protected void validateDataImpl(TenantId tenantId, ComponentDescriptor plugin) { protected void validateDataImpl(TenantId tenantId, ComponentDescriptor plugin) {
validateString("Component name", plugin.getName());
if (plugin.getType() == null) { if (plugin.getType() == null) {
throw new DataValidationException("Component type should be specified!"); throw new DataValidationException("Component type should be specified!");
} }
if (plugin.getScope() == null) { if (plugin.getScope() == null) {
throw new DataValidationException("Component scope should be specified!"); throw new DataValidationException("Component scope should be specified!");
} }
if (StringUtils.isEmpty(plugin.getName())) {
throw new DataValidationException("Component name should be specified!");
}
if (StringUtils.isEmpty(plugin.getClazz())) { if (StringUtils.isEmpty(plugin.getClazz())) {
throw new DataValidationException("Component clazz should be specified!"); throw new DataValidationException("Component clazz should be specified!");
} }

4
dao/src/main/java/org/thingsboard/server/dao/service/validator/CustomerDataValidator.java

@ -63,9 +63,7 @@ public class CustomerDataValidator extends DataValidator<Customer> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, Customer customer) { protected void validateDataImpl(TenantId tenantId, Customer customer) {
if (StringUtils.isEmpty(customer.getTitle())) { validateString("Customer title", customer.getTitle());
throw new DataValidationException("Customer title should be specified!");
}
if (customer.getTitle().equals(CustomerServiceImpl.PUBLIC_CUSTOMER_TITLE)) { if (customer.getTitle().equals(CustomerServiceImpl.PUBLIC_CUSTOMER_TITLE)) {
throw new DataValidationException("'Public' title for customer is system reserved!"); throw new DataValidationException("'Public' title for customer is system reserved!");
} }

9
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.springframework.stereotype.Component;
import org.thingsboard.server.common.data.Dashboard; import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.EntityType; 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.id.TenantId;
import org.thingsboard.server.dao.dashboard.DashboardDao;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.tenant.TenantService; import org.thingsboard.server.dao.tenant.TenantService;
@ -29,9 +27,6 @@ import org.thingsboard.server.dao.tenant.TenantService;
@Component @Component
public class DashboardDataValidator extends DataValidator<Dashboard> { public class DashboardDataValidator extends DataValidator<Dashboard> {
@Autowired
private DashboardDao dashboardDao;
@Autowired @Autowired
private TenantService tenantService; private TenantService tenantService;
@ -42,9 +37,7 @@ public class DashboardDataValidator extends DataValidator<Dashboard> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, Dashboard dashboard) { protected void validateDataImpl(TenantId tenantId, Dashboard dashboard) {
if (StringUtils.isEmpty(dashboard.getTitle())) { validateString("Dashboard title", dashboard.getTitle());
throw new DataValidationException("Dashboard title should be specified!");
}
if (dashboard.getTenantId() == null) { if (dashboard.getTenantId() == null) {
throw new DataValidationException("Dashboard should be assigned to tenant!"); throw new DataValidationException("Dashboard should be assigned to tenant!");
} else { } else {

5
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.Customer;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityType; 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.device.data.DeviceTransportConfiguration;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
@ -61,9 +60,7 @@ public class DeviceDataValidator extends AbstractHasOtaPackageValidator<Device>
@Override @Override
protected void validateDataImpl(TenantId tenantId, Device device) { protected void validateDataImpl(TenantId tenantId, Device device) {
if (StringUtils.isEmpty(device.getName()) || device.getName().trim().length() == 0) { validateString("Device name", device.getName());
throw new DataValidationException("Device name should be specified!");
}
if (device.getTenantId() == null) { if (device.getTenantId() == null) {
throw new DataValidationException("Device should be assigned to tenant!"); throw new DataValidationException("Device should be assigned to tenant!");
} else { } else {

4
dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java

@ -103,9 +103,7 @@ public class DeviceProfileDataValidator extends AbstractHasOtaPackageValidator<D
@Override @Override
protected void validateDataImpl(TenantId tenantId, DeviceProfile deviceProfile) { protected void validateDataImpl(TenantId tenantId, DeviceProfile deviceProfile) {
if (StringUtils.isEmpty(deviceProfile.getName())) { validateString("Device profile name", deviceProfile.getName());
throw new DataValidationException("Device profile name should be specified!");
}
if (deviceProfile.getType() == null) { if (deviceProfile.getType() == null) {
throw new DataValidationException("Device profile type should be specified!"); throw new DataValidationException("Device profile type should be specified!");
} }

8
dao/src/main/java/org/thingsboard/server/dao/service/validator/EdgeDataValidator.java

@ -49,12 +49,8 @@ public class EdgeDataValidator extends DataValidator<Edge> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, Edge edge) { protected void validateDataImpl(TenantId tenantId, Edge edge) {
if (StringUtils.isEmpty(edge.getType())) { validateString("Edge name", edge.getName());
throw new DataValidationException("Edge type should be specified!"); validateString("Edge type", edge.getType());
}
if (StringUtils.isEmpty(edge.getName())) {
throw new DataValidationException("Edge name should be specified!");
}
if (StringUtils.isEmpty(edge.getSecret())) { if (StringUtils.isEmpty(edge.getSecret())) {
throw new DataValidationException("Edge secret should be specified!"); throw new DataValidationException("Edge secret should be specified!");
} }

9
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.springframework.stereotype.Component;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.EntityView; 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.CustomerId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.customer.CustomerDao; import org.thingsboard.server.dao.customer.CustomerDao;
@ -59,12 +58,8 @@ public class EntityViewDataValidator extends DataValidator<EntityView> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, EntityView entityView) { protected void validateDataImpl(TenantId tenantId, EntityView entityView) {
if (StringUtils.isEmpty(entityView.getType())) { validateString("Entity view name", entityView.getName());
throw new DataValidationException("Entity View type should be specified!"); validateString("Entity view type", entityView.getType());
}
if (StringUtils.isEmpty(entityView.getName())) {
throw new DataValidationException("Entity view name should be specified!");
}
if (entityView.getTenantId() == null) { if (entityView.getTenantId() == null) {
throw new DataValidationException("Entity view should be assigned to tenant!"); throw new DataValidationException("Entity view should be assigned to tenant!");
} else { } else {

4
dao/src/main/java/org/thingsboard/server/dao/service/validator/ResourceDataValidator.java

@ -66,9 +66,7 @@ public class ResourceDataValidator extends DataValidator<TbResource> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, TbResource resource) { protected void validateDataImpl(TenantId tenantId, TbResource resource) {
if (StringUtils.isEmpty(resource.getTitle())) { validateString("Resource title", resource.getTitle());
throw new DataValidationException("Resource title should be specified!");
}
if (resource.getResourceType() == null) { if (resource.getResourceType() == null) {
throw new DataValidationException("Resource type should be specified!"); throw new DataValidationException("Resource type should be specified!");
} }

5
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.springframework.stereotype.Component;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.EntityType; 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.id.TenantId;
import org.thingsboard.server.common.data.rule.NodeConnectionInfo; import org.thingsboard.server.common.data.rule.NodeConnectionInfo;
import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChain;
@ -65,9 +64,7 @@ public class RuleChainDataValidator extends DataValidator<RuleChain> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, RuleChain ruleChain) { protected void validateDataImpl(TenantId tenantId, RuleChain ruleChain) {
if (StringUtils.isEmpty(ruleChain.getName())) { validateString("Rule chain name", ruleChain.getName());
throw new DataValidationException("Rule chain name should be specified!");
}
if (ruleChain.getType() == null) { if (ruleChain.getType() == null) {
ruleChain.setType(RuleChainType.CORE); ruleChain.setType(RuleChainType.CORE);
} }

4
dao/src/main/java/org/thingsboard/server/dao/service/validator/TenantDataValidator.java

@ -32,9 +32,7 @@ public class TenantDataValidator extends DataValidator<Tenant> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, Tenant tenant) { protected void validateDataImpl(TenantId tenantId, Tenant tenant) {
if (StringUtils.isEmpty(tenant.getTitle())) { validateString("Tenant title", tenant.getTitle());
throw new DataValidationException("Tenant title should be specified!");
}
if (!StringUtils.isEmpty(tenant.getEmail())) { if (!StringUtils.isEmpty(tenant.getEmail())) {
validateEmail(tenant.getEmail()); validateEmail(tenant.getEmail());
} }

5
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.context.annotation.Lazy;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.DataConstants; 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.TenantProfile;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.queue.ProcessingStrategy; import org.thingsboard.server.common.data.queue.ProcessingStrategy;
@ -48,9 +47,7 @@ public class TenantProfileDataValidator extends DataValidator<TenantProfile> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, TenantProfile tenantProfile) { protected void validateDataImpl(TenantId tenantId, TenantProfile tenantProfile) {
if (StringUtils.isEmpty(tenantProfile.getName())) { validateString("Tenant profile name", tenantProfile.getName());
throw new DataValidationException("Tenant profile name should be specified!");
}
if (tenantProfile.getProfileData() == null) { if (tenantProfile.getProfileData() == null) {
throw new DataValidationException("Tenant profile data should be specified!"); throw new DataValidationException("Tenant profile data should be specified!");
} }

6
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 lombok.AllArgsConstructor;
import org.springframework.stereotype.Component; 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.id.TenantId;
import org.thingsboard.server.common.data.widget.WidgetType; import org.thingsboard.server.common.data.widget.WidgetType;
import org.thingsboard.server.common.data.widget.WidgetTypeDetails; 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.exception.DataValidationException;
import org.thingsboard.server.dao.model.ModelConstants; import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.DataValidator;
@ -39,9 +37,7 @@ public class WidgetTypeDataValidator extends DataValidator<WidgetTypeDetails> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, WidgetTypeDetails widgetTypeDetails) { protected void validateDataImpl(TenantId tenantId, WidgetTypeDetails widgetTypeDetails) {
if (StringUtils.isEmpty(widgetTypeDetails.getName())) { validateString("Widgets type name", widgetTypeDetails.getName());
throw new DataValidationException("Widgets type name should be specified!");
}
if (widgetTypeDetails.getDescriptor() == null || widgetTypeDetails.getDescriptor().size() == 0) { if (widgetTypeDetails.getDescriptor() == null || widgetTypeDetails.getDescriptor().size() == 0) {
throw new DataValidationException("Widgets type descriptor can't be empty!"); throw new DataValidationException("Widgets type descriptor can't be empty!");
} }

5
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 lombok.AllArgsConstructor;
import org.springframework.stereotype.Component; 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.id.TenantId;
import org.thingsboard.server.common.data.widget.WidgetsBundle; import org.thingsboard.server.common.data.widget.WidgetsBundle;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
@ -35,9 +34,7 @@ public class WidgetsBundleDataValidator extends DataValidator<WidgetsBundle> {
@Override @Override
protected void validateDataImpl(TenantId tenantId, WidgetsBundle widgetsBundle) { protected void validateDataImpl(TenantId tenantId, WidgetsBundle widgetsBundle) {
if (StringUtils.isEmpty(widgetsBundle.getTitle())) { validateString("Widgets bundle title", widgetsBundle.getTitle());
throw new DataValidationException("Widgets bundle title should be specified!");
}
if (widgetsBundle.getTenantId() == null) { if (widgetsBundle.getTenantId() == null) {
widgetsBundle.setTenantId(TenantId.fromUUID(ModelConstants.NULL_UUID)); widgetsBundle.setTenantId(TenantId.fromUUID(ModelConstants.NULL_UUID));
} }

11
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 @Test
public void testSaveAssetWithEmptyTenant() { public void testSaveAssetWithEmptyTenant() {
Asset asset = new Asset(); Asset asset = new Asset();

113
dao/src/test/java/org/thingsboard/server/dao/service/DataValidatorTest.java

@ -15,62 +15,93 @@
*/ */
package org.thingsboard.server.dao.service; package org.thingsboard.server.dao.service;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions; 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 org.thingsboard.server.dao.exception.DataValidationException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
@Slf4j
public class DataValidatorTest { public class DataValidatorTest {
@Test DataValidator<?> dataValidator;
public void validateEmail() {
String email = "aZ1_!#$%&'*+/=?`{|}~^.-@mail.io"; @BeforeEach
DataValidator.validateEmail(email); void setUp() {
dataValidator = spy(DataValidator.class);
} }
@Test @ParameterizedTest
public void validateInvalidEmail1() { @ValueSource(strings = {
String email = "test:1@mail.io"; "coffee", "1", "big box", "世界", "!", "--", "~!@#$%^&*()_+=-/|\\[]{};:'`\"?<>,.", "\uD83D\uDC0C", "\041",
Assertions.assertThrows(DataValidationException.class, () -> { "Gdy Pomorze nie pomoże, to pomoże może morze, a gdy morze nie pomoże, to pomoże może Gdańsk",
DataValidator.validateEmail(email); })
}); 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() { @ParameterizedTest
String email = "test()1@mail.io"; @ValueSource(strings = {
Assertions.assertThrows(DataValidationException.class, () -> { "", " ", " ", "\n", "\r\n", "\t", "\000", "\000\000", "\001", "\002", "\040", "\u0000", "\u0000\u0000",
DataValidator.validateEmail(email); "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 @ParameterizedTest
public void validateInvalidEmail3() { @ValueSource(strings = {
String email = "test[]1@mail.io"; "aZ1_!#$%&'*+/=?`{|}~^.-@mail.io", "support@thingsboard.io",
Assertions.assertThrows(DataValidationException.class, () -> { })
DataValidator.validateEmail(email); public void validateEmail(String email) {
}); DataValidator.validateEmail(email);
} }
@Test @ParameterizedTest
public void validateInvalidEmail4() { @ValueSource(strings = {
String email = "test\\1@mail.io"; "test:1@mail.io", "test()1@mail.io", "test[]1@mail.io",
Assertions.assertThrows(DataValidationException.class, () -> { "test\\1@mail.io", "test\"1@mail.io", "test<>1@mail.io",
DataValidator.validateEmail(email); })
}); public void validateEmailInvalid(String email) {
Assertions.assertThrows(DataValidationException.class, () -> DataValidator.validateEmail(email));
} }
@Test @ParameterizedTest
public void validateInvalidEmail5() { @ValueSource(strings = {
String email = "test\"1@mail.io"; "azAZ09_.-", "topic",
Assertions.assertThrows(DataValidationException.class, () -> { })
DataValidator.validateEmail(email); public void validateQueueNameOrTopic(String value) {
}); DataValidator.validateQueueNameOrTopic(value, "name");
DataValidator.validateQueueNameOrTopic(value, "topic");
} }
@Test @ParameterizedTest
public void validateInvalidEmail6() { @ValueSource(strings = {
String email = "test<>1@mail.io"; "", " ", " ", "\n", "\r\n", "\t", "\000", "\000\000", "\001", "\002", "\040", "\u0000", "\u0000\u0000",
Assertions.assertThrows(DataValidationException.class, () -> { "topic@home", "!", ",", "Łódź",
DataValidator.validateEmail(email); "\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"));
} }
} }

11
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 @Test
public void testSaveDeviceWithInvalidTenant() { public void testSaveDeviceWithInvalidTenant() {
Device device = new Device(); Device device = new Device();

50
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());
}
}

61
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());
}
}

85
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 .*");
}
}

71
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());
}
}

61
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());
}
}

43
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());
}
}

59
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());
}
}

56
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());
}
}

85
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 .*");
}
}

83
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());
}
}

66
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());
}
}

64
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());
}
}

69
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());
}
}

64
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());
}
}

49
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());
}
}

56
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());
}
}

63
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());
}
}

59
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());
}
}

7
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.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -94,6 +95,12 @@ public class JpaAssetDaoTest extends AbstractJpaDaoTest {
savedAssetProfiles.clear(); 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 @Test
public void testFindAssetsByTenantId() { public void testFindAssetsByTenantId() {
PageLink pageLink = new PageLink(20, 0, "ASSET_"); PageLink pageLink = new PageLink(20, 0, "ASSET_");

15
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.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; 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 @Test
public void testFindDevicesByTenantId() { public void testFindDevicesByTenantId() {
PageLink pageLink = new PageLink(15, 0, PREFIX_FOR_DEVICE_NAME); PageLink pageLink = new PageLink(15, 0, PREFIX_FOR_DEVICE_NAME);
@ -155,16 +162,16 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest {
return savedDevicesUUID; return savedDevicesUUID;
} }
private Device getDevice(UUID tenantId, UUID customerID, int number) { private Device getDevice(UUID tenantId, UUID customerID, Object nameSuffix) {
return getDevice(tenantId, customerID, Uuids.timeBased(), number); 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 device = new Device();
device.setId(new DeviceId(deviceId)); device.setId(new DeviceId(deviceId));
device.setTenantId(TenantId.fromUUID(tenantId)); device.setTenantId(TenantId.fromUUID(tenantId));
device.setCustomerId(new CustomerId(customerID)); device.setCustomerId(new CustomerId(customerID));
device.setName(PREFIX_FOR_DEVICE_NAME + number); device.setName(PREFIX_FOR_DEVICE_NAME + nameSuffix);
device.setDeviceProfileId(savedDeviceProfile.getId()); device.setDeviceProfileId(savedDeviceProfile.getId());
return device; return device;
} }

Loading…
Cancel
Save