-
Notifications
You must be signed in to change notification settings - Fork 141
fixing inconsistency for data = null in device registry table: adding… #3708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DominikBasnerSotec
wants to merge
6
commits into
eclipse-hono:master
Choose a base branch
from
sotec-iot:fix/handle-device-data-is-null-jdbc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a4cdfb4
fixing inconsistency for data = null in device registry table: adding…
DominikBasnerSotec ca9aff6
adding comments and copyright header to TableAdapterStoreTest
DominikBasnerSotec 1a66492
fix checkstyle issues
DominikBasnerSotec 5b1fa27
Merge branch 'eclipse-hono:master' into fix/handle-device-data-is-nul…
DominikBasnerSotec 32cfb55
Updating indent in pom.xml and TableAdapterStoreTest.java
DominikBasnerSotec 5214878
putting test rules into methods
DominikBasnerSotec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,9 +49,7 @@ public void testValidateFields() { | |
| @Test | ||
| public void testValidateMissingField() { | ||
| final Statement statement = Statement.statement("SELECT foo FROM bar WHERE baz=:baz"); | ||
| assertThrows(IllegalStateException.class, () -> { | ||
| statement.validateParameters("bar"); | ||
| }); | ||
| assertThrows(IllegalStateException.class, () -> statement.validateParameters("bar")); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -68,14 +66,15 @@ public void testValidateAdditionalField() { | |
| */ | ||
| @Test | ||
| public void testValidateAdditionalField2() { | ||
| final Statement statement = Statement.statement("UPDATE devices\n" + | ||
| "SET\n" + | ||
| " data=:data::jsonb,\n" + | ||
| " version=:next_version\n" + | ||
| "WHERE\n" + | ||
| " tenant_id=:tenant_id\n" + | ||
| "AND\n" + | ||
| " device_id=:device_id"); | ||
| final Statement statement = Statement.statement(""" | ||
| UPDATE devices | ||
| SET | ||
| data=:data::jsonb, | ||
| version=:next_version | ||
| WHERE | ||
| tenant_id=:tenant_id | ||
| AND | ||
| device_id=:device_id"""); | ||
|
Comment on lines
+69
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice improvement 👍 |
||
| statement.validateParameters("data", "device_id", "next_version", "tenant_id"); | ||
| } | ||
|
|
||
|
|
@@ -102,7 +101,7 @@ public void testPlainYamlStillWorksForStatementConfig() { | |
| @Test | ||
| public void testObjectCreationRejectedMapValue(@TempDir final Path tempDir) { | ||
| final Path markerFile = tempDir.resolve("testObjectCreationRejectedMapValue.marker"); | ||
| final String yaml = "read: !!java.io.FileOutputStream [" + markerFile.toAbsolutePath().toString() + "]"; | ||
| final String yaml = "read: !!java.io.FileOutputStream [" + markerFile.toAbsolutePath() + "]"; | ||
|
|
||
| assertNoMarkerFile(markerFile, yaml); | ||
| } | ||
|
|
@@ -115,7 +114,7 @@ public void testObjectCreationRejectedMapValue(@TempDir final Path tempDir) { | |
| @Test | ||
| public void testObjectCreationRejectedPlainValue(@TempDir final Path tempDir) { | ||
| final Path markerFile = tempDir.resolve("testObjectCreationRejectedPlainValue.marker"); | ||
| final String yaml = "!!java.io.FileOutputStream [" + markerFile.toAbsolutePath().toString() + "]"; | ||
| final String yaml = "!!java.io.FileOutputStream [" + markerFile.toAbsolutePath() + "]"; | ||
|
|
||
| assertNoMarkerFile(markerFile, yaml); | ||
| } | ||
|
|
||
225 changes: 225 additions & 0 deletions
225
.../src/test/java/org/eclipse/hono/service/base/jdbc/store/device/TableAdapterStoreTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,225 @@ | ||||||||||
| /******************************************************************************* | ||||||||||
| * Copyright (c) 2025 Contributors to the Eclipse Foundation | ||||||||||
| * | ||||||||||
| * See the NOTICE file(s) distributed with this work for additional | ||||||||||
| * information regarding copyright ownership. | ||||||||||
| * | ||||||||||
| * This program and the accompanying materials are made available under the | ||||||||||
| * terms of the Eclipse Public License 2.0 which is available at | ||||||||||
| * http://www.eclipse.org/legal/epl-2.0 | ||||||||||
| * | ||||||||||
| * SPDX-License-Identifier: EPL-2.0 | ||||||||||
| *******************************************************************************/ | ||||||||||
|
|
||||||||||
| package org.eclipse.hono.service.base.jdbc.store.device; | ||||||||||
|
|
||||||||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||||||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||||||||||
| import static org.mockito.ArgumentMatchers.any; | ||||||||||
| import static org.mockito.ArgumentMatchers.anyString; | ||||||||||
| import static org.mockito.Mockito.doAnswer; | ||||||||||
| import static org.mockito.Mockito.mock; | ||||||||||
| import static org.mockito.Mockito.when; | ||||||||||
|
|
||||||||||
| import java.io.ByteArrayInputStream; | ||||||||||
| import java.nio.charset.StandardCharsets; | ||||||||||
| import java.util.Collections; | ||||||||||
| import java.util.HashMap; | ||||||||||
| import java.util.List; | ||||||||||
| import java.util.Map; | ||||||||||
| import java.util.Optional; | ||||||||||
| import java.util.function.Consumer; | ||||||||||
|
|
||||||||||
| import org.eclipse.hono.deviceregistry.service.device.DeviceKey; | ||||||||||
| import org.eclipse.hono.service.base.jdbc.store.StatementConfiguration; | ||||||||||
| import org.eclipse.hono.service.management.device.Device; | ||||||||||
| import org.jetbrains.annotations.NotNull; | ||||||||||
| import org.junit.jupiter.api.BeforeEach; | ||||||||||
| import org.junit.jupiter.api.Test; | ||||||||||
| import org.yaml.snakeyaml.Yaml; | ||||||||||
|
|
||||||||||
| import io.opentracing.Span; | ||||||||||
| import io.opentracing.SpanContext; | ||||||||||
| import io.opentracing.Tracer; | ||||||||||
| import io.opentracing.tag.StringTag; | ||||||||||
| import io.vertx.core.Future; | ||||||||||
| import io.vertx.core.json.JsonObject; | ||||||||||
| import io.vertx.jdbcclient.JDBCPool; | ||||||||||
| import io.vertx.sqlclient.Row; | ||||||||||
| import io.vertx.sqlclient.RowIterator; | ||||||||||
| import io.vertx.sqlclient.RowSet; | ||||||||||
| import io.vertx.sqlclient.Tuple; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Tests the handling of device data in the database. | ||||||||||
| */ | ||||||||||
| class TableAdapterStoreTest { | ||||||||||
|
|
||||||||||
| private final Span span = mock(Span.class); | ||||||||||
| private final SpanContext spanContext = mock(SpanContext.class); | ||||||||||
| private final io.vertx.sqlclient.SqlConnection sqlConnection = mock(io.vertx.sqlclient.SqlConnection.class); | ||||||||||
| private final RowSet<Row> rowSet = mock(RowSet.class); | ||||||||||
| private final Row row = mock(Row.class); | ||||||||||
| private final Tracer.SpanBuilder spanBuilder = mock(Tracer.SpanBuilder.class); | ||||||||||
| private final Tracer tracer = mock(Tracer.class); | ||||||||||
| private final JDBCPool client = mock(JDBCPool.class); | ||||||||||
|
|
||||||||||
| private String deviceJson; | ||||||||||
| private TableAdapterStore store; | ||||||||||
| private final String TENANT_ID = "test-tenant"; | ||||||||||
| private final String DEVICE_ID = "device-1"; | ||||||||||
|
Comment on lines
+69
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
And these should be moved to the top of the field declarations |
||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Creates a test device with default values. | ||||||||||
| */ | ||||||||||
| private Device createTestDevice() { | ||||||||||
| return new Device() | ||||||||||
| .setEnabled(true) | ||||||||||
| .setVia(Collections.singletonList("group1")); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Mocks a single row of device data. | ||||||||||
| * | ||||||||||
| * @param deviceJson The JSON string of the device data (can be null) | ||||||||||
| * @param versions The versions the mocked rows contain | ||||||||||
| */ | ||||||||||
| private void mockRows(final String deviceJson, final String[] versions) { | ||||||||||
| // Setup row data | ||||||||||
| for (String version : versions) { | ||||||||||
| when(row.size()).thenReturn(2); | ||||||||||
| when(row.getValue(0)).thenReturn(deviceJson); | ||||||||||
| when(row.getValue(1)).thenReturn(version); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| mockRowSet(versions.length); | ||||||||||
|
|
||||||||||
| // Mock direct query | ||||||||||
| doAnswer(invocation -> { | ||||||||||
| final io.vertx.core.Handler<io.vertx.core.AsyncResult<RowSet<Row>>> handler = invocation.getArgument(0); | ||||||||||
| handler.handle(Future.succeededFuture(rowSet)); | ||||||||||
| return null; | ||||||||||
| }).when(sqlConnection).query(anyString()); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private void mockRowSet(final int numRows) { | ||||||||||
| final var iterator = mock(RowIterator.class); | ||||||||||
| when(rowSet.iterator()).thenReturn(iterator); | ||||||||||
| when(rowSet.columnsNames()).thenReturn(List.of("data", "version")); | ||||||||||
| when(rowSet.size()).thenReturn(numRows); | ||||||||||
| doAnswer(invocation -> { | ||||||||||
| final Consumer<Row> consumer = invocation.getArgument(0); | ||||||||||
| for (int i = 0; i < numRows; i++) { | ||||||||||
| consumer.accept(row); | ||||||||||
| } | ||||||||||
| return null; | ||||||||||
| }).when(rowSet).forEach(any()); | ||||||||||
| for (int i = 0; i < numRows; i++) { | ||||||||||
| when(iterator.hasNext()).thenReturn(true); | ||||||||||
| } | ||||||||||
| when(iterator.hasNext()).thenReturn(false); | ||||||||||
| when(iterator.next()).thenReturn(row); | ||||||||||
|
|
||||||||||
| // Return rowSet in query: | ||||||||||
| final var preparedQueryMock = mock(io.vertx.sqlclient.PreparedQuery.class); | ||||||||||
| when(preparedQueryMock.execute(any(Tuple.class))).thenReturn(Future.succeededFuture(rowSet)); | ||||||||||
| when(sqlConnection.preparedQuery(anyString())).thenReturn(preparedQueryMock); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @BeforeEach | ||||||||||
| void setUp() { | ||||||||||
| // Setup tracing mocks | ||||||||||
| when(tracer.buildSpan(anyString())).thenReturn(spanBuilder); | ||||||||||
| when(spanBuilder.addReference(anyString(), any())).thenReturn(spanBuilder); | ||||||||||
| when(spanBuilder.withTag(anyString(), anyString())).thenReturn(spanBuilder); | ||||||||||
| when(spanBuilder.withTag(anyString(), any(Number.class))).thenReturn(spanBuilder); | ||||||||||
| when(spanBuilder.withTag(any(StringTag.class), anyString())).thenReturn(spanBuilder); | ||||||||||
| when(spanBuilder.ignoreActiveSpan()).thenReturn(spanBuilder); | ||||||||||
| when(spanBuilder.start()).thenReturn(span); | ||||||||||
| when(span.context()).thenReturn(spanContext); | ||||||||||
|
|
||||||||||
| // Setup JDBC client mocks | ||||||||||
| when(client.getConnection()).thenReturn(Future.succeededFuture(sqlConnection)); | ||||||||||
| when(sqlConnection.close()).thenReturn(Future.succeededFuture()); | ||||||||||
| final var statementConfig = StatementConfiguration.empty().overrideWith(getStatementsAsInputStream(), true); | ||||||||||
|
|
||||||||||
| store = new TableAdapterStore(client, tracer, statementConfig, null); | ||||||||||
| final var device = createTestDevice(); | ||||||||||
| deviceJson = JsonObject.mapFrom(device).encode(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private static @NotNull ByteArrayInputStream getStatementsAsInputStream() { | ||||||||||
| final Map<String, String> statements = new HashMap<>(); | ||||||||||
| statements.put("readRegistration", | ||||||||||
| "SELECT * FROM devices WHERE tenant_id = :tenant_id AND device_id = :device_id"); | ||||||||||
| statements.put("findCredentials", | ||||||||||
| "SELECT * FROM credentials WHERE tenant_id = :tenant_id AND type = :type AND auth_id = :auth_id"); | ||||||||||
| statements.put("resolveGroups", | ||||||||||
| "SELECT * FROM device_groups WHERE tenant_id = :tenant_id AND group_id = ANY(:group_ids)"); | ||||||||||
|
|
||||||||||
| final Yaml yaml = new Yaml(); | ||||||||||
| final String yamlString = yaml.dump(statements); | ||||||||||
| return new ByteArrayInputStream(yamlString.getBytes(StandardCharsets.UTF_8)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Test | ||||||||||
| void testReadDeviceSuccess() { | ||||||||||
| // Given | ||||||||||
| final var version = "v1"; | ||||||||||
| mockRows(deviceJson, new String[]{version}); | ||||||||||
|
|
||||||||||
| // When | ||||||||||
| final var result = store.readDevice(DeviceKey.from(TENANT_ID, DEVICE_ID), spanContext).result(); | ||||||||||
|
|
||||||||||
| // Then | ||||||||||
| validateReadDeviceResult(result, version); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Test | ||||||||||
| void testReadDeviceSuccess_dataNull() { | ||||||||||
| // Given | ||||||||||
| final var version = "v2"; | ||||||||||
| mockRows(null, new String[]{version}); | ||||||||||
|
|
||||||||||
| // When | ||||||||||
| final var result = store.readDevice(DeviceKey.from(TENANT_ID, DEVICE_ID), spanContext).result(); | ||||||||||
|
|
||||||||||
| // Then | ||||||||||
| validateReadDeviceResult(result, version); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private void validateReadDeviceResult(final Optional<DeviceReadResult> result, final String version) { | ||||||||||
| assertTrue(result.isPresent()); | ||||||||||
| final var deviceResult = result.get(); | ||||||||||
| assertTrue(deviceResult.getDevice().isEnabled()); | ||||||||||
| assertEquals(version, deviceResult.getResourceVersion().orElse("")); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Test | ||||||||||
| void testReadDeviceNotFound() { | ||||||||||
| // Given | ||||||||||
| final var key = DeviceKey.from(TENANT_ID, "non-existent-device"); | ||||||||||
| mockRows(deviceJson, new String[]{}); | ||||||||||
|
|
||||||||||
| // When | ||||||||||
| final var future = store.readDevice(key, spanContext); | ||||||||||
|
|
||||||||||
| // Then | ||||||||||
| assertTrue(future.succeeded()); | ||||||||||
| assertTrue(future.result().isEmpty()); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Test | ||||||||||
| void testReadDeviceMultipleEntries() { | ||||||||||
| // Given | ||||||||||
| final var key = DeviceKey.from(TENANT_ID, "duplicate-device"); | ||||||||||
| mockRows(deviceJson, new String[]{"v3", "v4"}); | ||||||||||
|
|
||||||||||
| // When | ||||||||||
| final var future = store.readDevice(key, spanContext); | ||||||||||
|
|
||||||||||
| // Then | ||||||||||
| assertTrue(future.failed()); | ||||||||||
| assertEquals("Found multiple entries for a single device", future.cause().getMessage()); | ||||||||||
| } | ||||||||||
| } | ||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.