-
Notifications
You must be signed in to change notification settings - Fork 14
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
Move to core-api UPayload data model #41
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,14 +25,20 @@ | |
package org.eclipse.uprotocol.rpc; | ||
|
||
import com.google.protobuf.Any; | ||
import com.google.protobuf.ByteString; | ||
import com.google.protobuf.Int32Value; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.google.rpc.Code; | ||
import com.google.rpc.Status; | ||
import org.eclipse.uprotocol.transport.builder.UAttributesBuilder; | ||
import org.eclipse.uprotocol.transport.datamodel.UPayload; | ||
import org.eclipse.uprotocol.uri.serializer.LongUriSerializer; | ||
import org.eclipse.uprotocol.v1.*; | ||
import org.eclipse.uprotocol.v1.UUri; | ||
import org.eclipse.uprotocol.v1.UPayload; | ||
import org.eclipse.uprotocol.v1.UPayloadFormat; | ||
import org.eclipse.uprotocol.v1.UAttributes; | ||
import org.eclipse.uprotocol.v1.UEntity; | ||
import org.eclipse.uprotocol.v1.UPriority; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
@@ -46,7 +52,10 @@ class RpcTest { | |
RpcClient ReturnsNumber3 = new RpcClient() { | ||
@Override | ||
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) { | ||
UPayload data = new UPayload(Any.pack(Int32Value.of(3)).toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF); | ||
UPayload data = UPayload.newBuilder() | ||
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF) | ||
.setValue(Any.pack(Int32Value.of(3)).toByteString()) | ||
.build(); | ||
return CompletableFuture.completedFuture(data); | ||
} | ||
}; | ||
|
@@ -64,8 +73,10 @@ public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UA | |
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) { | ||
Status status = Status.newBuilder().setCode(Code.INVALID_ARGUMENT_VALUE).setMessage("boom").build(); | ||
Any any = Any.pack(status); | ||
UPayload data = new UPayload(any.toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF); | ||
|
||
UPayload data = UPayload.newBuilder() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. personally I prefer working with the constructors and not verbose builders if they are not really needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UPayload is to wrap the data + metadata and keep it separate from the message attributes (priority, addressing, timeout, etc...). Yes you are right for the data but the metadata will most likely be mapped somehow into the local transport (HTTP header, zenoh header, etc...). |
||
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF) | ||
.setValue(any.toByteString()) | ||
.build(); | ||
return CompletableFuture.completedFuture(data); | ||
} | ||
}; | ||
|
@@ -75,16 +86,21 @@ public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UA | |
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) { | ||
Status status = Status.newBuilder().setCode(Code.OK_VALUE).setMessage("all good").build(); | ||
Any any = Any.pack(status); | ||
UPayload data = new UPayload(any.toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF); | ||
|
||
UPayload data = UPayload.newBuilder() | ||
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF) | ||
.setValue(any.toByteString()) | ||
.build(); | ||
return CompletableFuture.completedFuture(data); | ||
} | ||
}; | ||
|
||
RpcClient ThatBarfsCrapyPayload = new RpcClient() { | ||
@Override | ||
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) { | ||
UPayload response = new UPayload(new byte[]{0}, UPayloadFormat.UPAYLOAD_FORMAT_RAW); | ||
UPayload response = UPayload.newBuilder() | ||
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_RAW) | ||
.setValue(ByteString.copyFrom(new byte[]{0})) | ||
.build(); | ||
return CompletableFuture.completedFuture(response); | ||
} | ||
}; | ||
|
@@ -102,7 +118,11 @@ public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UA | |
@Override | ||
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) { | ||
Any any = Any.pack(Int32Value.of(42)); | ||
return CompletableFuture.completedFuture(new UPayload(any.toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF)); | ||
UPayload data = UPayload.newBuilder() | ||
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF) | ||
.setValue(any.toByteString()) | ||
.build(); | ||
return CompletableFuture.completedFuture(data); | ||
} | ||
}; | ||
|
||
|
@@ -121,7 +141,10 @@ private static io.cloudevents.v1.proto.CloudEvent buildCloudEvent() { | |
|
||
private static UPayload buildUPayload() { | ||
Any any = Any.pack(buildCloudEvent()); | ||
return new UPayload(any.toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF); | ||
return UPayload.newBuilder() | ||
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF) | ||
.setValue(any.toByteString()) | ||
.build(); | ||
} | ||
|
||
private static UUri buildTopic() { | ||
|
@@ -142,7 +165,7 @@ private static CompletableFuture<io.cloudevents.v1.proto.CloudEvent> rpcResponse | |
(payload, exception) -> { | ||
Any any; | ||
try { | ||
any = Any.parseFrom(payload.data()); | ||
any = Any.parseFrom(payload.getValue()); | ||
} catch (InvalidProtocolBufferException e) { | ||
throw new RuntimeException(e.getMessage(), e); | ||
} | ||
|
@@ -379,7 +402,7 @@ void test_success_invoke_method_happy_flow() { | |
assertFalse(true); | ||
|
||
try { | ||
any = Any.parseFrom(payload.data()); | ||
any = Any.parseFrom(payload.getValue()); | ||
// happy flow, no exception | ||
assertNull(exception); | ||
|
||
|
@@ -409,7 +432,7 @@ void test_fail_invoke_method_when_invoke_method_returns_a_status() { | |
final CompletableFuture<io.cloudevents.v1.proto.CloudEvent> stubReturnValue = rpcResponse.handle( | ||
(payload, exception) -> { | ||
try { | ||
Any any = Any.parseFrom(payload.data()); | ||
Any any = Any.parseFrom(payload.getValue()); | ||
// happy flow, no exception | ||
assertNull(exception); | ||
|
||
|
@@ -472,7 +495,7 @@ void test_fail_invoke_method_when_invoke_method_returns_a_bad_proto() { | |
final CompletableFuture<io.cloudevents.v1.proto.CloudEvent> stubReturnValue = rpcResponse.handle( | ||
(payload, exception) -> { | ||
try { | ||
Any any = Any.parseFrom(payload.data()); | ||
Any any = Any.parseFrom(payload.getValue()); | ||
// happy flow, no exception | ||
assertNull(exception); | ||
|
||
|
@@ -609,7 +632,7 @@ void what_the_stub_looks_like() throws InterruptedException { | |
RpcClient client = new RpcClient() { | ||
@Override | ||
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) { | ||
return CompletableFuture.completedFuture(UPayload.empty()); | ||
return CompletableFuture.completedFuture(UPayload.getDefaultInstance()); | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we have the version in the package name. makes upgrading even messier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to ensure that if/when we go to a non-backwards compatible version of uProtocol (ex. v1) that we have differentiate the data model between them like we do for uSubscription, uDiscovery, etc...