Skip to content

Commit

Permalink
PIN-4844 purpose-process: Cloned purpose must have an unique title (#205
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 May 28, 2024
1 parent 195d0dd commit b736134
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import it.pagopa.interop.purposeprocess.service._
import java.util.UUID
import scala.concurrent.{ExecutionContext, Future}
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

final case class PurposeApiServiceImpl(
agreementManagementService: AgreementManagementService,
Expand All @@ -56,6 +57,8 @@ final case class PurposeApiServiceImpl(
private implicit val logger: LoggerTakingImplicit[ContextFieldsToLog] =
Logger.takingImplicit[ContextFieldsToLog](this.getClass)

private val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss")

private[this] val purposeVersionActivation = PurposeVersionActivation(
agreementManagementService,
authorizationManagementService,
Expand Down Expand Up @@ -605,6 +608,17 @@ final case class PurposeApiServiceImpl(
singleAnswers = riskAnalysis.singleAnswers.map(singleAnswerToSeed),
multiAnswers = riskAnalysis.multiAnswers.map(multiAnswerToSeed)
)
def generateTitle(title: String): String = {
val dots: String = "..."
val suffix: String = s" - clone - ${dateTimeSupplier.get().format(dtf)}"
val maxTitleLength: Int = 60
val prefixLengthAllowance: Int = maxTitleLength - suffix.length - dots.length

if ((title.length + suffix.length) <= maxTitleLength)
s"${title}${suffix}"
else
s"${title.substring(0, prefixLengthAllowance)}${dots}${suffix}"
}

def createPurposeSeed(
purpose: PersistentPurpose,
Expand All @@ -615,7 +629,7 @@ final case class PurposeApiServiceImpl(
eserviceId = eserviceId,
consumerId = purpose.consumerId,
riskAnalysisForm = purpose.riskAnalysisForm.map(riskAnalysisToSeed),
title = s"${purpose.title} - clone",
title = generateTitle(purpose.title),
description = purpose.description,
isFreeOfCharge = purpose.isFreeOfCharge,
freeOfChargeReason = purpose.freeOfChargeReason,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate

val purposeCloneSeed = PurposeCloneSeed(eserviceId = eServiceId)

(() => mockDateTimeSupplier.get()).expects().returning(SpecData.timestamp).once()

val purposeCloned = PurposeManagementDependency.Purpose(
id = UUID.randomUUID(),
eserviceId = eServiceId,
consumerId = consumerId,
versions = Seq(SpecData.dependencyPurposeVersion),
suspendedByConsumer = None,
suspendedByProducer = None,
title = "title - clone",
title = s"${purposeToClone} - clone - ${SpecData.timestamp.format(SpecData.dtf)}",
description = "description",
riskAnalysisForm = Some(SpecData.validManagementRiskAnalysis(PersistentTenantKind.PRIVATE)),
createdAt = SpecData.timestamp,
Expand Down Expand Up @@ -169,6 +171,8 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
freeOfChargeReason = None
)

(() => mockDateTimeSupplier.get()).expects().returning(SpecData.timestamp).once()

val purposeCloned = PurposeManagementDependency.Purpose(
id = UUID.randomUUID(),
eserviceId = eServiceId,
Expand Down Expand Up @@ -197,7 +201,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
),
suspendedByConsumer = None,
suspendedByProducer = None,
title = "title - clone",
title = s"${purposeToClone} - clone - ${SpecData.timestamp.format(SpecData.dtf)}",
description = "description",
riskAnalysisForm = Some(SpecData.validManagementRiskAnalysis(PersistentTenantKind.PRIVATE)),
createdAt = SpecData.timestamp,
Expand Down Expand Up @@ -275,6 +279,8 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
freeOfChargeReason = None
)

(() => mockDateTimeSupplier.get()).expects().returning(SpecData.timestamp).once()

val purposeCloned = PurposeManagementDependency.Purpose(
id = UUID.randomUUID(),
eserviceId = eServiceId,
Expand Down Expand Up @@ -303,7 +309,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
),
suspendedByConsumer = None,
suspendedByProducer = None,
title = "title - clone",
title = s"${purposeToClone} - clone - ${SpecData.timestamp.format(SpecData.dtf)}",
description = "description",
riskAnalysisForm = Some(SpecData.validManagementRiskAnalysis(PersistentTenantKind.PA)),
createdAt = SpecData.timestamp,
Expand Down Expand Up @@ -369,6 +375,8 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
freeOfChargeReason = None
)

(() => mockDateTimeSupplier.get()).expects().returning(SpecData.timestamp).once()

val purposeCloned = PurposeManagementDependency.Purpose(
id = UUID.randomUUID(),
eserviceId = eServiceId,
Expand All @@ -388,7 +396,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
),
suspendedByConsumer = None,
suspendedByProducer = None,
title = "title - clone",
title = s"${purposeToClone.title} - clone - ${SpecData.timestamp.format(SpecData.dtf)}",
description = "description",
riskAnalysisForm = None,
createdAt = SpecData.timestamp,
Expand All @@ -400,7 +408,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
eserviceId = purposeToClone.eserviceId,
consumerId = purposeToClone.consumerId,
riskAnalysisForm = None,
title = s"${purposeToClone.title} - clone",
title = s"${purposeToClone.title} - clone - ${SpecData.timestamp.format(SpecData.dtf)}",
description = purposeToClone.description,
isFreeOfCharge = false,
dailyCalls = 500
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import it.pagopa.interop.agreementmanagement.model.agreement.{Active, Persistent
import it.pagopa.interop.tenantmanagement.model.tenant.{PersistentTenantKind, PersistentTenant, PersistentExternalId}
import it.pagopa.interop.purposeprocess.api.Adapters._
import java.time.{OffsetDateTime, ZoneOffset}
import java.time.format.DateTimeFormatter
import java.util.UUID

object SpecData {
Expand Down Expand Up @@ -324,6 +325,8 @@ object SpecData {
)
}

val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss")

val purpose: PersistentPurpose = PersistentPurpose(
id = UUID.randomUUID(),
eserviceId = UUID.randomUUID(),
Expand Down

0 comments on commit b736134

Please sign in to comment.