Skip to content

Commit

Permalink
PIN-4462 BKE - Check duplicated name in update
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli committed Feb 2, 2024
1 parent ab4f25d commit b17b2c1
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/main/resources/interface-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ paths:
application/problem+json:
schema:
$ref: '#/components/schemas/Problem'
'409':
description: Already exists an E-Service with the same name
content:
application/problem+json:
schema:
$ref: '#/components/schemas/Problem'
delete:
security:
- bearerAuth: []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,7 @@ final case class ProcessApiServiceImpl(
origin <- getExternalIdOriginFuture(contexts)
_ <- if (origin == IPA) Future.unit else Future.failed(OriginIsNotCompliant(IPA))
clientSeed = eServiceSeed.toDependency(organizationId)
maybeEservice <- catalogManagementService
.getEServices(
eServiceSeed.name.some,
Seq.empty,
Seq(clientSeed.producerId),
Seq.empty,
Seq.empty,
None,
0,
1,
exactMatchOnName = true
)
.map(_.results.headOption.map(_.name))

_ <- maybeEservice.fold(Future.unit)(_ => Future.failed(DuplicatedEServiceName(eServiceSeed.name)))
_ <- checkDuplicateName(None, eServiceSeed.name, clientSeed.producerId)
createdEService <- catalogManagementService.createEService(clientSeed)
} yield createdEService.toApi

Expand Down Expand Up @@ -457,6 +443,13 @@ final case class ProcessApiServiceImpl(
}
}

private def checkDuplicateName(eServiceId: Option[UUID], name: String, producerId: UUID): Future[Unit] = for {
result <- catalogManagementService
.getEServices(name.some, Seq.empty, Seq(producerId), Seq.empty, Seq.empty, None, 0, 1, exactMatchOnName = true)
eservice = eServiceId.fold(result.results)(id => result.results.filterNot(_.id == id))
_ <- eservice.headOption.map(_.name).fold(Future.unit)(_ => Future.failed(DuplicatedEServiceName(name)))
} yield ()

override def updateEServiceById(eServiceId: String, updateEServiceSeed: UpdateEServiceSeed)(implicit
contexts: Seq[(String, String)],
toEntityMarshallerProblem: ToEntityMarshaller[Problem],
Expand All @@ -471,6 +464,7 @@ final case class ProcessApiServiceImpl(
catalogItem <- catalogManagementService.getEServiceById(eServiceUuid)
_ <- assertRequesterAllowed(catalogItem.producerId)(organizationId)
_ <- eServiceCanBeUpdated(catalogItem).toFuture
_ <- checkDuplicateName(Some(eServiceUuid), updateEServiceSeed.name, catalogItem.producerId)
_ <- deleteRiskAnalysisOnModeUpdate(updateEServiceSeed.mode, catalogItem)
updatedEService <- catalogManagementService.updateEServiceById(eServiceId, updateEServiceSeed.toDependency)
} yield updatedEService.toApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ object ResponseHandlers extends AkkaResponses {
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceCannotBeUpdated) => badRequest(ex, logMessage)
case Failure(ex: DuplicatedEServiceName) => conflict(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,34 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
mode = CatalogManagementDependency.EServiceMode.DELIVER
)

(mockCatalogManagementService
.getEServices(
_: Option[String],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[CatalogDescriptorState],
_: Option[CatalogItemMode],
_: Int,
_: Int,
_: Boolean
)(_: ExecutionContext, _: ReadModelService))
.expects(
Some(updatedEServiceSeed.name),
Seq.empty,
Seq(requesterId),
Seq.empty,
Seq.empty,
None,
0,
1,
true,
*,
*
)
.once()
.returns(Future.successful(PaginatedResult(results = Seq.empty, 0)))

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(eService.id, *, *)
Expand All @@ -507,7 +535,96 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
status shouldEqual StatusCodes.OK
}
}
"succeed if use the same name" in {
val requesterId = UUID.randomUUID()

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

val descriptor =
SpecData.eServiceDescriptor.copy(state = CatalogManagementDependency.EServiceDescriptorState.DRAFT)

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

val eServiceSeed =
UpdateEServiceSeed(
name = "newName",
description = "newDescription",
technology = EServiceTechnology.REST,
mode = EServiceMode.DELIVER
)

val updatedEServiceSeed = CatalogManagementDependency.UpdateEServiceSeed(
name = "newName",
description = "newDescription",
technology = eService.technology,
mode = CatalogManagementDependency.EServiceMode.DELIVER
)

val updatedEService = CatalogManagementDependency.EService(
id = eService.id,
producerId = requesterId,
name = "newName",
description = "newDescription",
technology = eService.technology,
descriptors = Seq(descriptor),
riskAnalysis = Seq.empty,
mode = CatalogManagementDependency.EServiceMode.DELIVER
)

(mockCatalogManagementService
.getEServices(
_: Option[String],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[CatalogDescriptorState],
_: Option[CatalogItemMode],
_: Int,
_: Int,
_: Boolean
)(_: ExecutionContext, _: ReadModelService))
.expects(
Some(updatedEServiceSeed.name),
Seq.empty,
Seq(requesterId),
Seq.empty,
Seq.empty,
None,
0,
1,
true,
*,
*
)
.once()
.returns(
Future.successful(
PaginatedResult(
results = Seq(
SpecData.catalogItem.copy(id = eService.id, name = updatedEServiceSeed.name, producerId = requesterId)
),
0
)
)
)

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

(mockCatalogManagementService
.updateEServiceById(_: String, _: CatalogManagementDependency.UpdateEServiceSeed)(_: Seq[(String, String)]))
.expects(eService.id.toString, updatedEServiceSeed, *)
.returning(Future.successful(updatedEService))
.once()

Put() ~> service.updateEServiceById(eService.id.toString, eServiceSeed) ~> check {
status shouldEqual StatusCodes.OK
}
}
"succeed and delete riskAnalysis when mode move from Receive to Deliver" in {
val requesterId = UUID.randomUUID()

Expand Down Expand Up @@ -556,6 +673,34 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
)
)

