Skip to content

Commit

Permalink
Fix response handling and update UT
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Petrov committed Feb 8, 2024
1 parent b35024b commit 0907bb5
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 122 deletions.
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-SNAPSHOT"

[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
35 changes: 22 additions & 13 deletions library/src/main/java/org/eclipse/uprotocol/UPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,7 @@ public void onConnectionInterrupted() {
}
};

private final UListener mListener = new UListener() {

@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 @@ -678,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(mResponseUri, UPriority.UPRIORITY_CS4, methodUri, timeout);
final UAttributesBuilder builder = UAttributesBuilder.request(mResponseUri, methodUri, UPriority.UPRIORITY_CS4, timeout);
options.token().ifPresent(builder::withToken);
final UMessage requestMessage = UMessage.newBuilder().setPayload(requestPayload).setAttributes(builder.build()).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 @@ -766,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.getSink(), requestAttributes.getPriority(), requestMessage.getAttributes().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(UMessage.newBuilder().setPayload(responsePayload).setAttributes(builder.build()).build());
send(responseMessage);
});
return responseFuture;
}
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

0 comments on commit 0907bb5

Please sign in to comment.