Skip to content

Commit

Permalink
Accept unknown observatory and organizations ids when updating a reso…
Browse files Browse the repository at this point in the history
…urce (!1218)
  • Loading branch information
MarcelKonrad committed Feb 14, 2025
1 parent 6256cbe commit 037ec4e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<ObservatoryNotFound> { 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()
Expand All @@ -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<OrganizationNotFound> { 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()
Expand Down

0 comments on commit 037ec4e

Please sign in to comment.