(mockCatalogManagementService
.getEServices(
_: Option[String],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[CatalogDescriptorState],
_: Option[CatalogItemMode],
_: Int,
_: Int,
_: Boolean
)(_: ExecutionContext, _: ReadModelService))
.expects(
Some(updatedEServiceSeed.name),
Seq.empty,
Seq(requesterId),
Seq.empty,
Seq.empty,
None,
0,
1,
true,
*,
*
)
.once()
.returns(Future.successful(PaginatedResult(results = Seq.empty, 0)))

(mockCatalogManagementService
.deleteRiskAnalysis(_: UUID, _: UUID)(_: Seq[(String, String)]))
.expects(eService.id, SpecData.catalogRiskAnalysisFullValid.id, *)
Expand Down Expand Up @@ -614,6 +759,34 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
.once()
.returns(Future.successful(SpecData.catalogItem.copy(producerId = requesterId)))

(mockCatalogManagementService
.getEServices(
_: Option[String],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[CatalogDescriptorState],
_: Option[CatalogItemMode],
_: Int,
_: Int,
_: Boolean
)(_: ExecutionContext, _: ReadModelService))
.expects(
Some(updatedEServiceSeed.name),
Seq.empty,
Seq(requesterId),
Seq.empty,
Seq.empty,
None,
0,
1,
true,
*,
*
)
.once()
.returns(Future.successful(PaginatedResult(results = Seq.empty, 0)))

(mockCatalogManagementService
.updateEServiceById(_: String, _: CatalogManagementDependency.UpdateEServiceSeed)(_: Seq[(String, String)]))
.expects(eService.id.toString, updatedEServiceSeed, *)
Expand All @@ -624,6 +797,77 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
status shouldEqual StatusCodes.OK
}
}
"fail if exists another eService with the update name" in {
val requesterId = UUID.randomUUID()

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

val descriptor =
SpecData.eServiceDescriptor.copy(state = CatalogManagementDependency.EServiceDescriptorState.DRAFT)

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

val eServiceSeed =
UpdateEServiceSeed(
name = "newName",
description = "newDescription",
technology = EServiceTechnology.REST,
mode = EServiceMode.DELIVER
)

val updatedEServiceSeed = CatalogManagementDependency.UpdateEServiceSeed(
name = "newName",
description = "newDescription",
technology = eService.technology,
mode = CatalogManagementDependency.EServiceMode.DELIVER
)

(mockCatalogManagementService
.getEServices(
_: Option[String],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[UUID],
_: Seq[CatalogDescriptorState],
_: Option[CatalogItemMode],
_: Int,
_: Int,
_: Boolean
)(_: ExecutionContext, _: ReadModelService))
.expects(
Some(updatedEServiceSeed.name),
Seq.empty,
Seq(requesterId),
Seq.empty,
Seq.empty,
None,
0,
1,
true,
*,
*
)
.once()
.returns(
Future.successful(
PaginatedResult(
results = Seq(SpecData.catalogItem.copy(id = UUID.randomUUID(), name = updatedEServiceSeed.name)),
1
)
)
)

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

Put() ~> service.updateEServiceById(eService.id.toString, eServiceSeed) ~> check {
status shouldEqual StatusCodes.Conflict
}
}

"fail if descriptor state is not draft" in {
val requesterId = UUID.randomUUID()
Expand Down

0 comments on commit b17b2c1

Please sign in to comment.