Skip to content

Commit

Permalink
Address additional review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
neelam-kushwah committed Nov 16, 2023
1 parent ee99ddb commit 0722c2e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 79 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/org/eclipse/uprotocol/rpc/RpcMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ interface RpcMapper {
}
val any: Any
try {
any = Any.parseFrom(payload.data())
any = Any.parseFrom(payload.data)

// Expected type
if (any.`is`(expectedClazz)) {
Expand Down Expand Up @@ -102,7 +102,7 @@ interface RpcMapper {
}
val any: Any
try {
any = Any.parseFrom(payload.data())
any = Any.parseFrom(payload.data)

// Expected type
if (any.`is`(expectedClazz)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ class UAttributesBuilder
* @return Returns the UAttributesBuilder with the configured priority.
*/
fun publish(priority: UPriority): UAttributesBuilder {
Objects.requireNonNull(priority, "UPriority cannot be null.")
return UAttributesBuilder(UuidFactory.Factories.UPROTOCOL.factory().create(),
UMessageType.UMESSAGE_TYPE_PUBLISH, priority)
}
Expand All @@ -160,8 +159,6 @@ class UAttributesBuilder
* @return Returns the UAttributesBuilder with the configured priority and sink.
*/
fun notification(priority: UPriority, sink: UUri): UAttributesBuilder {
Objects.requireNonNull(priority, "UPriority cannot be null.")
Objects.requireNonNull(sink, "sink cannot be null.")
return UAttributesBuilder(UuidFactory.Factories.UPROTOCOL.factory().create(),
UMessageType.UMESSAGE_TYPE_PUBLISH, priority).withSink(sink)
}
Expand All @@ -174,9 +171,6 @@ class UAttributesBuilder
* @return Returns the UAttributesBuilder with the configured priority, sink and ttl.
*/
fun request(priority: UPriority, sink: UUri, ttl: Int): UAttributesBuilder {
Objects.requireNonNull(priority, "UPriority cannot be null.")
Objects.requireNonNull(ttl, "ttl cannot be null.")
Objects.requireNonNull(sink, "sink cannot be null.")
return UAttributesBuilder(UuidFactory.Factories.UPROTOCOL.factory().create(),
UMessageType.UMESSAGE_TYPE_REQUEST, priority).withTtl(ttl).withSink(sink)
}
Expand All @@ -189,9 +183,6 @@ class UAttributesBuilder
* @return Returns the UAttributesBuilder with the configured priority, sink and reqid.
*/
fun response(priority: UPriority, sink: UUri, reqid: UUID): UAttributesBuilder {
Objects.requireNonNull(priority, "UPriority cannot be null.")
Objects.requireNonNull(sink, "sink cannot be null.")
Objects.requireNonNull(reqid, "reqid cannot be null.")
return UAttributesBuilder(UuidFactory.Factories.UPROTOCOL.factory().create(),
UMessageType.UMESSAGE_TYPE_RESPONSE, priority).withSink(sink).withReqId(reqid)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,14 @@ import java.util.*
/**
* The UPayload contains the clean Payload information along with its raw serialized structure of a byte[].
*/
class UPayload(data: ByteArray?, hint: UPayloadFormat?) {
private val data: ByteArray
private val hint // Hint regarding the bytes contained within the UPayload
: UPayloadFormat


init {
this.data = Objects.requireNonNullElse(data, ByteArray(0))
this.hint = Objects.requireNonNullElse(hint, UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED)
}

/**
* The actual serialized or raw data, which can be deserialized or simply used as is.
* @return Returns the actual serialized or raw data, which can be deserialized or simply used as is.
*/
fun data(): ByteArray {
return data
}

/**
* The hint regarding the bytes contained within the UPayload.
* @return Returns the hint regarding the bytes contained within the UPayload.
*/
fun hint(): UPayloadFormat {
return hint
}
data class UPayload(val data: ByteArray = ByteArray(0), val hint: UPayloadFormat = UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED) {

val isEmpty: Boolean
/**
* @return Returns true if the data in the UPayload is empty.
*/
get() = data.isEmpty()

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || javaClass != other.javaClass) return false
val uPayload = other as UPayload
return data.contentEquals(uPayload.data) && hint == uPayload.hint
}

override fun hashCode(): Int {
return Objects.hash(data.contentHashCode(), hint)
}

override fun toString(): String {
return "UPayload{" +
"data=" + data().contentToString() +
", hint=" + hint + '}'
}

companion object {
private val EMPTY = UPayload(ByteArray(0), UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED)
Expand All @@ -90,4 +49,26 @@ class UPayload(data: ByteArray?, hint: UPayloadFormat?) {
return EMPTY
}
}

override fun toString(): String {
return "UPayload{" + "data=" + data.contentToString() + ", hint=" + hint + '}'
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as UPayload

if (!data.contentEquals(other.data)) return false
if (hint != other.hint) return false

return true
}

override fun hashCode(): Int {
var result = data.contentHashCode()
result = 31 * result + hint.hashCode()
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,22 @@ abstract class UStatus {
* @param value The integer value of the Code.
* @return Returns the Code if found, otherwise returns Optional.empty().
*/
fun from(value: Int): Optional<Code> {
return Arrays.stream(entries.toTypedArray()).filter { p: Code -> p.value() == value }.findAny()
}
fun from(value: Int): Optional<Code> =
Optional.ofNullable(entries.find { it.value() == value })

/**
* Get the Code from a google.rpc.Code.
* @param code The google.rpc.Code.
* @return Returns the Code if found, otherwise returns Optional.empty().
*/
fun from(code: com.google.rpc.Code?): Optional<Code> {
return if (code == null || code == com.google.rpc.Code.UNRECOGNIZED) {
Optional.empty()
} else Arrays.stream(entries.toTypedArray()).filter { p: Code -> p.value() == code.number }.findAny()
return Optional.ofNullable(
code.takeIf { it != null && it != com.google.rpc.Code.UNRECOGNIZED }
?.let { entries.find { p -> p.value() == it.number } }
)
}


}
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/org/eclipse/uprotocol/rpc/RpcTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ internal class RpcTest {
assertTrue(true)
assertFalse(true)
try {
any = Any.parseFrom(payload.data())
any = Any.parseFrom(payload.data)
// happy flow, no exception
assertNull(exception)

Expand Down Expand Up @@ -347,7 +347,7 @@ internal class RpcTest {
val stubReturnValue: CompletableFuture<CloudEvent> =
rpcResponse.handle { payload, exception ->
try {
val any: Any = Any.parseFrom(payload.data())
val any: Any = Any.parseFrom(payload.data)
// happy flow, no exception
assertNull(exception)

Expand Down Expand Up @@ -408,7 +408,7 @@ internal class RpcTest {
val stubReturnValue: CompletableFuture<CloudEvent> =
rpcResponse.handle { payload, exception ->
try {
val any: Any = Any.parseFrom(payload.data())
val any: Any = Any.parseFrom(payload.data)
// happy flow, no exception
assertNull(exception)

Expand Down Expand Up @@ -596,7 +596,7 @@ internal class RpcTest {
return invokeMethodResponse.handle { payload, exception ->
val any: Any
try {
any = Any.parseFrom(payload.data())
any = Any.parseFrom(payload.data)
} catch (e: InvalidProtocolBufferException) {
throw RuntimeException(e.message, e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,54 +39,54 @@ internal class UPayloadTest {
fun testToString_with_empty() {
val uPayload: UPayload = UPayload.empty()
assertEquals("UPayload{data=[], hint=UPAYLOAD_FORMAT_UNSPECIFIED}", uPayload.toString())
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED, uPayload.hint())
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED, uPayload.hint)
}

@Test
@DisplayName("Create an empty UPayload")
fun create_an_empty_upayload() {
val uPayload: UPayload = UPayload.empty()
assertEquals(0, uPayload.data().size)
assertEquals(0, uPayload.data.size)
assertTrue(uPayload.isEmpty)
}

@Test
@DisplayName("Create a UPayload with null")
fun create_upayload_with_null() {
val uPayload = UPayload(null, null)
assertEquals(0, uPayload.data().size)
@DisplayName("Create a UPayload with no data and hint")
fun create_upayload_with_no_data_hint() {
val uPayload = UPayload()
assertEquals(0, uPayload.data.size)
assertTrue(uPayload.isEmpty)
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED, uPayload.hint())
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED, uPayload.hint)
}

@Test
@DisplayName("Create a UPayload from string with hint")
fun create_upayload_from_string_with_hint() {
val stringData = "hello"
val uPayload = UPayload(stringData.toByteArray(StandardCharsets.UTF_8), UPayloadFormat.UPAYLOAD_FORMAT_TEXT)
assertEquals(stringData.length, uPayload.data().size)
assertEquals(stringData.length, uPayload.data.size)
assertFalse(uPayload.isEmpty)
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_TEXT, uPayload.hint())
assertEquals(stringData, String(uPayload.data()))
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_TEXT, uPayload.hint)
assertEquals(stringData, String(uPayload.data))
}

@Test
@DisplayName("Create a UPayload from some string without hint")
fun create_upayload_from_string_without_hint() {
val stringData = "hello"
val uPayload = UPayload(stringData.toByteArray(StandardCharsets.UTF_8), null)
assertEquals(stringData.length, uPayload.data().size)
val uPayload = UPayload(data=stringData.toByteArray(StandardCharsets.UTF_8))
assertEquals(stringData.length, uPayload.data.size)
assertFalse(uPayload.isEmpty)
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED, uPayload.hint())
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_UNSPECIFIED, uPayload.hint)
}

@Test
@DisplayName("Create a UPayload without a byte array but with some weird hint")
fun create_upayload_without_byte_array_but_with_weird_hint() {
val uPayload = UPayload(null, UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF)
assertEquals(0, uPayload.data().size)
val uPayload = UPayload(hint= UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF)
assertEquals(0, uPayload.data.size)
assertTrue(uPayload.isEmpty)
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF, uPayload.hint())
assertEquals(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF, uPayload.hint)
assertFalse(UPayload.empty() == uPayload)
}
}

0 comments on commit 0722c2e

Please sign in to comment.