Skip to content

Commit bc29a89

Browse files
RawaPururun
authored andcommitted
Add voucher code domain model
1 parent eb978f3 commit bc29a89

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.mullvad.mullvadvpn.lib.model
2+
3+
import arrow.core.Either
4+
import arrow.core.raise.either
5+
import arrow.core.raise.ensure
6+
7+
@JvmInline
8+
value class VoucherCode private constructor(val value: String) {
9+
10+
companion object {
11+
// Parsing reference:
12+
// <services-repository>/services/docs/adr/0018-distinguish-voucher-codes-from-account-numbers.md
13+
fun fromString(value: String): Either<ParseVoucherCodeError, VoucherCode> = either {
14+
val trimmedValue = value.trim()
15+
ensure(trimmedValue.length >= MIN_VOUCHER_LENGTH) {
16+
ParseVoucherCodeError.TooShort(trimmedValue)
17+
}
18+
ensure(!value.all { it.isDigit() }) { ParseVoucherCodeError.AllDigit(trimmedValue) }
19+
VoucherCode(trimmedValue)
20+
}
21+
22+
const val MIN_VOUCHER_LENGTH = 16
23+
}
24+
}
25+
26+
sealed interface ParseVoucherCodeError {
27+
28+
data class AllDigit(val value: String) : ParseVoucherCodeError
29+
30+
data class TooShort(val value: String) : ParseVoucherCodeError
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.mullvad.mullvadvpn.lib.model
2+
3+
import kotlin.test.assertEquals
4+
import kotlin.test.assertTrue
5+
import org.junit.jupiter.api.Test
6+
7+
class VoucherCodeTest {
8+
@Test
9+
fun `parsing a too short voucher code should return TooShort`() {
10+
val input = "mycode"
11+
val result = VoucherCode.fromString(input)
12+
13+
assertTrue(result.isLeft())
14+
assertEquals(ParseVoucherCodeError.TooShort(input), result.leftOrNull())
15+
}
16+
17+
@Test
18+
fun `numbers only should not be allowed`() {
19+
val input = "1234123412341234"
20+
val result = VoucherCode.fromString(input)
21+
22+
assertTrue(result.isLeft())
23+
assertEquals(ParseVoucherCodeError.AllDigit(input), result.leftOrNull())
24+
}
25+
26+
@Test
27+
fun `number only input when too short should return TooShort`() {
28+
val input = "123412341234"
29+
val result = VoucherCode.fromString(input)
30+
31+
assertTrue(result.isLeft())
32+
assertEquals(ParseVoucherCodeError.TooShort(input), result.leftOrNull())
33+
}
34+
}

0 commit comments

Comments
 (0)