From 443f471a990be99cebd1255231609a240f74c468 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 29 Feb 2024 15:58:23 +0100 Subject: [PATCH] fix: Fixed unnecessary column type changes in H2DbWireRecordStoreImpl [backport release-5.4.0] (#5158) * fix: Fixed unnecessary column type changes in H2DbWireRecordStoreImpl (#5157) fix: Fixed unnecessary column type changes in H2DbWireRecordStore Signed-off-by: Nicola Timeus (cherry picked from commit 9d119146d937f0cd63ce241ee075974b1e252a2c) * ore: Updated distrib reference to new core bundle Signed-off-by: MMaiero --------- Signed-off-by: MMaiero Co-authored-by: nicolatimeus Co-authored-by: MMaiero --- kura/distrib/config/kura.build.properties | 2 +- .../kura/core/db/H2DbWireRecordStoreImpl.java | 26 +++- .../wire/db/test/MultipleValueStoreTest.java | 116 ++++++++++++++++++ 3 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 kura/test/org.eclipse.kura.wire.db.component.provider.test/src/main/java/org/eclipse/kura/internal/wire/db/test/MultipleValueStoreTest.java diff --git a/kura/distrib/config/kura.build.properties b/kura/distrib/config/kura.build.properties index 9ccb113dc82..26b50824861 100644 --- a/kura/distrib/config/kura.build.properties +++ b/kura/distrib/config/kura.build.properties @@ -6,7 +6,7 @@ org.eclipse.kura.asset.helper.provider.version=1.4.0 org.eclipse.kura.broker.artemis.core.version=1.5.0 org.eclipse.kura.broker.artemis.simple.mqtt.version=1.4.0 org.eclipse.kura.broker.artemis.xml.version=1.4.0 -org.eclipse.kura.core.version=1.4.0 +org.eclipse.kura.core.version=1.4.1-SNAPSHOT org.eclipse.kura.core.system.version=1.4.0 org.eclipse.kura.core.certificates.version=1.4.0 org.eclipse.kura.core.keystore.version=1.4.1-SNAPSHOT diff --git a/kura/org.eclipse.kura.core/src/main/java/org/eclipse/kura/core/db/H2DbWireRecordStoreImpl.java b/kura/org.eclipse.kura.core/src/main/java/org/eclipse/kura/core/db/H2DbWireRecordStoreImpl.java index d0ac73b8981..27ee9ce381a 100644 --- a/kura/org.eclipse.kura.core/src/main/java/org/eclipse/kura/core/db/H2DbWireRecordStoreImpl.java +++ b/kura/org.eclipse.kura.core/src/main/java/org/eclipse/kura/core/db/H2DbWireRecordStoreImpl.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2023 Eurotech and/or its affiliates and others + * Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -15,11 +15,13 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.Optional; import org.eclipse.kura.KuraStoreException; import org.eclipse.kura.type.BooleanValue; import org.eclipse.kura.type.ByteArrayValue; +import org.eclipse.kura.type.DataType; import org.eclipse.kura.type.DoubleValue; import org.eclipse.kura.type.FloatValue; import org.eclipse.kura.type.IntegerValue; @@ -81,4 +83,26 @@ private static Map>, String> buildTypeMapping() { return Collections.unmodifiableMap(result); } + @Override + protected boolean isCorrectColumnType(final TypedValue value, String mappedType, String actualType) { + + final boolean mappedTypeEquals = Objects.equals(mappedType, actualType); + + if (mappedTypeEquals) { + return true; + } + + final DataType dataType = value.getType(); + + if (dataType == DataType.DOUBLE || dataType == DataType.FLOAT) { + return "DOUBLE PRECISION".equals(actualType) || actualType.startsWith("FLOAT"); + } else if (dataType == DataType.STRING) { + return actualType.startsWith("CHARACTER VARYING"); + } else if (dataType == DataType.BYTE_ARRAY) { + return "BINARY LARGE OBJECT".equals(actualType); + } else { + return false; + } + } + } diff --git a/kura/test/org.eclipse.kura.wire.db.component.provider.test/src/main/java/org/eclipse/kura/internal/wire/db/test/MultipleValueStoreTest.java b/kura/test/org.eclipse.kura.wire.db.component.provider.test/src/main/java/org/eclipse/kura/internal/wire/db/test/MultipleValueStoreTest.java new file mode 100644 index 00000000000..b9ba7188289 --- /dev/null +++ b/kura/test/org.eclipse.kura.wire.db.component.provider.test/src/main/java/org/eclipse/kura/internal/wire/db/test/MultipleValueStoreTest.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2024 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech + *******************************************************************************/ +package org.eclipse.kura.internal.wire.db.test; + +import static org.eclipse.kura.type.TypedValues.newTypedValue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + +import org.eclipse.kura.KuraException; +import org.eclipse.kura.type.TypedValue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.osgi.framework.InvalidSyntaxException; + +@RunWith(Parameterized.class) +public class MultipleValueStoreTest extends DbComponentsTestBase { + + @Test + public void shoudSupportStoringMultipleValuesOfSameType() + throws KuraException, InvalidSyntaxException, InterruptedException, ExecutionException, TimeoutException { + givenAnEnvelopeReceivedByStore("foo", insertedValue); + givenAnEnvelopeReceivedByStore("foo", insertedValue); + givenAnEnvelopeReceivedByStore("foo", insertedValue); + + whenQueryIsPerformed("SELECT * FROM \"" + tableName + "\" ORDER BY ID ASC;"); + + thenEmittedRecordCountIs(3); + thenFilterEmitsEnvelopeWithProperty(0, 0, "foo", expectedValue); + thenFilterEmitsEnvelopeWithProperty(0, 1, "foo", expectedValue); + thenFilterEmitsEnvelopeWithProperty(0, 2, "foo", expectedValue); + } + + private final TypedValue insertedValue; + private final TypedValue expectedValue; + + public MultipleValueStoreTest(WireComponentTestTarget wireComponentTestTarget, StoreTestTarget storeTestTarget, + final TypedValue insertedValue, final TypedValue extractedValue) + throws InterruptedException, ExecutionException, TimeoutException, KuraException, InvalidSyntaxException { + super(wireComponentTestTarget, storeTestTarget); + this.insertedValue = insertedValue; + this.expectedValue = extractedValue; + } + + @Parameters(name = "{0} with {1} : insert {2} expect {3}") + public static List parameters() { + + final Boolean testBoolean = true; + final Integer testInteger = 12; + final Long testLong = 1235L; + final Double testDouble = 1d; + final Float testFloat = 1f; + final String testString = "foo"; + final byte[] testBa = new byte[] { 1, 2, 3 }; + + final List wireComponentTestTargets = Arrays + .asList(WireComponentTestTarget.WIRE_RECORD_QUERY_AND_WIRE_RECORD_STORE); + + final List storeTestTargets = Arrays.asList(StoreTestTarget.SQLITE, StoreTestTarget.H2); + + final List insertedValues = Arrays.asList( // + testBoolean, // + testInteger, // + testLong, // + testDouble, // + testFloat, // + testString, // + testBa // + ); + + final List expectedValues = Arrays.asList( // + testBoolean, // + testInteger, // + testLong, // + testDouble, // + testDouble, // floats are returned as doubles + testString, // + testBa // + ); + + final List result = new ArrayList<>(); + + for (final WireComponentTestTarget wt : wireComponentTestTargets) { + for (final StoreTestTarget st : storeTestTargets) { + + final Iterator insertedIter = insertedValues.iterator(); + final Iterator expectedIter = expectedValues.iterator(); + + while (insertedIter.hasNext()) { + result.add(new Object[] { wt, st, newTypedValue(insertedIter.next()), + newTypedValue(expectedIter.next()) }); + } + } + } + + return result; + + } + +}