Skip to content

Commit

Permalink
PIN-4464 BKE - Allow document deletion only on Draft descriptor (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli authored Jan 31, 2024
1 parent fd1bad1 commit ab4f25d
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 13 deletions.
16 changes: 11 additions & 5 deletions src/main/resources/interface-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -792,14 +792,20 @@ paths:
responses:
'204':
description: Document deleted.
'404':
description: E-Service descriptor document not found
'400':
description: Bad request
content:
application/problem+json:
schema:
$ref: '#/components/schemas/Problem'
'400':
description: Bad request
$ref: '#/components/schemas/Problem'
'403':
description: Forbidden
content:
application/problem+json:
schema:
$ref: '#/components/schemas/Problem'
'404':
description: E-Service descriptor document not found
content:
application/problem+json:
schema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ final case class ProcessApiServiceImpl(
documentUuid <- documentId.toFutureUUID
catalogItem <- catalogManagementService.getEServiceById(eServiceUuid)
_ <- assertRequesterAllowed(catalogItem.producerId)(organizationId)
descriptor <- assertDescriptorExists(catalogItem, descriptorUuid)
_ <- isDraftDescriptor(descriptor)
result <- catalogManagementService.deleteEServiceDocument(
eServiceUuid.toString,
descriptorUuid.toString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,10 @@ object ResponseHandlers extends AkkaResponses {
)(result: Try[T])(implicit contexts: Seq[(String, String)], logger: LoggerTakingImplicit[ContextFieldsToLog]): Route =
result match {
case Success(s) => success(s)
case Failure(ex: NotValidDescriptor) => badRequest(ex, logMessage)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceDescriptorNotFound) => notFound(ex, logMessage)
case Failure(ex: DescriptorDocumentNotFound) => notFound(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,27 +786,40 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
}
}
"EService document deletion" should {
"succeed" in {
val requesterId = UUID.randomUUID()
"succeed only on Draft descriptor" in {
val requesterId = UUID.randomUUID()
val descriptorId = UUID.randomUUID()

implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString)

val eServiceDoc = SpecData.eServiceDoc

val descriptor =
val eServiceDescriptor =
SpecData.eServiceDescriptor.copy(
state = CatalogManagementDependency.EServiceDescriptorState.PUBLISHED,
id = descriptorId,
state = CatalogManagementDependency.EServiceDescriptorState.DRAFT,
docs = Seq(eServiceDoc)
)

val eService = SpecData.eService.copy(descriptors = Seq(descriptor), producerId = requesterId)
val eService = SpecData.eService.copy(descriptors = Seq(eServiceDescriptor), producerId = requesterId)

val descriptor =
SpecData.catalogDescriptor.copy(
id = descriptorId,
docs = Seq(SpecData.catalogDocument.copy(id = eServiceDoc.id)),
state = Draft
)

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(eService.id, *, *)
.once()
.returns(Future.successful(SpecData.catalogItem.copy(id = eService.id, producerId = requesterId)))
.returns(
Future.successful(
SpecData.catalogItem.copy(id = eService.id, producerId = requesterId, descriptors = Seq(descriptor))
)
)

(mockCatalogManagementService
.deleteEServiceDocument(_: String, _: String, _: String)(_: Seq[(String, String)]))
Expand Down Expand Up @@ -860,6 +873,82 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
status shouldEqual StatusCodes.NotFound
}
}
"fail if descriptor does not exists" in {
val requesterId = UUID.randomUUID()

implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString)

val eService = SpecData.eService.copy(descriptors = Seq.empty, producerId = requesterId)

val descriptor =
SpecData.catalogDescriptor.copy(
id = UUID.randomUUID(),
docs = Seq(SpecData.catalogDocument.copy(id = SpecData.eServiceDoc.id)),
state = Draft
)

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(eService.id, *, *)
.once()
.returns(
Future.successful(
SpecData.catalogItem.copy(id = eService.id, producerId = requesterId, descriptors = Seq(descriptor))
)
)

Delete() ~> service.deleteEServiceDocumentById(
eService.id.toString,
UUID.randomUUID().toString,
SpecData.eServiceDoc.id.toString
) ~> check {
status shouldEqual StatusCodes.NotFound
}
}
"fail if Descriptor is not Draft" in {
val requesterId = UUID.randomUUID()
val descriptorId = UUID.randomUUID()

implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString)

val eServiceDoc = SpecData.eServiceDoc

val eServiceDescriptor =
SpecData.eServiceDescriptor.copy(
id = descriptorId,
state = CatalogManagementDependency.EServiceDescriptorState.PUBLISHED,
docs = Seq(eServiceDoc)
)

val eService = SpecData.eService.copy(descriptors = Seq(eServiceDescriptor), producerId = requesterId)

val descriptor =
SpecData.catalogDescriptor.copy(
id = descriptorId,
docs = Seq(SpecData.catalogDocument.copy(id = eServiceDoc.id)),
state = Published
)

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(eService.id, *, *)
.once()
.returns(
Future.successful(
SpecData.catalogItem.copy(id = eService.id, producerId = requesterId, descriptors = Seq(descriptor))
)
)

Delete() ~> service.deleteEServiceDocumentById(
eService.id.toString,
descriptor.id.toString,
eServiceDoc.id.toString
) ~> check {
status shouldEqual StatusCodes.BadRequest
}
}
}
"Descriptor creation" should {
"succeed" in {
Expand Down Expand Up @@ -2749,7 +2838,7 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
}
}
"Document deletion" should {
"succeed" in {
"succeed only on Draft Descriptor" in {
val requesterId = UUID.randomUUID()
val descriptorId = UUID.randomUUID()
val documentId = UUID.randomUUID()
Expand All @@ -2758,7 +2847,11 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString)

val descriptor =
SpecData.catalogDescriptor.copy(id = descriptorId, docs = Seq(SpecData.catalogDocument.copy(id = documentId)))
SpecData.catalogDescriptor.copy(
id = descriptorId,
docs = Seq(SpecData.catalogDocument.copy(id = documentId)),
state = Draft
)

val eService = SpecData.catalogItem.copy(descriptors = Seq(descriptor), producerId = requesterId)

Expand Down Expand Up @@ -2820,6 +2913,67 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
status shouldEqual StatusCodes.Forbidden
}
}
"fail if descriptor does not exists" in {
val requesterId = UUID.randomUUID()
val descriptorId = UUID.randomUUID()
val documentId = UUID.randomUUID()

implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString)

