Browse Source

UPD. Refactoring and changes according to comments.

Signed-off-by: Oleksandra Matviienko <al.zzzeebra@gmail.com>
pull/10332/head
Oleksandra Matviienko 3 years ago
committed by Sergey Matvienko
parent
commit
c2b53479fb
  1. 17
      dao/src/main/java/org/thingsboard/server/dao/service/Validator.java
  2. 17
      dao/src/test/java/org/thingsboard/server/dao/service/ValidatorTest.java

17
dao/src/main/java/org/thingsboard/server/dao/service/Validator.java

@ -193,9 +193,8 @@ public class Validator {
if (ids == null || ids.isEmpty()) {
throw new IncorrectParameterException(errorMessageFunction.apply(ids));
} else {
Supplier<String> errorMessageSupplier = () -> errorMessageFunction.apply(ids);
for (UUIDBased id : ids) {
validateId(id, errorMessageSupplier);
validateId(id, errorMessageFunction.apply(ids));
}
}
}
@ -245,18 +244,4 @@ public class Validator {
throw new IncorrectParameterException(errorMessage);
}
}
/**
* This method validate list of <code>UUIDBased</code> ids. If at least one of the ids is null than throw
* <code>IncorrectParameterException</code> exception
*
* @param reference the list of ids
* @param errorMessageFunction the error message for exception that applies reference
*/
public static void checkNotNull(Object reference, Function<Object, String> errorMessageFunction) {
if (reference == null) {
throw new IncorrectParameterException(errorMessageFunction.apply(reference));
}
}
}

17
dao/src/test/java/org/thingsboard/server/dao/service/ValidatorTest.java

@ -105,11 +105,9 @@ class ValidatorTest {
}
@Test
void validateIds() {
void validateIdsTest() {
List<? extends UUIDBased> list = List.of(goodDeviceId);
Validator.validateIds( list, ids -> "Incorrect Id " + ids);
Validator.validateId(list.get(0), id -> "Incorrect Id " + id);
assertThatThrownBy(() -> Validator.validateIds(null, id -> "Incorrect Ids " + id))
.as("Ids are null")
@ -134,6 +132,7 @@ class ValidatorTest {
.hasMessageContaining("null");
}
@Test
void validateIdSupplier() {
Validator.validateId(TenantId.SYS_TENANT_ID, () -> "Incorrect Id null");
@ -149,16 +148,4 @@ class ValidatorTest {
.isInstanceOf(IncorrectParameterException.class)
.hasMessageContaining("Incorrect Id null");
}
@Test
void checkNotNullTest() {
Validator.checkNotNull("notnull", reference -> "Incorrect reference " + reference);
assertThatThrownBy(() -> Validator.checkNotNull(null, reference -> "Incorrect reference " + reference))
.as("Reference is null")
.isInstanceOf(IncorrectParameterException.class)
.hasMessageContaining("Incorrect reference null");
}
}

Loading…
Cancel
Save