diff --git a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/factory/CloudEventFactory.kt b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/factory/CloudEventFactory.kt index 411031f..81ae850 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/factory/CloudEventFactory.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/factory/CloudEventFactory.kt @@ -29,7 +29,7 @@ import com.google.protobuf.Empty import io.cloudevents.CloudEvent import io.cloudevents.core.builder.CloudEventBuilder import org.eclipse.uprotocol.cloudevent.datamodel.UCloudEventAttributes -import org.eclipse.uprotocol.uuid.factory.UuidFactory +import org.eclipse.uprotocol.uuid.factory.UUIDV8 import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer import org.eclipse.uprotocol.v1.UMessageType import org.eclipse.uprotocol.v1.UUID @@ -42,185 +42,184 @@ import java.net.URI * to formulate all kinds of events (messages) that will be sent to and from devices.

* The CloudEvent factory knows how to generate CloudEvents of the 4 core types: req.v1, res.v1, pub.v1, and file.v1

*/ -interface CloudEventFactory { - companion object { - /** - * Create a CloudEvent for an event for the use case of: RPC Request message. - * - * @param applicationUriForRPC The uri for the application requesting the RPC. - * @param serviceMethodUri The uri for the method to be called on the service Ex.: :/body.access/1/rpc.UpdateDoor - * @param protoPayload Protobuf Any object with the Message command to be executed on the sink service. - * @param attributes Additional attributes such as ttl, hash, priority and token. - * @return Returns an request CloudEvent. - */ - fun request( - applicationUriForRPC: String, - serviceMethodUri: String, - protoPayload: Any, - attributes: UCloudEventAttributes - ): CloudEvent { - val id = generateCloudEventId() - return buildBaseCloudEvent( - id, - applicationUriForRPC, - protoPayload.toByteArray(), - protoPayload.typeUrl, - attributes - ) - .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_REQUEST)) - .withExtension("sink", URI.create(serviceMethodUri)) - .build() - } +object CloudEventFactory { + /** + * Create a CloudEvent for an event for the use case of: RPC Request message. + * + * @param applicationUriForRPC The uri for the application requesting the RPC. + * @param serviceMethodUri The uri for the method to be called on the service Ex.: :/body.access/1/rpc.UpdateDoor + * @param protoPayload Protobuf Any object with the Message command to be executed on the sink service. + * @param attributes Additional attributes such as ttl, hash, priority and token. + * @return Returns an request CloudEvent. + */ + fun request( + applicationUriForRPC: String, + serviceMethodUri: String, + protoPayload: Any, + attributes: UCloudEventAttributes + ): CloudEvent { + val id = generateCloudEventId() + return buildBaseCloudEvent( + id, + applicationUriForRPC, + protoPayload.toByteArray(), + protoPayload.typeUrl, + attributes + ) + .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_REQUEST)) + .withExtension("sink", URI.create(serviceMethodUri)) + .build() + } - /** - * Create a CloudEvent for an event for the use case of: RPC Response message. - * - * @param applicationUriForRPC The destination of the response. The uri for the original application that requested the RPC and this response is for. - * @param serviceMethodUri The uri for the method that was called on the service Ex.: :/body.access/1/rpc.UpdateDoor - * @param requestId The cloud event id from the original request cloud event that this response if for. - * @param protoPayload The protobuf serialized response message as defined by the application interface or the - * google.rpc.Status message containing the details of an error. - * @param attributes Additional attributes such as ttl, hash and priority. - * @return Returns an response CloudEvent. - */ - fun response( - applicationUriForRPC: String, - serviceMethodUri: String, - requestId: String, - protoPayload: Any, - attributes: UCloudEventAttributes - ): CloudEvent { - val id = generateCloudEventId() - return buildBaseCloudEvent( - id, - serviceMethodUri, - protoPayload.toByteArray(), - protoPayload.typeUrl, - attributes - ) - .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE)) - .withExtension("sink", URI.create(applicationUriForRPC)) - .withExtension("reqid", requestId) - .build() - } + /** + * Create a CloudEvent for an event for the use case of: RPC Response message. + * + * @param applicationUriForRPC The destination of the response. The uri for the original application that requested the RPC and this response is for. + * @param serviceMethodUri The uri for the method that was called on the service Ex.: :/body.access/1/rpc.UpdateDoor + * @param requestId The cloud event id from the original request cloud event that this response if for. + * @param protoPayload The protobuf serialized response message as defined by the application interface or the + * google.rpc.Status message containing the details of an error. + * @param attributes Additional attributes such as ttl, hash and priority. + * @return Returns an response CloudEvent. + */ + fun response( + applicationUriForRPC: String, + serviceMethodUri: String, + requestId: String, + protoPayload: Any, + attributes: UCloudEventAttributes + ): CloudEvent { + val id = generateCloudEventId() + return buildBaseCloudEvent( + id, + serviceMethodUri, + protoPayload.toByteArray(), + protoPayload.typeUrl, + attributes + ) + .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE)) + .withExtension("sink", URI.create(applicationUriForRPC)) + .withExtension("reqid", requestId) + .build() + } - /** - * Create a CloudEvent for an event for the use case of: RPC Response message that failed. - * - * @param applicationUriForRPC The destination of the response. The uri for the original application that requested the RPC and this response is for. - * @param serviceMethodUri The uri for the method that was called on the service Ex.: :/body.access/1/rpc.UpdateDoor - * @param requestId The cloud event id from the original request cloud event that this response if for. - * @param communicationStatus A UCode value that indicates of a platform communication error while delivering this CloudEvent. - * @param attributes Additional attributes such as ttl, hash and priority. - * @return Returns a response CloudEvent Response for the use case of RPC Response message that failed. - */ - fun failedResponse( - applicationUriForRPC: String, - serviceMethodUri: String, - requestId: String, - communicationStatus: Int, - attributes: UCloudEventAttributes - ): CloudEvent { - val id = generateCloudEventId() - val protoPayload: Any = Any.pack(Empty.getDefaultInstance()) - return buildBaseCloudEvent( - id, - serviceMethodUri, - protoPayload.toByteArray(), - protoPayload.typeUrl, - attributes - ) - .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE)) - .withExtension("sink", URI.create(applicationUriForRPC)) - .withExtension("reqid", requestId) - .withExtension("commstatus", communicationStatus) - .build() - } + /** + * Create a CloudEvent for an event for the use case of: RPC Response message that failed. + * + * @param applicationUriForRPC The destination of the response. The uri for the original application that requested the RPC and this response is for. + * @param serviceMethodUri The uri for the method that was called on the service Ex.: :/body.access/1/rpc.UpdateDoor + * @param requestId The cloud event id from the original request cloud event that this response if for. + * @param communicationStatus A UCode value that indicates of a platform communication error while delivering this CloudEvent. + * @param attributes Additional attributes such as ttl, hash and priority. + * @return Returns a response CloudEvent Response for the use case of RPC Response message that failed. + */ + fun failedResponse( + applicationUriForRPC: String, + serviceMethodUri: String, + requestId: String, + communicationStatus: Int, + attributes: UCloudEventAttributes + ): CloudEvent { + val id = generateCloudEventId() + val protoPayload: Any = Any.pack(Empty.getDefaultInstance()) + return buildBaseCloudEvent( + id, + serviceMethodUri, + protoPayload.toByteArray(), + protoPayload.typeUrl, + attributes + ) + .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE)) + .withExtension("sink", URI.create(applicationUriForRPC)) + .withExtension("reqid", requestId) + .withExtension("commstatus", communicationStatus) + .build() + } - /** - * Create a CloudEvent for an event for the use case of: Publish generic message. - * - * @param source The uri of the topic being published. - * @param protoPayload protobuf Any object with the Message to be published. - * @param attributes Additional attributes such as ttl, hash and priority. - * @return Returns a publish CloudEvent. - */ - fun publish(source: String, protoPayload: Any, attributes: UCloudEventAttributes): CloudEvent { - val id = generateCloudEventId() - return buildBaseCloudEvent(id, source, protoPayload.toByteArray(), protoPayload.typeUrl, attributes) - .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) - .build() - } + /** + * Create a CloudEvent for an event for the use case of: Publish generic message. + * + * @param source The uri of the topic being published. + * @param protoPayload protobuf Any object with the Message to be published. + * @param attributes Additional attributes such as ttl, hash and priority. + * @return Returns a publish CloudEvent. + */ + fun publish(source: String, protoPayload: Any, attributes: UCloudEventAttributes): CloudEvent { + val id = generateCloudEventId() + return buildBaseCloudEvent(id, source, protoPayload.toByteArray(), protoPayload.typeUrl, attributes) + .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) + .build() + } - /** - * Create a CloudEvent for an event for the use case of: Publish a notification message.

- * A published event containing the sink (destination) is often referred to as a notification, it is an event sent to a specific consumer. - * - * @param source The uri of the topic being published. - * @param sink The uri of the destination of this notification. - * @param protoPayload protobuf Any object with the Message to be published. - * @param attributes Additional attributes such as ttl, hash and priority. - * @return Returns a publish CloudEvent. - */ - fun notification( - source: String, - sink: String, - protoPayload: Any, - attributes: UCloudEventAttributes - ): CloudEvent { - val id = generateCloudEventId() - return buildBaseCloudEvent(id, source, protoPayload.toByteArray(), protoPayload.typeUrl, attributes) - .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) - .withExtension("sink", URI.create(sink)) - .build() - } + /** + * Create a CloudEvent for an event for the use case of: Publish a notification message.

+ * A published event containing the sink (destination) is often referred to as a notification, it is an event sent to a specific consumer. + * + * @param source The uri of the topic being published. + * @param sink The uri of the destination of this notification. + * @param protoPayload protobuf Any object with the Message to be published. + * @param attributes Additional attributes such as ttl, hash and priority. + * @return Returns a publish CloudEvent. + */ + fun notification( + source: String, + sink: String, + protoPayload: Any, + attributes: UCloudEventAttributes + ): CloudEvent { + val id = generateCloudEventId() + return buildBaseCloudEvent(id, source, protoPayload.toByteArray(), protoPayload.typeUrl, attributes) + .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) + .withExtension("sink", URI.create(sink)) + .build() + } - /** - * @return Returns a UUIDv8 id. - */ - private fun generateCloudEventId(): String { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - return LongUuidSerializer.instance().serialize(uuid) - } + /** + * @return Returns a UUIDv8 id. + */ + private fun generateCloudEventId(): String { + val uuid: UUID = UUIDV8() + return LongUuidSerializer.INSTANCE.serialize(uuid) + } - /** - * Base CloudEvent builder that is the same for all CloudEvent types. - * - * @param id Event unique identifier. - * @param source Identifies who is sending this event in the format of a uProtocol URI that - * can be built from a [UUri] object. - * @param protoPayloadBytes The serialized Event data with the content type of "application/x-protobuf". - * @param protoPayloadSchema The schema of the proto payload bytes, for example you can use `protoPayload.typeUrl` on your service/app object. - * @param attributes Additional cloud event attributes that can be passed in. All attributes are optional and will be added only if they - * were configured. - * @return Returns a CloudEventBuilder that can be additionally configured and then by calling .build() construct a CloudEvent - * ready to be serialized and sent to the transport layer. - */ - fun buildBaseCloudEvent( - id: String?, source: String, - protoPayloadBytes: ByteArray, - protoPayloadSchema: String?, - attributes: UCloudEventAttributes - ): CloudEventBuilder { - val cloudEventBuilder: CloudEventBuilder = CloudEventBuilder.v1() - .withId(id) - .withSource(URI.create(source)) /* Not needed: + /** + * Base CloudEvent builder that is the same for all CloudEvent types. + * + * @param id Event unique identifier. + * @param source Identifies who is sending this event in the format of a uProtocol URI that + * can be built from a [UUri] object. + * @param protoPayloadBytes The serialized Event data with the content type of "application/x-protobuf". + * @param protoPayloadSchema The schema of the proto payload bytes, for example you can use `protoPayload.typeUrl` on your service/app object. + * @param attributes Additional cloud event attributes that can be passed in. All attributes are optional and will be added only if they + * were configured. + * @return Returns a CloudEventBuilder that can be additionally configured and then by calling .build() construct a CloudEvent + * ready to be serialized and sent to the transport layer. + */ + fun buildBaseCloudEvent( + id: String?, source: String, + protoPayloadBytes: ByteArray, + protoPayloadSchema: String?, + attributes: UCloudEventAttributes + ): CloudEventBuilder { + val cloudEventBuilder: CloudEventBuilder = CloudEventBuilder.v1() + .withId(id) + .withSource(URI.create(source)) /* Not needed: .withDataContentType(PROTOBUF_CONTENT_TYPE) .withDataSchema(URI.create(protoPayloadSchema)) */ - .withData(protoPayloadBytes) - attributes.ttl().ifPresent { ttl -> cloudEventBuilder.withExtension("ttl", ttl) } - attributes.priority() - .ifPresent { priority -> cloudEventBuilder.withExtension("priority", priority.toString()) } - attributes.hash().ifPresent { hash -> cloudEventBuilder.withExtension("hash", hash) } - attributes.token().ifPresent { token -> cloudEventBuilder.withExtension("token", token) } - attributes.traceparent().ifPresent { traceparent: String -> - cloudEventBuilder.withExtension("traceparent", traceparent) - } - - return cloudEventBuilder + .withData(protoPayloadBytes) + attributes.ttl().ifPresent { ttl -> cloudEventBuilder.withExtension("ttl", ttl) } + attributes.priority() + .ifPresent { priority -> cloudEventBuilder.withExtension("priority", priority.toString()) } + attributes.hash().ifPresent { hash -> cloudEventBuilder.withExtension("hash", hash) } + attributes.token().ifPresent { token -> cloudEventBuilder.withExtension("token", token) } + attributes.traceparent().ifPresent { traceparent: String -> + cloudEventBuilder.withExtension("traceparent", traceparent) } - const val PROTOBUF_CONTENT_TYPE = "application/x-protobuf" + return cloudEventBuilder } + + const val PROTOBUF_CONTENT_TYPE = "application/x-protobuf" } + diff --git a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/factory/UCloudEvent.kt b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/factory/UCloudEvent.kt index 7d7c392..a031a45 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/factory/UCloudEvent.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/factory/UCloudEvent.kt @@ -33,7 +33,8 @@ import io.cloudevents.CloudEventData import io.cloudevents.core.builder.CloudEventBuilder import org.eclipse.uprotocol.UprotocolOptions import org.eclipse.uprotocol.uri.serializer.LongUriSerializer -import org.eclipse.uprotocol.uuid.factory.UuidUtils +import org.eclipse.uprotocol.uuid.factory.getTime +import org.eclipse.uprotocol.uuid.factory.isUuid import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer import org.eclipse.uprotocol.v1.* import org.eclipse.uprotocol.v1.UUID @@ -168,14 +169,14 @@ object UCloudEvent { } /** - * Extract the timestamp from the UUIDV8 CloudEvent Id, with Unix epoch as the + * Extract the timestamp from the UUIDV8 CloudEvent ID, with Unix epoch as the * @param cloudEvent The CloudEvent with the timestamp to extract. - * @return Return the timestamp from the UUIDV8 CloudEvent Id or an empty Optional if timestamp can't be extracted. + * @return Return the timestamp from the UUIDV8 CloudEvent ID or an empty Optional if timestamp can't be extracted. */ fun getCreationTimestamp(cloudEvent: CloudEvent): Optional { val cloudEventId = cloudEvent.id - val uuid = LongUuidSerializer.instance().deserialize(cloudEventId) - return UuidUtils.getTime(uuid) + val uuid = LongUuidSerializer.INSTANCE.deserialize(cloudEventId) + return Optional.ofNullable(uuid.getTime()) } /** @@ -215,11 +216,11 @@ object UCloudEvent { return false } val cloudEventId: String = cloudEvent.id - val uuid = LongUuidSerializer.instance().deserialize(cloudEventId) + val uuid = LongUuidSerializer.INSTANCE.deserialize(cloudEventId) if (uuid == UUID.getDefaultInstance()) { return false } - val delta: Long = System.currentTimeMillis() - UuidUtils.getTime(uuid).orElse(0L) + val delta: Long = System.currentTimeMillis() - (uuid.getTime()?:0L) return delta >= ttl } @@ -230,8 +231,8 @@ object UCloudEvent { */ fun isCloudEventId(cloudEvent: CloudEvent): Boolean { val cloudEventId: String = cloudEvent.id - val uuid = LongUuidSerializer.instance().deserialize(cloudEventId) - return UuidUtils.isUuid(uuid) + val uuid = LongUuidSerializer.INSTANCE.deserialize(cloudEventId) + return uuid.isUuid() } /** @@ -352,7 +353,7 @@ object UCloudEvent { } val msgAttributes = uAttributes { - id = LongUuidSerializer.instance().deserialize(event.id) + id = LongUuidSerializer.INSTANCE.deserialize(event.id) type = getMessageType(event.type) source = LongUriSerializer.instance().deserialize(event.source.toString()) if (hasCommunicationStatusProblem(event)) { @@ -367,7 +368,7 @@ object UCloudEvent { getSink(event).ifPresent { sink = LongUriSerializer.instance().deserialize(it) } - getRequestId(event).ifPresent { reqid = LongUuidSerializer.instance().deserialize(it) } + getRequestId(event).ifPresent { reqid = LongUuidSerializer.INSTANCE.deserialize(it) } getTtl(event).ifPresent { ttl = it } @@ -395,7 +396,7 @@ object UCloudEvent { val attributes: UAttributes = message.attributes ?: UAttributes.getDefaultInstance() val payload = message.payload ?: UPayload.getDefaultInstance() val builder: CloudEventBuilder = - CloudEventBuilder.v1().withId(LongUuidSerializer.instance().serialize(attributes.id)) + CloudEventBuilder.v1().withId(LongUuidSerializer.INSTANCE.serialize(attributes.id)) builder.withType(getEventType(attributes.type)) builder.withSource(URI.create(LongUriSerializer.instance().serialize(attributes.source))) val contentType = getContentTypeFromUPayloadFormat(payload.format) @@ -427,7 +428,7 @@ object UCloudEvent { } if (attributes.hasReqid()) { - builder.withExtension("reqid", LongUuidSerializer.instance().serialize(attributes.reqid)) + builder.withExtension("reqid", LongUuidSerializer.INSTANCE.serialize(attributes.reqid)) } if (attributes.hasPermissionLevel()) { diff --git a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/Base64ProtobufSerializer.kt b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/Base64ProtobufSerializer.kt index a934796..caa9b59 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/Base64ProtobufSerializer.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/Base64ProtobufSerializer.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 General Motors GTO LLC + * Copyright (c) 2024 General Motors GTO LLC * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -29,28 +29,26 @@ import java.util.Base64 /** * Helper for serializing Base64 protobuf data. */ -interface Base64ProtobufSerializer { - companion object { - /** - * Deserialize a base64 protobuf payload into a Base64 String. - * @param bytes byte[] data - * @return Returns a String from the base64 protobuf payload. - */ - fun deserialize(bytes: ByteArray?): String { - return if (bytes == null) { - "" - } else Base64.getEncoder().encodeToString(bytes) - } +object Base64ProtobufSerializer { + /** + * Deserialize a base64 protobuf payload into a Base64 String. + * @param bytes byte[] data + * @return Returns a String from the base64 protobuf payload. + */ + fun deserialize(bytes: ByteArray?): String { + return if (bytes == null) { + "" + } else Base64.getEncoder().encodeToString(bytes) + } - /** - * Serialize a String into Base64 format. - * @param stringToSerialize String to serialize. - * @return Returns the Base64 formatted String as a byte[]. - */ - fun serialize(stringToSerialize: String?): ByteArray { - return if (stringToSerialize == null) { - ByteArray(0) - } else Base64.getDecoder().decode(stringToSerialize.toByteArray()) - } + /** + * Serialize a String into Base64 format. + * @param stringToSerialize String to serialize. + * @return Returns the Base64 formatted String as a byte[]. + */ + fun serialize(stringToSerialize: String?): ByteArray { + return if (stringToSerialize == null) { + ByteArray(0) + } else Base64.getDecoder().decode(stringToSerialize.toByteArray()) } } diff --git a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/CloudEventSerializers.kt b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/CloudEventSerializers.kt index aacd466..1b7cdbd 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/CloudEventSerializers.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/CloudEventSerializers.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 General Motors GTO LLC + * Copyright (c) 2024 General Motors GTO LLC * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -27,16 +27,11 @@ package org.eclipse.uprotocol.cloudevent.serialize /** * Provides Singleton instances of the CloudEvent Serializers. */ -enum class CloudEventSerializers(cloudEventSerializer: CloudEventSerializer) { +enum class CloudEventSerializers(private val cloudEventSerializer: CloudEventSerializer) { JSON(CloudEventToJsonSerializer()), PROTOBUF(CloudEventToProtobufSerializer()); - private val cloudEventSerializer: CloudEventSerializer fun serializer(): CloudEventSerializer { return cloudEventSerializer } - - init { - this.cloudEventSerializer = cloudEventSerializer - } } diff --git a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/CloudEventToProtobufSerializer.kt b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/CloudEventToProtobufSerializer.kt index 88c815f..68ffd10 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/CloudEventToProtobufSerializer.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/serialize/CloudEventToProtobufSerializer.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 General Motors GTO LLC + * Copyright (c) 2024 General Motors GTO LLC * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -31,11 +31,6 @@ import io.cloudevents.protobuf.ProtobufFormat * CloudEventSerializer to serialize and deserialize CloudEvents to protobuf format. */ class CloudEventToProtobufSerializer : CloudEventSerializer { - - companion object { - private val serializer: ProtobufFormat = ProtobufFormat() - } - override fun serialize(cloudEvent: CloudEvent): ByteArray { return serializer.serialize(cloudEvent) } @@ -43,4 +38,8 @@ class CloudEventToProtobufSerializer : CloudEventSerializer { override fun deserialize(bytes: ByteArray): CloudEvent { return serializer.deserialize(bytes) } + + companion object { + private val serializer: ProtobufFormat = ProtobufFormat() + } } diff --git a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/validate/CloudEventValidator.kt b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/validate/CloudEventValidator.kt index e43676c..a862b56 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/cloudevent/validate/CloudEventValidator.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/cloudevent/validate/CloudEventValidator.kt @@ -28,7 +28,8 @@ import io.cloudevents.CloudEvent import org.eclipse.uprotocol.cloudevent.factory.UCloudEvent import org.eclipse.uprotocol.validation.ValidationResult import org.eclipse.uprotocol.uri.serializer.LongUriSerializer -import org.eclipse.uprotocol.uri.validator.UriValidator +import org.eclipse.uprotocol.uri.validator.isRpcMethod +import org.eclipse.uprotocol.uri.validator.validate import org.eclipse.uprotocol.v1.* import java.util.Optional @@ -324,7 +325,7 @@ abstract class CloudEventValidator { } private fun validateUEntityUri(uri: UUri): ValidationResult { - return UriValidator.validate(uri) + return uri.validate() } /** @@ -405,7 +406,7 @@ abstract class CloudEventValidator { ) ) } - return if (!UriValidator.isRpcMethod(uri)) { + return if (!uri.isRpcMethod()) { ValidationResult.failure("Invalid RPC method uri. UriPart should be the method to be called, or method from response.") } else ValidationResult.success() } diff --git a/src/main/kotlin/org/eclipse/uprotocol/transport/builder/UAttributesBuilder.kt b/src/main/kotlin/org/eclipse/uprotocol/transport/builder/UAttributesBuilder.kt index ddc093c..d37f7b6 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/transport/builder/UAttributesBuilder.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/transport/builder/UAttributesBuilder.kt @@ -24,7 +24,7 @@ package org.eclipse.uprotocol.transport.builder -import org.eclipse.uprotocol.uuid.factory.UuidFactory +import org.eclipse.uprotocol.uuid.factory.UUIDV8 import org.eclipse.uprotocol.v1.* /** @@ -159,7 +159,7 @@ class UAttributesBuilder fun publish(source: UUri, priority: UPriority): UAttributesBuilder { return UAttributesBuilder( source, - UuidFactory.Factories.UPROTOCOL.factory().create(), + UUIDV8(), UMessageType.UMESSAGE_TYPE_PUBLISH, priority ) } @@ -174,7 +174,7 @@ class UAttributesBuilder fun notification(source: UUri, sink: UUri, priority: UPriority): UAttributesBuilder { return UAttributesBuilder( source, - UuidFactory.Factories.UPROTOCOL.factory().create(), + UUIDV8(), UMessageType.UMESSAGE_TYPE_PUBLISH, priority ).withSink(sink) } @@ -190,7 +190,7 @@ class UAttributesBuilder fun request(source: UUri, sink: UUri, priority: UPriority, ttl: Int): UAttributesBuilder { return UAttributesBuilder( source, - UuidFactory.Factories.UPROTOCOL.factory().create(), + UUIDV8(), UMessageType.UMESSAGE_TYPE_REQUEST, priority ).withTtl(ttl).withSink(sink) } @@ -206,7 +206,7 @@ class UAttributesBuilder fun response(source: UUri, sink: UUri, priority: UPriority, reqid: UUID): UAttributesBuilder { return UAttributesBuilder( source, - UuidFactory.Factories.UPROTOCOL.factory().create(), + UUIDV8(), UMessageType.UMESSAGE_TYPE_RESPONSE, priority ).withSink(sink).withReqId(reqid) } diff --git a/src/main/kotlin/org/eclipse/uprotocol/transport/builder/UPayloadBuilder.kt b/src/main/kotlin/org/eclipse/uprotocol/transport/builder/UPayloadBuilder.kt index 4f86056..168ed03 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/transport/builder/UPayloadBuilder.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/transport/builder/UPayloadBuilder.kt @@ -30,7 +30,6 @@ import com.google.protobuf.Message import org.eclipse.uprotocol.v1.UPayload import org.eclipse.uprotocol.v1.UPayloadFormat import org.eclipse.uprotocol.v1.uPayload -import java.util.* object UPayloadBuilder { diff --git a/src/main/kotlin/org/eclipse/uprotocol/transport/validate/UAttributesValidator.kt b/src/main/kotlin/org/eclipse/uprotocol/transport/validate/UAttributesValidator.kt index 3eb8f46..dd55855 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/transport/validate/UAttributesValidator.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/transport/validate/UAttributesValidator.kt @@ -24,8 +24,11 @@ package org.eclipse.uprotocol.transport.validate -import org.eclipse.uprotocol.uri.validator.UriValidator -import org.eclipse.uprotocol.uuid.factory.UuidUtils +import org.eclipse.uprotocol.uri.validator.validate +import org.eclipse.uprotocol.uri.validator.validateRpcMethod +import org.eclipse.uprotocol.uri.validator.validateRpcResponse +import org.eclipse.uprotocol.uuid.factory.getTime +import org.eclipse.uprotocol.uuid.factory.isUuid import org.eclipse.uprotocol.v1.* import org.eclipse.uprotocol.validation.ValidationResult import java.util.stream.Collectors @@ -51,7 +54,7 @@ abstract class UAttributesValidator { * invalid configurations. */ fun validate(attributes: UAttributes): ValidationResult { - val errorMessage = Stream.of(validateType(attributes), + val errorMessage = Stream.of(validateType(attributes), validateTtl(attributes), validateSink(attributes), validateCommStatus(attributes), validatePermissionLevel(attributes), validateReqId(attributes)) .filter(ValidationResult::isFailure).map { obj: ValidationResult -> obj.getMessage() }.collect(Collectors.joining(",")) @@ -69,14 +72,14 @@ abstract class UAttributesValidator { */ fun isExpired(uAttributes: UAttributes): Boolean { val ttl = uAttributes.ttl - val maybeTime = UuidUtils.getTime(uAttributes.id) + val maybeTime = uAttributes.id.getTime() // if the message does not have a ttl or the original time is not present or the ttl is less than 0 - if (!uAttributes.hasTtl() || maybeTime.isEmpty || ttl <= 0) { + if (!uAttributes.hasTtl() || maybeTime==null || ttl <= 0) { return false } // the original time plus the ttl is less than the current time, the message has expired - return (maybeTime.get() + ttl) < System.currentTimeMillis(); + return (maybeTime + ttl) < System.currentTimeMillis() } /** @@ -104,7 +107,7 @@ abstract class UAttributesValidator { */ open fun validateSink(attributes: UAttributes): ValidationResult { return if (attributes.hasSink()) { - UriValidator.validate(attributes.sink) + attributes.sink.validate() } else { ValidationResult.success() } @@ -154,7 +157,7 @@ abstract class UAttributesValidator { * @return Returns a [ValidationResult] that is success or failed with a failure message. */ open fun validateReqId(attributes: UAttributes): ValidationResult { - return if (attributes.hasReqid() && !UuidUtils.isUuid(attributes.reqid)) { + return if (attributes.hasReqid() && !attributes.reqid.isUuid()) { ValidationResult.failure("Invalid UUID") } else { ValidationResult.success() @@ -226,7 +229,7 @@ abstract class UAttributesValidator { override fun validateSink(attributes: UAttributes): ValidationResult { return if (!attributes.hasSink()) { ValidationResult.failure("Missing Sink") - } else UriValidator.validateRpcMethod(attributes.sink) + } else attributes.sink.validateRpcMethod() } /** @@ -278,7 +281,7 @@ abstract class UAttributesValidator { if (!attributes.hasSink() || attributes.sink === UUri.getDefaultInstance()) { return ValidationResult.failure("Missing Sink") } - return UriValidator.validateRpcResponse(attributes.sink) + return attributes.sink.validateRpcResponse() } /** @@ -291,7 +294,7 @@ abstract class UAttributesValidator { if (!attributes.hasReqid() || attributes.reqid === UUID.getDefaultInstance()) { return ValidationResult.failure("Missing correlationId") } - return if (!UuidUtils.isUuid(attributes.reqid)) { + return if (!attributes.reqid.isUuid()) { ValidationResult.failure(String.format("Invalid correlationId [%s]", attributes.reqid)) } else { ValidationResult.success() diff --git a/src/main/kotlin/org/eclipse/uprotocol/uri/README.adoc b/src/main/kotlin/org/eclipse/uprotocol/uri/README.adoc index a148f92..46a05c1 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uri/README.adoc +++ b/src/main/kotlin/org/eclipse/uprotocol/uri/README.adoc @@ -35,7 +35,7 @@ val uri = uUri { === Validating [,kotlin] ---- - val status:ValidationResult = UriValidator.validateRpcMethod(uuri) + val status:ValidationResult = uuri.validateRpcMethod() assertTrue(status.isSuccess()) ---- @@ -43,8 +43,8 @@ val uri = uUri { [,kotlin] ---- val uri: UUri = .../* UUri example above */ - val micro: ByteArray = MicroUriSerializer.instance().serialize(uri) - val long: String = LongUriSerializer.instance().serialize(uri) + val micro: ByteArray = MicroUriSerializer.INSTANCE.serialize(uri) + val long: String = LongUriSerializer.INSTANCE.serialize(uri) val uri2: UUri = UriSerializer.buildResolved(long, micro) assertTrue(uri.equals(uri2)) ---- \ No newline at end of file diff --git a/src/main/kotlin/org/eclipse/uprotocol/uri/factory/UResourceBuilder.kt b/src/main/kotlin/org/eclipse/uprotocol/uri/factory/UResourceBuilder.kt index 7246ea0..b4a84b9 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uri/factory/UResourceBuilder.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uri/factory/UResourceBuilder.kt @@ -27,7 +27,6 @@ package org.eclipse.uprotocol.uri.factory import com.google.protobuf.ProtocolMessageEnum import org.eclipse.uprotocol.v1.UResource import org.eclipse.uprotocol.v1.uResource -import java.util.* object UResourceBuilder { diff --git a/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/LongUriSerializer.kt b/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/LongUriSerializer.kt index b55ad57..200e476 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/LongUriSerializer.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/LongUriSerializer.kt @@ -24,7 +24,7 @@ package org.eclipse.uprotocol.uri.serializer -import org.eclipse.uprotocol.uri.validator.UriValidator +import org.eclipse.uprotocol.uri.validator.isEmpty import org.eclipse.uprotocol.v1.* import java.util.* @@ -39,7 +39,7 @@ class LongUriSerializer private constructor() : UriSerializer { * @return Returns the String format of the supplied [UUri] that can be used as a sink or a source in a uProtocol publish communication. */ override fun serialize(uri: UUri?): String { - if (uri == null || UriValidator.isEmpty(uri)) { + if (uri == null || uri.isEmpty()) { return "" } val sb = StringBuilder() diff --git a/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/MicroUriSerializer.kt b/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/MicroUriSerializer.kt index 5643c8e..f6975fd 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/MicroUriSerializer.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/MicroUriSerializer.kt @@ -26,7 +26,8 @@ package org.eclipse.uprotocol.uri.serializer import com.google.protobuf.ByteString import org.eclipse.uprotocol.uri.factory.UResourceBuilder -import org.eclipse.uprotocol.uri.validator.UriValidator +import org.eclipse.uprotocol.uri.validator.isEmpty +import org.eclipse.uprotocol.uri.validator.isMicroForm import org.eclipse.uprotocol.v1.* import java.io.ByteArrayOutputStream import java.io.IOException @@ -60,7 +61,7 @@ class MicroUriSerializer private constructor() : UriSerializer { * @return Returns a byte[] representing the serialized [UUri]. */ override fun serialize(uri: UUri?): ByteArray { - if (uri == null || UriValidator.isEmpty(uri) || !UriValidator.isMicroForm(uri)) { + if (uri == null || uri.isEmpty() || !uri.isMicroForm()) { return ByteArray(0) } val maybeUeId: Optional = Optional.ofNullable(uri.entity.id) @@ -70,12 +71,16 @@ class MicroUriSerializer private constructor() : UriSerializer { os.write(UP_VERSION.toInt()) val type = if (uri.authority.hasIp()) { val length = uri.authority.getIp().size() - if (length == 4) { - AddressType.IPv4 - } else if (length == 16) { - AddressType.IPv6 - } else { - return ByteArray(0) + when (length) { + 4 -> { + AddressType.IPv4 + } + 16 -> { + AddressType.IPv6 + } + else -> { + return ByteArray(0) + } } } else if (uri.authority.hasId()) { AddressType.ID diff --git a/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/UriSerializer.kt b/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/UriSerializer.kt index e33fafc..9113789 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/UriSerializer.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uri/serializer/UriSerializer.kt @@ -24,7 +24,7 @@ package org.eclipse.uprotocol.uri.serializer -import org.eclipse.uprotocol.uri.validator.UriValidator +import org.eclipse.uprotocol.uri.validator.isResolved import org.eclipse.uprotocol.v1.UUri import org.eclipse.uprotocol.v1.copy import org.eclipse.uprotocol.v1.uUri @@ -77,6 +77,6 @@ interface UriSerializer { } } - return if (UriValidator.isResolved(uri)) Optional.of(uri) else Optional.empty() + return if (uri.isResolved()) Optional.of(uri) else Optional.empty() } } \ No newline at end of file diff --git a/src/main/kotlin/org/eclipse/uprotocol/uri/validator/UriValidator.kt b/src/main/kotlin/org/eclipse/uprotocol/uri/validator/UriValidator.kt index 9d64f4f..d0d12d4 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uri/validator/UriValidator.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uri/validator/UriValidator.kt @@ -30,168 +30,162 @@ import org.eclipse.uprotocol.v1.UUri import org.eclipse.uprotocol.validation.ValidationResult /** - * class for validating Uris. + * Validate a [UUri] to ensure that it has at least a name for the uEntity. + * + * @return Returns UStatus containing a success or a failure with the error message. */ -object UriValidator { - /** - * Validate a [UUri] to ensure that it has at least a name for the uEntity. - * - * @param uri [UUri] to validate. - * @return Returns UStatus containing a success or a failure with the error message. - */ - fun validate(uri: UUri): ValidationResult { - if (isEmpty(uri)) { - return ValidationResult.failure("Uri is empty.") - } - if (uri.hasAuthority() && !isRemote(uri.authority)) { - return ValidationResult.failure("Uri is remote missing uAuthority.") - } - return if (uri.entity.name.isBlank()) { - ValidationResult.failure("Uri is missing uSoftware Entity name.") - } else ValidationResult.success() +fun UUri.validate(): ValidationResult { + if (isEmpty()) { + return ValidationResult.failure("Uri is empty.") } - - /** - * Validate a [UUri] that is meant to be used as an RPC method URI. Used in Request sink values and Response source values. - * - * @param uri [UUri] to validate. - * @return Returns UStatus containing a success or a failure with the error message. - */ - fun validateRpcMethod(uri: UUri): ValidationResult { - val status: ValidationResult = validate(uri) - if (status.isFailure()) { - return status - } - return if (!isRpcMethod(uri)) { - ValidationResult.failure("Invalid RPC method uri. Uri should be the method to be called, or method from response.") - } else ValidationResult.success() + if (hasAuthority() && !authority.isRemote()) { + return ValidationResult.failure("Uri is remote missing uAuthority.") } + return if (entity.name.isBlank()) { + ValidationResult.failure("Uri is missing uSoftware Entity name.") + } else ValidationResult.success() +} - /** - * Validate a [UUri] that is meant to be used as an RPC response URI. Used in Request source values and Response sink values. - * - * @param uri [UUri] to validate. - * @return Returns UStatus containing a success or a failure with the error message. - */ - fun validateRpcResponse(uri: UUri): ValidationResult { - val status: ValidationResult = validate(uri) - if (status.isFailure()) { - return status - } - return if (!isRpcResponse(uri)) { - ValidationResult.failure("Invalid RPC response type.") - } else ValidationResult.success() - } - /** - * Indicates that this URI is an empty as it does not contain authority, entity, and resource. - * - * @param uri [UUri] to check if it is empty - * @return Returns true if this URI is an empty container and has no valuable information in building uProtocol sinks or sources. - */ - fun isEmpty(uri: UUri): Boolean { - return !uri.hasAuthority() && !uri.hasEntity() && !uri.hasResource() +/** + * Validate a [UUri] that is meant to be used as an RPC method URI. Used in Request sink values and Response source values. + * + * @return Returns UStatus containing a success or a failure with the error message. + */ +fun UUri.validateRpcMethod(): ValidationResult { + val status: ValidationResult = validate() + if (status.isFailure()) { + return status } + return if (!isRpcMethod()) { + ValidationResult.failure("Invalid RPC method uri. Uri should be the method to be called, or method from response.") + } else ValidationResult.success() +} - /** - * Returns true if URI is of type RPC. - * - * @param uri [UUri] to check if it is of type RPC method - * @return Returns true if URI is of type RPC. - */ - fun isRpcMethod(uri: UUri): Boolean { - return uri.resource.name.contains("rpc") && (uri.resource - .hasInstance() && uri.resource.instance.trim().isNotEmpty() || uri.resource - .hasId() && uri.resource.id != 0) +/** + * Validate a [UUri] that is meant to be used as an RPC response URI. Used in Request source values and Response sink values. + * + * @return Returns UStatus containing a success or a failure with the error message. + */ +fun UUri.validateRpcResponse(): ValidationResult { + val status: ValidationResult = validate() + if (status.isFailure()) { + return status } + return if (!isRpcResponse()) { + ValidationResult.failure("Invalid RPC response type.") + } else ValidationResult.success() +} - /** - * Returns true if URI contains both names and numeric representations of the names inside its belly. - * Meaning that this UUri can be serialized to long or micro formats. - * - * @param uri [UUri] to check if resolved. - * @return Returns true if URI contains both names and numeric representations of the names inside its belly. - * Meaning that this UUri can buree serialized to long or micro formats. - */ - fun isResolved(uri: UUri): Boolean { - return !isEmpty(uri) && isLongForm(uri) && isMicroForm(uri) - } - /** - * Returns true if URI is of type RPC response. - * - * @param uri [UUri] to check response - * @return Returns true if URI is of type RPC response. - */ - fun isRpcResponse(uri: UUri): Boolean { - val resource: UResource = uri.resource - return resource.getName().contains("rpc") && - resource.hasInstance() && resource.getInstance().contains("response") && - resource.hasId() && resource.id == 0 - } - /** - * Returns true if URI contains numbers so that it can be serialized into micro format. - * - * @param uri [UUri] to check - * @return Returns true if URI contains numbers so that it can be serialized into micro format. - */ - fun isMicroForm(uri: UUri): Boolean { - return !isEmpty(uri) && uri.entity.hasId() && uri.resource - .hasId() && isMicroForm(uri.authority) - } +/** + * Returns true if URI is of type RPC. + * + * @return Returns true if URI is of type RPC. + */ +fun UUri.isRpcMethod(): Boolean { + return resource.name.contains("rpc") && (resource + .hasInstance() && resource.instance.trim().isNotEmpty() || resource + .hasId() && resource.id != 0) +} - /** - * check if UAuthority can be represented in micro format. Micro UAuthorities are local or ones - * that contain IP address or IDs. - * - * @param authority [UAuthority] to check - * @return Returns true if UAuthority can be represented in micro format - */ - fun isMicroForm(authority: UAuthority?): Boolean { - return isLocal(authority) || (authority?.hasIp() == true || authority?.hasId() == true) - } +/** + * Returns true if URI contains both names and numeric representations of the names inside its belly. + * Meaning that this UUri can be serialized to long or micro formats. + * + * @return Returns true if URI contains both names and numeric representations of the names inside its belly. + * Meaning that this UUri can buree serialized to long or micro formats. + */ +fun UUri.isResolved(): Boolean { + return !isEmpty() && isLongForm() && isMicroForm() +} - /** - * Returns true if URI contains names so that it can be serialized into long format. - * - * @param uri [UUri] to check - * @return Returns true if URI contains names so that it can be serialized into long format. - */ - fun isLongForm(uri: UUri): Boolean { - return !isEmpty(uri) && - isLongForm(uri.authority) && - uri.entity.getName().isNotBlank() && uri.resource.getName().isNotBlank() +/** + * Returns true if URI is of type RPC response. + * + * @return Returns true if URI is of type RPC response. + */ +fun UUri.isRpcResponse(): Boolean { + val resource: UResource = resource + return resource.getName().contains("rpc") && + resource.hasInstance() && resource.getInstance().contains("response") && + resource.hasId() && resource.id == 0 +} - } +/** + * Indicates that this URI is an empty as it does not contain authority, entity, and resource. + * + * @return Returns true if this URI is an empty container and has no valuable information in building uProtocol sinks or sources. + */ +fun UUri.isEmpty(): Boolean { + return !this.hasAuthority() && !this.hasEntity() && !this.hasResource() +} - /** - * Returns true if UAuthority contains names so that it can be serialized into long format. - * - * @param authority [UAuthority] to check - * @return Returns true if URI contains names so that it can be serialized into long format. - */ - fun isLongForm(authority: UAuthority?): Boolean { - return (authority != null) && authority.hasName() && authority.name.isNotBlank() - } +/** + * Indicates that this URI is NOT an empty as it does contain authority or entity or resource. + * + * @return Returns true if this URI is NOT an empty container and has valuable information in building uProtocol sinks or sources. + */ +fun UUri.isNotEmpty(): Boolean { + return !this.isEmpty() +} - /** - * Returns true if UAuthority is local meaning there is no name/ip/id set. - * - * @param authority [UAuthority] to check if it is local or not - * @return Returns true if UAuthority is local meaning the Authority is not populated with name, ip and id - */ - fun isLocal(authority: UAuthority?): Boolean { - return (authority == null) || authority == UAuthority.getDefaultInstance() - } +/** + * Returns true if URI contains numbers so that it can be serialized into micro format. + * + * @return Returns true if URI contains numbers so that it can be serialized into micro format. + */ +fun UUri.isMicroForm(): Boolean { + return !isEmpty() && entity.hasId() && resource + .hasId() && authority.isMicroForm() +} - /** - * Returns true if UAuthority is remote meaning the name and/or ip/id is populated. - * @param authority {@link UAuthority} to check if it is remote or not - * @return Returns true if UAuthority is remote meaning the name and/or ip/id is populated. - */ - fun isRemote(authority: UAuthority?): Boolean { - return (authority != null) && authority != UAuthority.getDefaultInstance() && - (isLongForm(authority) || isMicroForm(authority)) - } +/** + * check if UAuthority can be represented in micro format. Micro UAuthorities are local or ones + * that contain IP address or IDs. + * + * @return Returns true if UAuthority can be represented in micro format + */ +fun UAuthority?.isMicroForm(): Boolean { + return isLocal() || (this?.hasIp() == true || this?.hasId() == true) +} + +/** + * Returns true if URI contains names so that it can be serialized into long format. + * + * @return Returns true if URI contains names so that it can be serialized into long format. + */ +fun UUri.isLongForm(): Boolean { + return !isEmpty() && + authority.isLongForm() && + entity.getName().isNotBlank() && resource.getName().isNotBlank() +} + +/** + * Returns true if UAuthority contains names so that it can be serialized into long format. + * + * @return Returns true if URI contains names so that it can be serialized into long format. + */ +fun UAuthority?.isLongForm(): Boolean { + return (this != null) && this.hasName() && this.name.isNotBlank() +} + +/** + * Returns true if UAuthority is local meaning there is no name/ip/id set. + * + * @return Returns true if UAuthority is local meaning the Authority is not populated with name, ip and id + */ +fun UAuthority?.isLocal(): Boolean { + return (this == null) || this == UAuthority.getDefaultInstance() +} + +/** + * Returns true if UAuthority is remote meaning the name and/or ip/id is populated. + * + * @return Returns true if UAuthority is remote meaning the name and/or ip/id is populated. + */ +fun UAuthority?.isRemote(): Boolean { + return (this != null) && this != UAuthority.getDefaultInstance() && + (this.isLongForm() || this.isMicroForm()) } diff --git a/src/main/kotlin/org/eclipse/uprotocol/uuid/README.adoc b/src/main/kotlin/org/eclipse/uprotocol/uuid/README.adoc index 68f7ac6..55144b5 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uuid/README.adoc +++ b/src/main/kotlin/org/eclipse/uprotocol/uuid/README.adoc @@ -10,29 +10,26 @@ Implementation of https://github.com/eclipse-uprotocol/uprotocol-spec/blob/main/ [source,kotlin] ---- - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val version: Optional = UuidUtils.getVersion(uuid); - val time: Optional = UuidUtils.getTime(uuid); - val bytes = MicroUuidSerializer.instance().serialize(uuid) - val uuidString = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val version: UUIDVersion = uuid.getVersion() + val time: Long? = uuid.getTime() + val bytes = MicroUuidSerializer.INSTANCE.serialize(uuid) + val uuidString = LongUuidSerializer.INSTANCE.serialize(uuid) assertNotNull(uuid) - assertFalse(UuidUtils.isUuidv6(uuid)) - assertFalse(UuidUtils.isUProtocol(uuid)) + assertFalse(uuid.isUuidv6()) + assertFalse(uuid.isUProtocol()) assertTrue(version.isPresent) assertEquals(version.get(), UuidUtils.Version.VERSION_UNKNOWN) assertFalse(time.isPresent) assertTrue(bytes.isNotEmpty()) assertFalse(uuidString.isBlank()) - assertFalse(UuidUtils.isUuidv6(null)) - assertFalse(UuidUtils.isUProtocol(null)) - assertFalse(UuidUtils.isUuid(null)) - val uuid1: UUID = MicroUuidSerializer.instance().deserialize(bytes) + val uuid1: UUID = MicroUuidSerializer.INSTANCE.deserialize(bytes) assertTrue(uuid1 == UUID.getDefaultInstance()) assertEquals(uuid, uuid1) - val uuid2: UUID = LongUuidSerializer.instance().deserialize(uuidString) + val uuid2: UUID = LongUuidSerializer.INSTANCE.deserialize(uuidString) assertTrue(uuid2 == UUID.getDefaultInstance()) assertEquals(uuid, uuid2) ---- \ No newline at end of file diff --git a/src/main/kotlin/org/eclipse/uprotocol/uuid/factory/UuidFactory.kt b/src/main/kotlin/org/eclipse/uprotocol/uuid/factory/UuidFactory.kt index 6a1f156..7ceb0fc 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uuid/factory/UuidFactory.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uuid/factory/UuidFactory.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 General Motors GTO LLC + * Copyright (c) 2024 General Motors GTO LLC * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -30,32 +30,38 @@ import org.eclipse.uprotocol.v1.uUID import java.time.Instant import java.util.* -abstract class UuidFactory { - fun create(): UUID { - return this.create(Instant.now()) - } - - abstract fun create(instant: Instant?): UUID - enum class Factories(private val factory: UuidFactory) { - UUIDV6(Uuidv6Factory()), UPROTOCOL(Uuidv8Factory()); - fun factory(): UuidFactory { - return factory - } - } +sealed class UuidFactory { + abstract operator fun invoke(instant: Instant = Instant.now()): UUID +} - private class Uuidv6Factory : UuidFactory() { - override fun create(instant: Instant?): UUID { - val uuidJava: java.util.UUID = UuidCreator.getTimeOrdered( - Objects.requireNonNullElse(instant, Instant.now()), null, null - ) - return uUID { - msb = uuidJava.mostSignificantBits - lsb = uuidJava.leastSignificantBits - } +/** + * Factory to generate UUID following Uuidv6 data model. + * + * @param instant An instantaneous point on the time-line + * @return return UUID generated in UUIDV6 + */ +@Suppress("KDocUnresolvedReference") +data object UUIDV6 : UuidFactory() { + override operator fun invoke(instant: Instant): UUID { + val uuidJava: java.util.UUID = UuidCreator.getTimeOrdered( + Objects.requireNonNullElse(instant, Instant.now()), null, null + ) + return uUID { + msb = uuidJava.mostSignificantBits + lsb = uuidJava.leastSignificantBits } } +} +/** + * Factory to generate UUID following Uuidv8 data model. + * + * @param instant An instantaneous point on the time-line + * @return return UUID generated in UUIDV8 + */ +@Suppress("KDocUnresolvedReference") +data object UUIDV8 : UuidFactory() { /** * uProtocol UUIDv8 data model * UUIDv8 can only be built using the static factory methods of the class @@ -89,38 +95,34 @@ abstract class UuidFactory { * |rand_b | MUST 62 bits random number that is generated at initialization time of the uE only and reused * otherwise | */ - private class Uuidv8Factory : UuidFactory() { - @Synchronized - override fun create(instant: Instant?): UUID { - val time: Long = Objects.requireNonNullElse(instant, Instant.now())!!.toEpochMilli() + private const val UUIDV8_VERSION = 8 + private const val MAX_COUNT = 0xfff - // Check if the current time is the same as the previous time - if (time == msBits shr 16) { - // Increment the counter if we are not at MAX_COUNT - if (msBits and 0xFFFL < MAX_COUNT) { - msBits++ - } + //private val lsb: Long = (Random().nextLong() and 0x3fffffffffffffffL) or (-0x8000000000000000L).toLong() + private val lsBits: Long = (Random().nextLong() and 0x3fffffffffffffffL) or (1L shl 63) - // The previous time is not the same tick as the current, so we reset msb - } else { - msBits = time shl 16 or (8L shl 12) - } - return uUID { - msb = msBits - lsb = lsBits - } - } + // Keep track of the time and counters + private var msBits = (UUIDV8_VERSION shl 12 // Version is 8 + ).toLong() - companion object { - const val UUIDV8_VERSION = 8 - private const val MAX_COUNT = 0xfff + @Synchronized + override operator fun invoke(instant: Instant): UUID { + val time: Long = instant.toEpochMilli() - //private val lsb: Long = (Random().nextLong() and 0x3fffffffffffffffL) or (-0x8000000000000000L).toLong() - private val lsBits: Long = (Random().nextLong() and 0x3fffffffffffffffL) or (1L shl 63) + // Check if the current time is the same as the previous time + if (time == msBits shr 16) { + // Increment the counter if we are not at MAX_COUNT + if (msBits and 0xFFFL < MAX_COUNT) { + msBits++ + } - // Keep track of the time and counters - private var msBits = (UUIDV8_VERSION shl 12 // Version is 8 - ).toLong() + // The previous time is not the same tick as the current, so we reset msb + } else { + msBits = time shl 16 or (8L shl 12) + } + return uUID { + msb = msBits + lsb = lsBits } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/org/eclipse/uprotocol/uuid/factory/UuidUtils.kt b/src/main/kotlin/org/eclipse/uprotocol/uuid/factory/UuidUtils.kt index b21fc40..1bca319 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uuid/factory/UuidUtils.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uuid/factory/UuidUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 General Motors GTO LLC + * Copyright (c) 2024 General Motors GTO LLC * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -29,145 +29,107 @@ import com.github.f4b6a3.uuid.util.UuidTime import com.github.f4b6a3.uuid.util.UuidUtil import org.eclipse.uprotocol.v1.UUID -import java.util.Optional /** - * UUID Utils class that provides utility methods for uProtocol IDs + * Fetch the UUID version. + * + * @return the UUID version from the UUID object or Optional.empty() if the uuid is null. */ -interface UuidUtils { - /** - * UUID Version - */ - enum class Version(val value: Int) { - /** - * An unknown version. - */ - VERSION_UNKNOWN(0), +fun UUID.getVersion(): UUIDVersion { + // Version is bits masked by 0x000000000000F000 in MS long + return UUIDVersion.getVersion((msb shr 12 and 0x0f).toInt()) +} - /** - * The randomly or pseudo-randomly generated version specified in RFC-4122. - */ - VERSION_RANDOM_BASED(4), +/** + * Fetch the Variant from the passed UUID. + * + * @return UUID variant. + */ +fun UUID.getVariant(): Int = (lsb ushr (64 - (lsb ushr 62)).toInt() and (lsb shr 63)).toInt() - /** - * The time-ordered version with gregorian epoch proposed by Peabody and Davis. - */ - VERSION_TIME_ORDERED(6), +/** + * Verify if version is a formal UUIDv8 uProtocol ID. + * + * @return true if is a uProtocol UUID or false if the UUID is not uProtocol format. + */ +fun UUID.isUProtocol(): Boolean = getVersion() == UUIDVersion.VERSION_UPROTOCOL - /** - * The custom or free-form version proposed by Peabody and Davis. - */ - VERSION_UPROTOCOL(8); - - companion object { - /** - * Get the Version from the passed integer representation of the version. - * - * @param value The integer representation of the version. - * @return The Version object or Optional.empty() if the value is not a valid version. - */ - fun getVersion(value: Int): Optional { - for (version in entries) { - if (version.value == value) { - return Optional.of(version) - } - } - return Optional.empty() - } - } - } +/** + * Verify if version is UUIDv6 + * + * @return true if is UUID version 6 or false if uuid is not version 6 + */ +fun UUID.isUuidv6(): Boolean = + getVersion() == UUIDVersion.VERSION_TIME_ORDERED && getVariant() == UuidVariant.VARIANT_RFC_4122.value - companion object { - /** - * Fetch the UUID version. - * - * @param uuid The UUID to fetch the version from. - * @return the UUID version from the UUID object or Optional.empty() if the uuid is null. - */ - fun getVersion(uuid: UUID?): Optional { - // Version is bits masked by 0x000000000000F000 in MS long - return if (uuid == null) Optional.empty() else Version.getVersion((uuid.msb shr 12 and 0x0f).toInt()) - } +/** + * Verify uuid is either v6 or v8 + * + * @return true if is UUID version 6 or 8 + */ +fun UUID.isUuid(): Boolean = isUProtocol() || isUuidv6() - /** - * Fetch the Variant from the passed UUID. - * - * @param uuid The UUID to fetch the variant from. - * @return UUID variant or Empty if uuid is null. - */ - fun getVariant(uuid: UUID?): Optional { - return if (uuid == null) Optional.empty() else Optional.of( - (uuid.lsb ushr (64 - (uuid.lsb ushr 62)).toInt() and (uuid.lsb shr 63)).toInt() - ) +/** + * Return the number of milliseconds since unix epoch from a passed UUID. + * + * @return number of milliseconds since unix epoch or empty if uuid is null. + */ +fun UUID.getTime(): Long? { + return when (getVersion()) { + UUIDVersion.VERSION_UPROTOCOL -> { + msb ushr 16 } - - /** - * Verify if version is a formal UUIDv8 uProtocol ID. - * - * @return true if is a uProtocol UUID or false if uuid passed is null - * or the UUID is not uProtocol format. - */ - fun isUProtocol(uuid: UUID?): Boolean { - val version: Optional = getVersion(uuid) - return uuid != null && version.isPresent && version.get() === Version.VERSION_UPROTOCOL + UUIDVersion.VERSION_TIME_ORDERED -> { + try { + val uuidJava = java.util.UUID(msb, lsb) + UuidTime.toUnixTimestamp(UuidUtil.getTimestamp(uuidJava)) / UuidTime.TICKS_PER_MILLI + } catch (e: IllegalArgumentException) { + null + } } - - /** - * Verify if version is UUIDv6 - * - * @return true if is UUID version 6 or false if uuid is null or not version 6 - */ - fun isUuidv6(uuid: UUID?): Boolean { - val version: Optional = getVersion(uuid) - val variant: Optional = getVariant(uuid) - return uuid != null && version.isPresent && version.get() === Version.VERSION_TIME_ORDERED && variant.get() == UuidVariant.VARIANT_RFC_4122.value + else -> { + null } + } +} - /** - * Verify uuid is either v6 or v8 - * - * @return true if is UUID version 6 or 8 - */ - fun isUuid(uuid: UUID?): Boolean { - return isUProtocol(uuid) || isUuidv6(uuid) - } +/** + * UUID Version + */ +enum class UUIDVersion(val value: Int) { + /** + * An unknown version. + */ + VERSION_UNKNOWN(0), - /** - * Return the number of milliseconds since unix epoch from a passed UUID. - * - * @param uuid passed uuid to fetch the time. - * @return number of milliseconds since unix epoch or empty if uuid is null. - */ - fun getTime(uuid: UUID?): Optional { - var time: Long? = null + /** + * The randomly or pseudo-randomly generated version specified in RFC-4122. + */ + VERSION_RANDOM_BASED(4), - if(uuid==null){ - return Optional.empty() - } - val version = getVersion(uuid) + /** + * The time-ordered version with gregorian epoch proposed by Peabody and Davis. + */ + VERSION_TIME_ORDERED(6), - if (version.isEmpty) { - return Optional.empty() - } + /** + * The custom or free-form version proposed by Peabody and Davis. + */ + VERSION_UPROTOCOL(8); - when (version.get()) { - Version.VERSION_UPROTOCOL -> { - time = uuid.msb ushr 16 - } - Version.VERSION_TIME_ORDERED -> { - try { - val uuidJava = java.util.UUID(uuid.msb, uuid.lsb) - time = UuidTime.toUnixTimestamp(UuidUtil.getTimestamp(uuidJava)) / UuidTime.TICKS_PER_MILLI - } catch (e: IllegalArgumentException) { - return Optional.empty() - } - } - else -> { - } - } + companion object { + /** + * Get the Version from the passed integer representation of the version. + * + * @param value The integer representation of the version. + * @return The Version object or VERSION_UNKNOWN if the value is not a valid version. + */ - return Optional.ofNullable(time) + fun getVersion(value: Int): UUIDVersion { + return entries.find { + it.value == value + } ?: VERSION_UNKNOWN } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/org/eclipse/uprotocol/uuid/serializer/LongUuidSerializer.kt b/src/main/kotlin/org/eclipse/uprotocol/uuid/serializer/LongUuidSerializer.kt index 5736e67..df14bb8 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uuid/serializer/LongUuidSerializer.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uuid/serializer/LongUuidSerializer.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 General Motors GTO LLC + * Copyright (c) 2024 General Motors GTO LLC * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -50,9 +50,6 @@ class LongUuidSerializer private constructor() : UuidSerializer { } companion object { - private val INSTANCE = LongUuidSerializer() - fun instance(): LongUuidSerializer { - return INSTANCE - } + val INSTANCE : LongUuidSerializer by lazy { LongUuidSerializer() } } } diff --git a/src/main/kotlin/org/eclipse/uprotocol/uuid/serializer/MicroUuidSerializer.kt b/src/main/kotlin/org/eclipse/uprotocol/uuid/serializer/MicroUuidSerializer.kt index b208ecc..fdd3a85 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uuid/serializer/MicroUuidSerializer.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uuid/serializer/MicroUuidSerializer.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 General Motors GTO LLC + * Copyright (c) 2024 General Motors GTO LLC * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -49,9 +49,6 @@ class MicroUuidSerializer private constructor() : UuidSerializer { } companion object { - private val INSTANCE = MicroUuidSerializer() - fun instance(): MicroUuidSerializer { - return INSTANCE - } + val INSTANCE = MicroUuidSerializer() } } diff --git a/src/main/kotlin/org/eclipse/uprotocol/uuid/validate/UuidValidator.kt b/src/main/kotlin/org/eclipse/uprotocol/uuid/validate/UuidValidator.kt index a0256d1..e207a75 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/uuid/validate/UuidValidator.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/uuid/validate/UuidValidator.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 General Motors GTO LLC + * Copyright (c) 2024 General Motors GTO LLC * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -25,27 +25,22 @@ package org.eclipse.uprotocol.uuid.validate import com.github.f4b6a3.uuid.enums.UuidVariant -import org.eclipse.uprotocol.uuid.factory.UuidUtils +import org.eclipse.uprotocol.uuid.factory.* import org.eclipse.uprotocol.v1.UCode import org.eclipse.uprotocol.v1.UStatus import org.eclipse.uprotocol.v1.UUID import org.eclipse.uprotocol.v1.uStatus import org.eclipse.uprotocol.validation.ValidationResult -import java.util.* + /** * UUID Validator class that validates UUIDs */ -abstract class UuidValidator { - enum class Validators(private val uuidValidator: UuidValidator) { - UNKNOWN(InvalidValidator()), UUIDV6(UUIDv6Validator()), UPROTOCOL(UUIDv8Validator()); - - fun validator(): UuidValidator { - return uuidValidator - } - } +sealed class UuidValidator { + abstract fun validateVersion(uuid: UUID): ValidationResult + abstract fun validateVariant(uuid: UUID): ValidationResult - fun validate(uuid: UUID?): UStatus { + fun validate(uuid: UUID): UStatus { val errorMessages = listOf( validateVersion(uuid), validateVariant(uuid), validateTime(uuid) ).filter { it.isFailure() }.joinToString(",") { it.getMessage() } @@ -57,66 +52,69 @@ abstract class UuidValidator { } } - - abstract fun validateVersion(uuid: UUID?): ValidationResult - private fun validateTime(uuid: UUID?): ValidationResult { - val time: Optional = UuidUtils.getTime(uuid) - return if (time.isPresent && time.get() > 0) ValidationResult.success() else ValidationResult.failure( - String.format( - "Invalid UUID Time" - ) - ) + private fun validateTime(uuid: UUID): ValidationResult { + val time = uuid.getTime() + return if (time != null && time > 0) { + ValidationResult.success() + } else { + ValidationResult.failure("Invalid UUID Time") + } } - abstract fun validateVariant(uuid: UUID?): ValidationResult - private class InvalidValidator : UuidValidator() { - override fun validateVersion(uuid: UUID?): ValidationResult { - return ValidationResult.failure(String.format("Invalid UUID Version")) + companion object { + fun getValidator(uuid: UUID): UuidValidator { + return if (uuid.isUuidv6()) { + UUIDv6Validator + } else if (uuid.isUProtocol()) { + UUIDv8Validator + } else { + InvalidValidator + } } + } +} - override fun validateVariant(uuid: UUID?): ValidationResult { - return ValidationResult.failure(String.format("Invalid UUID Variant")) - } +data object InvalidValidator : UuidValidator() { + override fun validateVersion(uuid: UUID): ValidationResult { + return ValidationResult.failure("Invalid UUID Version") } - private class UUIDv6Validator : UuidValidator() { - override fun validateVersion(uuid: UUID?): ValidationResult { - val version: Optional = UuidUtils.getVersion(uuid) - return if (version.isPresent && version.get() == UuidUtils.Version.VERSION_TIME_ORDERED) ValidationResult.success() else ValidationResult.failure( - String.format("Not a UUIDv6 Version") - ) - } + override fun validateVariant(uuid: UUID): ValidationResult { + return ValidationResult.failure("Invalid UUID Variant") + } +} - override fun validateVariant(uuid: UUID?): ValidationResult { - val variant: Optional = UuidUtils.getVariant(uuid) - return if (variant.isPresent && variant.get() == UuidVariant.VARIANT_RFC_4122.value) ValidationResult.success() else ValidationResult.failure( - String.format("Invalid UUIDv6 variant") - ) +data object UUIDv6Validator : UuidValidator() { + override fun validateVersion(uuid: UUID): ValidationResult { + return if (uuid.getVersion() == UUIDVersion.VERSION_TIME_ORDERED) { + ValidationResult.success() + } else { + ValidationResult.failure("Not a UUIDv6 Version") } } - private class UUIDv8Validator : UuidValidator() { - override fun validateVersion(uuid: UUID?): ValidationResult { - val version: Optional = UuidUtils.getVersion(uuid) - return if (version.isPresent && version.get() == UuidUtils.Version.VERSION_UPROTOCOL) ValidationResult.success() else ValidationResult.failure( - String.format("Invalid UUIDv8 Version") - ) + override fun validateVariant(uuid: UUID): ValidationResult { + return if (uuid.getVariant() == UuidVariant.VARIANT_RFC_4122.value) { + ValidationResult.success() + } else { + ValidationResult.failure("Invalid UUIDv6 variant") } + } +} - override fun validateVariant(uuid: UUID?): ValidationResult { - return ValidationResult.success() +data object UUIDv8Validator : UuidValidator() { + override fun validateVersion(uuid: UUID): ValidationResult { + return if (uuid.getVersion() == UUIDVersion.VERSION_UPROTOCOL) { + ValidationResult.success() + } else { + ValidationResult.failure("Invalid UUIDv8 Version") } } - companion object { - fun getValidator(uuid: UUID?): UuidValidator { - return if (UuidUtils.isUuidv6(uuid)) { - Validators.UUIDV6.validator() - } else if (UuidUtils.isUProtocol(uuid)) { - Validators.UPROTOCOL.validator() - } else { - Validators.UNKNOWN.validator() - } - } + override fun validateVariant(uuid: UUID): ValidationResult { + return ValidationResult.success() } -} \ No newline at end of file +} + + + diff --git a/src/main/kotlin/org/eclipse/uprotocol/validation/ValidationResult.kt b/src/main/kotlin/org/eclipse/uprotocol/validation/ValidationResult.kt index 146c91b..3a730d1 100644 --- a/src/main/kotlin/org/eclipse/uprotocol/validation/ValidationResult.kt +++ b/src/main/kotlin/org/eclipse/uprotocol/validation/ValidationResult.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 General Motors GTO LLC + * Copyright (c) 2024 General Motors GTO LLC * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -34,17 +34,6 @@ import java.util.* */ sealed class ValidationResult { - companion object { - val STATUS_SUCCESS: UStatus = uStatus { - code = UCode.OK - message = "OK" - } - private val SUCCESS: ValidationResult = Success - - fun success(): ValidationResult = SUCCESS - fun failure(message: String): ValidationResult = Failure(message) - } - fun isFailure(): Boolean { return !isSuccess() } @@ -96,4 +85,15 @@ sealed class ValidationResult { return "ValidationResult.Success()" } } + + companion object { + val STATUS_SUCCESS: UStatus = uStatus { + code = UCode.OK + message = "OK" + } + private val SUCCESS: ValidationResult = Success + + fun success(): ValidationResult = SUCCESS + fun failure(message: String): ValidationResult = Failure(message) + } } diff --git a/src/test/kotlin/org/eclipse/uprotocol/cloudevent/factory/UCloudEventTest.kt b/src/test/kotlin/org/eclipse/uprotocol/cloudevent/factory/UCloudEventTest.kt index fd99e0c..59de53b 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/cloudevent/factory/UCloudEventTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/cloudevent/factory/UCloudEventTest.kt @@ -27,7 +27,7 @@ import io.cloudevents.CloudEventData import io.cloudevents.core.builder.CloudEventBuilder import org.eclipse.uprotocol.cloudevent.datamodel.UCloudEventAttributes import org.eclipse.uprotocol.uri.serializer.LongUriSerializer -import org.eclipse.uprotocol.uuid.factory.UuidFactory +import org.eclipse.uprotocol.uuid.factory.UUIDV8 import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer import org.eclipse.uprotocol.v1.* import org.eclipse.uprotocol.v1.UUID @@ -278,8 +278,8 @@ internal class UCloudEventTest { @Test @DisplayName("Test extracting creation timestamp from the CloudEvent UUIDV8 id when the id is valid.") fun test_extract_creation_timestamp_from_cloudevent_UUIDV8_Id_when_UUIDV8_id_is_valid() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) val cloudEvent: CloudEvent = builder.build() @@ -363,8 +363,8 @@ internal class UCloudEventTest { @Test @DisplayName("Test if the CloudEvent is not expired when no ttl is configured.") fun test_cloudevent_is_not_expired_when_no_ttl_configured() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withoutExtension("ttl").withId(strUuid) val cloudEvent: CloudEvent = builder.build() assertFalse(UCloudEvent.isExpired(cloudEvent)) @@ -373,8 +373,8 @@ internal class UCloudEventTest { @Test @DisplayName("Test if the CloudEvent is not expired when configured ttl is zero.") fun test_cloudevent_is_not_expired_when_ttl_is_zero() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withExtension("ttl", 0).withId(strUuid) val cloudEvent: CloudEvent = builder.build() assertFalse(UCloudEvent.isExpired(cloudEvent)) @@ -383,8 +383,8 @@ internal class UCloudEventTest { @Test @DisplayName("Test if the CloudEvent is not expired when configured ttl is minus one.") fun test_cloudevent_is_not_expired_when_ttl_is_minus_one() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withExtension("ttl", -1).withId(strUuid) val cloudEvent: CloudEvent = builder.build() assertFalse(UCloudEvent.isExpired(cloudEvent)) @@ -393,8 +393,8 @@ internal class UCloudEventTest { @Test @DisplayName("Test if the CloudEvent is not expired when configured ttl is large number.") fun test_cloudevent_is_not_expired_when_ttl_is_large_number_mili() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withExtension("ttl", Integer.MAX_VALUE).withId(strUuid) val cloudEvent: CloudEvent = builder.build() @@ -407,8 +407,8 @@ internal class UCloudEventTest { InterruptedException::class ) fun test_cloudevent_is_expired_when_ttl_1_mili() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withExtension("ttl", 1).withId(strUuid) val cloudEvent: CloudEvent = builder.build() Thread.sleep(800) @@ -426,8 +426,8 @@ internal class UCloudEventTest { @Test @DisplayName("Test if the CloudEvent has a UUIDV8 id.") fun test_cloudevent_has_a_UUIDV8_id() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) val cloudEvent: CloudEvent = builder.build() assertTrue(UCloudEvent.isCloudEventId(cloudEvent)) @@ -441,7 +441,7 @@ internal class UCloudEventTest { msb = uuidJava.mostSignificantBits lsb = uuidJava.leastSignificantBits } - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withExtension("ttl", 3).withId(strUuid) val cloudEvent: CloudEvent = builder.build() assertFalse(UCloudEvent.isCloudEventId(cloudEvent)) @@ -737,7 +737,7 @@ internal class UCloudEventTest { val cloudEvent = CloudEventFactory.response( buildSourceForTest(), "//bo.cloud/petapp/1/rpc" + ".response", - LongUuidSerializer.instance().serialize(UuidFactory.Factories.UPROTOCOL.factory().create()), + LongUuidSerializer.INSTANCE.serialize(UUIDV8()), buildProtoPayloadForTest(), uCloudEventAttributes ) @@ -745,7 +745,7 @@ internal class UCloudEventTest { assertNotNull(result) assertTrue(UCloudEvent.getRequestId(cloudEvent).isPresent) assertEquals( - UCloudEvent.getRequestId(cloudEvent).get(), LongUuidSerializer.instance().serialize(result.attributes.reqid) + UCloudEvent.getRequestId(cloudEvent).get(), LongUuidSerializer.INSTANCE.serialize(result.attributes.reqid) ) assertTrue(UCloudEvent.getTtl(cloudEvent).isPresent) assertEquals(UCloudEvent.getTtl(cloudEvent).get(), result.attributes.ttl) @@ -775,9 +775,9 @@ internal class UCloudEventTest { val protoPayload = buildProtoPayloadForTest() val cloudEventBuilder = CloudEventFactory.buildBaseCloudEvent( - LongUuidSerializer.instance().serialize( - UuidFactory.Factories.UPROTOCOL.factory().create() - ), buildSourceForTest(), protoPayload.toByteArray(), protoPayload.getTypeUrl(), uCloudEventAttributes + LongUuidSerializer.INSTANCE.serialize( + UUIDV8() + ), buildSourceForTest(), protoPayload.toByteArray(), protoPayload.typeUrl, uCloudEventAttributes ) cloudEventBuilder.withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) .withExtension("commstatus", UCode.ABORTED_VALUE).withExtension("plevel", 2) @@ -800,9 +800,9 @@ internal class UCloudEventTest { .build() val protoPayload = buildProtoPayloadForTest() val cloudEventBuilder = CloudEventFactory.buildBaseCloudEvent( - LongUuidSerializer.instance().serialize( - UuidFactory.Factories.UPROTOCOL.factory().create() - ), buildSourceForTest(), protoPayload.toByteArray(), protoPayload.getTypeUrl(), uCloudEventAttributes + LongUuidSerializer.INSTANCE.serialize( + UUIDV8() + ), buildSourceForTest(), protoPayload.toByteArray(), protoPayload.typeUrl, uCloudEventAttributes ) cloudEventBuilder.withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) @@ -869,9 +869,9 @@ internal class UCloudEventTest { val uCloudEventAttributes = UCloudEventAttributes.UCloudEventAttributesBuilder().withTtl(3).build() val protoPayload = buildProtoPayloadForTest() val cloudEventBuilder = CloudEventFactory.buildBaseCloudEvent( - LongUuidSerializer.instance().serialize( - UuidFactory.Factories.UPROTOCOL.factory().create() - ), buildSourceForTest(), protoPayload.toByteArray(), protoPayload.getTypeUrl(), uCloudEventAttributes + LongUuidSerializer.INSTANCE.serialize( + UUIDV8() + ), buildSourceForTest(), protoPayload.toByteArray(), protoPayload.typeUrl, uCloudEventAttributes ) cloudEventBuilder.withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) cloudEventBuilder.withExtension("priority", "CS4") diff --git a/src/test/kotlin/org/eclipse/uprotocol/cloudevent/validate/CloudEventValidatorTest.kt b/src/test/kotlin/org/eclipse/uprotocol/cloudevent/validate/CloudEventValidatorTest.kt index 6315b96..70beac3 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/cloudevent/validate/CloudEventValidatorTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/cloudevent/validate/CloudEventValidatorTest.kt @@ -27,7 +27,8 @@ import org.eclipse.uprotocol.cloudevent.datamodel.UCloudEventAttributes import org.eclipse.uprotocol.cloudevent.factory.CloudEventFactory import org.eclipse.uprotocol.cloudevent.factory.UCloudEvent import org.eclipse.uprotocol.uri.serializer.LongUriSerializer -import org.eclipse.uprotocol.uuid.factory.UuidFactory +import org.eclipse.uprotocol.uuid.factory.UUIDV6 +import org.eclipse.uprotocol.uuid.factory.UUIDV8 import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer import org.eclipse.uprotocol.v1.* import org.eclipse.uprotocol.validation.ValidationResult @@ -147,8 +148,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test validate version") fun validate_cloud_event_version_when_valid() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) .withId(strUuid) @@ -174,8 +175,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test validate cloudevent id when valid") fun validate_cloud_event_id_when_valid() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) .withId(strUuid) @@ -192,7 +193,7 @@ internal class CloudEventValidatorTest { msb = uuidJava.mostSignificantBits lsb = uuidJava.leastSignificantBits } - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) .withId(strUuid) @@ -219,8 +220,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test local Publish type CloudEvent is valid everything is valid") fun test_publish_type_cloudevent_is_valid_when_everything_is_valid_local() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("/body.access/1/door.front_left#Door")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) @@ -233,8 +234,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test microRemote Publish type CloudEvent is valid everything is valid") fun test_publish_type_cloudevent_is_valid_when_everything_is_valid_remote() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("//VCU.myvin/body.access/1/door.front_left#Door")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) @@ -247,8 +248,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test microRemote Publish type CloudEvent is valid everything is valid with a sink") fun test_publish_type_cloudevent_is_valid_when_everything_is_valid_remote_with_a_sink() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("//VCU.myvin/body.access/1/door.front_left#Door")) .withExtension("sink", "//bo.cloud/petapp") @@ -262,8 +263,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test microRemote Publish type CloudEvent is not valid everything is valid with invalid sink") fun test_publish_type_cloudevent_is_not_valid_when_remote_with_invalid_sink() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("//VCU.myvin/body.access/1/door.front_left#Door")) .withExtension("sink", "//bo.cloud").withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) @@ -278,8 +279,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Publish type CloudEvent is not valid when source is empty") fun test_publish_type_cloudevent_is_not_valid_when_source_is_empty() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid).withSource(URI.create("/")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) val cloudEvent: CloudEvent = builder.build() @@ -321,8 +322,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Notification type CloudEvent is valid everything is valid") fun test_notification_type_cloudevent_is_valid_when_everything_is_valid() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("/body.access/1/door.front_left#Door")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) @@ -336,8 +337,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Notification type CloudEvent is not valid missing sink") fun test_notification_type_cloudevent_is_not_valid_missing_sink() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("/body.access/1/door.front_left#Door")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)) @@ -350,8 +351,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Notification type CloudEvent is not valid invalid sink") fun test_notification_type_cloudevent_is_not_valid_invalid_sink() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("/body.access/1/door.front_left#Door")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)).withExtension("sink", "//bo.cloud") @@ -367,8 +368,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Request type CloudEvent is valid everything is valid") fun test_request_type_cloudevent_is_valid_when_everything_is_valid() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("//bo.cloud/petapp//rpc.response")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_REQUEST)) @@ -382,8 +383,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Request type CloudEvent is not valid invalid source") fun test_request_type_cloudevent_is_not_valid_invalid_source() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid).withSource(URI.create("//bo.cloud/petapp//dog")) .withExtension("sink", "//VCU.myvin/body.access/1/rpc.UpdateDoor") @@ -400,8 +401,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Request type CloudEvent is not valid missing sink") fun test_request_type_cloudevent_is_not_valid_missing_sink() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("//bo.cloud/petapp//rpc.response")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_REQUEST)) @@ -417,8 +418,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Request type CloudEvent is not valid sink not rpc command") fun test_request_type_cloudevent_is_not_valid_invalid_sink_not_rpc_command() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("//bo.cloud/petapp//rpc.response")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_REQUEST)) @@ -435,8 +436,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Response type CloudEvent is valid everything is valid") fun test_response_type_cloudevent_is_valid_when_everything_is_valid() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("//VCU.myvin/body.access/1/rpc.UpdateDoor")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE)) @@ -450,8 +451,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Response type CloudEvent is not valid invalid source") fun test_response_type_cloudevent_is_not_valid_invalid_source() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("//VCU.myvin/body.access/1/UpdateDoor")) .withExtension("sink", "//bo.cloud/petapp//rpc.response") @@ -468,8 +469,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Response type CloudEvent is not valid missing sink and invalid source") fun test_response_type_cloudevent_is_not_valid_missing_sink_and_invalid_source() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withSource(URI.create("//VCU.myvin/body.access/1/UpdateDoor")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE)) @@ -485,8 +486,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Response type CloudEvent is not valid sink and source, missing entity name.") fun test_response_type_cloudevent_is_not_valid_invalid_sink() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE)) .withSource(URI.create("//VCU.myvin")).withExtension("sink", "//bo.cloud") @@ -502,8 +503,8 @@ internal class CloudEventValidatorTest { @Test @DisplayName("Test Response type CloudEvent is not valid source not rpc command") fun test_response_type_cloudevent_is_not_valid_invalid_source_not_rpc_command() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val strUuid = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8() + val strUuid = LongUuidSerializer.INSTANCE.serialize(uuid) val builder: CloudEventBuilder = buildBaseCloudEventBuilderForTest().withId(strUuid).withSource(URI.create("//bo.cloud/petapp/1/dog")) .withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE)) @@ -553,8 +554,8 @@ internal class CloudEventValidatorTest { // source val source = buildLongUriForTest() - val uuid: UUID = UuidFactory.Factories.UUIDV6.factory().create() - val id = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV6() + val id = LongUuidSerializer.INSTANCE.serialize(uuid) // fake payload val protoPayload = buildProtoPayloadForTest() @@ -579,8 +580,8 @@ internal class CloudEventValidatorTest { // source val source = buildLongUriForTest() - val uuid: UUID = UuidFactory.Factories.UUIDV6.factory().create(Instant.now().minusSeconds(100)) - val id = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV6(Instant.now().minusSeconds(100)) + val id = LongUuidSerializer.INSTANCE.serialize(uuid) // fake payload val protoPayload = buildProtoPayloadForTest() diff --git a/src/test/kotlin/org/eclipse/uprotocol/transport/validator/UAttributesValidatorTest.kt b/src/test/kotlin/org/eclipse/uprotocol/transport/validator/UAttributesValidatorTest.kt index 674bf8e..45d49ea 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/transport/validator/UAttributesValidatorTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/transport/validator/UAttributesValidatorTest.kt @@ -24,7 +24,7 @@ import org.eclipse.uprotocol.transport.builder.UAttributesBuilder import org.eclipse.uprotocol.transport.validate.UAttributesValidator import org.eclipse.uprotocol.uri.factory.UResourceBuilder.forRpcResponse import org.eclipse.uprotocol.uri.serializer.LongUriSerializer -import org.eclipse.uprotocol.uuid.factory.UuidFactory +import org.eclipse.uprotocol.uuid.factory.UUIDV8 import org.eclipse.uprotocol.v1.* import org.eclipse.uprotocol.validation.ValidationResult import org.junit.jupiter.api.Assertions.* @@ -49,7 +49,7 @@ internal class UAttributesValidatorTest { val response: UAttributesValidator = UAttributesValidator.getValidator( UAttributesBuilder.response( source, sink, - UPriority.UPRIORITY_CS4, UuidFactory.Factories.UPROTOCOL.factory().create() + UPriority.UPRIORITY_CS4, UUIDV8() ).build() ) assertEquals("UAttributesValidator.Response", response.toString()) @@ -70,7 +70,7 @@ internal class UAttributesValidatorTest { fun test_validate_uAttributes_for_publish_message_payload_all_values() { val attributes: UAttributes = UAttributesBuilder.publish(source, UPriority.UPRIORITY_CS0).withTtl(1000).withSink(sink) - .withPermissionLevel(2).withCommStatus(3).withReqId(UuidFactory.Factories.UPROTOCOL.factory().create()) + .withPermissionLevel(2).withCommStatus(3).withReqId(UUIDV8()) .build() val validator: UAttributesValidator = UAttributesValidator.Validators.PUBLISH.validator() val status: ValidationResult = validator.validate(attributes) @@ -83,7 +83,7 @@ internal class UAttributesValidatorTest { fun test_validate_uAttributes_for_publish_message_payload_invalid_type() { val attributes: UAttributes = UAttributesBuilder.response( source, sink, - UPriority.UPRIORITY_CS0, UuidFactory.Factories.UPROTOCOL.factory().create() + UPriority.UPRIORITY_CS0, UUIDV8() ).build() val validator: UAttributesValidator = UAttributesValidator.Validators.PUBLISH.validator() val status: ValidationResult = validator.validate(attributes) @@ -168,7 +168,7 @@ internal class UAttributesValidatorTest { fun test_validate_uAttributes_for_rpc_request_message_payload_all_values() { val attributes: UAttributes = UAttributesBuilder.request(source, sink, UPriority.UPRIORITY_CS4, 1000).withPermissionLevel(2) - .withCommStatus(3).withReqId(UuidFactory.Factories.UPROTOCOL.factory().create()).build() + .withCommStatus(3).withReqId(UUIDV8()).build() val validator: UAttributesValidator = UAttributesValidator.Validators.REQUEST.validator() val status: ValidationResult = validator.validate(attributes) assertTrue(status.isSuccess()) @@ -180,7 +180,7 @@ internal class UAttributesValidatorTest { fun test_validate_uAttributes_for_rpc_request_message_payload_invalid_type() { val attributes: UAttributes = UAttributesBuilder.response( source, sink, - UPriority.UPRIORITY_CS4, UuidFactory.Factories.UPROTOCOL.factory().create() + UPriority.UPRIORITY_CS4, UUIDV8() ).withTtl(1000).build() val validator: UAttributesValidator = UAttributesValidator.Validators.REQUEST.validator() val status: ValidationResult = validator.validate(attributes) @@ -257,7 +257,7 @@ internal class UAttributesValidatorTest { fun test_validate_uAttributes_for_rpc_response_message_payload() { val attributes: UAttributes = UAttributesBuilder.response( source, sink, - UPriority.UPRIORITY_CS4, UuidFactory.Factories.UPROTOCOL.factory().create() + UPriority.UPRIORITY_CS4, UUIDV8() ).build() val validator: UAttributesValidator = UAttributesValidator.Validators.RESPONSE.validator() val status: ValidationResult = validator.validate(attributes) @@ -270,7 +270,7 @@ internal class UAttributesValidatorTest { fun test_validate_uAttributes_for_rpc_response_message_payload_all_values() { val attributes: UAttributes = UAttributesBuilder.response( source, sink, - UPriority.UPRIORITY_CS4, UuidFactory.Factories.UPROTOCOL.factory().create() + UPriority.UPRIORITY_CS4, UUIDV8() ).withPermissionLevel(2).withCommStatus(3).build() val validator: UAttributesValidator = UAttributesValidator.Validators.RESPONSE.validator() val status: ValidationResult = validator.validate(attributes) @@ -294,7 +294,7 @@ internal class UAttributesValidatorTest { fun test_validate_uAttributes_for_rpc_response_message_payload_invalid_ttl() { val attributes: UAttributes = UAttributesBuilder.response( source, sink, - UPriority.UPRIORITY_CS4, UuidFactory.Factories.UPROTOCOL.factory().create() + UPriority.UPRIORITY_CS4, UUIDV8() ).withTtl(-1).build() val validator: UAttributesValidator = UAttributesValidator.Validators.RESPONSE.validator() val status: ValidationResult = validator.validate(attributes) @@ -326,7 +326,7 @@ internal class UAttributesValidatorTest { fun test_validate_uAttributes_for_rpc_response_message_payload_invalid_permission_level() { val attributes: UAttributes = UAttributesBuilder.response( source, sink, - UPriority.UPRIORITY_CS4, UuidFactory.Factories.UPROTOCOL.factory().create() + UPriority.UPRIORITY_CS4, UUIDV8() ).withPermissionLevel(-42).build() val validator: UAttributesValidator = UAttributesValidator.Validators.RESPONSE.validator() val status: ValidationResult = validator.validate(attributes) @@ -339,7 +339,7 @@ internal class UAttributesValidatorTest { fun test_validate_uAttributes_for_rpc_response_message_payload_invalid_communication_status() { val attributes: UAttributes = UAttributesBuilder.response( source, sink, - UPriority.UPRIORITY_CS4, UuidFactory.Factories.UPROTOCOL.factory().create() + UPriority.UPRIORITY_CS4, UUIDV8() ).withCommStatus(-42).build() val validator: UAttributesValidator = UAttributesValidator.Validators.RESPONSE.validator() val status: ValidationResult = validator.validate(attributes) @@ -489,7 +489,7 @@ internal class UAttributesValidatorTest { @DisplayName("test validating valid ReqId attribute") fun test_validating_valid_ReqId_attribute() { val attributes: UAttributes = UAttributesBuilder.publish(source, UPriority.UPRIORITY_CS0) - .withReqId(UuidFactory.Factories.UPROTOCOL.factory().create()).build() + .withReqId(UUIDV8()).build() val validator: UAttributesValidator = UAttributesValidator.Validators.PUBLISH.validator() val status: ValidationResult = validator.validateReqId(attributes) assertEquals(ValidationResult.success(), status) @@ -593,7 +593,7 @@ internal class UAttributesValidatorTest { source, LongUriSerializer.instance().deserialize("/hartley/1/rpc.response"), UPriority.UPRIORITY_CS6, - UuidFactory.Factories.UPROTOCOL.factory().create() + UUIDV8() ).withTtl(-1).build() val validator: UAttributesValidator = UAttributesValidator.Validators.RESPONSE.validator() assertEquals("UAttributesValidator.Response", validator.toString()) @@ -671,7 +671,7 @@ internal class UAttributesValidatorTest { source, sink, UPriority.UPRIORITY_CS0, - UuidFactory.Factories.UPROTOCOL.factory().create() + UUIDV8() ).build() val validator = UAttributesValidator.getValidator(attributes) assertEquals("UAttributesValidator.Response", validator.toString()) @@ -687,7 +687,7 @@ internal class UAttributesValidatorTest { source, sink, UPriority.UPRIORITY_CS0, - UuidFactory.Factories.UPROTOCOL.factory().create() + UUIDV8() ).build() val validator = UAttributesValidator.getValidator(attributes) assertEquals("UAttributesValidator.Response", validator.toString()) diff --git a/src/test/kotlin/org/eclipse/uprotocol/uri/factory/UResourceBuilderTest.kt b/src/test/kotlin/org/eclipse/uprotocol/uri/factory/UResourceBuilderTest.kt index 1d0dfb0..dc449ad 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/uri/factory/UResourceBuilderTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/uri/factory/UResourceBuilderTest.kt @@ -21,13 +21,9 @@ package org.eclipse.uprotocol.uri.factory -import com.google.protobuf.* -import org.eclipse.uprotocol.core.usubscription.v3.USubscriptionProto import org.eclipse.uprotocol.uri.factory.UResourceBuilder.forRpcRequest import org.eclipse.uprotocol.uri.factory.UResourceBuilder.forRpcResponse import org.eclipse.uprotocol.uri.factory.UResourceBuilder.fromId -import org.eclipse.uprotocol.uri.factory.UResourceBuilder.fromProto -import org.eclipse.uprotocol.v1.* import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test import kotlin.random.Random diff --git a/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/LongUriSerializerTest.kt b/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/LongUriSerializerTest.kt index 9ea7675..a617e75 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/LongUriSerializerTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/LongUriSerializerTest.kt @@ -21,7 +21,10 @@ package org.eclipse.uprotocol.uri.serializer import org.eclipse.uprotocol.uri.factory.UResourceBuilder -import org.eclipse.uprotocol.uri.validator.UriValidator +import org.eclipse.uprotocol.uri.validator.isEmpty +import org.eclipse.uprotocol.uri.validator.isLongForm +import org.eclipse.uprotocol.uri.validator.isRemote +import org.eclipse.uprotocol.uri.validator.isResolved import org.eclipse.uprotocol.v1.* import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.DisplayName @@ -48,9 +51,9 @@ class LongUriSerializerTest { @DisplayName("Test parse uProtocol uri that is null") fun test_parse_protocol_uri_when_is_null() { val uri: UUri = LongUriSerializer.instance().deserialize(null) - assertTrue(UriValidator.isEmpty(uri)) - assertFalse(UriValidator.isResolved(uri)) - assertFalse(UriValidator.isLongForm(uri)) + assertTrue(uri.isEmpty()) + assertFalse(uri.isResolved()) + assertFalse(uri.isLongForm()) } @Test @@ -58,7 +61,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_when_is_empty_string() { val uri = "" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) val uri2: String = LongUriSerializer.instance().serialize(null) assertTrue(uri2.isEmpty()) } @@ -69,7 +72,7 @@ class LongUriSerializerTest { val uri = "/" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) assertFalse(uuri.hasAuthority()) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) assertFalse(uuri.hasResource()) assertFalse(uuri.hasEntity()) val uri2: String = LongUriSerializer.instance().serialize(uUri { }) @@ -84,7 +87,7 @@ class LongUriSerializerTest { assertFalse(uuri.hasAuthority()) assertFalse(uuri.hasResource()) assertFalse(uuri.hasEntity()) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) } @Test @@ -95,7 +98,7 @@ class LongUriSerializerTest { assertFalse(uuri.hasAuthority()) assertFalse(uuri.hasResource()) assertFalse(uuri.hasEntity()) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) assertNotEquals("body.access", uuri.entity.name) assertEquals(0, uuri.entity.versionMajor) } @@ -105,7 +108,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_schema_and_4_slash_and_something() { val uri = "////body.access" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertFalse(uuri.hasResource()) assertFalse(uuri.hasEntity()) assertTrue(uuri.entity.name.isBlank()) @@ -117,10 +120,10 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_schema_and_5_slash_and_something() { val uri = "/////body.access" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertFalse(uuri.hasResource()) assertFalse(uuri.hasEntity()) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) } @Test @@ -128,8 +131,8 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_schema_and_6_slash_and_something() { val uri = "//////body.access" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) - assertTrue(UriValidator.isEmpty(uuri)) + assertFalse(uuri.authority.isRemote()) + assertTrue(uuri.isEmpty()) } @Test @@ -137,7 +140,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_local_service_no_version() { val uri = "/body.access" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("body.access", uuri.entity.name) assertEquals(0, uuri.entity.versionMajor) assertEquals(0, uuri.entity.versionMinor) @@ -149,7 +152,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_local_service_with_version() { val uri = "/body.access/1" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("body.access", uuri.entity.name) assertEquals(1, uuri.entity.versionMajor) assertFalse(uuri.hasResource()) @@ -160,7 +163,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_local_service_no_version_with_resource_name_only() { val uri = "/body.access//door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("body.access", uuri.entity.name) assertEquals(0, uuri.entity.versionMajor) assertEquals(0, uuri.entity.versionMinor) @@ -174,7 +177,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_local_service_with_version_with_resource_name_only() { val uri = "/body.access/1/door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("body.access", uuri.entity.name) assertEquals(1, uuri.entity.versionMajor) assertEquals("door", uuri.resource.name) @@ -187,7 +190,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_local_service_no_version_with_resource_with_instance() { val uri = "/body.access//door.front_left" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("body.access", uuri.entity.name) assertEquals(0, uuri.entity.versionMajor) assertEquals("door", uuri.resource.name) @@ -201,7 +204,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_local_service_with_version_with_resource_with_getMessage() { val uri = "/body.access/1/door.front_left" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("body.access", uuri.entity.name) assertNotEquals(0, uuri.entity.versionMajor) assertEquals(1, uuri.entity.versionMajor) @@ -216,7 +219,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_local_service_no_version_with_resource_with_instance_and_getMessage() { val uri = "/body.access//door.front_left#Door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("body.access", uuri.entity.name) assertEquals(0, uuri.entity.versionMajor) assertEquals("door", uuri.resource.name) @@ -231,7 +234,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_local_service_with_version_with_resource_with_instance_and_getMessage() { val uri = "/body.access/1/door.front_left#Door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("body.access", uuri.entity.name) assertNotEquals(0, uuri.entity.versionMajor) assertEquals(1, uuri.entity.versionMajor) @@ -247,7 +250,7 @@ class LongUriSerializerTest { fun test_parse_protocol_rpc_uri_with_local_service_no_version() { val uri = "/petapp//rpc.response" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("petapp", uuri.entity.name) assertEquals(0, uuri.entity.versionMajor) assertEquals("rpc", uuri.resource.name) @@ -261,7 +264,7 @@ class LongUriSerializerTest { fun test_parse_protocol_rpc_uri_with_local_service_with_version() { val uri = "/petapp/1/rpc.response" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("petapp", uuri.entity.name) assertNotEquals(0, uuri.entity.versionMajor) assertEquals(1, uuri.entity.versionMajor) @@ -276,7 +279,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_only_device_and_domain() { val uri = "//VCU.MY_CAR_VIN" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU.MY_CAR_VIN", uuri.authority.name) } @@ -286,7 +289,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_only_device_and_cloud_domain() { val uri = "//cloud.uprotocol.example.com" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("cloud.uprotocol.example.com", uuri.authority.name) assertFalse(uuri.hasEntity()) @@ -298,7 +301,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_no_version() { val uri = "//VCU.MY_CAR_VIN/body.access" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU.MY_CAR_VIN", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -312,7 +315,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_cloud_service_no_version() { val uri = "//cloud.uprotocol.example.com/body.access" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("cloud.uprotocol.example.com", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -326,7 +329,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_with_version() { val uri = "//VCU.MY_CAR_VIN/body.access/1" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU.MY_CAR_VIN", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -341,7 +344,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_cloud_service_with_version() { val uri = "//cloud.uprotocol.example.com/body.access/1" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("cloud.uprotocol.example.com", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -356,7 +359,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_no_version_with_resource_name_only() { val uri = "//VCU.MY_CAR_VIN/body.access//door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU.MY_CAR_VIN", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -372,7 +375,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_cloud_service_no_version_with_resource_name_only() { val uri = "//cloud.uprotocol.example.com/body.access//door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("cloud.uprotocol.example.com", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -388,7 +391,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_with_version_with_resource_name_only() { val uri = "//VCU.MY_CAR_VIN/body.access/1/door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU.MY_CAR_VIN", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -405,7 +408,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_cloud_with_version_with_resource_name_only() { val uri = "//cloud.uprotocol.example.com/body.access/1/door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("cloud.uprotocol.example.com", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -422,7 +425,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_no_version_with_resource_and_instance_no_getMessage() { val uri = "//VCU.MY_CAR_VIN/body.access//door.front_left" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU.MY_CAR_VIN", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -439,7 +442,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_with_version_with_resource_and_instance_no_getMessage() { val uri = "//VCU.MY_CAR_VIN/body.access/1/door.front_left" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU.MY_CAR_VIN", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -457,7 +460,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_no_version_with_resource_and_instance_and_getMessage() { val uri = "//VCU.MY_CAR_VIN/body.access//door.front_left#Door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU.MY_CAR_VIN", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -475,7 +478,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_cloud_service_no_version_with_resource_and_instance_and_getMessage() { val uri = "//cloud.uprotocol.example.com/body.access//door.front_left#Door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("cloud.uprotocol.example.com", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -493,7 +496,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_with_version_with_resource_and_instance_and_getMessage() { val uri = "//VCU.MY_CAR_VIN/body.access/1/door.front_left#Door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU.MY_CAR_VIN", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -512,7 +515,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_cloud_service_with_version_with_resource_and_instance_and_getMessage() { val uri = "//cloud.uprotocol.example.com/body.access/1/door.front_left#Door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("cloud.uprotocol.example.com", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -531,7 +534,7 @@ class LongUriSerializerTest { fun test_parse_protocol_uri_with_remote_service_with_version_with_resource_with_message_device_no_domain() { val uri = "//VCU/body.access/1/door.front_left" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("VCU", uuri.authority.name) assertFalse(uuri.authority.name.isEmpty()) @@ -549,7 +552,7 @@ class LongUriSerializerTest { fun test_parse_protocol_rpc_uri_with_remote_service_no_version() { val uri = "//bo.cloud/petapp//rpc.response" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("bo.cloud", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -566,7 +569,7 @@ class LongUriSerializerTest { fun test_parse_protocol_rpc_uri_with_remote_service_with_version() { val uri = "//bo.cloud/petapp/1/rpc.response" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertFalse(uuri.authority.name.isBlank()) assertEquals("bo.cloud", uuri.authority.name) assertFalse(uuri.authority.name.isBlank()) @@ -1000,7 +1003,7 @@ class LongUriSerializerTest { fun test_parse_local_protocol_uri_with_custom_scheme() { val uri = "custom:/body.access//door.front_left#Door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertFalse(UriValidator.isRemote(uuri.authority)) + assertFalse(uuri.authority.isRemote()) assertEquals("body.access", uuri.entity.name) assertEquals(0, uuri.entity.versionMajor) assertEquals("door", uuri.resource.name) @@ -1016,7 +1019,7 @@ class LongUriSerializerTest { val uri = "custom://vcu.vin/body.access//door.front_left#Door" val uri2 = "//vcu.vin/body.access//door.front_left#Door" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - assertTrue(UriValidator.isRemote(uuri.authority)) + assertTrue(uuri.authority.isRemote()) assertEquals("vcu.vin", uuri.authority.name) assertEquals("body.access", uuri.entity.name) assertEquals(0, uuri.entity.versionMajor) diff --git a/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/MicroUriSerializerTest.kt b/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/MicroUriSerializerTest.kt index 835eb2b..5204e1b 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/MicroUriSerializerTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/MicroUriSerializerTest.kt @@ -22,7 +22,8 @@ package org.eclipse.uprotocol.uri.serializer import com.google.protobuf.ByteString import org.eclipse.uprotocol.uri.factory.UResourceBuilder -import org.eclipse.uprotocol.uri.validator.UriValidator +import org.eclipse.uprotocol.uri.validator.isEmpty +import org.eclipse.uprotocol.uri.validator.isMicroForm import org.eclipse.uprotocol.v1.* import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue @@ -38,7 +39,7 @@ class MicroUriSerializerTest { val bytes: ByteArray = MicroUriSerializer.instance().serialize(UUri.getDefaultInstance()) assertEquals(bytes.size, 0) val uri2: UUri = MicroUriSerializer.instance().deserialize(bytes) - assertTrue(UriValidator.isEmpty(uri2)) + assertTrue(uri2.isEmpty()) } @Test @@ -47,7 +48,7 @@ class MicroUriSerializerTest { val bytes: ByteArray = MicroUriSerializer.instance().serialize(null) assertEquals(bytes.size, 0) val uri2: UUri = MicroUriSerializer.instance().deserialize(null) - assertTrue(UriValidator.isEmpty(uri2)) + assertTrue(uri2.isEmpty()) } @Test @@ -63,7 +64,7 @@ class MicroUriSerializerTest { val bytes: ByteArray = MicroUriSerializer.instance().serialize(uri) val uri2: UUri = MicroUriSerializer.instance().deserialize(bytes) - assertTrue(UriValidator.isMicroForm(uri)) + assertTrue(uri.isMicroForm()) assertTrue(bytes.isNotEmpty()) assertEquals(uri, uri2) } @@ -111,10 +112,10 @@ class MicroUriSerializerTest { fun test_deserialize_bad_microuri_length() { var badMicroUUri = byteArrayOf(0x1, 0x0, 0x0, 0x0, 0x0) var uuri: UUri = MicroUriSerializer.instance().deserialize(badMicroUUri) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) badMicroUUri = byteArrayOf(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) uuri = MicroUriSerializer.instance().deserialize(badMicroUUri) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) } @Test @@ -122,7 +123,7 @@ class MicroUriSerializerTest { fun test_deserialize_bad_microuri_not_version_1() { val badMicroUUri = byteArrayOf(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) val uuri: UUri = MicroUriSerializer.instance().deserialize(badMicroUUri) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) } @Test @@ -130,7 +131,7 @@ class MicroUriSerializerTest { fun test_deserialize_bad_microuri_not_valid_address_type() { val badMicroUUri = byteArrayOf(0x1, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) val uuri: UUri = MicroUriSerializer.instance().deserialize(badMicroUUri) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) } @Test @@ -138,13 +139,13 @@ class MicroUriSerializerTest { fun test_deserialize_bad_microuri_valid_address_type_invalid_length() { var badMicroUUri = byteArrayOf(0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) var uuri: UUri = MicroUriSerializer.instance().deserialize(badMicroUUri) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) badMicroUUri = byteArrayOf(0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) uuri = MicroUriSerializer.instance().deserialize(badMicroUUri) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) badMicroUUri = byteArrayOf(0x1, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) uuri = MicroUriSerializer.instance().deserialize(badMicroUUri) - assertTrue(UriValidator.isEmpty(uuri)) + assertTrue(uuri.isEmpty()) } @Test @@ -164,8 +165,8 @@ class MicroUriSerializerTest { val bytes: ByteArray = MicroUriSerializer.instance().serialize(uri) val uri2: UUri = MicroUriSerializer.instance().deserialize(bytes) assertTrue(bytes.isNotEmpty()) - assertTrue(UriValidator.isMicroForm(uri)) - assertTrue(UriValidator.isMicroForm(uri2)) + assertTrue(uri.isMicroForm()) + assertTrue(uri2.isMicroForm()) assertEquals(uri.toString(), uri2.toString()) assertTrue(uri == uri2) } @@ -190,7 +191,7 @@ class MicroUriSerializerTest { val bytes: ByteArray = MicroUriSerializer.instance().serialize(uri) val uri2: UUri = MicroUriSerializer.instance().deserialize(bytes) - assertTrue(UriValidator.isMicroForm(uri)) + assertTrue(uri.isMicroForm()) assertTrue(bytes.isNotEmpty()) assertTrue(uri == uri2) } @@ -215,7 +216,7 @@ class MicroUriSerializerTest { val bytes: ByteArray = MicroUriSerializer.instance().serialize(uri) val uri2: UUri = MicroUriSerializer.instance().deserialize(bytes) - assertTrue(UriValidator.isMicroForm(uri)) + assertTrue(uri.isMicroForm()) assertTrue(bytes.isNotEmpty()) assertTrue(uri == uri2) } @@ -261,7 +262,7 @@ class MicroUriSerializerTest { val bytes: ByteArray = MicroUriSerializer.instance().serialize(uri) assertEquals(bytes.size, 9 + size) val uri2: UUri = MicroUriSerializer.instance().deserialize(bytes) - assertTrue(UriValidator.isMicroForm(uri)) + assertTrue(uri.isMicroForm()) assertTrue(uri == uri2) } } diff --git a/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/UriSerializerTest.kt b/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/UriSerializerTest.kt index 89b0ba2..485fa23 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/UriSerializerTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/uri/serializer/UriSerializerTest.kt @@ -23,8 +23,8 @@ package org.eclipse.uprotocol.uri.serializer import com.google.protobuf.ByteString import org.eclipse.uprotocol.core.usubscription.v3.Update import org.eclipse.uprotocol.uri.factory.UResourceBuilder.fromProto -import org.eclipse.uprotocol.uri.validator.UriValidator.isEmpty -import org.eclipse.uprotocol.uri.validator.UriValidator.isMicroForm +import org.eclipse.uprotocol.uri.validator.isEmpty +import org.eclipse.uprotocol.uri.validator.isMicroForm import org.eclipse.uprotocol.v1.* import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.DisplayName @@ -60,7 +60,7 @@ class UriSerializerTest { val resolvedUUri: Optional = LongUriSerializer.instance().buildResolved(longuri, microuri) assertTrue(resolvedUUri.isPresent) - assertFalse(isEmpty(resolvedUUri.get())) + assertFalse(resolvedUUri.get().isEmpty()) assertEquals("testauth", resolvedUUri.get().authority.name) assertEquals("neelam", resolvedUUri.get().entity.name) assertEquals(29999, resolvedUUri.get().entity.id) @@ -78,7 +78,7 @@ class UriSerializerTest { val result: Optional = MicroUriSerializer.instance().buildResolved(null, null) assertTrue(result.isPresent) // Assert that the result is empty - assertTrue(isEmpty(result.get())) + assertTrue(result.get().isEmpty()) } @Test @@ -89,7 +89,7 @@ class UriSerializerTest { val result: Optional = MicroUriSerializer.instance().buildResolved(null, ByteArray(0)) assertTrue(result.isPresent) // Assert that the result is empty - assertTrue(isEmpty(result.get())) + assertTrue(result.get().isEmpty()) } @Test @@ -100,7 +100,7 @@ class UriSerializerTest { val result: Optional = MicroUriSerializer.instance().buildResolved("", ByteArray(0)) assertTrue(result.isPresent) // Assert that the result is not empty - assertTrue(isEmpty(result.get())) + assertTrue(result.get().isEmpty()) } @Test @@ -110,7 +110,7 @@ class UriSerializerTest { entity = uEntity { id = 0 } resource = fromProto(Update.Resources.subscriptions) } - assertFalse(isEmpty(uUri)) - assertTrue(isMicroForm(uUri)) + assertFalse(uUri.isEmpty()) + assertTrue(uUri.isMicroForm()) } } diff --git a/src/test/kotlin/org/eclipse/uprotocol/uri/validator/UriValidatorTest.kt b/src/test/kotlin/org/eclipse/uprotocol/uri/validator/UriValidatorTest.kt index 6928f00..e9ea6bd 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/uri/validator/UriValidatorTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/uri/validator/UriValidatorTest.kt @@ -22,7 +22,6 @@ package org.eclipse.uprotocol.uri.validator import org.eclipse.uprotocol.uri.factory.UResourceBuilder.forRpcResponse import org.eclipse.uprotocol.uri.serializer.LongUriSerializer -import org.eclipse.uprotocol.uri.validator.UriValidator.isRemote import org.eclipse.uprotocol.v1.* import org.eclipse.uprotocol.validation.ValidationResult import org.json.JSONArray @@ -41,8 +40,8 @@ internal class UriValidatorTest { @DisplayName("Test validate blank uri") fun test_validate_blank_uri() { val uri: UUri = LongUriSerializer.instance().deserialize(null) - val status: ValidationResult = UriValidator.validate(uri) - assertTrue(UriValidator.isEmpty(uri)) + val status: ValidationResult = uri.validate() + assertTrue(uri.isEmpty()) assertEquals("Uri is empty.", status.getMessage()) } @@ -50,8 +49,8 @@ internal class UriValidatorTest { @DisplayName("Test validate uri with no device name") fun test_validate_uri_with_no_entity_getName() { val uri: UUri = LongUriSerializer.instance().deserialize("//") - val status: ValidationResult = UriValidator.validate(uri) - assertTrue(UriValidator.isEmpty(uri)) + val status: ValidationResult = uri.validate() + assertTrue(uri.isEmpty()) assertEquals("Uri is empty.", status.getMessage()) } @@ -59,7 +58,7 @@ internal class UriValidatorTest { @DisplayName("Test validate uri with uEntity") fun test_validate_uri_with_getEntity() { val uri: UUri = LongUriSerializer.instance().deserialize("/hartley") - val status: ValidationResult = UriValidator.validate(uri) + val status: ValidationResult = uri.validate() assertEquals(ValidationResult.success(), status) } @@ -67,15 +66,15 @@ internal class UriValidatorTest { @DisplayName("Test validate with malformed URI") fun test_validate_with_malformed_uri() { val uri: UUri = LongUriSerializer.instance().deserialize("hartley") - val status: ValidationResult = UriValidator.validate(uri) - assertTrue(UriValidator.isEmpty(uri)) + val status: ValidationResult = uri.validate() + assertTrue(uri.isEmpty()) assertEquals("Uri is empty.", status.getMessage()) } @Test @DisplayName("Test validate with blank UEntity Name") fun test_validate_with_blank_uentity_name_uri() { - val status: ValidationResult = UriValidator.validate(UUri.getDefaultInstance()) + val status: ValidationResult = UUri.getDefaultInstance().validate() assertTrue(status.isFailure()) assertEquals("Uri is empty.", status.getMessage()) } @@ -84,7 +83,7 @@ internal class UriValidatorTest { @DisplayName("Test validateRpcMethod with valid URI") fun test_validateRpcMethod_with_valid_uri() { val uri: UUri = LongUriSerializer.instance().deserialize("/hartley//rpc.echo") - val status: ValidationResult = UriValidator.validateRpcMethod(uri) + val status: ValidationResult = uri.validateRpcMethod() assertEquals(ValidationResult.success(), status) } @@ -92,7 +91,7 @@ internal class UriValidatorTest { @DisplayName("Test validateRpcMethod with invalid URI") fun test_validateRpcMethod_with_invalid_uri() { val uri: UUri = LongUriSerializer.instance().deserialize("/hartley/echo") - val status: ValidationResult = UriValidator.validateRpcMethod(uri) + val status: ValidationResult = uri.validateRpcMethod() assertTrue(status.isFailure()) assertEquals("Uri is empty.", status.getMessage()) } @@ -101,8 +100,8 @@ internal class UriValidatorTest { @DisplayName("Test validateRpcMethod with malformed URI") fun test_validateRpcMethod_with_malformed_uri() { val uri: UUri = LongUriSerializer.instance().deserialize("hartley") - val status: ValidationResult = UriValidator.validateRpcMethod(uri) - assertTrue(UriValidator.isEmpty(uri)) + val status: ValidationResult = uri.validateRpcMethod() + assertTrue(uri.isEmpty()) assertEquals("Uri is empty.", status.getMessage()) } @@ -110,7 +109,7 @@ internal class UriValidatorTest { @DisplayName("Test validateRpcResponse with valid URI") fun test_validateRpcResponse_with_valid_uri() { val uri: UUri = LongUriSerializer.instance().deserialize("/hartley//rpc.response") - val status: ValidationResult = UriValidator.validateRpcResponse(uri) + val status: ValidationResult = uri.validateRpcResponse() assertEquals(ValidationResult.success(), status) } @@ -118,8 +117,8 @@ internal class UriValidatorTest { @DisplayName("Test validateRpcResponse with malformed URI") fun test_validateRpcResponse_with_malformed_uri() { val uri: UUri = LongUriSerializer.instance().deserialize("hartley") - val status: ValidationResult = UriValidator.validateRpcResponse(uri) - assertTrue(UriValidator.isEmpty(uri)) + val status: ValidationResult = uri.validateRpcResponse() + assertTrue(uri.isEmpty()) assertEquals("Uri is empty.", status.getMessage()) } @@ -127,7 +126,7 @@ internal class UriValidatorTest { @DisplayName("Test validateRpcResponse with rpc type") fun test_validateRpcResponse_with_rpc_type() { val uri: UUri = LongUriSerializer.instance().deserialize("/hartley//dummy.wrong") - val status: ValidationResult = UriValidator.validateRpcResponse(uri) + val status: ValidationResult = uri.validateRpcResponse() assertTrue(status.isFailure()) assertEquals("Invalid RPC response type.", status.getMessage()) } @@ -136,7 +135,7 @@ internal class UriValidatorTest { @DisplayName("Test validateRpcResponse with invalid rpc response type") fun test_validateRpcResponse_with_invalid_rpc_response_type() { val uri: UUri = LongUriSerializer.instance().deserialize("/hartley//rpc.wrong") - val status: ValidationResult = UriValidator.validateRpcResponse(uri) + val status: ValidationResult = uri.validateRpcResponse() assertTrue(status.isFailure()) assertEquals("Invalid RPC response type.", status.getMessage()) } @@ -145,7 +144,7 @@ internal class UriValidatorTest { @DisplayName("Test validate topic uri with version, when it is valid microRemote") fun test_topic_uri_with_version_when_it_is_valid_remote() { val uri = "//VCU.MY_CAR_VIN/body.access/1/door.front_left#Door" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isSuccess()) } @@ -153,7 +152,7 @@ internal class UriValidatorTest { @DisplayName("Test validate topic uri no version, when it is valid microRemote") fun test_topic_uri_no_version_when_it_is_valid_remote() { val uri = "//VCU.MY_CAR_VIN/body.access//door.front_left#Door" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isSuccess()) } @@ -161,7 +160,7 @@ internal class UriValidatorTest { @DisplayName("Test validate topic uri with version, when it is valid local") fun test_topic_uri_with_version_when_it_is_valid_local() { val uri = "/body.access/1/door.front_left#Door" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isSuccess()) } @@ -169,7 +168,7 @@ internal class UriValidatorTest { @DisplayName("Test validate topic uri no version, when it is valid local") fun test_topic_uri_no_version_when_it_is_valid_local() { val uri = "/body.access//door.front_left#Door" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isSuccess()) } @@ -177,7 +176,7 @@ internal class UriValidatorTest { @DisplayName("Test validate topic uri is invalid when uri contains nothing but schema") fun test_topic_uri_invalid_when_uri_has_schema_only() { val uri = ":" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isFailure()) } @@ -185,7 +184,7 @@ internal class UriValidatorTest { @DisplayName("Test validate topic uri is invalid when uri contains empty use name local") fun test_topic_uri_invalid_when_uri_has_empty_use_name_local() { val uri = "/" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isFailure()) } @@ -193,7 +192,7 @@ internal class UriValidatorTest { @DisplayName("Test validate topic uri is invalid when uri is microRemote but missing authority") fun test_topic_uri_invalid_when_uri_is_remote_no_authority() { val uri = "//" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isFailure()) } @@ -201,7 +200,7 @@ internal class UriValidatorTest { @DisplayName("Test validate topic uri is invalid when uri is microRemote with use but missing authority") fun test_topic_uri_invalid_when_uri_is_remote_no_authority_with_use() { val uri = "///body.access/1/door.front_left#Door" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isFailure()) } @@ -209,7 +208,7 @@ internal class UriValidatorTest { @DisplayName("Test validate topic uri is invalid when uri has no use information") fun test_topic_uri_invalid_when_uri_is_missing_use_remote() { val uri = "//VCU.myvin///door.front_left#Door" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isFailure()) } @@ -217,7 +216,7 @@ internal class UriValidatorTest { @DisplayName("Test validate microRemote topic uri is invalid when uri is missing use name") fun test_topic_uri_invalid_when_uri_is_missing_use_name_remote() { val uri = "/1/door.front_left#Door" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isFailure()) } @@ -225,7 +224,7 @@ internal class UriValidatorTest { @DisplayName("Test validate local topic uri is invalid when uri is missing use name") fun test_topic_uri_invalid_when_uri_is_missing_use_name_local() { val uri = "//VCU.myvin//1" - val status: ValidationResult = UriValidator.validate(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validate() assertTrue(status.isFailure()) } @@ -233,7 +232,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri with version, when it is valid microRemote") fun test_rpc_topic_uri_with_version_when_it_is_valid_remote() { val uri = "//bo.cloud/petapp/1/rpc.response" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isSuccess()) } @@ -241,7 +240,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri no version, when it is valid microRemote") fun test_rpc_topic_uri_no_version_when_it_is_valid_remote() { val uri = "//bo.cloud/petapp//rpc.response" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isSuccess()) } @@ -249,7 +248,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri with version, when it is valid local") fun test_rpc_topic_uri_with_version_when_it_is_valid_local() { val uri = "/petapp/1/rpc.response" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isSuccess()) } @@ -257,7 +256,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri no version, when it is valid local") fun test_rpc_topic_uri_no_version_when_it_is_valid_local() { val uri = "/petapp//rpc.response" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isSuccess()) } @@ -265,7 +264,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri is invalid when uri contains nothing but schema") fun test_rpc_topic_uri_invalid_when_uri_has_schema_only() { val uri = ":" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -273,7 +272,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri with version, when it is local but missing rpc.respons") fun test_rpc_topic_uri_with_version_when_it_is_not_valid_missing_rpc_response_local() { val uri = "/petapp/1/dog" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -281,7 +280,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri with version, when it is microRemote but missing rpc.respons") fun test_rpc_topic_uri_with_version_when_it_is_not_valid_missing_rpc_response_remote() { val uri = "//petapp/1/dog" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -289,7 +288,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri is invalid when uri is microRemote but missing authority") fun test_rpc_topic_uri_invalid_when_uri_is_remote_no_authority() { val uri = "//" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -297,7 +296,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri is invalid when uri is microRemote with use but missing authority") fun test_rpc_topic_uri_invalid_when_uri_is_remote_no_authority_with_use() { val uri = "///body.access/1" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -305,7 +304,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc topic uri is invalid when uri has no use information") fun test_rpc_topic_uri_invalid_when_uri_is_missing_use() { val uri = "//VCU.myvin" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -313,7 +312,7 @@ internal class UriValidatorTest { @DisplayName("Test validate microRemote rpc topic uri is invalid when uri is missing use name") fun test_rpc_topic_uri_invalid_when_uri_is_missing_use_name_remote() { val uri = "/1" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -321,7 +320,7 @@ internal class UriValidatorTest { @DisplayName("Test validate local rpc topic uri is invalid when uri is missing use name") fun test_rpc_topic_uri_invalid_when_uri_is_missing_use_name_local() { val uri = "//VCU.myvin//1" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -329,7 +328,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc method uri with version, when it is valid microRemote") fun test_rpc_method_uri_with_version_when_it_is_valid_remote() { val uri = "//VCU.myvin/body.access/1/rpc.UpdateDoor" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isSuccess()) } @@ -337,7 +336,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc method uri no version, when it is valid microRemote") fun test_rpc_method_uri_no_version_when_it_is_valid_remote() { val uri = "//VCU.myvin/body.access//rpc.UpdateDoor" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isSuccess()) } @@ -345,7 +344,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc method uri with version, when it is valid local") fun test_rpc_method_uri_with_version_when_it_is_valid_local() { val uri = "/body.access/1/rpc.UpdateDoor" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isSuccess()) } @@ -353,7 +352,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc method uri no version, when it is valid local") fun test_rpc_method_uri_no_version_when_it_is_valid_local() { val uri = "/body.access//rpc.UpdateDoor" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isSuccess()) } @@ -361,7 +360,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc method uri is invalid when uri contains nothing but schema") fun test_rpc_method_uri_invalid_when_uri_has_schema_only() { val uri = ":" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -369,7 +368,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc method uri with version, when it is local but not an rpc method") fun test_rpc_method_uri_with_version_when_it_is_not_valid_not_rpc_method_local() { val uri = "/body.access//UpdateDoor" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -377,7 +376,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc method uri with version, when it is microRemote but not an rpc method") fun test_rpc_method_uri_with_version_when_it_is_not_valid_not_rpc_method_remote() { val uri = "//body.access/1/UpdateDoor" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -385,7 +384,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc method uri is invalid when uri is microRemote but missing authority") fun test_rpc_method_uri_invalid_when_uri_is_remote_no_authority() { val uri = "//" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -394,7 +393,7 @@ internal class UriValidatorTest { fun test_rpc_method_uri_invalid_when_uri_is_remote_no_authority_with_use() { val uri = "///body.access/1/rpc.UpdateDoor" val uuri: UUri = LongUriSerializer.instance().deserialize(uri) - val status: ValidationResult = UriValidator.validateRpcMethod(uuri) + val status: ValidationResult = uuri.validateRpcMethod() assertEquals("", uuri.toString()) assertTrue(status.isFailure()) } @@ -412,7 +411,7 @@ internal class UriValidatorTest { } - val status: ValidationResult = UriValidator.validateRpcMethod(uuri) + val status: ValidationResult = uuri.validateRpcMethod() assertTrue(status.isFailure()) assertEquals("Uri is remote missing uAuthority.", status.getMessage()) } @@ -421,7 +420,7 @@ internal class UriValidatorTest { @DisplayName("Test validate rpc method uri is invalid when uri has no use information") fun test_rpc_method_uri_invalid_when_uri_is_missing_use() { val uri = "//VCU.myvin" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -429,7 +428,7 @@ internal class UriValidatorTest { @DisplayName("Test validate local rpc method uri is invalid when uri is missing use name") fun test_rpc_method_uri_invalid_when_uri_is_missing_use_name_local() { val uri = "/1/rpc.UpdateDoor" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -437,7 +436,7 @@ internal class UriValidatorTest { @DisplayName("Test validate microRemote rpc method uri is invalid when uri is missing use name") fun test_rpc_method_uri_invalid_when_uri_is_missing_use_name_remote() { val uri = "//VCU.myvin//1/rpc.UpdateDoor" - val status: ValidationResult = UriValidator.validateRpcMethod(LongUriSerializer.instance().deserialize(uri)) + val status: ValidationResult = LongUriSerializer.instance().deserialize(uri).validateRpcMethod() assertTrue(status.isFailure()) } @@ -449,7 +448,7 @@ internal class UriValidatorTest { val validUris: JSONArray = jsonObject.getJSONArray("validUris") for (i in 0 until validUris.length()) { val uuri: UUri = LongUriSerializer.instance().deserialize(validUris.getString(i)) - val status: ValidationResult = UriValidator.validate(uuri) + val status: ValidationResult = uuri.validate() assertTrue(status.isSuccess()) } } @@ -463,7 +462,7 @@ internal class UriValidatorTest { for (i in 0 until invalidUris.length()) { val uriObject: JSONObject = invalidUris.getJSONObject(i) val uuri: UUri = LongUriSerializer.instance().deserialize(uriObject.getString("uri")) - val status: ValidationResult = UriValidator.validate(uuri) + val status: ValidationResult = uuri.validate() assertTrue(status.isFailure()) assertEquals(status.getMessage(), uriObject.getString("status_message")) } @@ -477,7 +476,7 @@ internal class UriValidatorTest { val validRpcUris: JSONArray = jsonObject.getJSONArray("validRpcUris") for (i in 0 until validRpcUris.length()) { val uuri: UUri = LongUriSerializer.instance().deserialize(validRpcUris.getString(i)) - val status: ValidationResult = UriValidator.validateRpcMethod(uuri) + val status: ValidationResult = uuri.validateRpcMethod() assertTrue(status.isSuccess()) } } @@ -491,7 +490,7 @@ internal class UriValidatorTest { for (i in 0 until invalidRpcUris.length()) { val uriObject: JSONObject = invalidRpcUris.getJSONObject(i) val uuri: UUri = LongUriSerializer.instance().deserialize(uriObject.getString("uri")) - val status: ValidationResult = UriValidator.validateRpcMethod(uuri) + val status: ValidationResult = uuri.validateRpcMethod() assertTrue(status.isFailure()) assertEquals(status.getMessage(), uriObject.getString("status_message")) } @@ -505,8 +504,8 @@ internal class UriValidatorTest { val validRpcResponseUris: JSONArray = jsonObject.getJSONArray("validRpcResponseUris") for (i in 0 until validRpcResponseUris.length()) { val uuri: UUri = LongUriSerializer.instance().deserialize(validRpcResponseUris.getString(i)) - val status: ValidationResult = UriValidator.validateRpcResponse(uuri) - assertTrue(UriValidator.isRpcResponse(uuri)) + val status: ValidationResult = uuri.validateRpcResponse() + assertTrue(uuri.isRpcResponse()) assertTrue(status.isSuccess()) } } @@ -520,8 +519,8 @@ internal class UriValidatorTest { resource = forRpcResponse() } - val status: ValidationResult = UriValidator.validateRpcResponse(uuri) - assertTrue(UriValidator.isRpcResponse(uuri)) + val status: ValidationResult = uuri.validateRpcResponse() + assertTrue(uuri.isRpcResponse()) assertTrue(status.isSuccess()) } @@ -536,8 +535,8 @@ internal class UriValidatorTest { id = 19999 } } - val status = UriValidator.validateRpcResponse(uuri) - assertFalse(UriValidator.isRpcResponse(uuri)) + val status = uuri.validateRpcResponse() + assertFalse(uuri.isRpcResponse()) assertFalse(status.isSuccess()) } @@ -553,8 +552,8 @@ internal class UriValidatorTest { } } - val status = UriValidator.validateRpcResponse(uuri) - assertFalse(UriValidator.isRpcResponse(uuri)) + val status = uuri.validateRpcResponse() + assertFalse(uuri.isRpcResponse()) assertFalse(status.isSuccess()) } @@ -566,7 +565,7 @@ internal class UriValidatorTest { val invalidRpcResponseUris: JSONArray = jsonObject.getJSONArray("invalidRpcResponseUris") for (i in 0 until invalidRpcResponseUris.length()) { val uuri: UUri = LongUriSerializer.instance().deserialize(invalidRpcResponseUris.getString(i)) - val status: ValidationResult = UriValidator.validateRpcResponse(uuri) + val status: ValidationResult = uuri.validateRpcResponse() assertTrue(status.isFailure()) } } @@ -579,8 +578,8 @@ internal class UriValidatorTest { entity = uEntity { name = "hartley" } resource = forRpcResponse() } - assertFalse(isRemote(UAuthority.getDefaultInstance())) - assertFalse(isRemote(uri.authority)) + assertFalse(UAuthority.getDefaultInstance().isRemote()) + assertFalse(uri.authority.isRemote()) } @get:Throws(IOException::class) diff --git a/src/test/kotlin/org/eclipse/uprotocol/uuid/factory/UUIDFactoryTest.kt b/src/test/kotlin/org/eclipse/uprotocol/uuid/factory/UUIDFactoryTest.kt index 8b00303..a3cf7b5 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/uuid/factory/UUIDFactoryTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/uuid/factory/UUIDFactoryTest.kt @@ -38,47 +38,23 @@ class UUIDFactoryTest { @DisplayName("Test UUIDv8 Creation") fun test_uuidv8_creation() { val now: Instant = Instant.now() - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create(now) - val version: Optional = UuidUtils.getVersion(uuid) - val time: Optional = UuidUtils.getTime(uuid) - val bytes = MicroUuidSerializer.instance().serialize(uuid) - val uuidString = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV8(now) + val version= uuid.getVersion() + val time: Long? = uuid.getTime() + val bytes = MicroUuidSerializer.INSTANCE.serialize(uuid) + val uuidString = LongUuidSerializer.INSTANCE.serialize(uuid) assertNotNull(uuid) - assertTrue(UuidUtils.isUProtocol(uuid)) - assertTrue(UuidUtils.isUuid(uuid)) - assertFalse(UuidUtils.isUuidv6(uuid)) - assertTrue(version.isPresent) - assertTrue(time.isPresent) - assertEquals(time.get(), now.toEpochMilli()) + assertTrue(uuid.isUProtocol()) + assertTrue(uuid.isUuid()) + assertFalse(uuid.isUuidv6()) + assertNotNull(version) + assertNotNull(time) + assertEquals(now.toEpochMilli(), time) assertTrue(bytes.isNotEmpty()) assertFalse(uuidString.isBlank()) - val uuid1: UUID = MicroUuidSerializer.instance().deserialize(bytes) - val uuid2: UUID = LongUuidSerializer.instance().deserialize(uuidString) - assertFalse(uuid1 == UUID.getDefaultInstance()) - assertFalse(uuid2 == UUID.getDefaultInstance()) - assertEquals(uuid, uuid1) - assertEquals(uuid, uuid2) - } - - @Test - @DisplayName("Test UUIDv8 Creation with null Instant") - fun test_uuidv8_creation_with_null_instant() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create(null) - val version: Optional = UuidUtils.getVersion(uuid) - val time: Optional = UuidUtils.getTime(uuid) - val bytes = MicroUuidSerializer.instance().serialize(uuid) - val uuidString = LongUuidSerializer.instance().serialize(uuid) - assertNotNull(uuid) - assertTrue(UuidUtils.isUProtocol(uuid)) - assertTrue(UuidUtils.isUuid(uuid)) - assertFalse(UuidUtils.isUuidv6(uuid)) - assertTrue(version.isPresent) - assertTrue(time.isPresent) - assertTrue(bytes.isNotEmpty()) - assertFalse(uuidString.isBlank()) - val uuid1: UUID = MicroUuidSerializer.instance().deserialize(bytes) - val uuid2: UUID = LongUuidSerializer.instance().deserialize(uuidString) + val uuid1: UUID = MicroUuidSerializer.INSTANCE.deserialize(bytes) + val uuid2: UUID = LongUuidSerializer.INSTANCE.deserialize(uuidString) assertFalse(uuid1 == UUID.getDefaultInstance()) assertFalse(uuid2 == UUID.getDefaultInstance()) assertEquals(uuid, uuid1) @@ -90,20 +66,20 @@ class UUIDFactoryTest { fun test_uuidv8_overflow() { val uuidList: MutableList = ArrayList() - val MAX_COUNT = 4095 + val maxCount = 4095 // Build UUIDs above MAX_COUNT (4095) so we can test the limits val now: Instant = Instant.now() - for (i in 0 until MAX_COUNT * 2) { - uuidList.add(UuidFactory.Factories.UPROTOCOL.factory().create(now)) + for (i in 0 until maxCount * 2) { + uuidList.add(UUIDV8(now)) // Time should be the same as the 1st - assertEquals(UuidUtils.getTime(uuidList[0]), UuidUtils.getTime(uuidList[i])) + assertEquals(uuidList[0].getTime(), uuidList[i].getTime()) // Random should always remain the same be the same assertEquals(uuidList[0].lsb, uuidList[i].lsb) - if (i > MAX_COUNT) { - assertEquals(uuidList[MAX_COUNT].msb, uuidList[i].msb) + if (i > maxCount) { + assertEquals(uuidList[maxCount].msb, uuidList[i].msb) } } } @@ -112,48 +88,22 @@ class UUIDFactoryTest { @DisplayName("Test UUIDv6 creation with Instance") fun test_uuidv6_creation_with_instant() { val now: Instant = Instant.now() - val uuid: UUID = UuidFactory.Factories.UUIDV6.factory().create(now) - val version: Optional = UuidUtils.getVersion(uuid) - val time: Optional = UuidUtils.getTime(uuid) - val bytes = MicroUuidSerializer.instance().serialize(uuid) - val uuidString = LongUuidSerializer.instance().serialize(uuid) - assertNotNull(uuid) - assertTrue(UuidUtils.isUuidv6(uuid)) - assertTrue(UuidUtils.isUuid(uuid)) - assertFalse(UuidUtils.isUProtocol(uuid)) - assertTrue(version.isPresent) - assertTrue(time.isPresent) - assertEquals(time.get(), now.toEpochMilli()) - assertTrue(bytes.isNotEmpty()) - assertFalse(uuidString.isBlank()) - val uuid1: UUID = MicroUuidSerializer.instance().deserialize(bytes) - val uuid2: UUID = LongUuidSerializer.instance().deserialize(uuidString) - assertFalse(uuid1 == UUID.getDefaultInstance()) - assertFalse(uuid2 == UUID.getDefaultInstance()) - assertEquals(uuid, uuid1) - assertEquals(uuid, uuid2) - } - - @Test - @DisplayName("Test UUIDv6 creation with null Instant") - fun test_uuidv6_creation_with_null_instant() { - val uuid: UUID = UuidFactory.Factories.UUIDV6.factory().create(null) - val version: Optional = UuidUtils.getVersion(uuid) - val time: Optional = UuidUtils.getTime(uuid) - val bytes = MicroUuidSerializer.instance().serialize(uuid) - val uuidString = LongUuidSerializer.instance().serialize(uuid) + val uuid: UUID = UUIDV6(now) + val version = uuid.getVersion() + val time: Long? = uuid.getTime() + val bytes = MicroUuidSerializer.INSTANCE.serialize(uuid) + val uuidString = LongUuidSerializer.INSTANCE.serialize(uuid) assertNotNull(uuid) - assertTrue(UuidUtils.isUuidv6(uuid)) - assertFalse(UuidUtils.isUProtocol(uuid)) - assertTrue(UuidUtils.isUuid(uuid)) - assertTrue(version.isPresent) - assertTrue(time.isPresent) + assertTrue(uuid.isUuidv6()) + assertTrue(uuid.isUuid()) + assertFalse(uuid.isUProtocol()) + assertNotNull(version) + assertNotNull(time) + assertEquals(now.toEpochMilli(), time) assertTrue(bytes.isNotEmpty()) assertFalse(uuidString.isBlank()) - - val uuid1: UUID = MicroUuidSerializer.instance().deserialize(bytes) - val uuid2: UUID = LongUuidSerializer.instance().deserialize(uuidString) - + val uuid1: UUID = MicroUuidSerializer.INSTANCE.deserialize(bytes) + val uuid2: UUID = LongUuidSerializer.INSTANCE.deserialize(uuidString) assertFalse(uuid1 == UUID.getDefaultInstance()) assertFalse(uuid2 == UUID.getDefaultInstance()) assertEquals(uuid, uuid1) @@ -168,21 +118,21 @@ class UUIDFactoryTest { msb = uuidJava.mostSignificantBits lsb = uuidJava.leastSignificantBits } - val version: Optional = UuidUtils.getVersion(uuid) - val time: Optional = UuidUtils.getTime(uuid) - val bytes = MicroUuidSerializer.instance().serialize(uuid) - val uuidString = LongUuidSerializer.instance().serialize(uuid) + val version = uuid.getVersion() + val time: Long? = uuid.getTime() + val bytes = MicroUuidSerializer.INSTANCE.serialize(uuid) + val uuidString = LongUuidSerializer.INSTANCE.serialize(uuid) assertNotNull(uuid) - assertFalse(UuidUtils.isUuidv6(uuid)) - assertFalse(UuidUtils.isUProtocol(uuid)) - assertFalse(UuidUtils.isUuid(uuid)) - assertTrue(version.isPresent) - assertFalse(time.isPresent) + assertFalse(uuid.isUuidv6()) + assertFalse(uuid.isUProtocol()) + assertFalse(uuid.isUuid()) + assertNotNull(version) + assertNull(time) assertTrue(bytes.isNotEmpty()) assertFalse(uuidString.isBlank()) - val uuid1: UUID = MicroUuidSerializer.instance().deserialize(bytes) - val uuid2: UUID = LongUuidSerializer.instance().deserialize(uuidString) + val uuid1: UUID = MicroUuidSerializer.INSTANCE.deserialize(bytes) + val uuid2: UUID = LongUuidSerializer.INSTANCE.deserialize(uuidString) assertFalse(uuid1 == UUID.getDefaultInstance()) assertFalse(uuid2 == UUID.getDefaultInstance()) @@ -197,26 +147,23 @@ class UUIDFactoryTest { msb = 0L lsb = 0L } - val version: Optional = UuidUtils.getVersion(uuid) - val time: Optional = UuidUtils.getTime(uuid) - val bytes = MicroUuidSerializer.instance().serialize(uuid) - val uuidString = LongUuidSerializer.instance().serialize(uuid) + val version = uuid.getVersion() + val time: Long? = uuid.getTime() + val bytes = MicroUuidSerializer.INSTANCE.serialize(uuid) + val uuidString = LongUuidSerializer.INSTANCE.serialize(uuid) assertNotNull(uuid) - assertFalse(UuidUtils.isUuidv6(uuid)) - assertFalse(UuidUtils.isUProtocol(uuid)) - assertTrue(version.isPresent) - assertEquals(version.get(), UuidUtils.Version.VERSION_UNKNOWN) - assertFalse(time.isPresent) + assertFalse(uuid.isUuidv6()) + assertFalse(uuid.isUProtocol()) + assertNotNull(version) + assertEquals(UUIDVersion.VERSION_UNKNOWN, version) + assertNull(time) assertTrue(bytes.isNotEmpty()) assertFalse(uuidString.isBlank()) - assertFalse(UuidUtils.isUuidv6(null)) - assertFalse(UuidUtils.isUProtocol(null)) - assertFalse(UuidUtils.isUuid(null)) - val uuid1: UUID = MicroUuidSerializer.instance().deserialize(bytes) + val uuid1: UUID = MicroUuidSerializer.INSTANCE.deserialize(bytes) assertTrue(uuid1 == UUID.getDefaultInstance()) assertEquals(uuid, uuid1) - val uuid2: UUID = LongUuidSerializer.instance().deserialize(uuidString) + val uuid2: UUID = LongUuidSerializer.INSTANCE.deserialize(uuidString) assertTrue(uuid2 == UUID.getDefaultInstance()) assertEquals(uuid, uuid2) } @@ -224,13 +171,8 @@ class UUIDFactoryTest { @Test @DisplayName("Test UuidUtils for a null UUID") fun test_uuidutils_for_null_uuid() { - assertFalse(UuidUtils.getVersion(null).isPresent) - assertTrue(MicroUuidSerializer.instance().serialize(null).isEmpty()) - assertTrue(LongUuidSerializer.instance().serialize(null).isBlank()) - assertFalse(UuidUtils.isUuidv6(null)) - assertFalse(UuidUtils.isUProtocol(null)) - assertFalse(UuidUtils.isUuid(null)) - assertFalse(UuidUtils.getTime(null).isPresent) + assertTrue(MicroUuidSerializer.INSTANCE.serialize(null).isEmpty()) + assertTrue(LongUuidSerializer.INSTANCE.serialize(null).isBlank()) } @Test @@ -240,31 +182,30 @@ class UUIDFactoryTest { msb = (9 shl 12).toLong() lsb = 0L } // Invalid UUID type - assertFalse(UuidUtils.getVersion(uuid).isPresent) - assertFalse(UuidUtils.getTime(uuid).isPresent) - assertTrue(MicroUuidSerializer.instance().serialize(uuid).isNotEmpty()) - assertFalse(LongUuidSerializer.instance().serialize(uuid).isBlank()) - assertFalse(UuidUtils.isUuidv6(uuid)) - assertFalse(UuidUtils.isUProtocol(uuid)) - assertFalse(UuidUtils.isUuid(uuid)) - assertFalse(UuidUtils.getTime(uuid).isPresent) + assertEquals(UUIDVersion.VERSION_UNKNOWN, uuid.getVersion()) + assertNull(uuid.getTime()) + assertTrue(MicroUuidSerializer.INSTANCE.serialize(uuid).isNotEmpty()) + assertFalse(LongUuidSerializer.INSTANCE.serialize(uuid).isBlank()) + assertFalse(uuid.isUuidv6()) + assertFalse(uuid.isUProtocol()) + assertFalse(uuid.isUuid()) } @Test @DisplayName("Test UuidUtils fromString with invalid string") fun test_uuidutils_fromstring_with_invalid_string() { - val uuid: UUID = LongUuidSerializer.instance().deserialize(null) + val uuid: UUID = LongUuidSerializer.INSTANCE.deserialize(null) assertTrue(uuid == UUID.getDefaultInstance()) - val uuid1: UUID = LongUuidSerializer.instance().deserialize("") + val uuid1: UUID = LongUuidSerializer.INSTANCE.deserialize("") assertTrue(uuid1 == UUID.getDefaultInstance()) } @Test @DisplayName("Test UuidUtils fromBytes with invalid bytes") fun test_uuidutils_frombytes_with_invalid_bytes() { - val uuid: UUID = MicroUuidSerializer.instance().deserialize(null) + val uuid: UUID = MicroUuidSerializer.INSTANCE.deserialize(null) assertTrue(uuid == UUID.getDefaultInstance()) - val uuid1: UUID = MicroUuidSerializer.instance().deserialize(ByteArray(0)) + val uuid1: UUID = MicroUuidSerializer.INSTANCE.deserialize(ByteArray(0)) assertTrue(uuid1 == UUID.getDefaultInstance()) } @@ -272,29 +213,29 @@ class UUIDFactoryTest { @DisplayName("Test Create UProtocol UUID in the past") fun test_create_uprotocol_uuid_in_the_past() { val past: Instant = Instant.now().minusSeconds(10) - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create(past) - val time: Optional = UuidUtils.getTime(uuid) - assertTrue(UuidUtils.isUProtocol(uuid)) - assertTrue(UuidUtils.isUuid(uuid)) - assertTrue(time.isPresent) - assertEquals(time.get(), past.toEpochMilli()) + val uuid: UUID = UUIDV8(past) + val time: Long? = uuid.getTime() + assertTrue(uuid.isUProtocol()) + assertTrue(uuid.isUuid()) + assertNotNull(time) + assertEquals(past.toEpochMilli(), time) } @Test @DisplayName("Test Create UProtocol UUID with different time values") @Throws(InterruptedException::class) fun test_create_uprotocol_uuid_with_different_time_values() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() + val uuid: UUID = UUIDV8() Thread.sleep(10) - val uuid1: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val time: Optional = UuidUtils.getTime(uuid) - val time1: Optional = UuidUtils.getTime(uuid1) - assertTrue(UuidUtils.isUProtocol(uuid)) - assertTrue(UuidUtils.isUuid(uuid)) - assertTrue(UuidUtils.isUProtocol(uuid1)) - assertTrue(UuidUtils.isUuid(uuid1)) - assertTrue(time.isPresent) - assertNotEquals(time.get(), time1.get()) + val uuid1: UUID = UUIDV8() + val time: Long? = uuid.getTime() + val time1: Long? = uuid1.getTime() + assertTrue(uuid.isUProtocol()) + assertTrue(uuid.isUuid()) + assertTrue(uuid1.isUProtocol()) + assertTrue(uuid1.isUuid()) + assertNotNull(time) + assertNotEquals(time, time1) } @Test @@ -303,17 +244,17 @@ class UUIDFactoryTest { fun test_create_both_uuidv6_and_v8_to_compare_performance() { val uuidv6List: MutableList = ArrayList() val uuidv8List: MutableList = ArrayList() - val MAX_COUNT = 10000 + val maxCount = 10000 var start: Instant = Instant.now() - for (i in 0 until MAX_COUNT) { - uuidv8List.add(UuidFactory.Factories.UPROTOCOL.factory().create()) + for (i in 0 until maxCount) { + uuidv8List.add(UUIDV8()) } val v8Diff: Duration = Duration.between(start, Instant.now()) start = Instant.now() - for (i in 0 until MAX_COUNT) { - uuidv6List.add(UuidFactory.Factories.UUIDV6.factory().create()) + for (i in 0 until maxCount) { + uuidv6List.add(UUIDV6()) } val v6Diff: Duration = Duration.between(start, Instant.now()) - println((("UUIDv8:[" + v8Diff.toNanos() / MAX_COUNT) + "ns]" + " UUIDv6:[" + v6Diff.toNanos() / MAX_COUNT) + "ns]") + println((("UUIDv8:[" + v8Diff.toNanos() / maxCount) + "ns]" + " UUIDv6:[" + v6Diff.toNanos() / maxCount) + "ns]") } } diff --git a/src/test/kotlin/org/eclipse/uprotocol/uuid/validator/UuidValidatorTest.kt b/src/test/kotlin/org/eclipse/uprotocol/uuid/validator/UuidValidatorTest.kt index d1a910c..0f0f2f9 100644 --- a/src/test/kotlin/org/eclipse/uprotocol/uuid/validator/UuidValidatorTest.kt +++ b/src/test/kotlin/org/eclipse/uprotocol/uuid/validator/UuidValidatorTest.kt @@ -21,9 +21,12 @@ package org.eclipse.uprotocol.uuid.validator -import org.eclipse.uprotocol.uuid.factory.UuidFactory -import org.eclipse.uprotocol.uuid.factory.UuidUtils +import org.eclipse.uprotocol.uuid.factory.UUIDV6 +import org.eclipse.uprotocol.uuid.factory.UUIDV8 +import org.eclipse.uprotocol.uuid.factory.isUuidv6 import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer +import org.eclipse.uprotocol.uuid.validate.UUIDv6Validator +import org.eclipse.uprotocol.uuid.validate.UUIDv8Validator import org.eclipse.uprotocol.uuid.validate.UuidValidator import org.eclipse.uprotocol.v1.UCode import org.eclipse.uprotocol.v1.UStatus @@ -39,8 +42,7 @@ class UuidValidatorTest { @Test @DisplayName("Test validator with good uuid") fun test_validator_with_good_uuid() { - //final UuidValidator validator = new UuidValidator(); - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() + val uuid: UUID = UUIDV8() val status: UStatus = UuidValidator.getValidator(uuid).validate(uuid) assertEquals(ValidationResult.STATUS_SUCCESS, status) } @@ -49,7 +51,7 @@ class UuidValidatorTest { @DisplayName("Test Good uuid Check") fun test_good_uuid_string() { val status: UStatus = - UuidValidator.Validators.UPROTOCOL.validator().validate(UuidFactory.Factories.UPROTOCOL.factory().create()) + UUIDv8Validator.validate(UUIDV8()) assertEquals(status, ValidationResult.STATUS_SUCCESS) } @@ -68,26 +70,16 @@ class UuidValidatorTest { @Test @DisplayName("Test invalid time uuid") fun test_invalid_time_uuid() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create(Instant.ofEpochSecond(0)) - val status: UStatus = UuidValidator.Validators.UPROTOCOL.validator().validate(uuid) + val uuid: UUID = UUIDV8(Instant.ofEpochSecond(0)) + val status = UUIDv8Validator.validate(uuid) assertEquals(UCode.INVALID_ARGUMENT, status.code) assertEquals("Invalid UUID Time", status.message) } - @Test - @DisplayName("Test UUIDv8 validator for null UUID") - fun test_uuidv8_with_invalid_uuids() { - val validator: UuidValidator = UuidValidator.Validators.UPROTOCOL.validator() - assertNotNull(validator) - val status: UStatus = validator.validate(null) - assertEquals(UCode.INVALID_ARGUMENT, status.code) - assertEquals("Invalid UUIDv8 Version,Invalid UUID Time", status.message) - } - @Test @DisplayName("Test UUIDv8 validator for invalid types") fun test_uuidv8_with_invalid_types() { - val uuidv6: UUID = UuidFactory.Factories.UUIDV6.factory().create() + val uuidv6: UUID = UUIDV6() val uuid: UUID = uUID { msb = 0L lsb = 0L @@ -97,7 +89,7 @@ class UuidValidatorTest { msb = uuidJava.mostSignificantBits lsb = uuidJava.leastSignificantBits } - val validator: UuidValidator = UuidValidator.Validators.UPROTOCOL.validator() + val validator: UuidValidator = UUIDv8Validator assertNotNull(validator) val status: UStatus = validator.validate(uuidv6) assertEquals(UCode.INVALID_ARGUMENT, status.code) @@ -113,17 +105,17 @@ class UuidValidatorTest { @Test @DisplayName("Test good UUIDv6") fun test_good_uuidv6() { - val uuid: UUID = UuidFactory.Factories.UUIDV6.factory().create() + val uuid: UUID = UUIDV6() val validator: UuidValidator = UuidValidator.getValidator(uuid) assertNotNull(validator) - assertTrue(UuidUtils.isUuidv6(uuid)) + assertTrue(uuid.isUuidv6()) assertEquals(UCode.OK, validator.validate(uuid).code) } @Test @DisplayName("Test UUIDv6 with bad variant") fun test_uuidv6_with_bad_variant() { - val uuid: UUID = LongUuidSerializer.instance().deserialize("1ee57e66-d33a-65e0-4a77-3c3f061c1e9e") + val uuid: UUID = LongUuidSerializer.INSTANCE.deserialize("1ee57e66-d33a-65e0-4a77-3c3f061c1e9e") assertFalse(uuid == UUID.getDefaultInstance()) val validator: UuidValidator = UuidValidator.getValidator(uuid) assertNotNull(validator) @@ -139,28 +131,18 @@ class UuidValidatorTest { msb = 9 shl 12 lsb = 0L } - val validator: UuidValidator = UuidValidator.Validators.UUIDV6.validator() + val validator: UuidValidator = UUIDv6Validator assertNotNull(validator) val status: UStatus = validator.validate(uuid) assertEquals("Not a UUIDv6 Version,Invalid UUIDv6 variant,Invalid UUID Time", status.message) assertEquals(UCode.INVALID_ARGUMENT, status.code) } - @Test - @DisplayName("Test using UUIDv6 Validator to validate null UUID") - fun test_uuidv6_with_null_uuid() { - val validator: UuidValidator = UuidValidator.Validators.UUIDV6.validator() - assertNotNull(validator) - val status: UStatus = validator.validate(null) - assertEquals("Not a UUIDv6 Version,Invalid UUIDv6 variant,Invalid UUID Time", status.message) - assertEquals(UCode.INVALID_ARGUMENT, status.code) - } - @Test @DisplayName("Test using UUIDv6 Validator to validate a different types of UUIDs") fun test_uuidv6_with_uuidv8() { - val uuid: UUID = UuidFactory.Factories.UPROTOCOL.factory().create() - val validator: UuidValidator = UuidValidator.Validators.UUIDV6.validator() + val uuid: UUID = UUIDV8() + val validator: UuidValidator = UUIDv6Validator assertNotNull(validator) val status: UStatus = validator.validate(uuid) assertEquals("Not a UUIDv6 Version", status.message)