val descriptor =
SpecData.catalogDescriptor.copy(
id = UUID.randomUUID(),
docs = Seq(SpecData.catalogDocument.copy(id = documentId))
)

val eService = SpecData.catalogItem.copy(descriptors = Seq(descriptor), producerId = requesterId)

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(SpecData.catalogItem.id, *, *)
.once()
.returns(Future.successful(eService))

Delete() ~> service.deleteEServiceDocumentById(
SpecData.catalogItem.id.toString,
descriptorId.toString,
documentId.toString
) ~> check {
status shouldEqual StatusCodes.NotFound
}
}
"fail if descriptor is not Draft" in {
val requesterId = UUID.randomUUID()
val descriptorId = UUID.randomUUID()
val documentId = UUID.randomUUID()

implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString)

val descriptor =
SpecData.catalogDescriptor.copy(
id = descriptorId,
docs = Seq(SpecData.catalogDocument.copy(id = documentId)),
state = Published
)

val eService = SpecData.catalogItem.copy(descriptors = Seq(descriptor), producerId = requesterId)

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(SpecData.catalogItem.id, *, *)
.once()
.returns(Future.successful(eService))

Delete() ~> service.deleteEServiceDocumentById(
SpecData.catalogItem.id.toString,
descriptorId.toString,
documentId.toString
) ~> check {
status shouldEqual StatusCodes.BadRequest
}
}
}
"Risk Analysis creation" should {
"succeed" in {
Expand Down

0 comments on commit ab4f25d

Please sign in to comment.