refactored tests

This commit is contained in:
IrynaMatveieva 2024-06-19 16:15:08 +03:00
parent da3b42754a
commit bef6cfd339
3 changed files with 27 additions and 25 deletions

View File

@ -103,18 +103,17 @@ public class TbSaveToCustomCassandraTableNode implements TbNode {
saveStmt = getSaveStmt(); saveStmt = getSaveStmt();
} }
void onTenantProfileUpdate(TenantProfile tenantProfile) { private void onTenantProfileUpdate(TenantProfile tenantProfile) {
DefaultTenantProfileConfiguration configuration = (DefaultTenantProfileConfiguration) tenantProfile.getProfileData().getConfiguration(); DefaultTenantProfileConfiguration configuration = (DefaultTenantProfileConfiguration) tenantProfile.getProfileData().getConfiguration();
long tenantProfileDefaultStorageTtl = TimeUnit.DAYS.toSeconds(configuration.getDefaultStorageTtlDays());
ttl = config.getDefaultTTL(); ttl = config.getDefaultTTL();
if (ttl == 0L) { if (ttl == 0L) {
ttl = tenantProfileDefaultStorageTtl; ttl = TimeUnit.DAYS.toSeconds(configuration.getDefaultStorageTtlDays());
} }
} }
@Override @Override
public void onMsg(TbContext ctx, TbMsg msg) { public void onMsg(TbContext ctx, TbMsg msg) {
withCallback(save(msg, ctx, ttl), aVoid -> ctx.tellSuccess(msg), e -> ctx.tellFailure(msg, e), ctx.getDbCallbackExecutor()); withCallback(save(msg, ctx), aVoid -> ctx.tellSuccess(msg), e -> ctx.tellFailure(msg, e), ctx.getDbCallbackExecutor());
} }
@Override @Override
@ -187,7 +186,7 @@ public class TbSaveToCustomCassandraTableNode implements TbNode {
return query.toString(); return query.toString();
} }
private ListenableFuture<Void> save(TbMsg msg, TbContext ctx, long ttl) { private ListenableFuture<Void> save(TbMsg msg, TbContext ctx) {
JsonElement data = JsonParser.parseString(msg.getData()); JsonElement data = JsonParser.parseString(msg.getData());
if (!data.isJsonObject()) { if (!data.isJsonObject()) {
throw new IllegalStateException("Invalid message structure, it is not a JSON Object: " + data); throw new IllegalStateException("Invalid message structure, it is not a JSON Object: " + data);

View File

@ -75,7 +75,7 @@ public class TbMsgTimeseriesNode implements TbNode {
onTenantProfileUpdate(ctx.getTenantProfile()); onTenantProfileUpdate(ctx.getTenantProfile());
} }
void onTenantProfileUpdate(TenantProfile tenantProfile) { private void onTenantProfileUpdate(TenantProfile tenantProfile) {
DefaultTenantProfileConfiguration configuration = (DefaultTenantProfileConfiguration) tenantProfile.getProfileData().getConfiguration(); DefaultTenantProfileConfiguration configuration = (DefaultTenantProfileConfiguration) tenantProfile.getProfileData().getConfiguration();
tenantProfileDefaultStorageTtl = TimeUnit.DAYS.toSeconds(configuration.getDefaultStorageTtlDays()); tenantProfileDefaultStorageTtl = TimeUnit.DAYS.toSeconds(configuration.getDefaultStorageTtlDays());
} }

View File

@ -57,6 +57,7 @@ import org.thingsboard.server.dao.nosql.CassandraStatementTask;
import org.thingsboard.server.dao.nosql.TbResultSet; import org.thingsboard.server.dao.nosql.TbResultSet;
import org.thingsboard.server.dao.nosql.TbResultSetFuture; import org.thingsboard.server.dao.nosql.TbResultSetFuture;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -75,10 +76,10 @@ import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.never;
import static org.mockito.BDDMockito.spy; import static org.mockito.BDDMockito.spy;
import static org.mockito.BDDMockito.then; import static org.mockito.BDDMockito.then;
import static org.mockito.BDDMockito.willAnswer; import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.BDDMockito.never;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
public class TbSaveToCustomCassandraTableNodeTest extends AbstractRuleNodeUpgradeTest { public class TbSaveToCustomCassandraTableNodeTest extends AbstractRuleNodeUpgradeTest {
@ -136,7 +137,9 @@ public class TbSaveToCustomCassandraTableNodeTest extends AbstractRuleNodeUpgrad
var configuration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); var configuration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
assertThatThrownBy(() -> node.init(ctxMock, configuration)) assertThatThrownBy(() -> node.init(ctxMock, configuration))
.isInstanceOf(TbNodeException.class) .isInstanceOf(TbNodeException.class)
.hasMessage("Unable to connect to Cassandra database"); .hasMessage("Unable to connect to Cassandra database")
.extracting(e -> ((TbNodeException) e).isUnrecoverable())
.isEqualTo(false);
} }
@Test @Test
@ -238,15 +241,15 @@ public class TbSaveToCustomCassandraTableNodeTest extends AbstractRuleNodeUpgrad
return Stream.of( return Stream.of(
Arguments.of(0, 0, "INSERT INTO cs_tb_readings(entityIdTableColumn) VALUES(?)", Arguments.of(0, 0, "INSERT INTO cs_tb_readings(entityIdTableColumn) VALUES(?)",
(Consumer<BoundStatementBuilder>) builder -> { (Consumer<BoundStatementBuilder>) builder -> {
then(builder).should(never()).setInt(anyInt(), eq(0)); then(builder).should(never()).setInt(anyInt(), anyInt());
}), }),
Arguments.of(0, 5, "INSERT INTO cs_tb_readings(entityIdTableColumn) VALUES(?) USING TTL ?", Arguments.of(0, 5, "INSERT INTO cs_tb_readings(entityIdTableColumn) VALUES(?) USING TTL ?",
(Consumer<BoundStatementBuilder>) builder -> { (Consumer<BoundStatementBuilder>) builder -> {
then(builder).should().setInt(anyInt(), eq(432000)); then(builder).should().setInt(1, 432000);
}), }),
Arguments.of(20, 1, "INSERT INTO cs_tb_readings(entityIdTableColumn) VALUES(?) USING TTL ?", Arguments.of(20, 1, "INSERT INTO cs_tb_readings(entityIdTableColumn) VALUES(?) USING TTL ?",
(Consumer<BoundStatementBuilder>) builder -> { (Consumer<BoundStatementBuilder>) builder -> {
then(builder).should().setInt(anyInt(), eq(20)); then(builder).should().setInt(1, 20);
}) })
); );
} }
@ -255,14 +258,14 @@ public class TbSaveToCustomCassandraTableNodeTest extends AbstractRuleNodeUpgrad
public void givenValidMsgStructure_whenOnMsg_thenSaveToCustomCassandraTable() throws TbNodeException { public void givenValidMsgStructure_whenOnMsg_thenSaveToCustomCassandraTable() throws TbNodeException {
config.setDefaultTTL(25L); config.setDefaultTTL(25L);
config.setTableName("readings"); config.setTableName("readings");
config.setFieldsMapping(Map.of( Map<String, String> mappings = new LinkedHashMap<>();
"$entityId", "entityIdTableColumn", mappings.put("$entityId", "entityIdTableColumn");
"doubleField", "doubleTableColumn", mappings.put("doubleField", "doubleTableColumn");
"longField", "longTableColumn", mappings.put("longField", "longTableColumn");
"booleanField", "booleanTableColumn", mappings.put("booleanField", "booleanTableColumn");
"stringField", "stringTableColumn", mappings.put("stringField", "stringTableColumn");
"jsonField", "jsonTableColumn" mappings.put("jsonField", "jsonTableColumn");
)); config.setFieldsMapping(mappings);
mockOnInit(); mockOnInit();
mockBoundStatementBuilder(); mockBoundStatementBuilder();
@ -350,12 +353,12 @@ public class TbSaveToCustomCassandraTableNodeTest extends AbstractRuleNodeUpgrad
} }
private void verifySettingStatementBuilder() { private void verifySettingStatementBuilder() {
then(boundStatementBuilderMock).should().setUuid(anyInt(), eq(DEVICE_ID.getId())); then(boundStatementBuilderMock).should().setUuid(0, DEVICE_ID.getId());
then(boundStatementBuilderMock).should().setDouble(anyInt(), eq(22.5)); then(boundStatementBuilderMock).should().setDouble(1, 22.5);
then(boundStatementBuilderMock).should().setLong(anyInt(), eq(56L)); then(boundStatementBuilderMock).should().setLong(2, 56L);
then(boundStatementBuilderMock).should().setBoolean(anyInt(), eq(true)); then(boundStatementBuilderMock).should().setBoolean(3, true);
then(boundStatementBuilderMock).should().setString(anyInt(), eq("some string")); then(boundStatementBuilderMock).should().setString(4, "some string");
then(boundStatementBuilderMock).should().setString(anyInt(), eq("{\"key\":\"value\"}")); then(boundStatementBuilderMock).should().setString(5, "{\"key\":\"value\"}");
then(boundStatementBuilderMock).should().setInt(anyInt(), eq(25)); then(boundStatementBuilderMock).should().setInt(anyInt(), eq(25));
} }