Skip to content

Commit

Permalink
Merge branch '1.0.x' into PIN-4448
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli authored Jan 26, 2024
2 parents d4b3c10 + 4a4dd5a commit cb1548e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import it.pagopa.interop.commons.riskanalysis.{model => Commons}
import it.pagopa.interop.commons.utils.AkkaUtils._
import it.pagopa.interop.commons.utils.OpenapiUtils.parseArrayParameters
import it.pagopa.interop.commons.utils.TypeConversions._
import it.pagopa.interop.commons.utils.errors.GenericComponentErrors
import it.pagopa.interop.commons.utils.errors.{ComponentError, GenericComponentErrors}

import java.util.UUID
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -106,6 +106,7 @@ final case class ProcessApiServiceImpl(
organizationId <- getOrganizationIdFutureUUID(contexts)
eServiceUuid <- eServiceId.toFutureUUID
catalogItem <- catalogManagementService.getEServiceById(eServiceUuid)
_ <- descriptorDeletable(catalogItem, descriptorId).toFuture
_ <- assertRequesterAllowed(catalogItem.producerId)(organizationId)
result <- catalogManagementService.deleteDraft(eServiceId, descriptorId)
} yield result
Expand All @@ -115,6 +116,21 @@ final case class ProcessApiServiceImpl(
}
}

private def descriptorDeletable(catalogItem: CatalogItem, descriptorId: String): Either[ComponentError, Unit] =
getDescriptor(catalogItem, descriptorId).flatMap(descriptor =>
Left(NotValidDescriptor(descriptorId, descriptor.state.toString))
.withRight[Unit]
.unlessA(descriptor.state == Draft)
)

private def getDescriptor(
eService: CatalogItem,
descriptorId: String
): Either[EServiceDescriptorNotFound, CatalogDescriptor] =
eService.descriptors
.find(_.id.toString == descriptorId)
.toRight(EServiceDescriptorNotFound(eService.id.toString, descriptorId))

override def getEServices(
name: Option[String],
eServicesIds: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ object ResponseHandlers extends AkkaResponses {
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: NotValidDescriptor) => badRequest(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

Expand All @@ -189,6 +190,7 @@ object ResponseHandlers extends AkkaResponses {
case Failure(ex: EServiceDescriptorWithoutInterface) => badRequest(ex, logMessage)
case Failure(ex: EServiceRiskAnalysisIsRequired) => badRequest(ex, logMessage)
case Failure(ex: RiskAnalysisNotValid.type) => badRequest(ex, logMessage)
case Failure(ex: NotValidDescriptor) => badRequest(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
SpecData.catalogItem.id.toString,
SpecData.catalogDescriptor.id.toString
) ~> check {
status shouldEqual StatusCodes.InternalServerError
status shouldEqual StatusCodes.BadRequest
}
}
"fail if EService does not exist" in {
Expand Down Expand Up @@ -2099,13 +2099,18 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> UUID.randomUUID().toString)

val draftDescriptor = SpecData.catalogDescriptor.copy(state = Draft)

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

Delete() ~> service.deleteDraft(SpecData.catalogItem.id.toString, UUID.randomUUID.toString) ~> check {
Delete() ~> service.deleteDraft(SpecData.catalogItem.id.toString, draftDescriptor.id.toString) ~> check {
status shouldEqual StatusCodes.Forbidden
}
}
Expand Down

0 comments on commit cb1548e

Please sign in to comment.