Asset tests: name validation added. validate name contains 0x00 as well to crush tests before fix

This commit is contained in:
Sergey Matvienko 2023-09-01 15:41:22 +02:00
parent f621c10260
commit c14719a4f8
4 changed files with 108 additions and 1 deletions

View File

@ -85,6 +85,17 @@ public class AssetServiceTest extends AbstractServiceTest {
}); });
} }
@Test
public void testSaveDeviceWithNameContains0x00_thenDataValidationException() {
Asset asset = new Asset();
asset.setTenantId(tenantId);
asset.setType("default");
asset.setName("F0929906\000\000\000\000\000\000\000\000\000");
Assertions.assertThrows(DataValidationException.class, () -> {
assetService.saveAsset(asset);
});
}
@Test @Test
public void testSaveAssetWithEmptyTenant() { public void testSaveAssetWithEmptyTenant() {
Asset asset = new Asset(); Asset asset = new Asset();

View File

@ -0,0 +1,89 @@
/**
* Copyright © 2016-2023 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.validator;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.asset.AssetDao;
import org.thingsboard.server.dao.customer.CustomerDao;
import org.thingsboard.server.dao.exception.DataValidationException;
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.mockito.BDDMockito.willReturn;
@SpringBootTest(classes = AssetDataValidator.class)
@Slf4j
class AssetDataValidatorTest {
@MockBean
AssetDao assetDao;
@MockBean
TenantService tenantService;
@MockBean
CustomerDao customerDao;
@Autowired
AssetDataValidator validator;
TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da"));
@BeforeEach
void setUp() {
willReturn(true).given(tenantService).tenantExists(tenantId);
}
@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 testAssetName_thenOK(final String name) {
Asset asset = new Asset();
asset.setTenantId(tenantId);
asset.setName(name);
validator.validateDataImpl(tenantId, asset);
}
@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 testAssetName_thenDataValidationException(final String name) {
Asset asset = new Asset();
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.*");
}
}

View File

@ -84,7 +84,7 @@ class DeviceDataValidatorTest {
assertThatThrownBy(() -> validator.validateDataImpl(tenantId, device)) assertThatThrownBy(() -> validator.validateDataImpl(tenantId, device))
.isInstanceOf(DataValidationException.class) .isInstanceOf(DataValidationException.class)
.hasMessageMatching(".*\\S.*"); .hasMessageMatching(".*Device.*");
} }
} }

View File

@ -44,6 +44,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -94,6 +95,12 @@ public class JpaAssetDaoTest extends AbstractJpaDaoTest {
savedAssetProfiles.clear(); savedAssetProfiles.clear();
} }
@Test
public void testSaveDeviceName0x00_thenSomeDatabaseException() {
assertThatThrownBy(() -> assets.add(
saveAsset(UUID.randomUUID(), tenantId2, customerId2, "F0929906\000\000\000\000\000\000\000\000\000")));
}
@Test @Test
public void testFindAssetsByTenantId() { public void testFindAssetsByTenantId() {
PageLink pageLink = new PageLink(20, 0, "ASSET_"); PageLink pageLink = new PageLink(20, 0, "ASSET_");