Tests for Validator are added.
Signed-off-by: Oleksandra Matviienko <al.zzzeebra@gmail.com>
This commit is contained in:
parent
5e39af738b
commit
85e80a56ae
@ -68,7 +68,7 @@ public abstract class UUIDBased implements HasUUID, Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return id.toString();
|
return String.valueOf(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,6 +28,7 @@ import org.thingsboard.server.dao.exception.IncorrectParameterException;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class Validator {
|
public class Validator {
|
||||||
@ -151,6 +152,19 @@ public class Validator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method validate <code>UUIDBased</code> id. If id is null than throw
|
||||||
|
* <code>IncorrectParameterException</code> exception
|
||||||
|
*
|
||||||
|
* @param id the id
|
||||||
|
* @param errorMessageSupplier the error message for exception supplier
|
||||||
|
*/
|
||||||
|
static void validateId(UUIDBased id, Supplier<String> errorMessageSupplier) {
|
||||||
|
if (id == null || id.getId() == null) {
|
||||||
|
throw new IncorrectParameterException(errorMessageSupplier.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method validate list of <code>UUIDBased</code> ids. If at least one of the ids is null than throw
|
* This method validate list of <code>UUIDBased</code> ids. If at least one of the ids is null than throw
|
||||||
* <code>IncorrectParameterException</code> exception
|
* <code>IncorrectParameterException</code> exception
|
||||||
@ -179,8 +193,9 @@ public class Validator {
|
|||||||
if (ids == null || ids.isEmpty()) {
|
if (ids == null || ids.isEmpty()) {
|
||||||
throw new IncorrectParameterException(errorMessageFunction.apply(ids));
|
throw new IncorrectParameterException(errorMessageFunction.apply(ids));
|
||||||
} else {
|
} else {
|
||||||
|
Supplier<String> errorMessageSupplier = () -> errorMessageFunction.apply(ids);
|
||||||
for (UUIDBased id : ids) {
|
for (UUIDBased id : ids) {
|
||||||
validateId(id, errorMessageFunction.apply(ids));
|
validateId(id, errorMessageSupplier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,164 @@
|
|||||||
|
/**
|
||||||
|
* Copyright © 2016-2024 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;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.thingsboard.server.common.data.id.*;
|
||||||
|
import org.thingsboard.server.dao.exception.IncorrectParameterException;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
|
|
||||||
|
class ValidatorTest {
|
||||||
|
|
||||||
|
final DeviceId goodDeviceId = new DeviceId(UUID.fromString("18594c15-9f05-4cda-b58e-70172467c3e5"));
|
||||||
|
final UserId nullUserId = new UserId(null);
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateEntityIdTest() {
|
||||||
|
Validator.validateEntityId(TenantId.SYS_TENANT_ID, id -> "Incorrect entityId " + id);
|
||||||
|
Validator.validateEntityId((goodDeviceId), id -> "Incorrect entityId " + id);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateEntityId( null, id -> "Incorrect entityId " + id))
|
||||||
|
.as("EntityId is null")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect entityId null");
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateEntityId(nullUserId, id -> "Incorrect entityId " + id))
|
||||||
|
.as("EntityId with null UUID")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect entityId null");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateStringTest() {
|
||||||
|
Validator.validateString("Hello", s -> "Incorrect string " + s);
|
||||||
|
Validator.validateString(" ", s -> "Incorrect string " + s);
|
||||||
|
Validator.validateString("\n", s -> "Incorrect string " + s);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateString(null, s -> "Incorrect string " + s))
|
||||||
|
.as("String is null")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect string null");
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateString("", s -> "Incorrect string " + s))
|
||||||
|
.as("String is empty")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessage("Incorrect string ");
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateString("", s -> "Incorrect string [" + s + "]"))
|
||||||
|
.as("String is empty []")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessage("Incorrect string []");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateUUIDIdTest() {
|
||||||
|
Validator.validateId(UUID.randomUUID(), id -> "Incorrect Id " + id);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateId((UUID) null, id -> "Incorrect Id " + id))
|
||||||
|
.as("Id is null")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect Id null");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateUUIDBasedIdTest() {
|
||||||
|
Validator.validateId(TenantId.SYS_TENANT_ID, id -> "Incorrect Id " + id);
|
||||||
|
Validator.validateId((goodDeviceId), id -> "Incorrect Id " + id);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateId((UUIDBased) null, id -> "Incorrect Id " + id))
|
||||||
|
.as("Id is null")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect Id null");
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateId(nullUserId, id -> "Incorrect Id " + id))
|
||||||
|
.as("Id with null UUIDBased")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect Id null");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateIds() {
|
||||||
|
|
||||||
|
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")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect Ids null");
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateIds(Collections.emptyList(), ids -> "Incorrect Ids " + ids))
|
||||||
|
.as("List is empty")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect Ids []");
|
||||||
|
|
||||||
|
ArrayList<DeviceId> badList = new ArrayList<>(2);
|
||||||
|
badList.add(goodDeviceId);
|
||||||
|
badList.add(null);
|
||||||
|
|
||||||
|
// Incorrect Ids [18594c15-9f05-4cda-b58e-70172467c3e5, null]
|
||||||
|
assertThatThrownBy(() -> Validator.validateIds(badList, ids -> "Incorrect Ids " + ids))
|
||||||
|
.as("List contains null")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect Ids ")
|
||||||
|
.hasMessageContaining(goodDeviceId.getId().toString())
|
||||||
|
.hasMessageContaining("null");
|
||||||
|
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void validateIdSupplier() {
|
||||||
|
Validator.validateId(TenantId.SYS_TENANT_ID, () -> "Incorrect Id null");
|
||||||
|
Validator.validateId((goodDeviceId), () -> "Incorrect Id null");
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateId((UUIDBased) null, () -> "Incorrect Id null"))
|
||||||
|
.as("Id is null")
|
||||||
|
.isInstanceOf(IncorrectParameterException.class)
|
||||||
|
.hasMessageContaining("Incorrect Id null");
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> Validator.validateId(nullUserId, () -> "Incorrect Id null"))
|
||||||
|
.as("Id with null UUIDBased")
|
||||||
|
.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…
x
Reference in New Issue
Block a user