Browse Source

DataValidator: extracted validateName method from Asset and Device to use in all other validators. Tests added

pull/9163/head
Sergey Matvienko 3 years ago
parent
commit
608d3a6621
  1. 9
      dao/src/main/java/org/thingsboard/server/dao/service/DataValidator.java
  2. 44
      dao/src/test/java/org/thingsboard/server/dao/service/DataValidatorTest.java

9
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) {
}
protected void validateName(String dataType, String name) {
if (StringUtils.isEmpty(name) || name.trim().length() == 0) {
throw new DataValidationException(dataType + " name should be specified!");
}
if (StringUtils.contains0x00(name)) {
throw new DataValidationException(dataType + " name should not contain 0x00 symbol!");
}
}
protected boolean isSameData(D existentData, D actualData) {
return actualData.getId() != null && existentData.getId().equals(actualData.getId());
}

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

@ -15,12 +15,56 @@
*/
package org.thingsboard.server.dao.service;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.thingsboard.server.dao.exception.DataValidationException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
@Slf4j
public class DataValidatorTest {
DataValidator<?> dataValidator;
@BeforeEach
void setUp() {
dataValidator = spy(DataValidator.class);
}
@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) {
dataValidator.validateName("Device", name);
dataValidator.validateName("Asset", name);
dataValidator.validateName("Customer", name);
dataValidator.validateName("Tenant", name);
}
@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) {
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("Device", name));
log.warn("Exception message Device: {}", exception.getMessage());
assertThat(exception.getMessage()).as("message Device").containsPattern("Device .*name.*");
}
@Test
public void validateEmail() {
String email = "aZ1_!#$%&'*+/=?`{|}~^.-@mail.io";

Loading…
Cancel
Save