Browse Source

DeviceDataValidatorTest implemented to expose 0x00 problem in the device name

pull/9163/head
Sergey Matvienko 3 years ago
parent
commit
7177f8143c
  1. 90
      dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java

90
dao/src/test/java/org/thingsboard/server/dao/service/validator/DeviceDataValidatorTest.java

@ -0,0 +1,90 @@
/**
* 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.Device;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.customer.CustomerDao;
import org.thingsboard.server.dao.device.DeviceDao;
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 = DeviceDataValidator.class)
@Slf4j
class DeviceDataValidatorTest {
@MockBean
DeviceDao deviceDao;
@MockBean
TenantService tenantService;
@MockBean
CustomerDao customerDao;
@Autowired
DeviceDataValidator 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 testDeviceName_thenOK(final String name) {
Device device = new Device();
device.setTenantId(tenantId);
device.setName(name);
validator.validateDataImpl(tenantId, device);
}
@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) {
Device device = new Device();
device.setTenantId(tenantId);
device.setName(name);
DataValidationException exception = catchThrowableOfType(() ->
validator.validateDataImpl(tenantId, device), DataValidationException.class);
log.warn("Exception message: {}", exception == null ? null : exception.getMessage());
assertThatThrownBy(() -> validator.validateDataImpl(tenantId, device))
.isInstanceOf(DataValidationException.class)
.hasMessageMatching(".*\\S.*");
}
}
Loading…
Cancel
Save