Skip to content

Commit fef4c21

Browse files
committed
Unit Testing | Patch 20
1 parent a394e7b commit fef4c21

File tree

16 files changed

+341
-20
lines changed

16 files changed

+341
-20
lines changed

core/common/src/main/java/com/shifthackz/aisdv1/core/common/extensions/StringExtensions.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ package com.shifthackz.aisdv1.core.common.extensions
33
private const val PROTOCOL_DELIMITER = "://"
44
private const val PROTOCOL_HOLDER = "[[_PROTOCOL_]]"
55

6-
fun String.withoutUrlProtocol(): String {
7-
if (!this.contains(PROTOCOL_DELIMITER)) return this
8-
val decomposed = this.split(PROTOCOL_DELIMITER)
9-
if (decomposed.size < 2) return this
10-
return decomposed.last()
11-
}
12-
136
fun String.fixUrlSlashes(): String = this
147
.replace(PROTOCOL_DELIMITER, PROTOCOL_HOLDER)
158
.replace(Regex("/{2,}"), "/")

core/validation/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ android {
1212
dependencies {
1313
implementation di.koinCore
1414
implementation reactive.rxkotlin
15+
testImplementation test.junit
16+
testImplementation test.mockk
1517
}

core/validation/src/main/java/com/shifthackz/aisdv1/core/validation/horde/CommonStringValidator.kt renamed to core/validation/src/main/java/com/shifthackz/aisdv1/core/validation/common/CommonStringValidator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.shifthackz.aisdv1.core.validation.horde
1+
package com.shifthackz.aisdv1.core.validation.common
22

33
import com.shifthackz.aisdv1.core.validation.ValidationResult
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.shifthackz.aisdv1.core.validation.horde
1+
package com.shifthackz.aisdv1.core.validation.common
22

33
import com.shifthackz.aisdv1.core.validation.ValidationResult
44

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.shifthackz.aisdv1.core.validation.di
22

3+
import com.shifthackz.aisdv1.core.validation.common.CommonStringValidator
4+
import com.shifthackz.aisdv1.core.validation.common.CommonStringValidatorImpl
35
import com.shifthackz.aisdv1.core.validation.dimension.DimensionValidator
46
import com.shifthackz.aisdv1.core.validation.dimension.DimensionValidatorImpl
5-
import com.shifthackz.aisdv1.core.validation.horde.CommonStringValidator
6-
import com.shifthackz.aisdv1.core.validation.horde.CommonStringValidatorImpl
77
import com.shifthackz.aisdv1.core.validation.url.UrlValidator
88
import com.shifthackz.aisdv1.core.validation.url.UrlValidatorImpl
99
import org.koin.core.module.dsl.factoryOf
@@ -13,7 +13,7 @@ import org.koin.dsl.module
1313
val validatorsModule = module {
1414
// !!! Do not use [factoryOf] for DimensionValidatorImpl, it has 2 default Ints in constructor
1515
factory<DimensionValidator> { DimensionValidatorImpl() }
16+
factory<UrlValidator> { UrlValidatorImpl() }
1617

17-
factoryOf(::UrlValidatorImpl) bind UrlValidator::class
1818
factoryOf(::CommonStringValidatorImpl) bind CommonStringValidator::class
1919
}

core/validation/src/main/java/com/shifthackz/aisdv1/core/validation/url/UrlValidatorImpl.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package com.shifthackz.aisdv1.core.validation.url
33
import android.util.Patterns
44
import android.webkit.URLUtil
55
import com.shifthackz.aisdv1.core.validation.ValidationResult
6+
import java.util.regex.Pattern
67

7-
internal class UrlValidatorImpl : UrlValidator {
8+
internal class UrlValidatorImpl(
9+
private val webUrlPattern: Pattern = Patterns.WEB_URL,
10+
) : UrlValidator {
811

912
override operator fun invoke(input: String?): ValidationResult<UrlValidator.Error> = when {
1013
input == null -> ValidationResult(
@@ -27,7 +30,7 @@ internal class UrlValidatorImpl : UrlValidator {
2730
isValid = false,
2831
validationError = UrlValidator.Error.Invalid,
2932
)
30-
!Patterns.WEB_URL.matcher(input).matches() -> ValidationResult(
33+
!webUrlPattern.matcher(input).matches() -> ValidationResult(
3134
isValid = false,
3235
validationError = UrlValidator.Error.Invalid,
3336
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.shifthackz.aisdv1.core.validation.common
2+
3+
import com.shifthackz.aisdv1.core.validation.ValidationResult
4+
import org.junit.Assert
5+
import org.junit.Test
6+
7+
class CommonStringValidatorImplTest {
8+
9+
private val validator = CommonStringValidatorImpl()
10+
11+
@Test
12+
fun `given input is null, expected not valid with Empty error`() {
13+
val expected = ValidationResult<CommonStringValidator.Error>(
14+
isValid = false,
15+
validationError = CommonStringValidator.Error.Empty,
16+
)
17+
val actual = validator(null)
18+
Assert.assertEquals(expected, actual)
19+
}
20+
21+
@Test
22+
fun `given input is empty, expected not valid with Empty error`() {
23+
val expected = ValidationResult<CommonStringValidator.Error>(
24+
isValid = false,
25+
validationError = CommonStringValidator.Error.Empty,
26+
)
27+
val actual = validator("")
28+
Assert.assertEquals(expected, actual)
29+
}
30+
31+
@Test
32+
fun `given input is blank, expected not valid with Empty error`() {
33+
val expected = ValidationResult<CommonStringValidator.Error>(
34+
isValid = false,
35+
validationError = CommonStringValidator.Error.Empty,
36+
)
37+
val actual = validator(" ")
38+
Assert.assertEquals(expected, actual)
39+
}
40+
41+
@Test
42+
fun `given input is non empty string, expected valid`() {
43+
val expected = ValidationResult<CommonStringValidator.Error>(true)
44+
val actual = validator("5598 is my favorite")
45+
Assert.assertEquals(expected, actual)
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.shifthackz.aisdv1.core.validation.dimension
2+
3+
import com.shifthackz.aisdv1.core.validation.ValidationResult
4+
import org.junit.Assert
5+
import org.junit.Test
6+
7+
class DimensionValidatorImplTest {
8+
9+
private val validator = DimensionValidatorImpl(MIN, MAX)
10+
11+
@Test
12+
fun `given input is null, expected not valid with Empty error`() {
13+
val expected = ValidationResult<DimensionValidator.Error>(
14+
isValid = false,
15+
validationError = DimensionValidator.Error.Empty,
16+
)
17+
val actual = validator(null)
18+
Assert.assertEquals(expected, actual)
19+
}
20+
21+
@Test
22+
fun `given input is empty, expected not valid with Empty error`() {
23+
val expected = ValidationResult<DimensionValidator.Error>(
24+
isValid = false,
25+
validationError = DimensionValidator.Error.Empty,
26+
)
27+
val actual = validator("")
28+
Assert.assertEquals(expected, actual)
29+
}
30+
31+
@Test
32+
fun `given input is unparsable to int, expected not valid with Unexpected error`() {
33+
val expected = ValidationResult<DimensionValidator.Error>(
34+
isValid = false,
35+
validationError = DimensionValidator.Error.Unexpected,
36+
)
37+
val actual = validator("5598❤")
38+
Assert.assertEquals(expected, actual)
39+
}
40+
41+
@Test
42+
fun `given input is less than minimum allowed value, expected not valid with LessThanMinimum error`() {
43+
val expected = ValidationResult<DimensionValidator.Error>(
44+
isValid = false,
45+
validationError = DimensionValidator.Error.LessThanMinimum(MIN),
46+
)
47+
val actual = validator("55")
48+
Assert.assertEquals(expected, actual)
49+
}
50+
51+
@Test
52+
fun `given input is bigger than maximum allowed value, expected not valid with BiggerThanMaximum error`() {
53+
val expected = ValidationResult<DimensionValidator.Error>(
54+
isValid = false,
55+
validationError = DimensionValidator.Error.BiggerThanMaximum(MAX),
56+
)
57+
val actual = validator("5598")
58+
Assert.assertEquals(expected, actual)
59+
}
60+
61+
@Test
62+
fun `given input is valid parsable int value, expected valid`() {
63+
val expected = ValidationResult<DimensionValidator.Error>(true)
64+
val actual = validator("1024")
65+
Assert.assertEquals(expected, actual)
66+
}
67+
68+
companion object {
69+
private const val MIN = 64
70+
private const val MAX = 2048
71+
}
72+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.shifthackz.aisdv1.core.validation.url
2+
3+
import android.webkit.URLUtil
4+
import com.shifthackz.aisdv1.core.validation.ValidationResult
5+
import io.mockk.every
6+
import io.mockk.mockk
7+
import io.mockk.mockkConstructor
8+
import io.mockk.mockkStatic
9+
import org.junit.Assert
10+
import org.junit.Test
11+
import java.util.regex.Matcher
12+
import java.util.regex.Pattern
13+
14+
class UrlValidatorImplTest {
15+
16+
private val stubMatcher = mockk<Matcher>()
17+
private val stubPattern = mockk<Pattern>()
18+
19+
private val validator = UrlValidatorImpl(stubPattern)
20+
21+
@Test
22+
fun `given input is null, expected not valid with Empty error`() {
23+
val expected = ValidationResult<UrlValidator.Error>(
24+
isValid = false,
25+
validationError = UrlValidator.Error.Empty,
26+
)
27+
val actual = validator(null)
28+
Assert.assertEquals(expected, actual)
29+
}
30+
31+
@Test
32+
fun `given input is empty, expected not valid with Empty error`() {
33+
val expected = ValidationResult<UrlValidator.Error>(
34+
isValid = false,
35+
validationError = UrlValidator.Error.Empty,
36+
)
37+
val actual = validator("")
38+
Assert.assertEquals(expected, actual)
39+
}
40+
41+
@Test
42+
fun `given input is blank, expected not valid with Empty error`() {
43+
val expected = ValidationResult<UrlValidator.Error>(
44+
isValid = false,
45+
validationError = UrlValidator.Error.Empty,
46+
)
47+
val actual = validator(" ")
48+
Assert.assertEquals(expected, actual)
49+
}
50+
51+
@Test
52+
fun `given input is url with ftp protocol, expected not valid with BadScheme error`() {
53+
val expected = ValidationResult<UrlValidator.Error>(
54+
isValid = false,
55+
validationError = UrlValidator.Error.BadScheme,
56+
)
57+
val actual = validator("ftp://5598.is.my.favorite.com:21/i_failed.dat")
58+
Assert.assertEquals(expected, actual)
59+
}
60+
61+
@Test
62+
fun `given input is http localhost ipv4 address, expected not valid with Localhost error`() {
63+
val expected = ValidationResult<UrlValidator.Error>(
64+
isValid = false,
65+
validationError = UrlValidator.Error.Localhost,
66+
)
67+
val actual = validator("http://127.0.0.1:7860")
68+
Assert.assertEquals(expected, actual)
69+
}
70+
71+
@Test
72+
fun `given input is https localhost ipv4 address, expected not valid with Localhost error`() {
73+
val expected = ValidationResult<UrlValidator.Error>(
74+
isValid = false,
75+
validationError = UrlValidator.Error.Localhost,
76+
)
77+
val actual = validator("https://127.0.0.1:7860")
78+
Assert.assertEquals(expected, actual)
79+
}
80+
81+
@Test
82+
fun `given input is not valid url, expected not valid with Invalid error`() {
83+
val mockInput = "https://968.666.777.5598:00000000"
84+
mockkConstructor(URLUtil::class)
85+
mockkStatic(URLUtil::class)
86+
every {
87+
URLUtil.isValidUrl(mockInput)
88+
} returns false
89+
val expected = ValidationResult<UrlValidator.Error>(
90+
isValid = false,
91+
validationError = UrlValidator.Error.Invalid,
92+
)
93+
val actual = validator(mockInput)
94+
Assert.assertEquals(expected, actual)
95+
}
96+
97+
@Test
98+
fun `given input is valid url, expected valid`() {
99+
val mockInput = "https://192.168.0.1:7860"
100+
mockkConstructor(URLUtil::class)
101+
mockkStatic(URLUtil::class)
102+
every {
103+
URLUtil.isValidUrl(mockInput)
104+
} returns true
105+
every {
106+
stubMatcher.matches()
107+
} returns true
108+
every {
109+
stubPattern.matcher(any())
110+
} returns stubMatcher
111+
112+
val expected = ValidationResult<UrlValidator.Error>(true)
113+
val actual = validator(mockInput)
114+
Assert.assertEquals(expected, actual)
115+
}
116+
}

data/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,4 @@ dependencies {
3131
testImplementation test.junit
3232
testImplementation test.mockito
3333
testImplementation test.mockk
34-
testImplementation project(':domain')
3534
}

0 commit comments

Comments
 (0)