Skip to content

Commit

Permalink
refactor: getEntityState -> isMarkedAsDeleted
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Dec 20, 2024
1 parent efa4ff0 commit 4b03b4a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.egm.stellio.search.authorization.service.AuthorizationService
import com.egm.stellio.search.common.util.allToMappedList
import com.egm.stellio.search.common.util.deserializeAsMap
import com.egm.stellio.search.common.util.oneToResult
import com.egm.stellio.search.common.util.toOptionalZonedDateTime
import com.egm.stellio.search.common.util.toUri
import com.egm.stellio.search.common.util.wrapToAndClause
import com.egm.stellio.search.entity.model.EntitiesQuery
Expand All @@ -29,7 +28,6 @@ import com.egm.stellio.shared.util.entityNotFoundMessage
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.stereotype.Service
import java.net.URI
import java.time.ZonedDateTime

@Service
class EntityQueryService(
Expand Down Expand Up @@ -279,9 +277,7 @@ class EntityQueryService(
* or, if it exists, whether it is currently deleted (in which case, it may be possible to create it again
* if authorized)
*/
suspend fun getEntityState(
entityId: URI
): Either<APIException, Pair<URI, ZonedDateTime?>> {
suspend fun isMarkedAsDeleted(entityId: URI): Either<APIException, Boolean> {
val selectQuery =
"""
select entity_id, deleted_at
Expand All @@ -292,7 +288,7 @@ class EntityQueryService(
return databaseClient
.sql(selectQuery)
.bind("entity_id", entityId)
.oneToResult { Pair(toUri(it["entity_id"]), toOptionalZonedDateTime(it["deleted_at"])) }
.oneToResult { it["deleted_at"] != null }
}

suspend fun filterExistingEntitiesAsIds(entitiesIds: List<URI>): List<URI> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ class EntityService(
expandedEntity: ExpandedEntity,
sub: Sub? = null
): Either<APIException, Unit> = either {
entityQueryService.getEntityState(ngsiLdEntity.id).let {
entityQueryService.isMarkedAsDeleted(ngsiLdEntity.id).let {
when (it) {
is Left -> authorizationService.userCanCreateEntities(sub.toOption()).bind()
is Right ->
if (it.value.second == null)
if (!it.value)
AlreadyExistsException(entityAlreadyExistsMessage(ngsiLdEntity.id.toString())).left().bind()
else
authorizationService.userCanAdminEntity(ngsiLdEntity.id, sub.toOption()).bind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TemporalService(
jsonLdTemporalEntity: ExpandedEntity,
sub: Sub? = null
): Either<APIException, CreateOrUpdateResult> = either {
entityQueryService.getEntityState(entityId).let {
entityQueryService.isMarkedAsDeleted(entityId).let {
when (it) {
is Left -> {
createTemporalEntity(
Expand All @@ -55,7 +55,7 @@ class TemporalService(
entityId,
jsonLdTemporalEntity,
jsonLdTemporalEntity.getAttributes().sorted(),
it.value.second != null,
it.value,
sub
).bind()
CreateOrUpdateResult.UPSERTED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
Expand Down Expand Up @@ -239,12 +239,11 @@ class EntityQueryServiceTests : WithTimescaleContainer, WithKafkaContainer {
)
}

entityQueryService.getEntityState(entity01Uri)
entityQueryService.isMarkedAsDeleted(entity01Uri)
.shouldSucceedWith {
assertEquals(entity01Uri, it.first)
assertNull(it.second)
assertFalse(it)
}
entityQueryService.getEntityState(entity02Uri)
entityQueryService.isMarkedAsDeleted(entity02Uri)
.shouldFail { assert(it is ResourceNotFoundException) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TemporalServiceTests {
fun `it should ask to create a temporal entity if it does not exist yet`() = runTest {
mockkAuthorizationForCreation()
coEvery {
entityQueryService.getEntityState(entityUri)
entityQueryService.isMarkedAsDeleted(entityUri)
} returns ResourceNotFoundException("Entity does not exist").left()
coEvery { entityService.createEntity(any(), any(), any()) } returns Unit.right()
coEvery { entityService.upsertAttributes(any(), any(), any()) } returns Unit.right()
Expand All @@ -66,9 +66,7 @@ class TemporalServiceTests {
@Test
fun `it should ask to create a temporal entity if it already exists but is deleted`() = runTest {
mockkAuthorizationForCreation()
coEvery {
entityQueryService.getEntityState(entityUri)
} returns Pair(entityUri, null).right()
coEvery { entityQueryService.isMarkedAsDeleted(entityUri) } returns false.right()
coEvery { entityService.createEntity(any(), any(), any()) } returns Unit.right()
coEvery { entityService.upsertAttributes(any(), any(), any()) } returns Unit.right()

Expand All @@ -80,9 +78,7 @@ class TemporalServiceTests {
@Test
fun `it should ask to upsert a temporal entity if it already exists but is not deleted`() = runTest {
mockkAuthorizationForCreation()
coEvery {
entityQueryService.getEntityState(entityUri)
} returns Pair(entityUri, null).right()
coEvery { entityQueryService.isMarkedAsDeleted(entityUri) } returns false.right()
coEvery { entityService.upsertAttributes(any(), any(), any()) } returns Unit.right()

val expandedEntity = loadAndExpandSampleData("temporal/beehive_create_temporal_entity.jsonld")
Expand Down

0 comments on commit 4b03b4a

Please sign in to comment.