Skip to content

Commit

Permalink
PIN-4061 Adapted route for the purpose clone (#188)
Browse files Browse the repository at this point in the history
Co-authored-by: nttdata-rtorsoli <[email protected]>
  • Loading branch information
nttdata-rtorsoli and nttdata-rtorsoli authored Nov 6, 2023
1 parent 8b75279 commit 277bb90
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/main/resources/interface-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,12 @@ paths:
post:
summary: Clone Purpose
operationId: clonePurpose
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/PurposeCloneSeed'
required: true
responses:
'200':
description: Purpose Cloned
Expand Down Expand Up @@ -1129,6 +1135,14 @@ components:
createdAt:
type: string
format: date-time
PurposeCloneSeed:
type: object
properties:
eserviceId:
type: string
format: uuid
required:
- eserviceId
EServicePurposeSeed:
type: object
description: contains the expected payload for purpose creation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ object PurposeApiMarshallerImpl extends PurposeApiMarshaller with SprayJsonSuppo
override implicit def fromEntityUnmarshallerReversePurposeUpdateContent
: FromEntityUnmarshaller[ReversePurposeUpdateContent] =
sprayJsonUnmarshaller[ReversePurposeUpdateContent]

override implicit def fromEntityUnmarshallerPurposeCloneSeed: FromEntityUnmarshaller[PurposeCloneSeed] =
sprayJsonUnmarshaller[PurposeCloneSeed]
}
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ final case class PurposeApiServiceImpl(
}
}

