Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Refactoring to match TBD style (#8)
Browse files Browse the repository at this point in the history
* Add DAP.parse

* Change DAP to Dap (etc) to match web5-kt naming

* use kotlin.test similar to web5-kt

* update URN parsing
  • Loading branch information
aparkersquare authored Jun 23, 2024
1 parent e9ca147 commit f52a634
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 136 deletions.
16 changes: 4 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,25 @@
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${version.org.assertj}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${version.org.junit.jupiter}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.org.junit.jupiter}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${version.com.squareup.okhttp3}</version>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>${version.kotlin}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
10 changes: 0 additions & 10 deletions src/main/kotlin/xyz/block/dap/DAP.kt

This file was deleted.

34 changes: 34 additions & 0 deletions src/main/kotlin/xyz/block/dap/Dap.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package xyz.block.dap

import java.util.regex.Pattern

data class Dap(
val handle: String,
val domain: String
) {
override fun toString(): String {
return "$PREFIX$handle$SEPARATOR$domain'"
}

companion object {
const val PREFIX = "@"
const val SEPARATOR = "/"

private const val DAP_REGEX = """^$PREFIX([^$PREFIX$SEPARATOR]+)$SEPARATOR([^$PREFIX$SEPARATOR]+)$"""
private val DAP_PATTERN = Pattern.compile(DAP_REGEX)

fun parse(dap: String): Dap {
val matcher = DAP_PATTERN.matcher(dap)
matcher.find()
if (!matcher.matches()) {
throw InvalidDapException
}
val handle = matcher.group(1)
val domain = matcher.group(2)

return Dap(handle, domain)
}
}
}

object InvalidDapException : Throwable(message = "Invalid DAP")
11 changes: 5 additions & 6 deletions src/main/kotlin/xyz/block/maddr/MoneyAddress.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package xyz.block.maddr

import xyz.block.maddr.MoneyAddress.Companion.KIND
import xyz.block.maddr.urn.URN
import xyz.block.maddr.urn.Urn

// TODO(aparker) - placeholder, this should come from web5-kt
data class DidService(val id: String, val type: String, val serviceEndpoints: List<String>)

