Skip to content

Commit

Permalink
Merge pull request #226 from pagopa/PIN-4448
Browse files Browse the repository at this point in the history
PIN-4448 Check in case of Interface load if there is already one present
  • Loading branch information
nttdata-rtorsoli authored Jan 30, 2024
2 parents 8785ab9 + 0febfb2 commit 3adf411
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ final case class ProcessApiServiceImpl(
catalogItem <- catalogManagementService.getEServiceById(eServiceUuid)
_ <- assertRequesterAllowed(catalogItem.producerId)(organizationId)
descriptor <- assertDescriptorExists(catalogItem, descriptorUuid)
_ <-
if (documentSeed.kind == EServiceDocumentKind.INTERFACE) assertInterfaceDoesNotExists(descriptor)
else Future.unit
updated <- catalogManagementService.createEServiceDocument(catalogItem.id, descriptor.id, managementSeed)
} yield updated.toApi

Expand Down Expand Up @@ -859,4 +862,9 @@ object ProcessApiServiceImpl {
.find(_.id == descriptorId)
.toFuture(EServiceDescriptorNotFound(eService.id.toString, descriptorId.toString))

def assertInterfaceDoesNotExists(descriptor: CatalogDescriptor): Future[Unit] =
descriptor.interface match {
case Some(_) => Future.failed(InterfaceAlreadyExists(descriptor.id))
case None => Future.unit
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ object ResponseHandlers extends AkkaResponses {
result match {
case Success(s) => success(s)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: InterfaceAlreadyExists) => badRequest(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceDescriptorNotFound) => notFound(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@ object CatalogProcessErrors {
final case class AttributeNotFound(attributeId: UUID)
extends ComponentError("0020", s"Attribute ${attributeId.toString} not found")

final case class InterfaceAlreadyExists(descriptorId: UUID)
extends ComponentError("0021", s"Descriptor ${descriptorId.toString} already has an interface")

}
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,52 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
status shouldEqual StatusCodes.Forbidden
}
}
"fail if document is an interface and it already exists" 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,
interface = Some(
CatalogDocument(
id = UUID.randomUUID(),
name = "name",
contentType = "application/yaml",
prettyName = "pretty",
path = "path",
checksum = "checksum",
uploadDate = OffsetDateTimeSupplier.get().minusDays(10)
)
)
)

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

val documentId = UUID.randomUUID()

val seed = CreateEServiceDescriptorDocumentSeed(
documentId = documentId,
kind = EServiceDocumentKind.INTERFACE,
prettyName = "prettyName",
filePath = "filePath",
fileName = "fileName",
contentType = "application/json",
checksum = "checksum",
serverUrls = Seq.empty
)

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

Post() ~> service.createEServiceDocument(SpecData.catalogItem.id.toString, descriptorId.toString, seed) ~> check {
status shouldEqual StatusCodes.BadRequest
}
}
}
"Document retrieve" should {
"succeed" in {
Expand Down

0 comments on commit 3adf411

Please sign in to comment.