|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Apache License Version 2.0 which is available at |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: Apache-2.0 |
| 12 | + */ |
| 13 | + |
| 14 | +package org.eclipse.uprotocol.client.utwin.v2; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import java.util.concurrent.CompletableFuture; |
| 26 | +import java.util.concurrent.CompletionStage; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.DisplayName; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.Mockito; |
| 33 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 34 | + |
| 35 | +import org.eclipse.uprotocol.communication.RpcClient; |
| 36 | +import org.eclipse.uprotocol.communication.UPayload; |
| 37 | +import org.eclipse.uprotocol.communication.UStatusException; |
| 38 | +import org.eclipse.uprotocol.core.utwin.v2.GetLastMessagesResponse; |
| 39 | +import org.eclipse.uprotocol.transport.UTransport; |
| 40 | +import org.eclipse.uprotocol.v1.UCode; |
| 41 | +import org.eclipse.uprotocol.v1.UUri; |
| 42 | +import org.eclipse.uprotocol.v1.UUriBatch; |
| 43 | + |
| 44 | +/** |
| 45 | + * The uTwin client implementation using RpcClient uP-L2 communication layer interface. |
| 46 | + * This is the test code for said implementation. |
| 47 | + */ |
| 48 | +@ExtendWith(MockitoExtension.class) |
| 49 | +public class SimpleUTwinClientTest { |
| 50 | + @Mock |
| 51 | + private UTransport transport; |
| 52 | + |
| 53 | + |
| 54 | + private final UUri topic = UUri.newBuilder().setAuthorityName("hartley").setUeId(3) |
| 55 | + .setUeVersionMajor(1).setResourceId(0x8000).build(); |
| 56 | + |
| 57 | + |
| 58 | + @BeforeEach |
| 59 | + public void setup() { |
| 60 | + transport = mock(UTransport.class); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("Test calling getLastMessages() with valid topics") |
| 66 | + void testGetLastMessages() { |
| 67 | + |
| 68 | + RpcClient rpcClient = Mockito.mock(RpcClient.class); |
| 69 | + |
| 70 | + UUriBatch topics = UUriBatch.newBuilder().addUris(topic).build(); |
| 71 | + |
| 72 | + when(rpcClient.invokeMethod(any(), any(), any())).thenReturn( |
| 73 | + CompletableFuture.completedFuture(UPayload.pack(GetLastMessagesResponse.getDefaultInstance()))); |
| 74 | + |
| 75 | + SimpleUTwinClient client = new SimpleUTwinClient(rpcClient); |
| 76 | + CompletionStage<GetLastMessagesResponse> response = client.getLastMessages(topics); |
| 77 | + assertNotNull(response); |
| 78 | + assertFalse(response.toCompletableFuture().isCompletedExceptionally()); |
| 79 | + assertDoesNotThrow(() -> response.toCompletableFuture().get()); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + @Test |
| 84 | + @DisplayName("Test calling getLastMessages() with empty topics") |
| 85 | + void testGetLastMessagesEmptyTopics() { |
| 86 | + RpcClient rpcClient = Mockito.mock(RpcClient.class); |
| 87 | + |
| 88 | + UUriBatch topics = UUriBatch.getDefaultInstance(); |
| 89 | + |
| 90 | + SimpleUTwinClient client = new SimpleUTwinClient(rpcClient); |
| 91 | + CompletionStage<GetLastMessagesResponse> response = client.getLastMessages(topics); |
| 92 | + assertNotNull(response); |
| 93 | + assertTrue(response.toCompletableFuture().isCompletedExceptionally()); |
| 94 | + assertDoesNotThrow(() -> { |
| 95 | + response |
| 96 | + .handle((r, e) -> { |
| 97 | + assertNotNull(e); |
| 98 | + assertEquals(((UStatusException)e).getCode(), UCode.INVALID_ARGUMENT); |
| 99 | + assertEquals(((UStatusException)e).getMessage(), "topics must not be empty"); |
| 100 | + return r; |
| 101 | + }) |
| 102 | + .toCompletableFuture().get(); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + @Test |
| 108 | + @DisplayName("Test calling getLastMessages() when the RpcClient completes exceptionally") |
| 109 | + void testGetLastMessagesException() { |
| 110 | + RpcClient rpcClient = Mockito.mock(RpcClient.class); |
| 111 | + |
| 112 | + UUriBatch topics = UUriBatch.newBuilder().addUris(topic).build(); |
| 113 | + |
| 114 | + when(rpcClient.invokeMethod(any(), any(), any())).thenReturn( |
| 115 | + CompletableFuture.failedFuture(new UStatusException(UCode.NOT_FOUND, "Not found"))); |
| 116 | + |
| 117 | + SimpleUTwinClient client = new SimpleUTwinClient(rpcClient); |
| 118 | + CompletionStage<GetLastMessagesResponse> response = client.getLastMessages(topics); |
| 119 | + assertNotNull(response); |
| 120 | + assertTrue(response.toCompletableFuture().isCompletedExceptionally()); |
| 121 | + assertDoesNotThrow(() -> { |
| 122 | + response |
| 123 | + .handle((r, e) -> { |
| 124 | + assertNotNull(e); |
| 125 | + UStatusException t = (UStatusException) e.getCause(); |
| 126 | + assertNotNull(t); |
| 127 | + assertEquals(t.getCode(), UCode.NOT_FOUND); |
| 128 | + assertEquals(t.getMessage(), "Not found"); |
| 129 | + return r; |
| 130 | + }) |
| 131 | + .toCompletableFuture().get(); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments