DataValidator: refactored validateName to have an exception Prefix to feet many entity types needs

This commit is contained in:
Sergey Matvienko 2023-09-04 21:30:15 +02:00
parent 77d4a29f2e
commit 8224051d35
6 changed files with 27 additions and 44 deletions

View File

@ -86,12 +86,12 @@ public abstract class DataValidator<D extends BaseData<?>> {
public void validateDelete(TenantId tenantId, EntityId entityId) {
}
protected void validateName(String dataType, String name) {
protected void validateName(String exceptionPrefix, String name) {
if (StringUtils.isEmpty(name) || name.trim().length() == 0) {
throw new DataValidationException(dataType + " name should be specified!");
throw new DataValidationException(exceptionPrefix + " should be specified!");
}
if (StringUtils.contains0x00(name)) {
throw new DataValidationException(dataType + " name should not contain 0x00 symbol!");
throw new DataValidationException(exceptionPrefix + " should not contain 0x00 symbol!");
}
}

View File

@ -64,12 +64,7 @@ public class AssetDataValidator extends DataValidator<Asset> {
@Override
protected void validateDataImpl(TenantId tenantId, Asset asset) {
if (StringUtils.isEmpty(asset.getName()) || asset.getName().trim().length() == 0) {
throw new DataValidationException("Asset name should be specified!");
}
if (StringUtils.contains0x00(asset.getName())) {
throw new DataValidationException("Asset name should not contain 0x00 symbol!");
}
validateName("Asset name", asset.getName());
if (asset.getTenantId() == null) {
throw new DataValidationException("Asset should be assigned to tenant!");
} else {

View File

@ -61,12 +61,7 @@ public class DeviceDataValidator extends AbstractHasOtaPackageValidator<Device>
@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!");
}
if (StringUtils.contains0x00(device.getName())) {
throw new DataValidationException("Device name should not contain 0x00 symbol!");
}
validateName("Device name", device.getName());
if (device.getTenantId() == null) {
throw new DataValidationException("Device should be assigned to tenant!");
} else {

View File

@ -41,10 +41,12 @@ public class DataValidatorTest {
"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.validateName("Device", name);
dataValidator.validateName("Asset", name);
dataValidator.validateName("Customer", name);
dataValidator.validateName("Tenant", name);
dataValidator.validateName("Device name", name);
dataValidator.validateName("Asset name", name);
dataValidator.validateName("Asset profile name", name);
dataValidator.validateName("Alarm type", name);
dataValidator.validateName("Customer name", name);
dataValidator.validateName("Tenant name", name);
}
@ParameterizedTest
@ -55,13 +57,13 @@ public class DataValidatorTest {
})
void validateName_thenDataValidationException(final String name) {
DataValidationException exception;
exception = Assertions.assertThrows(DataValidationException.class, () -> dataValidator.validateName("Asset", name));
log.warn("Exception message Asset: {}", exception.getMessage());
assertThat(exception.getMessage()).as("message Asset").containsPattern("Asset .*name.*");
exception = Assertions.assertThrows(DataValidationException.class, () -> dataValidator.validateName("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.validateName("Device", name));
log.warn("Exception message Device: {}", exception.getMessage());
assertThat(exception.getMessage()).as("message Device").containsPattern("Device .*name.*");
exception = Assertions.assertThrows(DataValidationException.class, () -> dataValidator.validateName("Device name", name));
log.warn("Exception message Device name: {}", exception.getMessage());
assertThat(exception.getMessage()).as("message Device name").containsPattern("Device name .*");
}
@ParameterizedTest

View File

@ -16,6 +16,7 @@
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;
@ -31,8 +32,7 @@ import org.thingsboard.server.dao.tenant.TenantService;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.ThrowableAssert.catchThrowableOfType;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.willReturn;
@SpringBootTest(classes = AssetDataValidator.class)
@ -77,13 +77,9 @@ class AssetDataValidatorTest {
asset.setTenantId(tenantId);
asset.setName(name);
DataValidationException exception = catchThrowableOfType(() ->
validator.validateDataImpl(tenantId, asset), DataValidationException.class);
log.warn("Exception message: {}", exception == null ? null : exception.getMessage());
assertThatThrownBy(() -> validator.validateDataImpl(tenantId, asset))
.isInstanceOf(DataValidationException.class)
.hasMessageMatching(".*Asset.*");
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 .*");
}
}

View File

@ -16,6 +16,7 @@
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;
@ -31,8 +32,7 @@ import org.thingsboard.server.dao.tenant.TenantService;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.ThrowableAssert.catchThrowableOfType;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.willReturn;
@SpringBootTest(classes = DeviceDataValidator.class)
@ -72,19 +72,14 @@ class DeviceDataValidatorTest {
"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 = catchThrowableOfType(() ->
validator.validateDataImpl(tenantId, device), DataValidationException.class);
log.warn("Exception message: {}", exception == null ? null : exception.getMessage());
assertThatThrownBy(() -> validator.validateDataImpl(tenantId, device))
.isInstanceOf(DataValidationException.class)
.hasMessageMatching(".*Device.*");
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 .*");
}
}