Skip to content

Commit

Permalink
Add UPayload::unpack() of UMessage (#171)
Browse files Browse the repository at this point in the history
Adding a method to unpack the payload from a UMessage

#169
  • Loading branch information
Steven Hartley authored Jul 25, 2024
1 parent b40b273 commit 583e93a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/main/java/org/eclipse/uprotocol/communication/UPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;

import org.eclipse.uprotocol.v1.UMessage;
import org.eclipse.uprotocol.v1.UPayloadFormat;

/**
Expand All @@ -31,11 +32,13 @@ public record UPayload (ByteString data, UPayloadFormat format) {
// Empty UPayload
public static final UPayload EMPTY = new UPayload();


public UPayload {
Objects.requireNonNull(data);
Objects.requireNonNull(format);
}


public UPayload() {
this(ByteString.EMPTY, UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED);
}
Expand Down Expand Up @@ -64,6 +67,7 @@ public static UPayload packToAny(Message message) {
new UPayload(Any.pack(message).toByteString(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY);
}


/**
* Build a uPayload from {@link google.protobuf.Message} using protobuf PayloadFormat.
*
Expand All @@ -75,6 +79,7 @@ public static UPayload pack(Message message) {
new UPayload(message.toByteString(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF);
}


/**
* Build a UPayload from specific data and passed format
* @param data payload data.
Expand All @@ -85,6 +90,22 @@ public static UPayload pack(ByteString data, UPayloadFormat format) {
return new UPayload(data, format);
}


/**
* Unpack a uMessage into {@link google.protobuf.Message}.
*
* @param message the message to unpack
* @param clazz the class of the message to unpack
* @return the unpacked message
*/
public static <T extends Message> Optional<T> unpack(UMessage message, Class<T> clazz) {
if (message == null) {
return Optional.empty();
}
return unpack(message.getPayload(), message.getAttributes().getPayloadFormat(), clazz);
}


/**
* Unpack a uPayload into {@link google.protobuf.Message}.<br>
* <br>
Expand All @@ -102,6 +123,7 @@ public static <T extends Message> Optional<T> unpack(UPayload payload, Class<T>
return unpack(payload.data(), payload.format(), clazz);
}


/**
* Unpack a uPayload into a {@link google.protobuf.Message}.
* <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,35 @@ public void test_building_a_UPayload_calling_pack_passing_null() {
assertTrue(UPayload.isEmpty(UPayload.packToAny(null)));
}


@Test
@DisplayName("Test iEmpty() when we build a valid UPayload that data is empty but format is not")
public void test_building_a_UPayload_calling_pack() {
UPayload payload = UPayload.pack(UUri.newBuilder().build());
assertFalse(UPayload.isEmpty(payload));
}


@Test
@DisplayName("Test iEmpty() when we build a valid UPayload where both data and format are not empty")
public void test_building_a_UPayload_calling_packToAny() {
UPayload payload = UPayload.packToAny(UUri.newBuilder().setAuthorityName("Hartley").build());
assertFalse(UPayload.isEmpty(payload));
}


@Test
@DisplayName("Test iEmpty() when we pass null")
public void test_building_a_UPayload_calling_pack_with_null() {
assertTrue(UPayload.isEmpty(null));
}


@Test
@DisplayName("Test unpack() passing null")
public void test_unpacking_a_UPayload_calling_unpack_with_null() {
assertFalse(UPayload.unpack(null, UUri.class).isPresent());
assertFalse(UPayload.unpack((UMessage)null, UUri.class).isPresent());
assertFalse(UPayload.unpack((UPayload)null, UUri.class).isPresent());
assertFalse(UPayload.unpack(UPayload.pack(null), UUri.class).isPresent());
}

Expand All @@ -68,6 +73,7 @@ public void test_unpack_passing_a_null_bytestring() {
assertFalse(UPayload.unpack(null, UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF, UUri.class).isPresent());
}


@Test
@DisplayName("Test unpack() passing google.protobuf.Any packed UPayload")
public void test_unpack_passing_a_google_protobuf_any_packed_UPayload() {
Expand All @@ -78,6 +84,7 @@ public void test_unpack_passing_a_google_protobuf_any_packed_UPayload() {
assertEquals(uri, unpacked.get());
}


@Test
@DisplayName("Test unpack() passing an unsupported format in UPayload")
public void test_unpack_passing_an_unsupported_format_in_UPayload() {
Expand All @@ -88,6 +95,7 @@ public void test_unpack_passing_an_unsupported_format_in_UPayload() {
assertEquals(unpacked, Optional.empty());
}


@Test
@DisplayName("Test unpack() to unpack a message of the wrong type")
public void test_unpack_to_unpack_a_message_of_the_wrong_type() {
Expand All @@ -98,6 +106,7 @@ public void test_unpack_to_unpack_a_message_of_the_wrong_type() {
assertEquals(unpacked, Optional.empty());
}


@Test
@DisplayName("Test equals when they are equal")
public void test_equals_when_they_are_equal() {
Expand All @@ -107,6 +116,7 @@ public void test_equals_when_they_are_equal() {
assertEquals(payload1, payload2);
}


@Test
@DisplayName("Test equals when they are not equal")
public void test_equals_when_they_are_not_equal() {
Expand All @@ -117,6 +127,7 @@ public void test_equals_when_they_are_not_equal() {
assertFalse(payload1.equals(payload2));
}


@Test
@DisplayName("Test equals when object is null")
public void test_equals_when_object_is_null() {
Expand All @@ -125,6 +136,7 @@ public void test_equals_when_object_is_null() {
assertFalse(payload.equals(null));
}


@Test
@DisplayName("Test equals when object is not an instance of UPayload")
public void test_equals_when_object_is_not_an_instance_of_UPayload() {
Expand All @@ -133,6 +145,7 @@ public void test_equals_when_object_is_not_an_instance_of_UPayload() {
assertFalse(payload.equals(uri));
}


@Test
@DisplayName("Test equals when it is the same object")
public void test_equals_when_it_is_the_same_object() {
Expand All @@ -150,6 +163,7 @@ public void test_equals_when_the_data_is_the_same_but_the_format_is_not() {
assertFalse(payload1.equals(payload2));
}


@Test
@DisplayName("Test hashCode")
public void test_hashCode() {
Expand All @@ -158,11 +172,24 @@ public void test_hashCode() {
assertEquals(payload.hashCode(), payload.hashCode());
}


@Test
@DisplayName("Test hashCode when they are not the same objects")
public void test_hashCode_when_they_are_not_the_same_objects() {
UUri uri1 = UUri.newBuilder().setAuthorityName("Hartley").build();
UPayload payload1 = UPayload.packToAny(uri1);
assertFalse(payload1.hashCode() == uri1.hashCode());
}


@Test
@DisplayName("Test unpack passing a valid UMessage")
public void test_unpack_passing_a_valid_UMessage() {
UUri uri = UUri.newBuilder().setAuthorityName("Hartley").build();
UPayload payload = UPayload.packToAny(uri);
UMessage message = UMessage.newBuilder().setPayload(payload.data()).build();
Optional<UUri> unpacked = UPayload.unpack(message, UUri.class);
assertTrue(unpacked.isPresent());
assertEquals(uri, unpacked.get());
}
}

0 comments on commit 583e93a

Please sign in to comment.