override def clonePurpose(purposeId: String)(implicit
override def clonePurpose(purposeId: String, seed: PurposeCloneSeed)(implicit
contexts: Seq[(String, String)],
toEntityMarshallerPurpose: ToEntityMarshaller[Purpose],
toEntityMarshallerProblem: ToEntityMarshaller[Problem]
Expand Down Expand Up @@ -599,9 +599,13 @@ final case class PurposeApiServiceImpl(
multiAnswers = riskAnalysis.multiAnswers.map(multiAnswerToSeed)
)

def createPurposeSeed(purpose: PersistentPurpose, dailyCalls: Int): PurposeManagementDependency.PurposeSeed =
def createPurposeSeed(
purpose: PersistentPurpose,
dailyCalls: Int,
eserviceId: UUID
): PurposeManagementDependency.PurposeSeed =
PurposeManagementDependency.PurposeSeed(
eserviceId = purpose.eserviceId,
eserviceId = eserviceId,
consumerId = purpose.consumerId,
riskAnalysisForm = purpose.riskAnalysisForm.map(riskAnalysisToSeed),
title = s"${purpose.title} - clone",
Expand Down Expand Up @@ -635,7 +639,7 @@ final case class PurposeApiServiceImpl(
_ <-
if (isClonable(purpose)) Future.successful(purpose)
else Future.failed(PurposeCannotBeCloned(purposeId))
dependencySeed = createPurposeSeed(purpose, dailyCalls)
dependencySeed = createPurposeSeed(purpose, dailyCalls, seed.eserviceId)
tenantKind <- tenant.kind.toFuture(TenantKindNotFound(tenant.id))
newPurpose <- purposeManagementService.createPurpose(dependencySeed)
isValidRiskAnalysisForm = isRiskAnalysisFormValid(newPurpose.riskAnalysisForm.map(_.toApi))(tenantKind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ package object impl extends SprayJsonSupport with DefaultJsonProtocol {
implicit def riskAnalysisFormConfigResponseFormat: RootJsonFormat[RiskAnalysisFormConfigResponse] =
jsonFormat2(RiskAnalysisFormConfigResponse)
implicit def eServicePurposeSeedFormat: RootJsonFormat[EServicePurposeSeed] = jsonFormat8(EServicePurposeSeed)
implicit def purposeCloneSeedFormat: RootJsonFormat[PurposeCloneSeed] = jsonFormat1(PurposeCloneSeed)

final val entityMarshallerProblem: ToEntityMarshaller[Problem] = sprayJsonMarshaller[Problem]
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object PDFCreatorImpl extends PDFCreator with PDFManager {
case Deliver => getLocalizedLabel(LocalizedText(it = "Eroga", en = "Delivers"), language)
}
} yield Map(
"dailyCalls" -> dailyCalls.toString,
"dailyCalls" -> dailyCalls.toString,
"answers" -> answers.mkString("\n"),
"eServiceName" -> eServiceInfo.name,
"producerText" -> getDescriptionText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
freeOfChargeReason = None
)

val purposeCloneSeed = PurposeCloneSeed(eserviceId = eServiceId)

val purposeCloned = PurposeManagementDependency.Purpose(
id = UUID.randomUUID(),
eserviceId = eServiceId,
Expand Down Expand Up @@ -109,7 +111,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
.once()
.returns(Future.successful(purposeCloned))

Get() ~> service.clonePurpose(purposeId.toString) ~> check {
Get() ~> service.clonePurpose(purposeId.toString, purposeCloneSeed) ~> check {
status shouldEqual StatusCodes.OK
}
}
Expand All @@ -122,6 +124,8 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> consumerId.toString)

val purposeCloneSeed = PurposeCloneSeed(eserviceId = eServiceId)

val purposeToClone = PersistentPurpose(
id = purposeId,
eserviceId = eServiceId,
Expand Down Expand Up @@ -211,7 +215,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
.once()
.returns(Future.successful(purposeCloned))

Get() ~> service.clonePurpose(purposeId.toString) ~> check {
Get() ~> service.clonePurpose(purposeId.toString, purposeCloneSeed) ~> check {
status shouldEqual StatusCodes.OK
}
}
Expand All @@ -224,6 +228,8 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> consumerId.toString)

val purposeCloneSeed = PurposeCloneSeed(eserviceId = eServiceId)

val purposeToClone = PersistentPurpose(
id = purposeId,
eserviceId = eServiceId,
Expand Down Expand Up @@ -313,7 +319,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
.once()
.returns(Future.successful(purposeCloned))

Get() ~> service.clonePurpose(purposeId.toString) ~> check {
Get() ~> service.clonePurpose(purposeId.toString, purposeCloneSeed) ~> check {
status shouldEqual StatusCodes.OK
}
}
Expand All @@ -326,6 +332,8 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> consumerId.toString)

val purposeCloneSeed = PurposeCloneSeed(eserviceId = eServiceId)

val purposeToClone = PersistentPurpose(
id = purposeId,
eserviceId = eServiceId,
Expand Down Expand Up @@ -405,7 +413,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
.once()
.returns(Future.successful(purposeCloned))

Get() ~> service.clonePurpose(purposeId.toString) ~> check {
Get() ~> service.clonePurpose(purposeId.toString, purposeCloneSeed) ~> check {
status shouldEqual StatusCodes.OK
}
}
Expand All @@ -418,6 +426,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> consumerId.toString)

val purposeCloneSeed = PurposeCloneSeed(eserviceId = UUID.randomUUID())
mockTenantRetrieve(consumerId, SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PRIVATE.some))

(mockPurposeManagementService
Expand All @@ -426,7 +435,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
.once()
.returns(Future.failed(PurposeNotFound(purposeId)))

Get() ~> service.clonePurpose(purposeId.toString) ~> check {
Get() ~> service.clonePurpose(purposeId.toString, purposeCloneSeed) ~> check {
status shouldEqual StatusCodes.NotFound
}
}
Expand All @@ -439,6 +448,8 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> consumerId.toString)

val purposeCloneSeed = PurposeCloneSeed(eserviceId = eServiceId)

val purposeToCloneDraft = PersistentPurpose(
id = purposeId,
eserviceId = eServiceId,
Expand All @@ -463,7 +474,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
.once()
.returns(Future.successful(purposeToCloneDraft))

Get() ~> service.clonePurpose(purposeId.toString) ~> check {
Get() ~> service.clonePurpose(purposeId.toString, purposeCloneSeed) ~> check {
status shouldEqual StatusCodes.Conflict
val problem = responseAs[Problem]
problem.status shouldBe StatusCodes.Conflict.intValue
Expand All @@ -479,6 +490,8 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> consumerId.toString)

val purposeCloneSeed = PurposeCloneSeed(eserviceId = eServiceId)

val purposeToCloneDraft = PersistentPurpose(
id = purposeId,
eserviceId = eServiceId,
Expand Down Expand Up @@ -515,7 +528,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
.once()
.returns(Future.successful(purposeToCloneDraft))

Get() ~> service.clonePurpose(purposeId.toString) ~> check {
Get() ~> service.clonePurpose(purposeId.toString, purposeCloneSeed) ~> check {
status shouldEqual StatusCodes.Conflict
val problem = responseAs[Problem]
problem.status shouldBe StatusCodes.Conflict.intValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ class PurposeApiAuthzSpec extends AnyWordSpecLike with BeforeAndAfterAll with Au
}

"accept authorized roles for clonePurpose" in {
val endpoint = AuthorizedRoutes.endpoints("clonePurpose")
validateAuthorization(endpoint, { implicit c: Seq[(String, String)] => service.clonePurpose("fakeSeed") })
val endpoint = AuthorizedRoutes.endpoints("clonePurpose")
val fakeCloneSeed = PurposeCloneSeed(eserviceId = UUID.randomUUID())
validateAuthorization(
endpoint,
{ implicit c: Seq[(String, String)] => service.clonePurpose("fakeSeed", fakeCloneSeed) }
)
}

"accept authorized roles for getPurpose" in {
Expand Down

0 comments on commit 277bb90

Please sign in to comment.