data class MoneyAddress(
val id: String,
val urn: URN,
val urn: Urn,
val currency: String,
val css: String
) {
Expand All @@ -19,11 +19,11 @@ data class MoneyAddress(

fun DidService.toMoneyAddresses(): List<MoneyAddress> {
if (type != KIND) {
throw InvalidMoneyAddressException("invalid service type: $type")
throw InvalidMoneyAddressException
}

return serviceEndpoints.map { endpoint ->
val urn = URN.parse(endpoint)
val urn = Urn.parse(endpoint)
MoneyAddress(
id = id,
urn = urn,
Expand All @@ -33,5 +33,4 @@ fun DidService.toMoneyAddresses(): List<MoneyAddress> {
}
}

data class InvalidMoneyAddressException(val invalidValue: String) :
Throwable("Invalid MoneyAddress: $invalidValue")
object InvalidMoneyAddressException : Throwable("Invalid MoneyAddress")
39 changes: 0 additions & 39 deletions src/main/kotlin/xyz/block/maddr/urn/URN.kt

This file was deleted.

35 changes: 35 additions & 0 deletions src/main/kotlin/xyz/block/maddr/urn/Urn.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package xyz.block.maddr.urn

import java.util.regex.Pattern

data class Urn(
val nid: String,
val nss: String
) {
private val urn: String = "$PREFIX$nid:$nss"

override fun toString(): String {
return urn
}

companion object {
const val PREFIX = "urn:"
private const val SEPARATOR = ":"

private val URN_PATTERN = Pattern.compile("""^${PREFIX}([^$SEPARATOR]+)$SEPARATOR(.+)$""")

fun parse(urn: String): Urn {
val matcher = URN_PATTERN.matcher(urn)
matcher.find()
if (!matcher.matches()) {
throw InvalidUrnException
}
val nid = matcher.group(1)
val nss = matcher.group(2)

return Urn(nid, nss)
}
}
}

object InvalidUrnException : Throwable("Invalid URN")
13 changes: 0 additions & 13 deletions src/test/kotlin/xyz/block/dap/DAPTest.kt

This file was deleted.

42 changes: 42 additions & 0 deletions src/test/kotlin/xyz/block/dap/DapTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package xyz.block.dap

import org.junit.jupiter.api.assertThrows
import kotlin.test.Test
import kotlin.test.assertEquals

class DapTest {

@Test
fun testDapToString() {
val dap = Dap("handle", "domain.com")
assertEquals("@handle/domain.com'", dap.toString())
}

@Test
fun testParseDap() {
val dap = Dap.parse("@handle/domain.com")
assertEquals("handle", dap.handle)
assertEquals("domain.com", dap.domain)
}

@Test
fun testParseInvalidDaps() {
val invalidDaps = listOf(
"",
"a",
"@handle",
"@handle/",
"@handle@/domain.com",
"@handle//domain.com",
"@handle/@domain.com",
"@handle/domain.com@",
"@handle/domain.com/",
)
for (dap in invalidDaps) {
val exception = assertThrows<InvalidDapException> {
Dap.parse(dap)
}
assertEquals("Invalid DAP", exception.message)
}
}
}
12 changes: 6 additions & 6 deletions src/test/kotlin/xyz/block/maddr/MoneyAddressTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package xyz.block.maddr

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import xyz.block.maddr.urn.InvalidUrnException
import kotlin.test.Test
import kotlin.test.assertEquals

class MoneyAddressTest {

@Test
fun testDidToMoneyAddress() {
val did = DidService("didpay", "maddr", listOf("urn:nid:nss"))
Expand Down Expand Up @@ -59,18 +60,17 @@ class MoneyAddressTest {

@Test
fun testDidToMoneyAddressInvalidServiceType() {
assertThrows(InvalidMoneyAddressException::class.java) {
assertThrows<InvalidMoneyAddressException> {
val did = DidService("didpay", "not-maddr", listOf("urn:nid:nss"))
did.toMoneyAddresses()
}
}

@Test
fun testDidToMoneyAddressInvalidUrn() {
assertThrows(InvalidUrnException::class.java) {
assertThrows<InvalidUrnException> {
val did = DidService("didpay", "maddr", listOf("not-a-urn"))
did.toMoneyAddresses()
}
}
}

50 changes: 0 additions & 50 deletions src/test/kotlin/xyz/block/maddr/urn/URNTest.kt

This file was deleted.

43 changes: 43 additions & 0 deletions src/test/kotlin/xyz/block/maddr/urn/UrnTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package xyz.block.maddr.urn

import org.junit.jupiter.api.assertThrows
import kotlin.test.Test
import kotlin.test.assertEquals

class UrnTest {

@Test
fun testUrnParsing() {
val urn = Urn.parse("urn:nid:nss")
assertEquals("urn:nid:nss", urn.toString())
assertEquals("nid", urn.nid)
assertEquals("nss", urn.nss)
}

@Test
fun testUrnParsingWithNssParts() {
val urn = Urn.parse("urn:nid:nss1:nss2:nss3")
assertEquals("urn:nid:nss1:nss2:nss3", urn.toString())
assertEquals("nid", urn.nid)
assertEquals("nss1:nss2:nss3", urn.nss)
}

@Test
fun testParseInvalidUrn() {
val invalidUrns = listOf(
"",
"invalid:nid:nss",
"invalid::nss",
"invalid:nid:",
"invalid:nid",
"invalid:",
"invalid::",
)
for (urn in invalidUrns) {
val exception = assertThrows<InvalidUrnException> {
Urn.parse(urn)
}
assertEquals("Invalid URN", exception.message)
}
}
}

0 comments on commit f52a634

Please sign in to comment.