diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b81911f..6e28c76 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -17,7 +17,7 @@ androidx-junit = "1.1.5" junit = "4.13.2" mockito = "4.6.1" robolectric = "4.7.3" -up-java = "1.5.7" +up-java = "1.5.8" [libraries] androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" } diff --git a/library/src/androidTest/java/org/eclipse/uprotocol/TestBase.java b/library/src/androidTest/java/org/eclipse/uprotocol/TestBase.java index a151e25..2bfc321 100644 --- a/library/src/androidTest/java/org/eclipse/uprotocol/TestBase.java +++ b/library/src/androidTest/java/org/eclipse/uprotocol/TestBase.java @@ -96,24 +96,20 @@ public class TestBase { protected static final long CONNECTION_TIMEOUT_MS = 3000; protected static final long DELAY_MS = 100; - protected static @NonNull UAttributes buildPublishAttributes() { - return newPublishAttributesBuilder().build(); + protected static @NonNull UAttributes buildPublishAttributes(@NonNull UUri source) { + return newPublishAttributesBuilder(source).build(); } - - protected static @NonNull UAttributesBuilder newPublishAttributesBuilder() { - return UAttributesBuilder.publish(UPriority.UPRIORITY_CS0); + protected static @NonNull UAttributesBuilder newPublishAttributesBuilder(@NonNull UUri source) { + return UAttributesBuilder.publish(source, UPriority.UPRIORITY_CS0); } - protected static @NonNull UAttributesBuilder newNotificationAttributesBuilder(@NonNull UUri sink) { - return UAttributesBuilder.notification(UPriority.UPRIORITY_CS0, sink); + protected static @NonNull UAttributesBuilder newNotificationAttributesBuilder(@NonNull UUri source, @NonNull UUri sink) { + return UAttributesBuilder.notification(source, sink, UPriority.UPRIORITY_CS0); } - protected static @NonNull UMessage buildMessage(UUri source, UPayload payload, UAttributes attributes) { + protected static @NonNull UMessage buildMessage(UPayload payload, UAttributes attributes) { final UMessage.Builder builder = UMessage.newBuilder(); - if (source != null) { - builder.setSource(source); - } if (payload != null) { builder.setPayload(payload); } diff --git a/library/src/androidTest/java/org/eclipse/uprotocol/UPClientTest.java b/library/src/androidTest/java/org/eclipse/uprotocol/UPClientTest.java index 6475efc..2f6b3d0 100644 --- a/library/src/androidTest/java/org/eclipse/uprotocol/UPClientTest.java +++ b/library/src/androidTest/java/org/eclipse/uprotocol/UPClientTest.java @@ -78,9 +78,9 @@ @RunWith(AndroidJUnit4.class) public class UPClientTest extends TestBase { - private static final UMessage MESSAGE = buildMessage(RESOURCE_URI, PAYLOAD, buildPublishAttributes()); - private static final UMessage NOTIFICATION_MESSAGE = buildMessage(RESOURCE_URI, PAYLOAD, - newNotificationAttributesBuilder(CLIENT_URI).build()); + private static final UMessage MESSAGE = buildMessage(PAYLOAD, buildPublishAttributes(RESOURCE_URI)); + private static final UMessage NOTIFICATION_MESSAGE = buildMessage(PAYLOAD, + newNotificationAttributesBuilder(RESOURCE_URI, CLIENT_URI).build()); private static final CallOptions OPTIONS = CallOptions.newBuilder() .withTimeout(TTL) .build(); @@ -220,13 +220,7 @@ public void testSend() { } @Test - public void testSendParts() { - createTopic(RESOURCE_URI); - assertStatus(UCode.OK, sClient.send(MESSAGE.getSource(), MESSAGE.getPayload(), MESSAGE.getAttributes())); - } - - @Test - public void testSendNotificationMassage() { + public void testSendNotificationMessage() { assertStatus(UCode.OK, sClient.send(NOTIFICATION_MESSAGE)); } @@ -330,7 +324,7 @@ public void testOnReceiveGenericMessageNotRegistered() { @Test public void testOnReceiveNotificationMessage() { testRegisterListener(); - testSendNotificationMassage(); + testSendNotificationMessage(); verify(sListener, timeout(DELAY_MS).times(1)).onReceive(NOTIFICATION_MESSAGE); } @@ -422,7 +416,7 @@ public void testInvokeMethod() throws Exception { verify(sRequestListener, timeout(DELAY_MS).times(1)) .onReceive(requestCaptor.capture(), responseFutureCaptor.capture()); final UMessage requestMessage = requestCaptor.getValue(); - assertEquals(RESPONSE_URI.getEntity(), requestMessage.getSource().getEntity()); + assertEquals(RESPONSE_URI.getEntity(), requestMessage.getAttributes().getSource().getEntity()); assertEquals(REQUEST_PAYLOAD, requestMessage.getPayload()); assertEquals(METHOD_URI, requestMessage.getAttributes().getSink()); assertEquals(OPTIONS.timeout(), requestMessage.getAttributes().getTtl()); @@ -430,7 +424,7 @@ public void testInvokeMethod() throws Exception { responseFutureCaptor.getValue().complete(RESPONSE_PAYLOAD); final UMessage responseMessage = responseFuture.get(DELAY_MS, TimeUnit.MILLISECONDS); - assertEquals(METHOD_URI, responseMessage.getSource()); + assertEquals(METHOD_URI, responseMessage.getAttributes().getSource()); assertEquals(RESPONSE_PAYLOAD, responseMessage.getPayload()); assertEquals(RESPONSE_URI, responseMessage.getAttributes().getSink()); assertEquals(UMessageType.UMESSAGE_TYPE_RESPONSE, responseMessage.getAttributes().getType()); diff --git a/library/src/main/java/org/eclipse/uprotocol/UPClient.java b/library/src/main/java/org/eclipse/uprotocol/UPClient.java index ab23698..c63b1ef 100644 --- a/library/src/main/java/org/eclipse/uprotocol/UPClient.java +++ b/library/src/main/java/org/eclipse/uprotocol/UPClient.java @@ -179,17 +179,7 @@ public void onConnectionInterrupted() { } }; - private final UListener mListener = new UListener() { - @Override - public void onReceive(UUri topic, UPayload payload, UAttributes attributes) { - handleMessage(buildMessage(topic, payload, attributes)); - } - - @Override - public void onReceive(UMessage message) { - handleMessage(message); - } - }; + private final UListener mListener = this::handleMessage; /** * The callback to notify the lifecycle of the uBus. @@ -480,32 +470,6 @@ UListener getListener() { return mListener; } - private static @NonNull UMessage buildMessage(UUri source, UPayload payload, UAttributes attributes) { - final UMessage.Builder builder = UMessage.newBuilder(); - if (source != null) { - builder.setSource(source); - } - if (payload != null) { - builder.setPayload(payload); - } - if (attributes != null) { - builder.setAttributes(attributes); - } - return builder.build(); - } - - /** - * Transmit a payload associated with a source using defined attributes. - * - * @param source A {@link UUri} associated with a payload. - * @param payload A {@link UPayload} to be sent. - * @param attributes {@link UAttributes} containing transport attributes. - * @return A {@link UStatus} which contains a result code and other details. - */ - @Override - public @NonNull UStatus send(@NonNull UUri source, @NonNull UPayload payload, @NonNull UAttributes attributes) { - return mUBusManager.send(buildMessage(source, payload, attributes)); - } /** * Transmit a message. @@ -708,9 +672,12 @@ private boolean unregisterListenerLocked(@NonNull UUri topic, @NonNull UListener checkNotNull(requestPayload, "Payload is null"); checkNotNull(options, "Options cannot be null"); final int timeout = checkArgumentPositive(options.timeout(), "Timeout is not positive"); - final UAttributesBuilder builder = UAttributesBuilder.request(UPriority.UPRIORITY_CS4, methodUri, timeout); + final UAttributesBuilder builder = UAttributesBuilder.request(mResponseUri, methodUri, UPriority.UPRIORITY_CS4, timeout); options.token().ifPresent(builder::withToken); - final UMessage requestMessage = buildMessage(mResponseUri, requestPayload, builder.build()); + final UMessage requestMessage = UMessage.newBuilder() + .setPayload(requestPayload) + .setAttributes(builder.build()) + .build(); return mRequests.compute(requestMessage.getAttributes().getId(), (requestId, currentRequest) -> { checkArgument(currentRequest == null, UCode.ABORTED, "Duplicated request found"); final UStatus status = send(requestMessage); @@ -765,7 +732,7 @@ private void handleGenericMessage(@NonNull UMessage message) { } } mCallbackExecutor.execute(() -> { - final UUri topic = message.getSource(); + final UUri topic = message.getAttributes().getSource(); final Set listeners; synchronized (mRegistrationLock) { listeners = new ArraySet<>(mListeners.get(topic)); @@ -796,14 +763,26 @@ private void handleRequestMessage(@NonNull UMessage requestMessage) { final CompletableFuture responseFuture = new CompletableFuture<>(); responseFuture.whenComplete((responsePayload, exception) -> { final UAttributes requestAttributes = requestMessage.getAttributes(); - final UAttributesBuilder builder = UAttributesBuilder - .response(requestAttributes.getPriority(), requestMessage.getSource(), requestAttributes.getId()); + final UAttributesBuilder builder = UAttributesBuilder.response( + requestAttributes.getSink(), + requestAttributes.getSource(), + requestAttributes.getPriority(), + requestAttributes.getId()); + final UMessage responseMessage; if (exception != null) { builder.withCommStatus(toStatus(exception).getCodeValue()); - } else if (responsePayload == null) { + responseMessage = UMessage.newBuilder() + .setAttributes(builder.build()) + .build(); + } else if (responsePayload != null) { + responseMessage = UMessage.newBuilder() + .setPayload(responsePayload) + .setAttributes(builder.build()) + .build(); + } else { return; } - send(buildMessage(requestAttributes.getSink(), responsePayload, builder.build())); + send(responseMessage); }); return responseFuture; } diff --git a/library/src/main/java/org/eclipse/uprotocol/common/util/log/Formatter.java b/library/src/main/java/org/eclipse/uprotocol/common/util/log/Formatter.java index f122f25..4497035 100644 --- a/library/src/main/java/org/eclipse/uprotocol/common/util/log/Formatter.java +++ b/library/src/main/java/org/eclipse/uprotocol/common/util/log/Formatter.java @@ -276,7 +276,7 @@ private static void appendPairsSeparator(@NonNull StringBuilder builder) { } final UAttributes attributes = message.getAttributes(); final boolean hasSink = attributes.hasSink(); - return joinGrouped(Key.ID, stringify(attributes.getId()), Key.SOURCE, stringify(message.getSource()), + return joinGrouped(Key.ID, stringify(attributes.getId()), Key.SOURCE, stringify(attributes.getSource()), hasSink ? Key.SINK : null, hasSink ? stringify(attributes.getSink()) : null, Key.TYPE, attributes.getType()); } diff --git a/library/src/test/java/org/eclipse/uprotocol/TestBase.java b/library/src/test/java/org/eclipse/uprotocol/TestBase.java index c66dbdb..515aa6a 100644 --- a/library/src/test/java/org/eclipse/uprotocol/TestBase.java +++ b/library/src/test/java/org/eclipse/uprotocol/TestBase.java @@ -137,6 +137,7 @@ public class TestBase { protected static final UAttributes ATTRIBUTES = UAttributes.newBuilder() .setId(ID) .setType(UMessageType.UMESSAGE_TYPE_RESPONSE) + .setSource(METHOD_URI) .setSink(RESPONSE_URI) .setPriority(UPriority.UPRIORITY_CS4) .setTtl(TTL) @@ -152,40 +153,40 @@ public class TestBase { return UuidFactory.Factories.UPROTOCOL.factory().create(); } - protected static @NonNull UAttributes buildPublishAttributes() { - return newPublishAttributesBuilder().build(); + protected static @NonNull UAttributes buildPublishAttributes(@NonNull UUri source) { + return newPublishAttributesBuilder(source).build(); } - protected static @NonNull UAttributes buildRequestAttributes(@NonNull UUri methodUri) { - return newRequestAttributesBuilder(methodUri).build(); + protected static @NonNull UAttributes buildRequestAttributes(@NonNull UUri responseUri, @NonNull UUri methodUri) { + return newRequestAttributesBuilder(responseUri, methodUri).build(); } - protected static @NonNull UAttributes buildResponseAttributes(@NonNull UUri responseUri, @NonNull UUID requestId) { - return newResponseAttributesBuilder(responseUri, requestId).build(); + protected static @NonNull UAttributes buildResponseAttributes( + @NonNull UUri methodUri, @NonNull UUri responseUri, @NonNull UUID requestId) { + return newResponseAttributesBuilder(methodUri, responseUri, requestId).build(); } - protected static @NonNull UAttributesBuilder newPublishAttributesBuilder() { - return UAttributesBuilder.publish(UPriority.UPRIORITY_CS0); + protected static @NonNull UAttributesBuilder newPublishAttributesBuilder(@NonNull UUri source) { + return UAttributesBuilder.publish(source, UPriority.UPRIORITY_CS0); } - protected static @NonNull UAttributesBuilder newNotificationAttributesBuilder(@NonNull UUri sink) { - return UAttributesBuilder.notification(UPriority.UPRIORITY_CS0, sink); + protected static @NonNull UAttributesBuilder newNotificationAttributesBuilder( + @NonNull UUri source, @NonNull UUri sink) { + return UAttributesBuilder.notification(source, sink, UPriority.UPRIORITY_CS0); } - protected static @NonNull UAttributesBuilder newRequestAttributesBuilder(@NonNull UUri methodUri) { - return UAttributesBuilder.request(UPriority.UPRIORITY_CS4, methodUri, TTL); + protected static @NonNull UAttributesBuilder newRequestAttributesBuilder( + @NonNull UUri responseUri, @NonNull UUri methodUri) { + return UAttributesBuilder.request(responseUri, methodUri, UPriority.UPRIORITY_CS4, TTL); } - protected static @NonNull UAttributesBuilder newResponseAttributesBuilder(@NonNull UUri responseUri, - @NonNull UUID requestId) { - return UAttributesBuilder.response(UPriority.UPRIORITY_CS4, responseUri, requestId); + protected static @NonNull UAttributesBuilder newResponseAttributesBuilder( + @NonNull UUri methodUri, @NonNull UUri responseUri, @NonNull UUID requestId) { + return UAttributesBuilder.response(methodUri, responseUri, UPriority.UPRIORITY_CS4, requestId); } - protected static @NonNull UMessage buildMessage(UUri source, UPayload payload, UAttributes attributes) { + protected static @NonNull UMessage buildMessage(UPayload payload, UAttributes attributes) { final UMessage.Builder builder = UMessage.newBuilder(); - if (source != null) { - builder.setSource(source); - } if (payload != null) { builder.setPayload(payload); } diff --git a/library/src/test/java/org/eclipse/uprotocol/UPClientTest.java b/library/src/test/java/org/eclipse/uprotocol/UPClientTest.java index 15049d2..26d451b 100644 --- a/library/src/test/java/org/eclipse/uprotocol/UPClientTest.java +++ b/library/src/test/java/org/eclipse/uprotocol/UPClientTest.java @@ -99,7 +99,7 @@ @RunWith(AndroidJUnit4.class) public class UPClientTest extends TestBase { - private static final UMessage MESSAGE = buildMessage(RESOURCE_URI, PAYLOAD, buildPublishAttributes()); + private static final UMessage MESSAGE = buildMessage(PAYLOAD, buildPublishAttributes(RESOURCE_URI)); private static final UPayload REQUEST_PAYLOAD = packToAny(Int32Value.newBuilder().setValue(1).build()); private static final UPayload RESPONSE_PAYLOAD = packToAny(STATUS_OK); @@ -319,21 +319,6 @@ public void testSend() { assertStatus(UCode.OK, mClient.send(MESSAGE)); } - @Test - public void testSendParts() { - doReturn(STATUS_OK).when(mManager).send(MESSAGE); - assertStatus(UCode.OK, mClient.send(MESSAGE.getSource(), MESSAGE.getPayload(), MESSAGE.getAttributes())); - } - - @Test - @SuppressWarnings("DataFlowIssue") - public void testSendPartsWithNulls() { - doReturn(STATUS_OK).when(mManager).send(any()); - assertStatus(UCode.OK, mClient.send(null, MESSAGE.getPayload(), MESSAGE.getAttributes())); - assertStatus(UCode.OK, mClient.send(MESSAGE.getSource(), null, MESSAGE.getAttributes())); - assertStatus(UCode.OK, mClient.send(MESSAGE.getSource(), MESSAGE.getPayload(), null)); - } - @Test public void testRegisterListener() { doReturn(STATUS_OK).when(mManager).enableDispatching(RESOURCE_URI); @@ -474,14 +459,6 @@ public void testOnReceiveGenericMessage() { verify(mListener2, timeout(DELAY_MS).times(1)).onReceive(MESSAGE); } - @Test - public void testOnReceiveGenericMessageParts() { - testRegisterListenerNotFirst(); - mClient.getListener().onReceive(MESSAGE.getSource(), MESSAGE.getPayload(), MESSAGE.getAttributes()); - verify(mListener, timeout(DELAY_MS).times(1)).onReceive(MESSAGE); - verify(mListener2, timeout(DELAY_MS).times(1)).onReceive(MESSAGE); - } - @Test public void testOnReceiveGenericMessageNotRegistered() { testUnregisterListener(); @@ -493,7 +470,7 @@ public void testOnReceiveGenericMessageNotRegistered() { public void testOnReceiveNotificationMessage() { testRegisterListener(); final UMessage message = - buildMessage(RESOURCE_URI, PAYLOAD,newNotificationAttributesBuilder(CLIENT_URI).build()); + buildMessage(PAYLOAD,newNotificationAttributesBuilder(RESOURCE_URI, CLIENT_URI).build()); mClient.getListener().onReceive(message); verify(mListener, timeout(DELAY_MS).times(1)).onReceive(message); } @@ -503,7 +480,7 @@ public void testOnReceiveNotificationMessageWrongSink() { mClient.setLoggable(Log.VERBOSE); testRegisterListener(); final UMessage message = - buildMessage(RESOURCE_URI, PAYLOAD, newNotificationAttributesBuilder(SERVICE_URI).build()); + buildMessage(PAYLOAD, newNotificationAttributesBuilder(RESOURCE_URI, SERVICE_URI).build()); mClient.getListener().onReceive(message); verify(mListener, timeout(DELAY_MS).times(0)).onReceive(message); } @@ -512,7 +489,7 @@ public void testOnReceiveNotificationMessageWrongSink() { public void testOnReceiveMessageExpired() { mClient.setLoggable(Log.VERBOSE); testRegisterListener(); - final UMessage message = buildMessage(RESOURCE_URI, PAYLOAD, newPublishAttributesBuilder().withTtl(1).build()); + final UMessage message = buildMessage(PAYLOAD, newPublishAttributesBuilder(RESOURCE_URI).withTtl(1).build()); sleep(DELAY_MS); mClient.getListener().onReceive(message); verify(mListener, timeout(DELAY_MS).times(0)).onReceive(message); @@ -521,7 +498,7 @@ public void testOnReceiveMessageExpired() { @Test public void testOnReceiveMessageWithoutAttributes() { testRegisterListener(); - final UMessage message = buildMessage(RESOURCE_URI, null, null); + final UMessage message = buildMessage(null, null); mClient.getListener().onReceive(message); verify(mListener, timeout(DELAY_MS).times(0)).onReceive(message); } @@ -530,7 +507,7 @@ public void testOnReceiveMessageWithoutAttributes() { public void testOnReceiveMessageWithUnknownType() { testRegisterListener(); try (MockedStatic mockedValidator = mockStatic(UAttributesValidator.class)) { - final UMessage message = buildMessage(RESOURCE_URI, null, null); + final UMessage message = buildMessage(null, null); final UAttributesValidator dummyValidator = new UAttributesValidator() { @Override public ValidationResult validateType(UAttributes attributes) { return ValidationResult.success(); @@ -633,7 +610,7 @@ public void testUnregisterRpcListenerNotRegistered() { public void testUnregisterRpcListenerWhenDisconnected() { testRegisterRpcListener(); mClient.getConnectionCallback().onDisconnected(); - final UMessage requestMessage = buildMessage(RESPONSE_URI, PAYLOAD, buildRequestAttributes(METHOD_URI)); + final UMessage requestMessage = buildMessage(PAYLOAD, buildRequestAttributes(RESPONSE_URI, METHOD_URI)); mClient.getListener().onReceive(requestMessage); verify(mRequestListener, timeout(DELAY_MS).times(0)).onReceive(eq(requestMessage), any()); } @@ -663,7 +640,7 @@ public void testUnregisterRpcListenerFromAllMethodsNotRegistered() { @Test public void testOnReceiveRequestMessage() { testRegisterRpcListener(); - final UMessage requestMessage = buildMessage(RESPONSE_URI, PAYLOAD, buildRequestAttributes(METHOD_URI)); + final UMessage requestMessage = buildMessage(PAYLOAD, buildRequestAttributes(RESPONSE_URI, METHOD_URI)); mClient.getListener().onReceive(requestMessage); verify(mRequestListener, timeout(DELAY_MS).times(1)).onReceive(eq(requestMessage), any()); } @@ -671,7 +648,7 @@ public void testOnReceiveRequestMessage() { @Test public void testOnReceiveRequestMessageNotRegistered() { testUnregisterRpcListener(); - final UMessage requestMessage = buildMessage(RESPONSE_URI, PAYLOAD, buildRequestAttributes(METHOD_URI)); + final UMessage requestMessage = buildMessage(PAYLOAD, buildRequestAttributes(RESPONSE_URI, METHOD_URI)); mClient.getListener().onReceive(requestMessage); verify(mRequestListener, timeout(DELAY_MS).times(0)).onReceive(eq(requestMessage), any()); } @@ -680,15 +657,15 @@ public void testOnReceiveRequestMessageNotRegistered() { @SuppressWarnings("unchecked") public void testSendResponseMessage() { testRegisterRpcListener(); - final UMessage requestMessage = buildMessage(RESPONSE_URI, PAYLOAD, buildRequestAttributes(METHOD_URI)); + final UMessage requestMessage = buildMessage(PAYLOAD, buildRequestAttributes(RESPONSE_URI, METHOD_URI)); mClient.getListener().onReceive(requestMessage); final ArgumentCaptor> captor = ArgumentCaptor.forClass(CompletableFuture.class); verify(mRequestListener, timeout(DELAY_MS).times(1)).onReceive(eq(requestMessage), captor.capture()); final CompletableFuture responseFuture = captor.getValue(); responseFuture.complete(PAYLOAD); - verify(mManager, times(1)).send(argThat(message -> { - assertEquals(METHOD_URI, message.getSource()); + verify(mManager, timeout(DELAY_MS).times(1)).send(argThat(message -> { assertEquals(PAYLOAD, message.getPayload()); + assertEquals(METHOD_URI, message.getAttributes().getSource()); assertEquals(RESPONSE_URI, message.getAttributes().getSink()); return true; })); @@ -698,15 +675,15 @@ public void testSendResponseMessage() { @SuppressWarnings("unchecked") public void testSendResponseMessageWithCommStatus() { testRegisterRpcListener(); - final UMessage requestMessage = buildMessage(RESPONSE_URI, PAYLOAD, buildRequestAttributes(METHOD_URI)); + final UMessage requestMessage = buildMessage(PAYLOAD, buildRequestAttributes(RESPONSE_URI, METHOD_URI)); mClient.getListener().onReceive(requestMessage); final ArgumentCaptor> captor = ArgumentCaptor.forClass(CompletableFuture.class); verify(mRequestListener, timeout(DELAY_MS).times(1)).onReceive(eq(requestMessage), captor.capture()); final CompletableFuture responseFuture = captor.getValue(); responseFuture.completeExceptionally(new UStatusException(UCode.ABORTED, "Aborted")); - verify(mManager, times(1)).send(argThat(message -> { - assertEquals(METHOD_URI, message.getSource()); + verify(mManager, timeout(DELAY_MS).times(1)).send(argThat(message -> { assertEquals(UPayload.getDefaultInstance(), message.getPayload()); + assertEquals(METHOD_URI, message.getAttributes().getSource()); assertEquals(RESPONSE_URI, message.getAttributes().getSink()); assertEquals(UCode.ABORTED_VALUE, message.getAttributes().getCommstatus()); return true; @@ -717,7 +694,7 @@ public void testSendResponseMessageWithCommStatus() { @SuppressWarnings("unchecked") public void testSendResponseMessageNoPayload() { testRegisterRpcListener(); - final UMessage requestMessage = buildMessage(RESPONSE_URI, PAYLOAD, buildRequestAttributes(METHOD_URI)); + final UMessage requestMessage = buildMessage(PAYLOAD, buildRequestAttributes(RESPONSE_URI, METHOD_URI)); mClient.getListener().onReceive(requestMessage); sleep(DELAY_MS); final ArgumentCaptor> captor = ArgumentCaptor.forClass(CompletableFuture.class); @@ -743,16 +720,16 @@ public void testInvokeMethod() throws Exception { verify(mRequestListener, timeout(DELAY_MS).times(1)) .onReceive(requestCaptor.capture(), responseFutureCaptor.capture()); final UMessage requestMessage = requestCaptor.getValue(); - assertEquals(RESPONSE_URI, requestMessage.getSource()); assertEquals(REQUEST_PAYLOAD, requestMessage.getPayload()); + assertEquals(RESPONSE_URI, requestMessage.getAttributes().getSource()); assertEquals(METHOD_URI, requestMessage.getAttributes().getSink()); assertEquals(OPTIONS.timeout(), requestMessage.getAttributes().getTtl()); assertEquals(UMessageType.UMESSAGE_TYPE_REQUEST, requestMessage.getAttributes().getType()); responseFutureCaptor.getValue().complete(RESPONSE_PAYLOAD); final UMessage responseMessage = responseFuture.get(DELAY_MS, TimeUnit.MILLISECONDS); - assertEquals(METHOD_URI, responseMessage.getSource()); assertEquals(RESPONSE_PAYLOAD, responseMessage.getPayload()); + assertEquals(METHOD_URI, responseMessage.getAttributes().getSource()); assertEquals(RESPONSE_URI, responseMessage.getAttributes().getSink()); assertEquals(UMessageType.UMESSAGE_TYPE_RESPONSE, responseMessage.getAttributes().getType()); assertEquals(requestMessage.getAttributes().getId(), responseMessage.getAttributes().getReqid()); @@ -782,7 +759,7 @@ public void testInvokeMethodOtherResponseReceive() { verify(mRequestListener, timeout(DELAY_MS).times(1)).onReceive(any(), any()); final UMessage responseMessage = - buildMessage(METHOD_URI, PAYLOAD, buildResponseAttributes(RESPONSE_URI, createId())); + buildMessage(PAYLOAD, buildResponseAttributes(METHOD_URI, RESPONSE_URI, createId())); mClient.getListener().onReceive(responseMessage); assertThrows(TimeoutException.class, () -> responseFuture.get(DELAY_MS, TimeUnit.MILLISECONDS)); @@ -800,7 +777,7 @@ public void testInvokeMethodWhenDisconnected() { verify(mRequestListener, timeout(DELAY_MS).times(1)).onReceive(any(), any()); final UMessage responseMessage = - buildMessage(METHOD_URI, PAYLOAD, buildResponseAttributes(RESPONSE_URI, createId())); + buildMessage(PAYLOAD, buildResponseAttributes(METHOD_URI, RESPONSE_URI, createId())); mClient.getListener().onReceive(responseMessage); testOnDisconnected(); @@ -842,8 +819,8 @@ public void testInvokeMethodCompletedWithCommStatusOk() throws Exception { responseFutureCaptor.getValue().completeExceptionally(new UStatusException(UCode.OK, "No error")); final UMessage responseMessage = responseFuture.get(DELAY_MS, TimeUnit.MILLISECONDS); - assertEquals(METHOD_URI, responseMessage.getSource()); assertEquals(UPayload.getDefaultInstance(), responseMessage.getPayload()); + assertEquals(METHOD_URI, responseMessage.getAttributes().getSource()); assertEquals(RESPONSE_URI, responseMessage.getAttributes().getSink()); assertEquals(UMessageType.UMESSAGE_TYPE_RESPONSE, responseMessage.getAttributes().getType()); } @@ -851,9 +828,9 @@ public void testInvokeMethodCompletedWithCommStatusOk() throws Exception { @Test public void testInvokeMethodSameRequest() { doReturn(buildStatus(UCode.OK)).when(mManager).send(any()); - final UAttributesBuilder builder = UAttributesBuilder.request(UPriority.UPRIORITY_CS4, METHOD_URI, TTL); + final UAttributesBuilder builder = UAttributesBuilder.request(RESPONSE_URI, METHOD_URI, UPriority.UPRIORITY_CS4, TTL); try (MockedStatic mockedBuilder = mockStatic(UAttributesBuilder.class)) { - mockedBuilder.when(() -> UAttributesBuilder.request(UPriority.UPRIORITY_CS4, METHOD_URI, TTL)) + mockedBuilder.when(() -> UAttributesBuilder.request(RESPONSE_URI, METHOD_URI, UPriority.UPRIORITY_CS4, TTL)) .thenReturn(builder); mClient.invokeMethod(METHOD_URI, PAYLOAD, OPTIONS); assertStatus(UCode.ABORTED, toStatus(assertThrows(ExecutionException.class, diff --git a/library/src/test/java/org/eclipse/uprotocol/common/util/log/FormatterTest.java b/library/src/test/java/org/eclipse/uprotocol/common/util/log/FormatterTest.java index 03e36a9..7030f4e 100644 --- a/library/src/test/java/org/eclipse/uprotocol/common/util/log/FormatterTest.java +++ b/library/src/test/java/org/eclipse/uprotocol/common/util/log/FormatterTest.java @@ -225,7 +225,7 @@ public void testStringifyUStatusNull() { @Test public void testStringifyUMessage() { - final UMessage message = buildMessage(METHOD_URI, PAYLOAD, ATTRIBUTES); + final UMessage message = buildMessage(PAYLOAD, ATTRIBUTES); assertEquals("[id: " + ID_STRING + ", " + "source: /test.service/1/rpc.method, sink: /test.client/1/rpc.response, " + "type: UMESSAGE_TYPE_RESPONSE]", Formatter.stringify(message)); @@ -233,7 +233,7 @@ public void testStringifyUMessage() { @Test public void testStringifyUMessageWithoutSink() { - final UMessage message = buildMessage(METHOD_URI, PAYLOAD, UAttributes.newBuilder(ATTRIBUTES).clearSink().build()); + final UMessage message = buildMessage(PAYLOAD, UAttributes.newBuilder(ATTRIBUTES).clearSink().build()); assertEquals("[id: " + ID_STRING + ", " + "source: /test.service/1/rpc.method, type: UMESSAGE_TYPE_RESPONSE]", Formatter.stringify(message)); } diff --git a/library/src/test/java/org/eclipse/uprotocol/core/ubus/UBusManagerTest.java b/library/src/test/java/org/eclipse/uprotocol/core/ubus/UBusManagerTest.java index b91579e..e666925 100644 --- a/library/src/test/java/org/eclipse/uprotocol/core/ubus/UBusManagerTest.java +++ b/library/src/test/java/org/eclipse/uprotocol/core/ubus/UBusManagerTest.java @@ -77,7 +77,7 @@ public class UBusManagerTest extends TestBase { private static final String SERVICE_PACKAGE = "org.eclipse.uprotocol.core.ubus"; private static final ComponentName SERVICE = new ComponentName(SERVICE_PACKAGE, SERVICE_PACKAGE + ".UBusService"); - private static final UMessage MESSAGE = buildMessage(RESOURCE_URI, PAYLOAD, buildPublishAttributes()); + private static final UMessage MESSAGE = buildMessage(PAYLOAD, buildPublishAttributes(RESOURCE_URI)); private static final long REBIND_DELAY_MS = 1000 + DELAY_MS; private Context mContext; diff --git a/library/src/test/java/org/eclipse/uprotocol/core/udiscovery/v3/UDiscoveryTest.java b/library/src/test/java/org/eclipse/uprotocol/core/udiscovery/v3/UDiscoveryTest.java index 0b447ff..c2618d7 100644 --- a/library/src/test/java/org/eclipse/uprotocol/core/udiscovery/v3/UDiscoveryTest.java +++ b/library/src/test/java/org/eclipse/uprotocol/core/udiscovery/v3/UDiscoveryTest.java @@ -60,7 +60,7 @@ public class UDiscoveryTest extends TestBase { private UDiscovery.Stub mStub; @Before - public void setUp() throws RemoteException { + public void setUp() { mClient = mock(RpcClient.class); mStub = UDiscovery.newStub(mClient); } @@ -68,8 +68,8 @@ public void setUp() throws RemoteException { private void simulateResponse(@NonNull Message response) { doAnswer(invocation -> { final UUri methodUri = invocation.getArgument(0); - final UAttributes responseAttributes = buildResponseAttributes(RESPONSE_URI, ID); - return CompletableFuture.completedFuture(buildMessage(methodUri, packToAny(response), responseAttributes)); + final UAttributes responseAttributes = buildResponseAttributes(methodUri, RESPONSE_URI, ID); + return CompletableFuture.completedFuture(buildMessage(packToAny(response), responseAttributes)); }).when(mClient).invokeMethod(any(), any(), any()); } diff --git a/library/src/test/java/org/eclipse/uprotocol/core/usubscription/v3/USubscriptionTest.java b/library/src/test/java/org/eclipse/uprotocol/core/usubscription/v3/USubscriptionTest.java index 500109b..5a4add9 100644 --- a/library/src/test/java/org/eclipse/uprotocol/core/usubscription/v3/USubscriptionTest.java +++ b/library/src/test/java/org/eclipse/uprotocol/core/usubscription/v3/USubscriptionTest.java @@ -60,7 +60,7 @@ public class USubscriptionTest extends TestBase { private USubscription.Stub mStub; @Before - public void setUp() throws RemoteException { + public void setUp() { mClient = mock(RpcClient.class); mStub = USubscription.newStub(mClient); } @@ -68,8 +68,8 @@ public void setUp() throws RemoteException { private void simulateResponse(@NonNull Message response) { doAnswer(invocation -> { final UUri methodUri = invocation.getArgument(0); - final UAttributes responseAttributes = buildResponseAttributes(RESPONSE_URI, ID); - return CompletableFuture.completedFuture(buildMessage(methodUri, packToAny(response), responseAttributes)); + final UAttributes responseAttributes = buildResponseAttributes(methodUri, RESPONSE_URI, ID); + return CompletableFuture.completedFuture(buildMessage(packToAny(response), responseAttributes)); }).when(mClient).invokeMethod(any(), any(), any()); } diff --git a/library/src/test/java/org/eclipse/uprotocol/core/utwin/v1/UTwinTest.java b/library/src/test/java/org/eclipse/uprotocol/core/utwin/v1/UTwinTest.java index 90d6069..eed4327 100644 --- a/library/src/test/java/org/eclipse/uprotocol/core/utwin/v1/UTwinTest.java +++ b/library/src/test/java/org/eclipse/uprotocol/core/utwin/v1/UTwinTest.java @@ -62,7 +62,7 @@ public class UTwinTest extends TestBase { private UTwin.Stub mStub; @Before - public void setUp() throws RemoteException { + public void setUp() { mClient = mock(RpcClient.class); mStub = UTwin.newStub(mClient); } @@ -70,8 +70,8 @@ public void setUp() throws RemoteException { private void simulateResponse(@NonNull Message response) { doAnswer(invocation -> { final UUri methodUri = invocation.getArgument(0); - final UAttributes responseAttributes = buildResponseAttributes(RESPONSE_URI, ID); - return CompletableFuture.completedFuture(buildMessage(methodUri, packToAny(response), responseAttributes)); + final UAttributes responseAttributes = buildResponseAttributes(methodUri, RESPONSE_URI, ID); + return CompletableFuture.completedFuture(buildMessage(packToAny(response), responseAttributes)); }).when(mClient).invokeMethod(any(), any(), any()); } diff --git a/library/src/test/java/org/eclipse/uprotocol/v1/internal/ParcelableUMessageTest.java b/library/src/test/java/org/eclipse/uprotocol/v1/internal/ParcelableUMessageTest.java index 788e3d9..9421776 100644 --- a/library/src/test/java/org/eclipse/uprotocol/v1/internal/ParcelableUMessageTest.java +++ b/library/src/test/java/org/eclipse/uprotocol/v1/internal/ParcelableUMessageTest.java @@ -39,7 +39,7 @@ @RunWith(AndroidJUnit4.class) public class ParcelableUMessageTest extends TestBase { - private static final UMessage MESSAGE = buildMessage(RESPONSE_URI, PAYLOAD, ATTRIBUTES); + private static final UMessage MESSAGE = buildMessage(PAYLOAD, ATTRIBUTES); private Parcel mParcel; @@ -76,11 +76,6 @@ public void testCreateFromParcel() { checkWriteAndRead(MESSAGE); } - @Test - public void testCreateFromParcelWithoutSource() { - checkWriteAndRead(UMessage.newBuilder(MESSAGE).clearSource().build()); - } - @Test public void testCreateFromParcelWithoutPayload() { checkWriteAndRead(UMessage.newBuilder(MESSAGE).clearPayload().build()); diff --git a/library/src/test/java/org/eclipse/uprotocol/v1/internal/PerformanceTest.java b/library/src/test/java/org/eclipse/uprotocol/v1/internal/PerformanceTest.java index 08378c5..ae0e34b 100644 --- a/library/src/test/java/org/eclipse/uprotocol/v1/internal/PerformanceTest.java +++ b/library/src/test/java/org/eclipse/uprotocol/v1/internal/PerformanceTest.java @@ -49,7 +49,7 @@ public class PerformanceTest extends TestBase { private static final UEntity ENTITY = CLIENT; private static final UUri URI = RESOURCE_URI; private static final UStatus STATUS = buildStatus(UCode.UNKNOWN, "Unknown error"); - private static final UMessage MESSAGE = buildMessage(METHOD_URI, packToAny(STATUS), ATTRIBUTES); + private static final UMessage MESSAGE = buildMessage(packToAny(STATUS), ATTRIBUTES); private static final String PROTOBUF = "Protobuf"; private static final List COUNTS = List.of(1000, 100, 10, 5, 1);