Browse Source

Merge remote-tracking branch 'origin/test-edge-latest-version-42' into test-edge-latest-version-43

pull/14955/head
Nikita Mazurenko 5 days ago
parent
commit
46b00a3a41
  1. 5
      common/edge-api/pom.xml
  2. 39
      common/edge-api/src/main/java/org/thingsboard/edge/rpc/EdgeGrpcClient.java
  3. 69
      common/edge-api/src/main/java/org/thingsboard/edge/rpc/EdgeVersionComparator.java
  4. 73
      common/edge-api/src/test/java/org/thingsboard/edge/rpc/EdgeVersionComparatorTest.java

5
common/edge-api/pom.xml

@ -106,6 +106,11 @@
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

39
common/edge-api/src/main/java/org/thingsboard/edge/rpc/EdgeGrpcClient.java

@ -143,44 +143,7 @@ public class EdgeGrpcClient implements EdgeRpcClient {
}
public static EdgeVersion getNewestEdgeVersion() {
EdgeVersion newest = null;
int[] newestParts = null;
for (EdgeVersion v : EdgeVersion.values()) {
if (v == EdgeVersion.V_LATEST || v == EdgeVersion.UNRECOGNIZED) {
continue;
}
int[] parts = parseVersionParts(v);
if (newest == null || compareVersionParts(parts, newestParts) > 0) {
newest = v;
newestParts = parts;
}
}
return newest;
}
private static int[] parseVersionParts(EdgeVersion version) {
String name = version.name();
if (name.startsWith("V_")) {
name = name.substring(2);
}
String[] parts = name.split("_");
int[] result = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
result[i] = Integer.parseInt(parts[i]);
}
return result;
}
private static int compareVersionParts(int[] a, int[] b) {
int maxLen = Math.max(a.length, b.length);
for (int i = 0; i < maxLen; i++) {
int partA = i < a.length ? a[i] : 0;
int partB = i < b.length ? b[i] : 0;
if (partA != partB) {
return Integer.compare(partA, partB);
}
}
return 0;
return EdgeVersionComparator.getNewestEdgeVersion();
}
private StreamObserver<ResponseMsg> initOutputStream(String edgeKey,

69
common/edge-api/src/main/java/org/thingsboard/edge/rpc/EdgeVersionComparator.java

@ -0,0 +1,69 @@
/**
* Copyright © 2016-2026 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.edge.rpc;
import org.thingsboard.server.gen.edge.v1.EdgeVersion;
import java.util.Comparator;
public class EdgeVersionComparator implements Comparator<EdgeVersion> {
public static final EdgeVersionComparator INSTANCE = new EdgeVersionComparator();
@Override
public int compare(EdgeVersion v1, EdgeVersion v2) {
return compareVersionParts(parseVersionParts(v1), parseVersionParts(v2));
}
public static EdgeVersion getNewestEdgeVersion() {
EdgeVersion newest = null;
for (EdgeVersion v : EdgeVersion.values()) {
if (v == EdgeVersion.V_LATEST || v == EdgeVersion.UNRECOGNIZED) {
continue;
}
if (newest == null || INSTANCE.compare(v, newest) > 0) {
newest = v;
}
}
return newest;
}
private static int[] parseVersionParts(EdgeVersion version) {
String name = version.name();
if (name.startsWith("V_")) {
name = name.substring(2);
}
String[] parts = name.split("_");
int[] result = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
result[i] = Integer.parseInt(parts[i]);
}
return result;
}
private static int compareVersionParts(int[] a, int[] b) {
int maxLen = Math.max(a.length, b.length);
for (int i = 0; i < maxLen; i++) {
int partA = i < a.length ? a[i] : 0;
int partB = i < b.length ? b[i] : 0;
if (partA != partB) {
return Integer.compare(partA, partB);
}
}
return 0;
}
}

73
common/edge-api/src/test/java/org/thingsboard/edge/rpc/EdgeVersionComparatorTest.java

@ -0,0 +1,73 @@
/**
* Copyright © 2016-2026 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.edge.rpc;
import org.junit.jupiter.api.Test;
import org.thingsboard.server.gen.edge.v1.EdgeVersion;
import static org.assertj.core.api.Assertions.assertThat;
class EdgeVersionComparatorTest {
@Test
void compare_sameVersion_returnsZero() {
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_3_3_0, EdgeVersion.V_3_3_0)).isEqualTo(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_4_0_0, EdgeVersion.V_4_0_0)).isEqualTo(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_4_2_1_2, EdgeVersion.V_4_2_1_2)).isEqualTo(0);
}
@Test
void compare_majorVersionDifference_returnsCorrectOrder() {
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_3_3_0, EdgeVersion.V_4_0_0)).isLessThan(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_4_0_0, EdgeVersion.V_3_3_0)).isGreaterThan(0);
}
@Test
void compare_minorVersionDifference_returnsCorrectOrder() {
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_3_3_0, EdgeVersion.V_3_6_0)).isLessThan(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_3_6_0, EdgeVersion.V_3_3_0)).isGreaterThan(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_4_0_0, EdgeVersion.V_4_1_0)).isLessThan(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_4_1_0, EdgeVersion.V_4_0_0)).isGreaterThan(0);
}
@Test
void compare_patchVersionDifference_returnsCorrectOrder() {
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_3_6_0, EdgeVersion.V_3_6_1)).isLessThan(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_3_6_1, EdgeVersion.V_3_6_0)).isGreaterThan(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_3_6_1, EdgeVersion.V_3_6_2)).isLessThan(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_3_6_2, EdgeVersion.V_3_6_4)).isLessThan(0);
}
@Test
void compare_fourPartVersion_returnsCorrectOrder() {
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_4_2_0, EdgeVersion.V_4_2_1_2)).isLessThan(0);
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_4_2_1_2, EdgeVersion.V_4_2_0)).isGreaterThan(0);
}
@Test
void compare_threePartVsFourPart_treatsImplicitZero() {
// V_4_2_0 should be less than V_4_2_1_2 (4.2.0.0 < 4.2.1.2)
assertThat(EdgeVersionComparator.INSTANCE.compare(EdgeVersion.V_4_2_0, EdgeVersion.V_4_2_1_2)).isLessThan(0);
}
@Test
void getNewestEdgeVersion_excludesLatestAndUnrecognized() {
EdgeVersion newest = EdgeVersionComparator.getNewestEdgeVersion();
assertThat(newest).isNotNull();
assertThat(newest).isNotEqualTo(EdgeVersion.V_LATEST);
assertThat(newest).isNotEqualTo(EdgeVersion.UNRECOGNIZED);
}
}
Loading…
Cancel
Save