Skip to content

Commit

Permalink
feat: add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Oct 16, 2023
1 parent e808f72 commit 545a002
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
14 changes: 7 additions & 7 deletions shared/src/main/kotlin/com/egm/stellio/shared/util/ApiUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ import java.time.format.DateTimeParseException
import java.util.Optional
import java.util.regex.Pattern

fun String.parseTimeParameter(errorMsg: String): Either<String, ZonedDateTime> =
try {
ZonedDateTime.parse(this).right()
} catch (e: DateTimeParseException) {
errorMsg.left()
}

const val RESULTS_COUNT_HEADER = "NGSILD-Results-Count"
const val JSON_LD_CONTENT_TYPE = "application/ld+json"
const val JSON_MERGE_PATCH_CONTENT_TYPE = "application/merge-patch+json"
Expand Down Expand Up @@ -226,3 +219,10 @@ fun List<MediaType>.getApplicable(): MediaType {
else
JSON_LD_MEDIA_TYPE
}

fun String.parseTimeParameter(errorMsg: String): Either<String, ZonedDateTime> =
try {
ZonedDateTime.parse(this).right()
} catch (e: DateTimeParseException) {
errorMsg.left()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.fail
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.test.context.ActiveProfiles
Expand Down Expand Up @@ -187,4 +188,39 @@ class ApiUtilsTests {
"(${defaultExpand}TypeA|${defaultExpand}TypeB);(${defaultExpand}TypeC,${defaultExpand}TypeD)"
assertEquals(expectedExpandTypeSelection, expandedQuery)
}

@Test
fun `it should parse a conform datetime parameter`() {
val parseResult = "2023-10-16T16:18:00Z".parseTimeParameter("Invalid date time")
parseResult.onLeft {
fail("it should have parsed the date time")
}
}

@Test
fun `it should return an error if datetime parameter is not conform`() {
val parseResult = "16/10/2023".parseTimeParameter("Invalid date time")
parseResult.fold(
{ assertEquals("Invalid date time", it) }
) {
fail("it should not have parsed the date time")
}
}

@Test
fun `it should validate a correct idPattern`() {
val validationResult = validateIdPattern("urn:ngsi-ld:Entity:*")
validationResult.onLeft {
fail("it should have parsed the date time")
}
}

@Test
fun `it should not validate an incorrect idPattern`() {
val validationResult = validateIdPattern("(?x)urn:ngsi-ld:Entity:{*}2")
validationResult.fold(
{ assertTrue(it.message.startsWith("Invalid value for idPattern: (?x)urn:ngsi-ld:Entity:{*}2")) },
{ fail("it should not have validated the idPattern") }
)
}
}

0 comments on commit 545a002

Please sign in to comment.