Skip to content

Commit

Permalink
PIN-4460 If operator try to delete an eservice with descriptors , he …
Browse files Browse the repository at this point in the history
…receives an error 409 from Management
  • Loading branch information
nttdata-rtorsoli committed Jan 23, 2024
1 parent 4a4dd5a commit 6e1d70c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ object ResponseHandlers extends AkkaResponses {
success: T => Route
)(result: Try[T])(implicit contexts: Seq[(String, String)], logger: LoggerTakingImplicit[ContextFieldsToLog]): Route =
result match {
case Success(s) => success(s)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
case Success(s) => success(s)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceWithDescriptorsNotDeletable) => conflict(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

def activateDescriptorResponse[T](logMessage: String)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ object CatalogProcessErrors {
}
final case class AttributeNotFound(attributeId: UUID)
extends ComponentError("0020", s"Attribute ${attributeId.toString} not found")

final case class EServiceWithDescriptorsNotDeletable(eServiceId: String)
extends ComponentError("0021", s"EService $eServiceId contains descriptors and cannot be deleted")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import it.pagopa.interop.catalogprocess.errors.CatalogProcessErrors.{
DescriptorDocumentNotFound,
EServiceDescriptorNotFound,
EServiceNotFound,
EServiceRiskAnalysisNotFound
EServiceRiskAnalysisNotFound,
EServiceWithDescriptorsNotDeletable
}
import it.pagopa.interop.commons.logging.{CanLogContextFields, ContextFieldsToLog}

Expand Down Expand Up @@ -74,6 +75,7 @@ final case class CatalogManagementServiceImpl(invoker: CatalogManagementInvoker,
invoker.invoke(request, s"E-Service $eServiceId deleted").recoverWith {
case err: ApiError[_] if err.code == 404 =>
Future.failed(EServiceNotFound(eServiceId))
case err: ApiError[_] if err.code == 409 => Future.failed(EServiceWithDescriptorsNotDeletable(eServiceId))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import it.pagopa.interop.catalogprocess.errors.CatalogProcessErrors.{
DescriptorDocumentNotFound,
EServiceNotFound,
EServiceRiskAnalysisNotFound,
AttributeNotFound
AttributeNotFound,
EServiceWithDescriptorsNotDeletable
}
import it.pagopa.interop.catalogprocess.model._
import it.pagopa.interop.commons.cqrs.service.ReadModelService
Expand Down Expand Up @@ -2118,14 +2119,12 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
"EService deletion" should {
"succeed" in {

val requesterId = UUID.randomUUID()
val descriptorId = UUID.randomUUID()
val requesterId = 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)

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

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
Expand Down Expand Up @@ -2159,6 +2158,33 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
status shouldEqual StatusCodes.NotFound
}
}
"fail if EService has descriptors" 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 descriptor = SpecData.catalogDescriptor.copy(id = descriptorId)

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

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

(mockCatalogManagementService
.deleteEService(_: String)(_: Seq[(String, String)]))
.expects(eService.id.toString, *)
.returning(Future.failed(EServiceWithDescriptorsNotDeletable(SpecData.catalogItem.id.toString)))
.once()

Delete() ~> service.deleteEService(SpecData.catalogItem.id.toString) ~> check {
status shouldEqual StatusCodes.Conflict
}
}
"fail if requester is not the Producer" in {
implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> UUID.randomUUID().toString)
Expand Down

0 comments on commit 6e1d70c

Please sign in to comment.