Skip to content

Commit

Permalink
Add backward compatibility for CloudEvents with old priority("CS1","…
Browse files Browse the repository at this point in the history
…CS2"....) (#68)
  • Loading branch information
neelam-kushwah authored Jan 8, 2024
1 parent ee75676 commit d0f026e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/eclipse/uprotocol/cloudevent/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ assertEquals(UCloudEvent.getTtl(cloudEvent).get(), result.getAttributes().getTtl
assertEquals(UCloudEvent.getPayload(cloudEvent).toByteString(),result.getPayload().getValue());
assertEquals(UCloudEvent.getSource(cloudEvent),LongUriSerializer.instance().serialize(result.getSource()));
assertTrue(UCloudEvent.getPriority(cloudEvent).isPresent());
assertEquals(UCloudEvent.getPriority(cloudEvent).get(), String.valueOf(result.getAttributes().getPriority()));
assertEquals(UCloudEvent.getPriority(cloudEvent).get(), result.getAttributes().getPriority().name());
final CloudEvent cloudEvent1 = UCloudEvent.fromMessage(result);
assertEquals(cloudEvent,cloudEvent1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static CloudEventBuilder buildBaseCloudEvent(String id, String source,

attributes.ttl().ifPresent(ttl -> cloudEventBuilder.withExtension("ttl", ttl));
attributes.priority().ifPresent(priority -> cloudEventBuilder.withExtension("priority",
String.valueOf(priority)));
priority.name()));
attributes.hash().ifPresent(hash -> cloudEventBuilder.withExtension("hash", hash));
attributes.token().ifPresent(token -> cloudEventBuilder.withExtension("token", token));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ static UMessage toMessage(CloudEvent event) {
if (hasCommunicationStatusProblem(event)) {
builder.setCommstatus(getCommunicationStatus(event));
}
getPriority(event).map(UPriority::valueOf).ifPresent(builder::setPriority);
getPriority(event).map(p -> p.startsWith("UPRIORITY_") ? p : "UPRIORITY_" + p).map(UPriority::valueOf).ifPresent(builder::setPriority);

getSink(event).map(LongUriSerializer.instance()::deserialize).ifPresent(builder::setSink);

Expand Down Expand Up @@ -422,7 +422,7 @@ static CloudEvent fromMessage(UMessage message) {
cloudEventBuilder.withExtension("token",attributes.getToken());

if(attributes.getPriorityValue()>0)
cloudEventBuilder.withExtension("priority",String.valueOf(attributes.getPriority()));
cloudEventBuilder.withExtension("priority",attributes.getPriority().name());

if(attributes.hasSink())
cloudEventBuilder.withExtension("sink",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ public void test_to_from_message_from_request_cloudevent() {
assertEquals(UCloudEvent.getPayload(cloudEvent).toByteString(),result.getPayload().getValue());
assertEquals(UCloudEvent.getSource(cloudEvent),LongUriSerializer.instance().serialize(result.getSource()));
assertTrue(UCloudEvent.getPriority(cloudEvent).isPresent());
assertEquals(UCloudEvent.getPriority(cloudEvent).get(), String.valueOf(result.getAttributes().getPriority()));
assertEquals(UCloudEvent.getPriority(cloudEvent).get(),result.getAttributes().getPriority().name());

final CloudEvent cloudEvent1 = UCloudEvent.fromMessage(result);
assertEquals(cloudEvent,cloudEvent1);
Expand Down Expand Up @@ -829,7 +829,7 @@ public void test_to_from_message_from_response_cloudevent() {
assertEquals(UCloudEvent.getPayload(cloudEvent).toByteString(),result.getPayload().getValue());
assertEquals(UCloudEvent.getSource(cloudEvent),LongUriSerializer.instance().serialize(result.getSource()));
assertTrue(UCloudEvent.getPriority(cloudEvent).isPresent());
assertEquals(UCloudEvent.getPriority(cloudEvent).get(), String.valueOf(result.getAttributes().getPriority()));
assertEquals(UCloudEvent.getPriority(cloudEvent).get(), result.getAttributes().getPriority().name());

final CloudEvent cloudEvent1 = UCloudEvent.fromMessage(result);
assertEquals(cloudEvent,cloudEvent1);
Expand Down Expand Up @@ -939,6 +939,30 @@ public void test_to_from_message_from_cloudevent_with_all_payload_formats() {

}

@Test
public void test_to_from_message_from_UCP_cloudevent(){
// additional attributes
final UCloudEventAttributes uCloudEventAttributes = new UCloudEventAttributes.UCloudEventAttributesBuilder()
.withTtl(3)
.build();

Any protoPayload= buildProtoPayloadForTest();
final CloudEventBuilder cloudEventBuilder =
CloudEventFactory.buildBaseCloudEvent(LongUuidSerializer.instance().serialize(UuidFactory.Factories.UPROTOCOL.factory().create()), buildSourceForTest(),
protoPayload.toByteArray(), protoPayload.getTypeUrl(), uCloudEventAttributes);
cloudEventBuilder.withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH));
cloudEventBuilder.withExtension("priority","CS4");

CloudEvent cloudEvent = cloudEventBuilder.build();

UMessage result = UCloudEvent.toMessage(cloudEvent);
assertNotNull(result);
assertEquals(UPriority.UPRIORITY_CS4.name(),result.getAttributes().getPriority().name());
CloudEvent cloudEvent1 = UCloudEvent.fromMessage(result);
assertTrue(UCloudEvent.getPriority(cloudEvent1).isPresent());
assertEquals(UPriority.UPRIORITY_CS4.name(),UCloudEvent.getPriority(cloudEvent1).get());

}
private String buildSourceForTest(){
UUri Uri = UUri.newBuilder().setEntity(UEntity.newBuilder().setName("body.access"))
.setResource(UResource.newBuilder().setName("door").setInstance("front_left").setMessage("Door"))
Expand Down

0 comments on commit d0f026e

Please sign in to comment.