Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utransport updates #6

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
18 changes: 7 additions & 11 deletions library/src/androidTest/java/org/eclipse/uprotocol/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -330,7 +324,7 @@ public void testOnReceiveGenericMessageNotRegistered() {
@Test
public void testOnReceiveNotificationMessage() {
testRegisterListener();
testSendNotificationMassage();
testSendNotificationMessage();
verify(sListener, timeout(DELAY_MS).times(1)).onReceive(NOTIFICATION_MESSAGE);
}

Expand Down Expand Up @@ -422,15 +416,15 @@ 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());
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(METHOD_URI, responseMessage.getAttributes().getSource());
assertEquals(RESPONSE_PAYLOAD, responseMessage.getPayload());
assertEquals(RESPONSE_URI, responseMessage.getAttributes().getSink());
assertEquals(UMessageType.UMESSAGE_TYPE_RESPONSE, responseMessage.getAttributes().getType());
Expand Down
67 changes: 23 additions & 44 deletions library/src/main/java/org/eclipse/uprotocol/UPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<UListener> listeners;
synchronized (mRegistrationLock) {
listeners = new ArraySet<>(mListeners.get(topic));
Expand Down Expand Up @@ -796,14 +763,26 @@ private void handleRequestMessage(@NonNull UMessage requestMessage) {
final CompletableFuture<UPayload> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
39 changes: 20 additions & 19 deletions library/src/test/java/org/eclipse/uprotocol/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
Loading
Loading