From f9e71d7eee9737d4176de8e109859ccbde9ba291 Mon Sep 17 00:00:00 2001 From: Kai Hudalla Date: Thu, 16 Oct 2025 16:22:53 +0200 Subject: [PATCH] Remove deprecated API usage from UTwin client implementation --- ...nt.java => RpcClientBasedUTwinClient.java} | 24 ++-- .../v2/RpcClientBasedUTwinClientTest.java | 107 ++++++++++++++ .../utwin/v2/SimpleUTwinClientTest.java | 135 ------------------ .../uprotocol/communication/UClientTest.java | 1 + 4 files changed, 120 insertions(+), 147 deletions(-) rename src/main/java/org/eclipse/uprotocol/client/utwin/v2/{SimpleUTwinClient.java => RpcClientBasedUTwinClient.java} (77%) create mode 100644 src/test/java/org/eclipse/uprotocol/client/utwin/v2/RpcClientBasedUTwinClientTest.java delete mode 100644 src/test/java/org/eclipse/uprotocol/client/utwin/v2/SimpleUTwinClientTest.java diff --git a/src/main/java/org/eclipse/uprotocol/client/utwin/v2/SimpleUTwinClient.java b/src/main/java/org/eclipse/uprotocol/client/utwin/v2/RpcClientBasedUTwinClient.java similarity index 77% rename from src/main/java/org/eclipse/uprotocol/client/utwin/v2/SimpleUTwinClient.java rename to src/main/java/org/eclipse/uprotocol/client/utwin/v2/RpcClientBasedUTwinClient.java index b5f61038..4b7b3a90 100644 --- a/src/main/java/org/eclipse/uprotocol/client/utwin/v2/SimpleUTwinClient.java +++ b/src/main/java/org/eclipse/uprotocol/client/utwin/v2/RpcClientBasedUTwinClient.java @@ -17,7 +17,6 @@ import java.util.concurrent.CompletionStage; import org.eclipse.uprotocol.communication.CallOptions; import org.eclipse.uprotocol.communication.RpcClient; -import org.eclipse.uprotocol.communication.RpcMapper; import org.eclipse.uprotocol.communication.UPayload; import org.eclipse.uprotocol.communication.UStatusException; import org.eclipse.uprotocol.core.utwin.v2.GetLastMessagesRequest; @@ -34,33 +33,34 @@ /** * The uTwin client implementation using the RpcClient uP-L2 communication layer interface. */ -public class SimpleUTwinClient implements UTwinClient { - private final RpcClient rpcClient; +public class RpcClientBasedUTwinClient implements UTwinClient { - private static final ServiceDescriptor UTWIN = UTwinProto.getDescriptor().getServices().get(0); + static final ServiceDescriptor UTWIN = UTwinProto.getDescriptor().getServices().get(0); // TODO: The following items eventually need to be pulled from generated code - private static final UUri GETLASTMESSAGE_METHOD = UriFactory.fromProto(UTWIN, 1); + static final UUri GETLASTMESSAGE_METHOD = UriFactory.fromProto(UTWIN, 1); + private final RpcClient rpcClient; /** - * Create a new instance of the uTwin client passing in the RPCClient to use for communication. + * Creates a new client for the uTwin service. * * @param rpcClient The RPC client to use for communication. */ - public SimpleUTwinClient(RpcClient rpcClient) { + public RpcClientBasedUTwinClient(RpcClient rpcClient) { this.rpcClient = rpcClient; } /** - * Fetch the last messages for a batch of topics. + * Fetches the last messages for a batch of topics. * * @param topics {@link UUriBatch} batch of 1 or more topics to fetch the last messages for. * @param options The call options. * @return CompletionStage completes successfully with {@link GetLastMessagesResponse} if uTwin was able * to fetch the topics or completes exceptionally with {@link UStatus} with the failure reason. * such as {@code UCode.NOT_FOUND}, {@code UCode.PERMISSION_DENIED} etc... + * @throws NullPointerException if topics or options is {@code null}. */ @Override public CompletionStage getLastMessages(UUriBatch topics, CallOptions options) { @@ -73,8 +73,8 @@ public CompletionStage getLastMessages(UUriBatch topics new UStatusException(UCode.INVALID_ARGUMENT, "topics must not be empty")); } - GetLastMessagesRequest request = GetLastMessagesRequest.newBuilder().setTopics(topics).build(); - return RpcMapper.mapResponse(rpcClient.invokeMethod( - GETLASTMESSAGE_METHOD, UPayload.pack(request), options), GetLastMessagesResponse.class); - } + final var request = GetLastMessagesRequest.newBuilder().setTopics(topics).build(); + return rpcClient.invokeMethod(GETLASTMESSAGE_METHOD, UPayload.pack(request), options) + .thenApply(response -> UPayload.unpackOrDefaultInstance(response, GetLastMessagesResponse.class)); + } } diff --git a/src/test/java/org/eclipse/uprotocol/client/utwin/v2/RpcClientBasedUTwinClientTest.java b/src/test/java/org/eclipse/uprotocol/client/utwin/v2/RpcClientBasedUTwinClientTest.java new file mode 100644 index 00000000..6e7a52d5 --- /dev/null +++ b/src/test/java/org/eclipse/uprotocol/client/utwin/v2/RpcClientBasedUTwinClientTest.java @@ -0,0 +1,107 @@ +/** + * SPDX-FileCopyrightText: 2024 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 Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.eclipse.uprotocol.client.utwin.v2; + +import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.eclipse.uprotocol.communication.CallOptions; +import org.eclipse.uprotocol.communication.RpcClient; +import org.eclipse.uprotocol.communication.UPayload; +import org.eclipse.uprotocol.communication.UStatusException; +import org.eclipse.uprotocol.core.utwin.v2.GetLastMessagesResponse; +import org.eclipse.uprotocol.v1.UCode; +import org.eclipse.uprotocol.v1.UUri; +import org.eclipse.uprotocol.v1.UUriBatch; + +/** + * The uTwin client implementation using RpcClient uP-L2 communication layer interface. + * This is the test code for said implementation. + */ +@ExtendWith(MockitoExtension.class) +class RpcClientBasedUTwinClientTest { + + private static final UUri TOPIC = UUri.newBuilder() + .setAuthorityName("hartley") + .setUeId(0x0003) + .setUeVersionMajor(0x01) + .setResourceId(0x8000) + .build(); + + @Mock + private RpcClient rpcClient; + private UTwinClient twinClient; + + @BeforeEach + void setUp() { + twinClient = new RpcClientBasedUTwinClient(rpcClient); + } + + @Test + @DisplayName("Test calling getLastMessages() with valid topics") + void testGetLastMessages() { + when(rpcClient.invokeMethod( + eq(RpcClientBasedUTwinClient.GETLASTMESSAGE_METHOD), + any(UPayload.class), + any(CallOptions.class))) + .thenReturn(CompletableFuture.completedFuture(UPayload.pack(GetLastMessagesResponse.getDefaultInstance()))); + + UUriBatch topics = UUriBatch.newBuilder().addUris(TOPIC).build(); + var response = twinClient.getLastMessages(topics).toCompletableFuture().join(); + assertEquals(0, response.getResponsesCount()); + } + + + @Test + @DisplayName("Test calling getLastMessages() with empty topics") + void testGetLastMessagesEmptyTopics() { + UUriBatch topics = UUriBatch.getDefaultInstance(); + var exception = assertThrows(CompletionException.class, () -> { + twinClient.getLastMessages(topics).toCompletableFuture().join(); + }); + assertInstanceOf(UStatusException.class, exception.getCause()); + assertEquals(UCode.INVALID_ARGUMENT, ((UStatusException) exception.getCause()).getCode()); + } + + + @Test + @DisplayName("Test calling getLastMessages() when the RpcClient completes exceptionally") + void testGetLastMessagesException() { + when(rpcClient.invokeMethod( + eq(RpcClientBasedUTwinClient.GETLASTMESSAGE_METHOD), + any(UPayload.class), + any(CallOptions.class))) + .thenReturn(CompletableFuture.failedFuture(new UStatusException(UCode.NOT_FOUND, "Not found"))); + + UUriBatch topics = UUriBatch.newBuilder().addUris(TOPIC).build(); + var exception = assertThrows(CompletionException.class, () -> { + twinClient.getLastMessages(topics).toCompletableFuture().join(); + }); + assertInstanceOf(UStatusException.class, exception.getCause()); + assertEquals(UCode.NOT_FOUND, ((UStatusException) exception.getCause()).getCode()); + } +} diff --git a/src/test/java/org/eclipse/uprotocol/client/utwin/v2/SimpleUTwinClientTest.java b/src/test/java/org/eclipse/uprotocol/client/utwin/v2/SimpleUTwinClientTest.java deleted file mode 100644 index c43fbd3f..00000000 --- a/src/test/java/org/eclipse/uprotocol/client/utwin/v2/SimpleUTwinClientTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/** - * SPDX-FileCopyrightText: 2024 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 Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.uprotocol.client.utwin.v2; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; - -import org.eclipse.uprotocol.communication.RpcClient; -import org.eclipse.uprotocol.communication.UPayload; -import org.eclipse.uprotocol.communication.UStatusException; -import org.eclipse.uprotocol.core.utwin.v2.GetLastMessagesResponse; -import org.eclipse.uprotocol.transport.UTransport; -import org.eclipse.uprotocol.v1.UCode; -import org.eclipse.uprotocol.v1.UUri; -import org.eclipse.uprotocol.v1.UUriBatch; - -/** - * The uTwin client implementation using RpcClient uP-L2 communication layer interface. - * This is the test code for said implementation. - */ -@ExtendWith(MockitoExtension.class) -public class SimpleUTwinClientTest { - @Mock - private UTransport transport; - - - private final UUri topic = UUri.newBuilder().setAuthorityName("hartley").setUeId(3) - .setUeVersionMajor(1).setResourceId(0x8000).build(); - - - @BeforeEach - public void setup() { - transport = mock(UTransport.class); - } - - - @Test - @DisplayName("Test calling getLastMessages() with valid topics") - void testGetLastMessages() { - - RpcClient rpcClient = Mockito.mock(RpcClient.class); - - UUriBatch topics = UUriBatch.newBuilder().addUris(topic).build(); - - when(rpcClient.invokeMethod(any(), any(), any())).thenReturn( - CompletableFuture.completedFuture(UPayload.pack(GetLastMessagesResponse.getDefaultInstance()))); - - SimpleUTwinClient client = new SimpleUTwinClient(rpcClient); - CompletionStage response = client.getLastMessages(topics); - assertNotNull(response); - assertFalse(response.toCompletableFuture().isCompletedExceptionally()); - assertDoesNotThrow(() -> response.toCompletableFuture().get()); - } - - - @Test - @DisplayName("Test calling getLastMessages() with empty topics") - void testGetLastMessagesEmptyTopics() { - RpcClient rpcClient = Mockito.mock(RpcClient.class); - - UUriBatch topics = UUriBatch.getDefaultInstance(); - - SimpleUTwinClient client = new SimpleUTwinClient(rpcClient); - CompletionStage response = client.getLastMessages(topics); - assertNotNull(response); - assertTrue(response.toCompletableFuture().isCompletedExceptionally()); - assertDoesNotThrow(() -> { - response - .handle((r, e) -> { - assertNotNull(e); - assertEquals(((UStatusException) e).getCode(), UCode.INVALID_ARGUMENT); - assertEquals(((UStatusException) e).getMessage(), "topics must not be empty"); - return r; - }) - .toCompletableFuture().get(); - }); - } - - - @Test - @DisplayName("Test calling getLastMessages() when the RpcClient completes exceptionally") - void testGetLastMessagesException() { - RpcClient rpcClient = Mockito.mock(RpcClient.class); - - UUriBatch topics = UUriBatch.newBuilder().addUris(topic).build(); - - when(rpcClient.invokeMethod(any(), any(), any())).thenReturn( - CompletableFuture.failedFuture(new UStatusException(UCode.NOT_FOUND, "Not found"))); - - SimpleUTwinClient client = new SimpleUTwinClient(rpcClient); - CompletionStage response = client.getLastMessages(topics); - assertNotNull(response); - assertTrue(response.toCompletableFuture().isCompletedExceptionally()); - assertDoesNotThrow(() -> { - response - .handle((r, e) -> { - assertNotNull(e); - UStatusException t = (UStatusException) e.getCause(); - assertNotNull(t); - assertEquals(t.getCode(), UCode.NOT_FOUND); - assertEquals(t.getMessage(), "Not found"); - return r; - }) - .toCompletableFuture().get(); - }); - } - -} diff --git a/src/test/java/org/eclipse/uprotocol/communication/UClientTest.java b/src/test/java/org/eclipse/uprotocol/communication/UClientTest.java index fd6a2c80..d3748f20 100644 --- a/src/test/java/org/eclipse/uprotocol/communication/UClientTest.java +++ b/src/test/java/org/eclipse/uprotocol/communication/UClientTest.java @@ -64,6 +64,7 @@ void setUp() { } @Test + @SuppressWarnings("unchecked") void testFactoryMethod() { var transport = mock(UTransport.class); when(transport.registerListener(any(UUri.class), any(Optional.class), any(UListener.class)))