Skip to content

Commit

Permalink
fixed some of the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hamada147 committed Dec 8, 2022
1 parent 64b894f commit e998ab6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.iohk.atala.prism.mercury.didpeer.core

import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded
import io.iohk.atala.prism.apollo.base64.base64UrlPadDecodedBytes
import io.iohk.atala.prism.mercury.didpeer.VerificationMaterialPeerDID
import io.iohk.atala.prism.mercury.didpeer.VerificationMethodTypeAgreement
import io.iohk.atala.prism.mercury.didpeer.VerificationMethodTypeAuthentication
Expand Down Expand Up @@ -37,5 +38,11 @@ fun fromJwk(verMaterial: VerificationMaterialPeerDID<out VerificationMethodTypeP

val value = jwkDict["x"].toString()
// Base64.decodeBase64(value) // this line in JVM handle both Base64 Standard & Base64 URL
return value.base64UrlDecodedBytes
// The following if condition is a workaround for a bug in Apollo Base64URLPad decoding which will be
// fixed in the next release
var decoded = value.base64UrlPadDecodedBytes
if (decoded.isNotEmpty() && decoded.last().toInt() == 0) {
decoded = decoded.dropLast(1).toByteArray()
}
return decoded
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ fun toMulticodec(value: ByteArray, keyType: VerificationMethodTypePeerDID): Byte
val prefix = getCodec(keyType).prefix
val byteBuffer = Buffer()
VarInt.writeVarInt(prefix, byteBuffer)
return byteBuffer.array().plus(value)
return byteBuffer.readByteArray().plus(value)
}

fun fromMulticodec(value: ByteArray): Pair<Codec, ByteArray> {
Buffer.UnsafeCursor
val prefix = VarInt.readVarInt(Buffer.wrap(value))
val prefix = VarInt.readVarInt(Buffer().write(value))
val codec = getCodec(prefix)
val byteBuffer = ByteBufferNativeType.allocate(2)
val byteBuffer = Buffer()
VarInt.writeVarInt(prefix, byteBuffer)
return Pair(codec, value.drop(byteBuffer.position()).toByteArray())
return Pair(codec, value.drop(2).toByteArray())
}

private fun getCodec(keyType: VerificationMethodTypePeerDID) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

fun Any?.toJsonElement(): JsonElement {
return when (this) {
Expand All @@ -33,12 +36,59 @@ fun toJson(value: Any?): String {
// GsonBuilder().create().toJson(value)
}

private fun extractFromJsonObject(jsonObject: JsonObject): Map<String, Any> {
val currentMap = mutableMapOf<String, Any>()
jsonObject.forEach {
if (it.value is JsonPrimitive) {
if (it.value.jsonPrimitive.isString) {
currentMap[it.key] = it.value.jsonPrimitive.content
} else {
currentMap[it.key] = it.value
}
} else if (it.value is JsonArray) {
val localArray = mutableListOf<String>()
for (arrayJsonElement in it.value.jsonArray) {
if (arrayJsonElement.jsonPrimitive.isString) {
localArray.add(arrayJsonElement.jsonPrimitive.content)
} else {
localArray.add(arrayJsonElement.jsonPrimitive.toString())
}
}
currentMap[it.key] = localArray
} else {
throw Exception("")
}
}
return currentMap
}

/**
* I'm expecting the value to be a JSON array
*/
fun fromJsonToList(value: String): List<Map<String, Any>> {
return Json.decodeFromString<List<Map<String, Any>>>(value)
val list: MutableList<Map<String, Any>> = mutableListOf()
val element = Json.parseToJsonElement(value)

if (element is JsonArray) {
for (jsonElement in element.jsonArray) {
list.add(extractFromJsonObject(jsonElement.jsonObject))
}
} else if (element is JsonObject) {
list.add(extractFromJsonObject(element.jsonObject))
} else {
throw Exception("")
}
return list
// return GsonBuilder().create().fromJson(value, object : TypeToken<List<Map<String, Any>>>() {}.type)
}

fun fromJsonToMap(value: String): Map<String, Any> {
return Json.decodeFromString<Map<String, Any>>(value)
val element = Json.parseToJsonElement(value)

if (element is JsonObject) {
return extractFromJsonObject(element)
} else {
throw Exception("")
}
// return GsonBuilder().create().fromJson(value, object : TypeToken<Map<String, Any>>() {}.type)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.iohk.atala.prism.mercury.didpeer

import io.iohk.atala.prism.mercury.didpeer.core.VarInt2
import io.iohk.atala.prism.mercury.didpeer.core.VarInt
import okio.Buffer
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -10,8 +10,8 @@ class TestDemo {
@Test
fun moussaTest() {
val buffer = Buffer()
VarInt2.writeVarInt(500, buffer)
val result = VarInt2.readVarInt(buffer)
VarInt.writeVarInt(500, buffer)
val result = VarInt.readVarInt(buffer)
assertEquals(500, result)
}

Expand Down

0 comments on commit e998ab6

Please sign in to comment.