Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Inline some UuidUtil functions and reduce deprecation warnings #2934

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,7 +5,9 @@

package org.knora.webapi.e2e.v2

import org.apache.pekko
import org.apache.pekko.http.scaladsl.model._
import org.apache.pekko.http.scaladsl.model.headers.BasicHttpCredentials
import org.apache.pekko.http.scaladsl.unmarshalling.Unmarshal
import org.xmlunit.builder.DiffBuilder
import org.xmlunit.builder.Input
import org.xmlunit.diff.Diff
Expand Down Expand Up @@ -39,10 +41,6 @@ import org.knora.webapi.routing.UnsafeZioRun
import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.util._

import pekko.http.scaladsl.model._
import pekko.http.scaladsl.model.headers.BasicHttpCredentials
import pekko.http.scaladsl.unmarshalling.Unmarshal

class ValuesRouteV2E2ESpec extends E2ESpec {

private implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
Expand Down Expand Up @@ -810,7 +808,7 @@ class ValuesRouteV2E2ESpec extends E2ESpec {
valueType should ===(KnoraApiV2Complex.IntValue.toSmartIri)
integerValueUUID = responseJsonDoc.body.requireStringWithValidation(
KnoraApiV2Complex.ValueHasUUID,
UuidUtil.validateBase64EncodedUuid
(key, errorFun) => UuidUtil.base64Decode(key).getOrElse(errorFun)
)

val savedValue: JsonLDObject = getValue(
Expand Down Expand Up @@ -3102,7 +3100,7 @@ class ValuesRouteV2E2ESpec extends E2ESpec {
valueType should ===(KnoraApiV2Complex.LinkValue.toSmartIri)
linkValueUUID = responseJsonDoc.body.requireStringWithValidation(
KnoraApiV2Complex.ValueHasUUID,
UuidUtil.validateBase64EncodedUuid
(key, errorFun) => UuidUtil.base64Decode(key).getOrElse(errorFun)
)

val savedValue: JsonLDObject = getValue(
Expand Down Expand Up @@ -3236,7 +3234,7 @@ class ValuesRouteV2E2ESpec extends E2ESpec {
val newIntegerValueUUID: UUID =
responseJsonDoc.body.requireStringWithValidation(
KnoraApiV2Complex.ValueHasUUID,
UuidUtil.validateBase64EncodedUuid
(key, errorFun) => UuidUtil.base64Decode(key).getOrElse(errorFun)
)
assert(newIntegerValueUUID == integerValueUUID) // The new version should have the same UUID.

Expand Down Expand Up @@ -4942,7 +4940,7 @@ class ValuesRouteV2E2ESpec extends E2ESpec {
val newLinkValueUUID: UUID =
responseJsonDoc.body.requireStringWithValidation(
KnoraApiV2Complex.ValueHasUUID,
UuidUtil.validateBase64EncodedUuid
(key, errorFun) => UuidUtil.base64Decode(key).getOrElse(errorFun)
)
assert(newLinkValueUUID != linkValueUUID)
linkValueUUID = newLinkValueUUID
Expand Down Expand Up @@ -5016,7 +5014,7 @@ class ValuesRouteV2E2ESpec extends E2ESpec {
val newLinkValueUUID: UUID =
responseJsonDoc.body.requireStringWithValidation(
KnoraApiV2Complex.ValueHasUUID,
UuidUtil.validateBase64EncodedUuid
(key, errorFun) => UuidUtil.base64Decode(key).getOrElse(errorFun)
)
assert(newLinkValueUUID == linkValueUUID)

Expand Down Expand Up @@ -5069,7 +5067,7 @@ class ValuesRouteV2E2ESpec extends E2ESpec {
val newLinkValueUUID: UUID =
responseJsonDoc.body.requireStringWithValidation(
KnoraApiV2Complex.ValueHasUUID,
UuidUtil.validateBase64EncodedUuid
(key, errorFun) => UuidUtil.base64Decode(key).getOrElse(errorFun)
)
assert(newLinkValueUUID == linkValueUUID)

Expand Down
33 changes: 1 addition & 32 deletions webapi/src/main/scala/dsp/valueobjects/UuidUtil.scala
Expand Up @@ -25,10 +25,7 @@
*
* @return a random, Base64-encoded UUID.
*/
def makeRandomBase64EncodedUuid: String = {
val uuid = UUID.randomUUID
base64Encode(uuid)
}
def makeRandomBase64EncodedUuid: String = base64Encode(UUID.randomUUID)

Check warning on line 28 in webapi/src/main/scala/dsp/valueobjects/UuidUtil.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/dsp/valueobjects/UuidUtil.scala#L28

Added line #L28 was not covered by tests

/**
* Base64-encodes a [[UUID]] using a URL and filename safe Base64 encoder from [[java.util.Base64]],
Expand Down Expand Up @@ -101,7 +98,6 @@
/**
* Calls `base64Decode`, throwing [[InconsistentRepositoryDataException]] if the string cannot be parsed.
*/
@deprecated("It is still throwing!")
def decode(uuidStr: String): UUID =
if (uuidStr.length == canonicalUuidLength) UUID.fromString(uuidStr)
else if (uuidStr.length == base64UuidLength)
Expand All @@ -111,31 +107,4 @@
.getOrElse(throw InconsistentRepositoryDataException(s"Invalid UUID: $uuidStr"))
else throw InconsistentRepositoryDataException(s"Invalid UUID: $uuidStr")

/**
* Encodes a [[UUID]] as a string in one of two formats:
*
* - The canonical 36-character format.
* - The 22-character Base64-encoded format returned by [[base64Encode]].
*
* @param uuid the UUID to be encoded.
* @param useBase64 if `true`, uses Base64 encoding.
* @return the encoded UUID.
*/
def encode(uuid: UUID, useBase64: Boolean): String =
if (useBase64) base64Encode(uuid)
else uuid.toString

/**
* Validates and decodes a Base64-encoded UUID.
*
* @param base64Uuid the UUID to be validated.
* @param errorFun a function that throws an exception. It will be called if the string cannot be parsed.
* @return the decoded UUID.
*/
@deprecated("Use validateBase64EncodedUuid(String) instead.")
def validateBase64EncodedUuid(base64Uuid: String, errorFun: => Nothing): UUID = // V2 / value objects
validateBase64EncodedUuid(base64Uuid).getOrElse(errorFun)

def validateBase64EncodedUuid(base64Uuid: String): Option[UUID] =
UuidUtil.base64Decode(base64Uuid).toOption
}
Expand Up @@ -957,7 +957,7 @@

val id = uuidsToDocumentSpecificIds.get(tag.uuid) match {
case Some(documentSpecificId) => documentSpecificId
case None => UuidUtil.encode(tag.uuid, writeBase64IDs)
case None => if (writeBase64IDs) UuidUtil.base64Encode(tag.uuid) else tag.uuid.toString

Check warning on line 960 in webapi/src/main/scala/org/knora/webapi/messages/util/standoff/XMLToStandoffUtil.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/messages/util/standoff/XMLToStandoffUtil.scala#L960

Added line #L960 was not covered by tests
}

val maybeIdAttr: Option[(String, String)] = if (writeUuidsToXml) {
Expand Down
Expand Up @@ -78,7 +78,7 @@
// Check that given entityIRI ends with a UUID
ending: String = UuidUtil.fromIri(entityIriAsString)
_ <- ZIO
.fromOption(UuidUtil.validateBase64EncodedUuid(ending))
.fromTry(UuidUtil.base64Decode(ending))

Check warning on line 81 in webapi/src/main/scala/org/knora/webapi/responders/IriService.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/responders/IriService.scala#L81

Added line #L81 was not covered by tests
.orElseFail(BadRequestException(s"IRI: '$entityIriAsString' must end with a valid base 64 UUID."))
} yield entityIriAsString

Expand Down