diff --git a/graph/graph-core-services/src/main/kotlin/org/orkg/graph/domain/ResourceService.kt b/graph/graph-core-services/src/main/kotlin/org/orkg/graph/domain/ResourceService.kt index 12c876c00..1b665b10e 100644 --- a/graph/graph-core-services/src/main/kotlin/org/orkg/graph/domain/ResourceService.kt +++ b/graph/graph-core-services/src/main/kotlin/org/orkg/graph/domain/ResourceService.kt @@ -124,42 +124,41 @@ class ResourceService( .orElseThrow { ResourceNotFound.withId(id) } override fun update(command: UpdateCommand) { - if (!command.hasNoContents()) { - val resource = repository.findById(command.id) - .orElseThrow { ResourceNotFound.withId(command.id) } - if (!resource.modifiable) { - throw ResourceNotModifiable(command.id) - } - command.label?.also { Label.ofOrNull(it) ?: throw InvalidLabel() } - command.classes?.also { validateClasses(it) } - command.observatoryId?.let { observatoryId -> - if (observatoryId != resource.observatoryId && !observatoryRepository.existsById(observatoryId)) { - throw ObservatoryNotFound(observatoryId) - } - } - command.organizationId?.let { organizationId -> - if (organizationId != resource.organizationId && organizationRepository.findById(organizationId).isEmpty) { - throw OrganizationNotFound(organizationId) - } + if (command.hasNoContents()) return + val resource = repository.findById(command.id) + .orElseThrow { ResourceNotFound.withId(command.id) } + if (!resource.modifiable) { + throw ResourceNotModifiable(command.id) + } + command.label?.also { Label.ofOrNull(it) ?: throw InvalidLabel() } + command.classes?.also { validateClasses(it) } + command.observatoryId?.let { observatoryId -> + if (observatoryId != resource.observatoryId && observatoryId != ObservatoryId.UNKNOWN && !observatoryRepository.existsById(observatoryId)) { + throw ObservatoryNotFound(observatoryId) } - val contributor by lazy { - contributorRepository.findById(command.contributorId) - .orElseThrow { ContributorNotFound(command.contributorId) } + } + command.organizationId?.let { organizationId -> + if (organizationId != resource.organizationId && organizationId != OrganizationId.UNKNOWN && organizationRepository.findById(organizationId).isEmpty) { + throw OrganizationNotFound(organizationId) } - if (command.visibility != null && command.visibility != resource.visibility) { - if (!resource.isOwnedBy(command.contributorId) || !isAllowedVisibilityChangeByOwner(command.visibility!!, resource.visibility)) { - if (!contributor.isCurator) { - throw NeitherOwnerNorCurator.cannotChangeVisibility(resource.id) - } + } + val contributor by lazy { + contributorRepository.findById(command.contributorId) + .orElseThrow { ContributorNotFound(command.contributorId) } + } + if (command.visibility != null && command.visibility != resource.visibility) { + if (!resource.isOwnedBy(command.contributorId) || !isAllowedVisibilityChangeByOwner(command.visibility!!, resource.visibility)) { + if (!contributor.isCurator) { + throw NeitherOwnerNorCurator.cannotChangeVisibility(resource.id) } } - if (command.verified != null && command.verified != resource.verified && !contributor.isCurator) { - throw NotACurator.cannotChangeVerifiedStatus(contributor.id) - } - val updated = resource.apply(command) - if (updated != resource) { - repository.save(updated) - } + } + if (command.verified != null && command.verified != resource.verified && !contributor.isCurator) { + throw NotACurator.cannotChangeVerifiedStatus(contributor.id) + } + val updated = resource.apply(command) + if (updated != resource) { + repository.save(updated) } } diff --git a/graph/graph-core-services/src/test/kotlin/org/orkg/graph/domain/ResourceServiceUnitTest.kt b/graph/graph-core-services/src/test/kotlin/org/orkg/graph/domain/ResourceServiceUnitTest.kt index 838727a53..1f6925251 100644 --- a/graph/graph-core-services/src/test/kotlin/org/orkg/graph/domain/ResourceServiceUnitTest.kt +++ b/graph/graph-core-services/src/test/kotlin/org/orkg/graph/domain/ResourceServiceUnitTest.kt @@ -1,6 +1,7 @@ package org.orkg.graph.domain import io.kotest.assertions.asClue +import io.kotest.assertions.throwables.shouldNotThrow import io.kotest.assertions.throwables.shouldThrow import io.kotest.matchers.shouldBe import io.mockk.every @@ -515,6 +516,24 @@ internal class ResourceServiceUnitTest : MockkBaseTest { verify(exactly = 1) { observatoryRepository.existsById(command.observatoryId!!) } } + @Test + fun `Given a resource update command, when observatory is unknown, is does not throw an exception`() { + val resource = createResource(observatoryId = ObservatoryId("04c4d2f9-82e0-47c3-b0ef-79c1f515a940")) + val command = UpdateResourceUseCase.UpdateCommand( + id = resource.id, + contributorId = ContributorId(MockUserId.USER), + observatoryId = ObservatoryId.UNKNOWN + ) + + every { repository.findById(resource.id) } returns Optional.of(resource) + every { repository.save(any()) } just runs + + shouldNotThrow { service.update(command) } + + verify(exactly = 1) { repository.findById(resource.id) } + verify(exactly = 1) { repository.save(withArg { it.observatoryId shouldBe ObservatoryId.UNKNOWN }) } + } + @Test fun `Given a resource update command, when organization does not exist, it throws an exception`() { val resource = createResource() @@ -533,6 +552,24 @@ internal class ResourceServiceUnitTest : MockkBaseTest { verify(exactly = 1) { organizationRepository.findById(command.organizationId!!) } } + @Test + fun `Given a resource update command, when organization is unknown, is does not throw an exception`() { + val resource = createResource(organizationId = OrganizationId("04c4d2f9-82e0-47c3-b0ef-79c1f515a940")) + val command = UpdateResourceUseCase.UpdateCommand( + id = resource.id, + contributorId = ContributorId(MockUserId.USER), + organizationId = OrganizationId.UNKNOWN + ) + + every { repository.findById(resource.id) } returns Optional.of(resource) + every { repository.save(any()) } just runs + + shouldNotThrow { service.update(command) } + + verify(exactly = 1) { repository.findById(resource.id) } + verify(exactly = 1) { repository.save(withArg { it.organizationId shouldBe OrganizationId.UNKNOWN }) } + } + @Test fun `Given a resource update command, when visibility is changed to unlisted, it sets the unlisted by metadata`() { val resource = createResource()