diff --git a/search-service/config/detekt/baseline.xml b/search-service/config/detekt/baseline.xml
index b30a0a9fc..33891c1f3 100644
--- a/search-service/config/detekt/baseline.xml
+++ b/search-service/config/detekt/baseline.xml
@@ -10,7 +10,6 @@
LongMethod:AttributeInstanceService.kt$AttributeInstanceService$@Transactional suspend fun create(attributeInstance: AttributeInstance): Either<APIException, Unit>
LongMethod:EnabledAuthorizationServiceTests.kt$EnabledAuthorizationServiceTests$@Test fun `it should return serialized access control entities with other rigths if user is owner`()
LongMethod:EntityAccessControlHandler.kt$EntityAccessControlHandler$@PostMapping("/{subjectId}/attrs", consumes = [MediaType.APPLICATION_JSON_VALUE, JSON_LD_CONTENT_TYPE]) suspend fun addRightsOnEntities( @RequestHeader httpHeaders: HttpHeaders, @PathVariable subjectId: String, @RequestBody requestBody: Mono<String> ): ResponseEntity<*>
- LongMethod:EntityOperationHandlerTests.kt$EntityOperationHandlerTests$@Test fun `create batch entity should return a 207 when some entities already exist`()
LongMethod:PatchAttributeTests.kt$PatchAttributeTests.Companion$@JvmStatic fun mergePatchProvider(): Stream<Arguments>
LongMethod:PatchAttributeTests.kt$PatchAttributeTests.Companion$@JvmStatic fun partialUpdatePatchProvider(): Stream<Arguments>
LongMethod:QueryServiceTests.kt$QueryServiceTests$@Test fun `it should query temporal entities as requested by query params`()
diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/service/EntityOperationService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/service/EntityOperationService.kt
index 935cbbf8d..809a2c7f1 100644
--- a/search-service/src/main/kotlin/com/egm/stellio/search/service/EntityOperationService.kt
+++ b/search-service/src/main/kotlin/com/egm/stellio/search/service/EntityOperationService.kt
@@ -5,6 +5,7 @@ import arrow.core.left
import arrow.core.raise.either
import arrow.core.right
import com.egm.stellio.search.authorization.AuthorizationService
+import com.egm.stellio.search.model.EntityPayload
import com.egm.stellio.search.model.UpdateResult
import com.egm.stellio.search.web.*
import com.egm.stellio.shared.model.APIException
@@ -21,7 +22,8 @@ import java.net.URI
class EntityOperationService(
private val entityPayloadService: EntityPayloadService,
private val temporalEntityAttributeService: TemporalEntityAttributeService,
- private val authorizationService: AuthorizationService
+ private val authorizationService: AuthorizationService,
+ private val entityEventService: EntityEventService
) {
/**
@@ -85,7 +87,13 @@ class EntityOperationService(
val creationResults = entities.map { jsonLdNgsiLdEntity ->
either {
entityPayloadService.createEntity(jsonLdNgsiLdEntity.second, jsonLdNgsiLdEntity.first, sub)
- .map {
+ .onRight {
+ entityEventService.publishEntityCreateEvent(
+ sub,
+ jsonLdNgsiLdEntity.second.id,
+ jsonLdNgsiLdEntity.second.types
+ )
+ }.map {
BatchEntitySuccess(jsonLdNgsiLdEntity.entityId())
}.mapLeft { apiException ->
BatchEntityError(jsonLdNgsiLdEntity.entityId(), arrayListOf(apiException.message))
@@ -104,14 +112,17 @@ class EntityOperationService(
return BatchOperationResult(creationResults.second.toMutableList(), creationResults.first.toMutableList())
}
- suspend fun delete(entitiesIds: Set): BatchOperationResult {
- val deletionResults = entitiesIds.map {
- val entityId = it
+ suspend fun delete(entities: Set, sub: Sub?): BatchOperationResult {
+ val deletionResults = entities.map { entity ->
+ val entityId = entity.entityId
either {
entityPayloadService.deleteEntity(entityId)
- .map {
+ .onRight {
authorizationService.removeRightsOnEntity(entityId)
}
+ .onRight {
+ entityEventService.publishEntityDeleteEvent(sub, entity)
+ }
.map {
BatchEntitySuccess(entityId)
}
@@ -229,21 +240,35 @@ class EntityOperationService(
jsonLdEntity.getModifiableMembers(),
disallowOverwrite,
sub
- ).bind()
+ ).bind().also {
+ entityEventService.publishEntityReplaceEvent(
+ sub,
+ ngsiLdEntity.id,
+ ngsiLdEntity.types
+ )
+ }
}
suspend fun updateEntity(
entity: JsonLdNgsiLdEntity,
disallowOverwrite: Boolean,
sub: Sub?
- ): Either {
+ ): Either = either {
val (jsonLdEntity, ngsiLdEntity) = entity
- return entityPayloadService.appendAttributes(
+ entityPayloadService.appendAttributes(
ngsiLdEntity.id,
jsonLdEntity.getModifiableMembers(),
disallowOverwrite,
sub
- )
+ ).bind().also {
+ entityEventService.publishAttributeChangeEvents(
+ sub,
+ ngsiLdEntity.id,
+ jsonLdEntity.members,
+ it,
+ true
+ )
+ }
}
@SuppressWarnings("UnusedParameter")
@@ -251,13 +276,21 @@ class EntityOperationService(
entity: JsonLdNgsiLdEntity,
disallowOverwrite: Boolean,
sub: Sub?
- ): Either {
+ ): Either = either {
val (jsonLdEntity, ngsiLdEntity) = entity
- return entityPayloadService.mergeEntity(
+ entityPayloadService.mergeEntity(
ngsiLdEntity.id,
jsonLdEntity.getModifiableMembers(),
null,
sub
- )
+ ).bind().also {
+ entityEventService.publishAttributeChangeEvents(
+ sub,
+ ngsiLdEntity.id,
+ jsonLdEntity.members,
+ it,
+ true
+ )
+ }
}
}
diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/web/EntityOperationHandler.kt b/search-service/src/main/kotlin/com/egm/stellio/search/web/EntityOperationHandler.kt
index 122198fec..92a7152a9 100644
--- a/search-service/src/main/kotlin/com/egm/stellio/search/web/EntityOperationHandler.kt
+++ b/search-service/src/main/kotlin/com/egm/stellio/search/web/EntityOperationHandler.kt
@@ -4,7 +4,6 @@ import arrow.core.*
import arrow.core.raise.either
import com.egm.stellio.search.authorization.AuthorizationService
import com.egm.stellio.search.model.Query
-import com.egm.stellio.search.service.EntityEventService
import com.egm.stellio.search.service.EntityOperationService
import com.egm.stellio.search.service.EntityPayloadService
import com.egm.stellio.search.service.QueryService
@@ -36,8 +35,7 @@ class EntityOperationHandler(
private val entityOperationService: EntityOperationService,
private val entityPayloadService: EntityPayloadService,
private val queryService: QueryService,
- private val authorizationService: AuthorizationService,
- private val entityEventService: EntityEventService
+ private val authorizationService: AuthorizationService
) {
/**
@@ -53,7 +51,6 @@ class EntityOperationHandler(
val (parsedEntities, unparsableEntities) = prepareEntitiesFromRequestBody(requestBody, httpHeaders).bind()
val (uniqueEntities, duplicateEntities) =
-
entityOperationService.splitEntitiesByUniqueness(parsedEntities)
val (existingEntities, newEntities) = entityOperationService.splitEntitiesByExistence(uniqueEntities)
val (unauthorizedEntities, authorizedEntities) = newEntities.partition {
@@ -121,11 +118,6 @@ class EntityOperationHandler(
else -> entityOperationService.replace(existingEntitiesAuthorized, sub.getOrNull())
}
- if (options == "update")
- publishUpdateEvents(sub.getOrNull(), updateOperationResult, parsedEntities)
- else
- publishReplaceEvents(sub.getOrNull(), updateOperationResult, parsedEntities)
-
batchOperationResult.errors.addAll(updateOperationResult.errors)
batchOperationResult.success.addAll(updateOperationResult.success)
}
@@ -171,8 +163,6 @@ class EntityOperationHandler(
val updateOperationResult =
entityOperationService.update(existingEntitiesAuthorized, disallowOverwrite, sub.getOrNull())
- publishUpdateEvents(sub.getOrNull(), updateOperationResult, parsedEntities)
-
batchOperationResult.errors.addAll(updateOperationResult.errors)
batchOperationResult.success.addAll(updateOperationResult.success)
}
@@ -213,8 +203,6 @@ class EntityOperationHandler(
val mergeOperationResult =
entityOperationService.merge(existingEntitiesAuthorized, sub.getOrNull())
- publishUpdateEvents(sub.getOrNull(), mergeOperationResult, parsedEntities)
-
batchOperationResult.errors.addAll(mergeOperationResult.errors)
batchOperationResult.success.addAll(mergeOperationResult.success)
}
@@ -261,15 +249,7 @@ class EntityOperationHandler(
}
if (entitiesUserCanDelete.isNotEmpty()) {
- val deleteOperationResult = entityOperationService.delete(entitiesUserCanDelete.toSet())
-
- deleteOperationResult.success.map { it.entityId }.forEach { uri ->
- val entity = entitiesBeforeDelete.find { it.entityId == uri }!!
- entityEventService.publishEntityDeleteEvent(
- sub.getOrNull(),
- entity
- )
- }
+ val deleteOperationResult = entityOperationService.delete(entitiesBeforeDelete.toSet(), sub.getOrNull())
batchOperationResult.errors.addAll(deleteOperationResult.errors)
batchOperationResult.success.addAll(deleteOperationResult.success)
@@ -391,50 +371,8 @@ class EntityOperationHandler(
if (entitiesToCreate.isNotEmpty()) {
val createOperationResult = entityOperationService.create(entitiesToCreate, sub.getOrNull())
authorizationService.createOwnerRights(createOperationResult.getSuccessfulEntitiesIds(), sub)
- entitiesToCreate
- .filter { it.second.id in createOperationResult.getSuccessfulEntitiesIds() }
- .forEach {
- val ngsiLdEntity = it.second
- entityEventService.publishEntityCreateEvent(
- sub.getOrNull(),
- ngsiLdEntity.id,
- ngsiLdEntity.types
- )
- }
batchOperationResult.errors.addAll(createOperationResult.errors)
batchOperationResult.success.addAll(createOperationResult.success)
}
}
-
- private suspend fun publishReplaceEvents(
- sub: String?,
- updateBatchOperationResult: BatchOperationResult,
- jsonLdNgsiLdEntities: List
- ) = jsonLdNgsiLdEntities.filter { it.entityId() in updateBatchOperationResult.getSuccessfulEntitiesIds() }
- .forEach {
- entityEventService.publishEntityReplaceEvent(
- sub,
- it.entityId(),
- it.second.types
- )
- }
-
- private suspend fun publishUpdateEvents(
- sub: String?,
- updateBatchOperationResult: BatchOperationResult,
- jsonLdNgsiLdEntities: List
- ) {
- updateBatchOperationResult.success.forEach {
- val (jsonLdEntity, _) = jsonLdNgsiLdEntities.find { jsonLdNgsiLdEntity ->
- jsonLdNgsiLdEntity.entityId() == it.entityId
- }!!
- entityEventService.publishAttributeChangeEvents(
- sub,
- it.entityId,
- jsonLdEntity.members,
- it.updateResult!!,
- true
- )
- }
- }
}
diff --git a/search-service/src/test/kotlin/com/egm/stellio/search/service/EntityOperationServiceTests.kt b/search-service/src/test/kotlin/com/egm/stellio/search/service/EntityOperationServiceTests.kt
index 7dd0be3ae..9cf6dd327 100644
--- a/search-service/src/test/kotlin/com/egm/stellio/search/service/EntityOperationServiceTests.kt
+++ b/search-service/src/test/kotlin/com/egm/stellio/search/service/EntityOperationServiceTests.kt
@@ -4,6 +4,7 @@ import arrow.core.left
import arrow.core.right
import com.egm.stellio.search.authorization.AuthorizationService
import com.egm.stellio.search.model.EMPTY_UPDATE_RESULT
+import com.egm.stellio.search.model.EntityPayload
import com.egm.stellio.search.model.NotUpdatedDetails
import com.egm.stellio.search.model.UpdateResult
import com.egm.stellio.search.web.BatchEntityError
@@ -15,10 +16,8 @@ import com.egm.stellio.shared.model.NgsiLdEntity
import com.egm.stellio.shared.util.Sub
import com.egm.stellio.shared.util.toUri
import com.ninjasquad.springmockk.MockkBean
-import io.mockk.coEvery
-import io.mockk.coVerify
-import io.mockk.every
-import io.mockk.mockkClass
+import io.mockk.*
+import kotlinx.coroutines.Job
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
@@ -41,6 +40,9 @@ class EntityOperationServiceTests {
@MockkBean
private lateinit var authorizationService: AuthorizationService
+ @MockkBean
+ private lateinit var entityEventService: EntityEventService
+
@Autowired
private lateinit var entityOperationService: EntityOperationService
@@ -130,6 +132,7 @@ class EntityOperationServiceTests {
coEvery {
entityPayloadService.appendAttributes(secondEntityURI, any(), any(), any())
} returns BadRequestDataException("error").left()
+ coEvery { entityEventService.publishAttributeChangeEvents(any(), any(), any(), any(), any()) } returns Job()
val batchOperationResult =
entityOperationService.processEntities(
@@ -150,53 +153,72 @@ class EntityOperationServiceTests {
listOf(BatchEntityError(secondEntityURI, arrayListOf("error"))),
batchOperationResult.errors
)
+ coVerify {
+ entityEventService.publishAttributeChangeEvents(
+ sub,
+ eq(firstEntityURI),
+ any(),
+ match { it.isSuccessful() },
+ any()
+ )
+ }
+ coVerify {
+ entityEventService.publishAttributeChangeEvents(
+ sub,
+ eq(secondEntityURI),
+ any(),
+ any(),
+ any()
+ ) wasNot Called
+ }
}
@Test
- fun `processEntities should count as error not processed attributes in entities`() =
- runTest {
- val updateResult = UpdateResult(
- emptyList(),
- listOf(
- NotUpdatedDetails("attribute#1", "reason"),
- NotUpdatedDetails("attribute#2", "reason")
- )
+ fun `processEntities should count as error not processed attributes in entities`() = runTest {
+ val updateResult = UpdateResult(
+ emptyList(),
+ listOf(
+ NotUpdatedDetails("attribute#1", "reason"),
+ NotUpdatedDetails("attribute#2", "reason")
)
- coEvery {
- entityPayloadService.appendAttributes(firstEntityURI, any(), any(), any())
- } returns EMPTY_UPDATE_RESULT.right()
- coEvery {
- entityPayloadService.appendAttributes(secondEntityURI, any(), any(), any())
- } returns updateResult.right()
+ )
+ coEvery {
+ entityPayloadService.appendAttributes(firstEntityURI, any(), any(), any())
+ } returns EMPTY_UPDATE_RESULT.right()
+ coEvery {
+ entityPayloadService.appendAttributes(secondEntityURI, any(), any(), any())
+ } returns updateResult.right()
+ coEvery { entityEventService.publishAttributeChangeEvents(any(), any(), any(), any(), any()) } returns Job()
- val batchOperationResult = entityOperationService.processEntities(
- listOf(
- firstExpandedEntity to firstEntity,
- secondExpandedEntity to secondEntity
- ),
- false,
- sub,
- entityOperationService::updateEntity
- )
+ val batchOperationResult = entityOperationService.processEntities(
+ listOf(
+ firstExpandedEntity to firstEntity,
+ secondExpandedEntity to secondEntity
+ ),
+ false,
+ sub,
+ entityOperationService::updateEntity
+ )
- assertEquals(
- listOf(BatchEntitySuccess(firstEntityURI, EMPTY_UPDATE_RESULT)),
- batchOperationResult.success
- )
- assertEquals(
- listOf(
- BatchEntityError(
- secondEntityURI,
- arrayListOf("attribute#1 : reason, attribute#2 : reason")
- )
- ),
- batchOperationResult.errors
- )
- }
+ assertEquals(
+ listOf(BatchEntitySuccess(firstEntityURI, EMPTY_UPDATE_RESULT)),
+ batchOperationResult.success
+ )
+ assertEquals(
+ listOf(
+ BatchEntityError(
+ secondEntityURI,
+ arrayListOf("attribute#1 : reason, attribute#2 : reason")
+ )
+ ),
+ batchOperationResult.errors
+ )
+ }
@Test
fun `batch create should ask to create all provided entities`() = runTest {
coEvery { entityPayloadService.createEntity(any(), any(), any()) } returns Unit.right()
+ coEvery { entityEventService.publishEntityCreateEvent(any(), any(), any()) } returns Job()
val batchOperationResult = entityOperationService.create(
listOf(
@@ -218,6 +240,9 @@ class EntityOperationServiceTests {
coVerify {
entityPayloadService.createEntity(secondEntity, secondExpandedEntity, sub)
}
+ coVerify(exactly = 2) {
+ entityEventService.publishEntityCreateEvent(any(), any(), any())
+ }
}
@Test
@@ -226,6 +251,7 @@ class EntityOperationServiceTests {
coEvery {
entityPayloadService.createEntity(secondEntity, any(), any())
} returns BadRequestDataException("Invalid entity").left()
+ coEvery { entityEventService.publishEntityCreateEvent(any(), any(), any()) } returns Job()
val batchOperationResult = entityOperationService.create(
listOf(
@@ -242,6 +268,9 @@ class EntityOperationServiceTests {
),
batchOperationResult.errors
)
+ coVerify(exactly = 1) {
+ entityEventService.publishEntityCreateEvent(any(), any(), any())
+ }
}
@Test
@@ -249,6 +278,7 @@ class EntityOperationServiceTests {
coEvery {
entityPayloadService.appendAttributes(any(), any(), any(), any())
} returns EMPTY_UPDATE_RESULT.right()
+ coEvery { entityEventService.publishAttributeChangeEvents(any(), any(), any(), any(), any()) } returns Job()
val batchOperationResult = entityOperationService.update(
listOf(
@@ -270,6 +300,9 @@ class EntityOperationServiceTests {
coVerify {
entityPayloadService.appendAttributes(eq(secondEntityURI), any(), false, sub)
}
+ coVerify(exactly = 2) {
+ entityEventService.publishAttributeChangeEvents(any(), any(), any(), any(), any())
+ }
}
@Test
@@ -280,6 +313,7 @@ class EntityOperationServiceTests {
coEvery {
entityPayloadService.appendAttributes(any(), any(), any(), any())
} returns EMPTY_UPDATE_RESULT.right()
+ coEvery { entityEventService.publishEntityReplaceEvent(any(), any(), any()) } returns Job()
val batchOperationResult = entityOperationService.replace(
listOf(
@@ -297,21 +331,30 @@ class EntityOperationServiceTests {
coVerify { temporalEntityAttributeService.deleteTemporalAttributesOfEntity(firstEntityURI) }
coVerify { temporalEntityAttributeService.deleteTemporalAttributesOfEntity(secondEntityURI) }
-
coVerify {
entityPayloadService.appendAttributes(eq(firstEntityURI), any(), false, sub)
}
coVerify {
entityPayloadService.appendAttributes(eq(secondEntityURI), any(), false, sub)
}
+ coVerify(exactly = 2) {
+ entityEventService.publishEntityReplaceEvent(any(), any(), any())
+ }
}
@Test
fun `batch delete should return the list of deleted entity ids when deletion is successful`() = runTest {
coEvery { entityPayloadService.deleteEntity(any()) } returns Unit.right()
coEvery { authorizationService.removeRightsOnEntity(any()) } returns Unit.right()
+ coEvery { entityEventService.publishEntityDeleteEvent(any(), any()) } returns Job()
- val batchOperationResult = entityOperationService.delete(setOf(firstEntityURI, secondEntityURI))
+ val batchOperationResult = entityOperationService.delete(
+ setOf(
+ mockkClass(EntityPayload::class) { every { entityId } returns firstEntityURI },
+ mockkClass(EntityPayload::class) { every { entityId } returns secondEntityURI },
+ ),
+ sub
+ )
assertEquals(
listOf(firstEntityURI, secondEntityURI),
@@ -325,19 +368,28 @@ class EntityOperationServiceTests {
authorizationService.removeRightsOnEntity(firstEntityURI)
authorizationService.removeRightsOnEntity(secondEntityURI)
}
+ coVerify(exactly = 2) {
+ entityEventService.publishEntityDeleteEvent(sub, any())
+ }
}
@Test
- @SuppressWarnings("MaxLineLength")
- fun `batch delete should return the list of deleted entity ids and in errors when deletion is partially successful`() =
+ fun `batch delete should return deleted entity ids and in errors when deletion is partially successful`() =
runTest {
coEvery { entityPayloadService.deleteEntity(firstEntityURI) } returns Unit.right()
coEvery {
entityPayloadService.deleteEntity(secondEntityURI)
} returns InternalErrorException("Something went wrong during deletion").left()
coEvery { authorizationService.removeRightsOnEntity(any()) } returns Unit.right()
+ coEvery { entityEventService.publishEntityDeleteEvent(any(), any()) } returns Job()
- val batchOperationResult = entityOperationService.delete(setOf(firstEntityURI, secondEntityURI))
+ val batchOperationResult = entityOperationService.delete(
+ setOf(
+ mockkClass(EntityPayload::class) { every { entityId } returns firstEntityURI },
+ mockkClass(EntityPayload::class) { every { entityId } returns secondEntityURI },
+ ),
+ sub
+ )
assertEquals(
listOf(BatchEntitySuccess(firstEntityURI)),
@@ -352,6 +404,10 @@ class EntityOperationServiceTests {
),
batchOperationResult.errors
)
+ coVerify(exactly = 1) {
+ authorizationService.removeRightsOnEntity(any())
+ entityEventService.publishEntityDeleteEvent(any(), any())
+ }
}
@Test
@@ -362,7 +418,13 @@ class EntityOperationServiceTests {
entityPayloadService.deleteEntity(any())
} returns InternalErrorException(deleteEntityErrorMessage).left()
- val batchOperationResult = entityOperationService.delete(setOf(firstEntityURI, secondEntityURI))
+ val batchOperationResult = entityOperationService.delete(
+ setOf(
+ mockkClass(EntityPayload::class) { every { entityId } returns firstEntityURI },
+ mockkClass(EntityPayload::class) { every { entityId } returns secondEntityURI },
+ ),
+ sub
+ )
assertEquals(emptyList(), batchOperationResult.success)
assertEquals(
@@ -381,6 +443,7 @@ class EntityOperationServiceTests {
coVerify { entityPayloadService.deleteEntity(firstEntityURI) }
coVerify { entityPayloadService.deleteEntity(secondEntityURI) }
+ coVerify { entityEventService.publishEntityDeleteEvent(any(), any()) wasNot Called }
}
@Test
@@ -388,6 +451,7 @@ class EntityOperationServiceTests {
coEvery {
entityPayloadService.mergeEntity(any(), any(), any(), any())
} returns EMPTY_UPDATE_RESULT.right()
+ coEvery { entityEventService.publishAttributeChangeEvents(any(), any(), any(), any(), any()) } returns Job()
val batchOperationResult = entityOperationService.merge(
listOf(
@@ -404,9 +468,10 @@ class EntityOperationServiceTests {
coVerify {
entityPayloadService.mergeEntity(eq(firstEntityURI), any(), null, sub)
- }
- coVerify {
entityPayloadService.mergeEntity(eq(secondEntityURI), any(), null, sub)
}
+ coVerify(exactly = 2) {
+ entityEventService.publishAttributeChangeEvents(any(), any(), any(), any(), any())
+ }
}
}
diff --git a/search-service/src/test/kotlin/com/egm/stellio/search/web/EntityOperationHandlerTests.kt b/search-service/src/test/kotlin/com/egm/stellio/search/web/EntityOperationHandlerTests.kt
index f7693e2c0..1ee376b73 100644
--- a/search-service/src/test/kotlin/com/egm/stellio/search/web/EntityOperationHandlerTests.kt
+++ b/search-service/src/test/kotlin/com/egm/stellio/search/web/EntityOperationHandlerTests.kt
@@ -7,7 +7,6 @@ import com.egm.stellio.search.config.SearchProperties
import com.egm.stellio.search.model.EMPTY_UPDATE_RESULT
import com.egm.stellio.search.model.EntityPayload
import com.egm.stellio.search.model.UpdateResult
-import com.egm.stellio.search.service.EntityEventService
import com.egm.stellio.search.service.EntityOperationService
import com.egm.stellio.search.service.EntityPayloadService
import com.egm.stellio.search.service.QueryService
@@ -19,10 +18,8 @@ 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.Job
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
-import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
@@ -59,9 +56,6 @@ class EntityOperationHandlerTests {
@MockkBean
private lateinit var authorizationService: AuthorizationService
- @MockkBean(relaxed = true)
- private lateinit var entityEventService: EntityEventService
-
private val logger = LoggerFactory.getLogger(javaClass)
private lateinit var mockedTemperatureSensorEntity: NgsiLdEntity
@@ -313,8 +307,6 @@ class EntityOperationHandlerTests {
fun `create batch entity should return a 201 if JSON-LD payload is correct`() = runTest {
val jsonLdFile = validJsonFile
val capturedExpandedEntities = slot>()
- val capturedEntitiesIds = mutableListOf()
- val capturedEntityTypes = slot>()
coEvery {
entityOperationService.splitEntitiesByUniqueness(capture(capturedExpandedEntities))
@@ -328,13 +320,6 @@ class EntityOperationHandlerTests {
arrayListOf()
)
coEvery { authorizationService.createOwnerRights(any(), eq(sub)) } returns Unit.right()
- coEvery {
- entityEventService.publishEntityCreateEvent(
- any(),
- capture(capturedEntitiesIds),
- capture(capturedEntityTypes)
- )
- } returns Job()
webClient.post()
.uri(batchCreateEndpoint)
@@ -348,11 +333,6 @@ class EntityOperationHandlerTests {
assertEquals(allEntitiesUris, capturedExpandedEntities.captured.map { it.entityId() })
coVerify { authorizationService.createOwnerRights(allEntitiesUris, sub) }
- coVerify(timeout = 1000, exactly = 3) {
- entityEventService.publishEntityCreateEvent(any(), any(), any())
- }
- capturedEntitiesIds.forEach { assertTrue(it in allEntitiesUris) }
- assertTrue(capturedEntityTypes.captured[0] in listOf(SENSOR_TYPE, DEVICE_TYPE))
}
@Test
@@ -360,8 +340,6 @@ class EntityOperationHandlerTests {
val jsonLdFile = validJsonFile
val capturedExpandedEntities = slot>()
val createdEntitiesIds = arrayListOf(dissolvedOxygenSensorUri, deviceUri)
- val capturedEntitiesIds = mutableListOf()
- val capturedEntityTypes = slot>()
coEvery {
entityOperationService.splitEntitiesByUniqueness(capture(capturedExpandedEntities))
@@ -385,13 +363,6 @@ class EntityOperationHandlerTests {
arrayListOf()
)
coEvery { authorizationService.createOwnerRights(any(), eq(sub)) } returns Unit.right()
- coEvery {
- entityEventService.publishEntityCreateEvent(
- any(),
- capture(capturedEntitiesIds),
- capture(capturedEntityTypes)
- )
- } returns Job()
webClient.post()
.uri(batchCreateEndpoint)
@@ -416,11 +387,6 @@ class EntityOperationHandlerTests {
)
coVerify { authorizationService.createOwnerRights(createdEntitiesIds, sub) }
- coVerify(timeout = 1000, exactly = 2) {
- entityEventService.publishEntityCreateEvent(any(), any(), any())
- }
- capturedEntitiesIds.forEach { assertTrue(it in createdEntitiesIds) }
- assertTrue(capturedEntityTypes.captured[0] in listOf(SENSOR_TYPE, DEVICE_TYPE))
}
@Test
@@ -428,8 +394,6 @@ class EntityOperationHandlerTests {
val jsonLdFile = validJsonFile
val capturedExpandedEntities = slot>()
val createdEntitiesIds = arrayListOf(dissolvedOxygenSensorUri, deviceUri)
- val capturedEntitiesIds = mutableListOf()
- val capturedEntityTypes = slot>()
coEvery {
entityOperationService.splitEntitiesByUniqueness(any())
@@ -451,13 +415,6 @@ class EntityOperationHandlerTests {
arrayListOf()
)
coEvery { authorizationService.createOwnerRights(any(), eq(sub)) } returns Unit.right()
- coEvery {
- entityEventService.publishEntityCreateEvent(
- any(),
- capture(capturedEntitiesIds),
- capture(capturedEntityTypes)
- )
- } returns Job()
webClient.post()
.uri(batchCreateEndpoint)
@@ -514,8 +471,6 @@ class EntityOperationHandlerTests {
}
""".trimIndent()
)
-
- verify { entityEventService wasNot called }
}
@Test
@@ -570,13 +525,9 @@ class EntityOperationHandlerTests {
coEvery { authorizationService.userCanCreateEntities(any()) } returns Unit.right()
coEvery { entityOperationService.create(any(), any()) } returns createdBatchResult
coEvery { authorizationService.createOwnerRights(any(), eq(sub)) } returns Unit.right()
- coEvery { entityEventService.publishEntityCreateEvent(any(), any(), any()) } returns Job()
coEvery { authorizationService.userCanUpdateEntity(any(), sub) } returns Unit.right()
coEvery { entityOperationService.update(any(), any(), any()) } returns updatedBatchResult
- coEvery {
- entityEventService.publishAttributeChangeEvents(any(), any(), any(), any(), true)
- } returns Job()
webClient.post()
.uri(batchUpsertWithUpdateEndpoint)
@@ -587,22 +538,6 @@ class EntityOperationHandlerTests {
.jsonPath("$[*]").isEqualTo(createdEntitiesIds.map { it.toString() })
coVerify { authorizationService.createOwnerRights(createdEntitiesIds, sub) }
- coVerify {
- entityEventService.publishEntityCreateEvent(
- eq(sub.value),
- match { it in createdEntitiesIds },
- eq(listOf(SENSOR_TYPE))
- )
- }
- coVerify(timeout = 1000, exactly = 2) {
- entityEventService.publishAttributeChangeEvents(
- eq(sub.value),
- match { it in updatedEntitiesIds },
- any(),
- match { it in updatedBatchResult.success.map { it.updateResult } },
- true
- )
- }
}
@Test
@@ -691,7 +626,6 @@ class EntityOperationHandlerTests {
)
coVerify { authorizationService.createOwnerRights(listOf(deviceUri), sub) }
- coVerify(exactly = 1) { entityEventService.publishEntityCreateEvent(any(), any(), any()) }
}
@Test
@@ -715,7 +649,6 @@ class EntityOperationHandlerTests {
entitiesIds.map { BatchEntitySuccess(it) }.toMutableList(),
arrayListOf()
)
- coEvery { entityEventService.publishEntityCreateEvent(any(), any(), any()) } returns Job()
webClient.post()
.uri(batchUpsertEndpoint)
@@ -726,13 +659,6 @@ class EntityOperationHandlerTests {
coVerify { entityOperationService.create(any(), any()) wasNot Called }
coVerify { entityOperationService.replace(any(), sub.getOrNull()) }
coVerify { entityOperationService.update(any(), any(), any()) wasNot Called }
- coVerify(timeout = 1000, exactly = 2) {
- entityEventService.publishEntityReplaceEvent(
- eq(sub.value),
- match { it in entitiesIds },
- eq(listOf(SENSOR_TYPE))
- )
- }
}
@Test
@@ -772,7 +698,6 @@ class EntityOperationHandlerTests {
""".trimIndent()
)
- coVerify { entityEventService wasNot called }
coVerify { entityOperationService.replace(any(), any()) wasNot Called }
}
@@ -824,13 +749,6 @@ class EntityOperationHandlerTests {
)
coVerify { entityOperationService.replace(any(), sub.getOrNull()) }
- coVerify {
- entityEventService.publishEntityReplaceEvent(
- eq(sub.value),
- eq(temperatureSensorUri),
- eq(listOf(SENSOR_TYPE))
- )
- }
}
@Test
@@ -868,7 +786,7 @@ class EntityOperationHandlerTests {
Pair(allEntitiesUris, emptyList())
}
coEvery { authorizationService.userCanAdminEntity(any(), any()) } returns Unit.right()
- coEvery { entityOperationService.delete(any()) } returns
+ coEvery { entityOperationService.delete(any(), any()) } returns
BatchOperationResult(
allEntitiesUris.map { BatchEntitySuccess(it) }.toMutableList(),
mutableListOf()
@@ -889,20 +807,12 @@ class EntityOperationHandlerTests {
every { types } returns listOf(DEVICE_TYPE)
}
)
- coEvery { entityEventService.publishEntityDeleteEvent(any(), any()) } returns Job()
webClient.post()
.uri(batchDeleteEndpoint)
.bodyValue(jsonLdFile)
.exchange()
.expectStatus().isNoContent
-
- coVerify(timeout = 1000, exactly = 3) {
- entityEventService.publishEntityDeleteEvent(
- eq(sub.value),
- any()
- )
- }
}
@Test
@@ -919,8 +829,7 @@ class EntityOperationHandlerTests {
coVerify { entityOperationService.splitEntitiesIdsByExistence(allEntitiesUris) }
coVerify { entityPayloadService wasNot called }
- coVerify { entityOperationService.delete(any()) wasNot Called }
- verify { entityEventService wasNot called }
+ coVerify { entityOperationService.delete(any(), any()) wasNot Called }
}
@Test
@@ -967,8 +876,7 @@ class EntityOperationHandlerTests {
performBatchDeleteAndCheck207Response(ENTITY_DELETE_FORBIDDEN_MESSAGE)
- coVerify { entityOperationService.delete(any()) wasNot Called }
- verify { entityEventService wasNot called }
+ coVerify { entityOperationService.delete(any(), any()) wasNot Called }
}
private fun performBatchDeleteAndCheck207Response(expectedErrorMessage: String) {