Skip to content

Commit

Permalink
feat: add a minimal test for the web handler
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Oct 16, 2023
1 parent b25c445 commit 576ad01
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ class EntitiesQueryUtilsTests {
}
}

@Test
fun `it should parse a valid simple query`() = runTest {
val query = """
{
"type": "Query",
"entities": [{
"type": "BeeHive"
}],
"attrs": ["attr1"],
"q": "temperature>32"
}
""".trimIndent()

parseQueryParamsForPost(
Pair(30, 100),
query,
LinkedMultiValueMap(),
APIC_COMPOUND_CONTEXT
).shouldSucceedWith {
assertEquals(BEEHIVE_TYPE, it.type)
assertEquals(setOf("${NGSILD_DEFAULT_VOCAB}attr1"), it.attrs)
assertEquals("temperature>32", it.q)
}
}

@Test
fun `it should not validate a query if the type is not correct`() {
val query = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.egm.stellio.shared.model.AccessDeniedException
import com.egm.stellio.shared.model.JsonLdEntity
import com.egm.stellio.shared.model.NgsiLdEntity
import com.egm.stellio.shared.util.*
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_DEFAULT_VOCAB
import com.ninjasquad.springmockk.MockkBean
import io.mockk.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -124,6 +125,7 @@ class EntityOperationHandlerTests {
private val batchUpsertWithUpdateEndpoint = "/ngsi-ld/v1/entityOperations/upsert?options=update"
private val batchUpdateEndpoint = "/ngsi-ld/v1/entityOperations/update"
private val batchDeleteEndpoint = "/ngsi-ld/v1/entityOperations/delete"
private val queryEntitiesEndpoint = "/ngsi-ld/v1/entityOperations/query"

private val hcmrContext = listOf(AQUAC_COMPOUND_CONTEXT)

Expand Down Expand Up @@ -814,4 +816,38 @@ class EntityOperationHandlerTests {
""".trimIndent()
)
}

@Test
fun `query entities should return a 200 if the query is correct`() = runTest {
coEvery { authorizationService.computeAccessRightFilter(any()) } returns { null }
coEvery { queryService.queryEntities(any(), any()) } returns Pair(emptyList<JsonLdEntity>(), 0).right()

val query = """
{
"type": "Query",
"entities": [{
"type": "$BEEHIVE_TYPE"
}],
"attrs": ["attr1", "attr2"]
}
""".trimIndent()

webClient.post()
.uri("$queryEntitiesEndpoint?limit=10&offset=20")
.bodyValue(query)
.exchange()
.expectStatus().isOk

coVerify {
queryService.queryEntities(
match {
it.paginationQuery.limit == 10 &&
it.paginationQuery.offset == 20 &&
it.type == BEEHIVE_TYPE &&
it.attrs == setOf("${NGSILD_DEFAULT_VOCAB}attr1", "${NGSILD_DEFAULT_VOCAB}attr2")
},
any()
)
}
}
}

0 comments on commit 576ad01

Please sign in to comment.