diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityQueryService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityQueryService.kt index 2251284e6..aed3750a8 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityQueryService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityQueryService.kt @@ -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 @@ -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( @@ -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> { + suspend fun isMarkedAsDeleted(entityId: URI): Either { val selectQuery = """ select entity_id, deleted_at @@ -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): List { diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityService.kt index 8eaf78df8..c87d6b5f2 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityService.kt @@ -74,11 +74,11 @@ class EntityService( expandedEntity: ExpandedEntity, sub: Sub? = null ): Either = 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() diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/temporal/service/TemporalService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/temporal/service/TemporalService.kt index 4ec13363f..047cfd024 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/temporal/service/TemporalService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/temporal/service/TemporalService.kt @@ -39,7 +39,7 @@ class TemporalService( jsonLdTemporalEntity: ExpandedEntity, sub: Sub? = null ): Either = either { - entityQueryService.getEntityState(entityId).let { + entityQueryService.isMarkedAsDeleted(entityId).let { when (it) { is Left -> { createTemporalEntity( @@ -55,7 +55,7 @@ class TemporalService( entityId, jsonLdTemporalEntity, jsonLdTemporalEntity.getAttributes().sorted(), - it.value.second != null, + it.value, sub ).bind() CreateOrUpdateResult.UPSERTED diff --git a/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityQueryServiceTests.kt b/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityQueryServiceTests.kt index 65d4789de..431f205e8 100644 --- a/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityQueryServiceTests.kt +++ b/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityQueryServiceTests.kt @@ -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 @@ -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) } } } diff --git a/search-service/src/test/kotlin/com/egm/stellio/search/temporal/service/TemporalServiceTests.kt b/search-service/src/test/kotlin/com/egm/stellio/search/temporal/service/TemporalServiceTests.kt index 0e1396f95..9e04afa27 100644 --- a/search-service/src/test/kotlin/com/egm/stellio/search/temporal/service/TemporalServiceTests.kt +++ b/search-service/src/test/kotlin/com/egm/stellio/search/temporal/service/TemporalServiceTests.kt @@ -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() @@ -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() @@ -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")