Minor changes of validation methods and code formatting.

Signed-off-by: Oleksandra Matviienko <al.zzzeebra@gmail.com>
This commit is contained in:
Oleksandra Matviienko 2024-03-28 15:37:00 +01:00 committed by Sergey Matvienko
parent a993f2133a
commit c785c8acfd
9 changed files with 19 additions and 24 deletions

View File

@ -117,7 +117,7 @@ public class CachedAttributesService implements AttributesService {
@Override
public ListenableFuture<Optional<AttributeKvEntry>> find(TenantId tenantId, EntityId entityId, AttributeScope scope, String attributeKey) {
validate(entityId, scope);
Validator.validateString(attributeKey, k ->"Incorrect attribute key " + k);
Validator.validateString(attributeKey, k -> "Incorrect attribute key " + k);
return cacheExecutor.submit(() -> {
AttributeCacheKey attributeCacheKey = new AttributeCacheKey(scope, entityId, attributeKey);

View File

@ -79,7 +79,7 @@ public class DeviceCredentialsServiceImpl extends AbstractCachedEntityService<St
@Override
public DeviceCredentials findDeviceCredentialsByCredentialsId(String credentialsId) {
log.trace("Executing findDeviceCredentialsByCredentialsId [{}]", credentialsId);
validateString(credentialsId, id ->"Incorrect credentialsId " + id);
validateString(credentialsId, id -> "Incorrect credentialsId " + id);
return cache.getAndPutInTransaction(credentialsId,
() -> deviceCredentialsDao.findByCredentialsId(TenantId.SYS_TENANT_ID, credentialsId),
true); // caching null values is essential for permanently invalid requests

View File

@ -389,7 +389,7 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
log.trace("Executing findDevicesByTenantIdAndTypeAndEmptyOtaPackage, tenantId [{}], deviceProfileId [{}], type [{}], pageLink [{}]",
tenantId, deviceProfileId, type, pageLink);
validateId(tenantId, id -> INCORRECT_TENANT_ID + id);
validateId(tenantId, id -> INCORRECT_DEVICE_PROFILE_ID + id);
validateId(deviceProfileId, id -> INCORRECT_DEVICE_PROFILE_ID + id);
validatePageLink(pageLink);
return deviceDao.findDevicesByTenantIdAndTypeAndEmptyOtaPackage(tenantId.getId(), deviceProfileId.getId(), type, pageLink);
}
@ -398,7 +398,7 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(TenantId tenantId, DeviceProfileId deviceProfileId, OtaPackageType type) {
log.trace("Executing countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage, tenantId [{}], deviceProfileId [{}], type [{}]", tenantId, deviceProfileId, type);
validateId(tenantId, id -> INCORRECT_TENANT_ID + id);
validateId(tenantId, id -> INCORRECT_DEVICE_PROFILE_ID + id);
validateId(deviceProfileId, id -> INCORRECT_DEVICE_PROFILE_ID + id);
return deviceDao.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(tenantId.getId(), deviceProfileId.getId(), type);
}

View File

@ -400,7 +400,7 @@ public class EdgeServiceImpl extends AbstractCachedEntityService<EdgeCacheKey, E
@Override
public PageData<Edge> findEdgesByTenantIdAndEntityId(TenantId tenantId, EntityId entityId, PageLink pageLink) {
log.trace("Executing findEdgesByTenantIdAndEntityId, tenantId [{}], entityId [{}], pageLink [{}]", tenantId, entityId, pageLink);
Validator.validateId(tenantId, id -> "Incorrect tenantId " + id);
Validator.validateId(tenantId, id -> INCORRECT_TENANT_ID + id);
validatePageLink(pageLink);
return edgeDao.findEdgesByTenantIdAndEntityId(tenantId.getId(), entityId.getId(), entityId.getEntityType(), pageLink);
}

View File

@ -28,7 +28,6 @@ import org.thingsboard.server.dao.exception.IncorrectParameterException;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Pattern;
public class Validator {
@ -52,8 +51,8 @@ public class Validator {
* This method validate <code>EntityId</code> entity id. If entity id is invalid than throw
* <code>IncorrectParameterException</code> exception
*
* @param entityId the entityId
* @param errorMessageFunction the error message for exception that applies entityId
* @param entityId the entityId
* @param errorMessageFunction the error message for exception that applies entityId
*/
public static void validateEntityId(EntityId entityId, Function<EntityId, String> errorMessageFunction) {
if (entityId == null || entityId.getId() == null) {
@ -231,4 +230,5 @@ public class Validator {
throw new IncorrectParameterException(errorMessage);
}
}
}

View File

@ -51,6 +51,7 @@ import static org.thingsboard.server.dao.service.Validator.validateId;
public class TenantProfileServiceImpl extends AbstractCachedEntityService<TenantProfileCacheKey, TenantProfile, TenantProfileEvictEvent> implements TenantProfileService {
private static final String INCORRECT_TENANT_PROFILE_ID = "Incorrect tenantProfileId ";
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
@Autowired
private TenantProfileDao tenantProfileDao;
@ -111,7 +112,8 @@ public class TenantProfileServiceImpl extends AbstractCachedEntityService<Tenant
@Override
public void deleteTenantProfile(TenantId tenantId, TenantProfileId tenantProfileId) {
log.trace("Executing deleteTenantProfile [{}]", tenantProfileId);
validateId(tenantId, id -> INCORRECT_TENANT_PROFILE_ID + id);
validateId(tenantId, id -> INCORRECT_TENANT_ID + id);
validateId(tenantProfileId, id -> INCORRECT_TENANT_PROFILE_ID + id);
TenantProfile tenantProfile = tenantProfileDao.findById(tenantId, tenantProfileId.getId());
if (tenantProfile != null && tenantProfile.isDefault()) {
throw new DataValidationException("Deletion of Default Tenant Profile is prohibited!");
@ -186,7 +188,8 @@ public class TenantProfileServiceImpl extends AbstractCachedEntityService<Tenant
@Override
public boolean setDefaultTenantProfile(TenantId tenantId, TenantProfileId tenantProfileId) {
log.trace("Executing setDefaultTenantProfile [{}]", tenantProfileId);
validateId(tenantId, id -> INCORRECT_TENANT_PROFILE_ID + id);
validateId(tenantId, id -> INCORRECT_TENANT_ID + id);
validateId(tenantProfileId, id -> INCORRECT_TENANT_PROFILE_ID + id);
TenantProfile tenantProfile = tenantProfileDao.findById(tenantId, tenantProfileId.getId());
if (!tenantProfile.isDefault()) {
tenantProfile.setDefault(true);

View File

@ -167,7 +167,7 @@ public class ApiUsageStateServiceImpl extends AbstractEntityService implements A
public ApiUsageState findApiUsageStateById(TenantId tenantId, ApiUsageStateId id) {
log.trace("Executing findApiUsageStateById, tenantId [{}], apiUsageStateId [{}]", tenantId, id);
validateId(tenantId, t -> INCORRECT_TENANT_ID + t);
validateId(id, usId -> "Incorrect apiUsageStateId " + usId);
validateId(id, u -> "Incorrect apiUsageStateId " + u);
return apiUsageStateDao.findById(tenantId, id.getId());
}

View File

@ -173,7 +173,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
@Override
public UserCredentials findUserCredentialsByResetToken(TenantId tenantId, String resetToken) {
log.trace("Executing findUserCredentialsByResetToken [{}]", resetToken);
validateString(resetToken, t -> "Incorrect activateToken " + t);
validateString(resetToken, t -> "Incorrect resetToken " + t);
return userCredentialsDao.findByResetToken(tenantId, resetToken);
}
@ -335,7 +335,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
log.trace("Executing findTenantAndCustomerUsers, tenantId [{}], customerIds [{}], pageLink [{}]", tenantId, customerIds, pageLink);
validateId(tenantId, id -> INCORRECT_TENANT_ID + id);
validatePageLink(pageLink);
customerIds.forEach(customerId -> {validateId(customerId, id -> "Incorrect customerId " + id);});
customerIds.forEach(customerId -> validateId(customerId, id -> "Incorrect customerId " + id));
return userDao.findUsersByCustomerIds(tenantId.getId(), customerIds, pageLink);
}

View File

@ -15,7 +15,6 @@
*/
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;
@ -27,20 +26,15 @@ 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);
Validator.validateEntityId(goodDeviceId, id -> "Incorrect entityId " + id);
assertThatThrownBy(() -> Validator.validateEntityId(null, id -> "Incorrect entityId " + id))
.as("EntityId is null")
@ -51,7 +45,6 @@ class ValidatorTest {
.as("EntityId with null UUID")
.isInstanceOf(IncorrectParameterException.class)
.hasMessageContaining("Incorrect entityId null");
}
@Test
@ -84,13 +77,12 @@ class ValidatorTest {
.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);
Validator.validateId(goodDeviceId, id -> "Incorrect Id " + id);
assertThatThrownBy(() -> Validator.validateId((UUIDBased) null, id -> "Incorrect Id " + id))
.as("Id is null")
@ -119,7 +111,7 @@ class ValidatorTest {
.isInstanceOf(IncorrectParameterException.class)
.hasMessageContaining("Incorrect Ids []");
ArrayList<DeviceId> badList = new ArrayList<>(2);
List<DeviceId> badList = new ArrayList<>(2);
badList.add(goodDeviceId);
badList.add(null);