Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
beetlecrunch committed Nov 24, 2023
1 parent 3b6783a commit 70170ef
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ final case class PurposeApiServiceImpl(
s"Creating Purposes for EService ${seed.eServiceId}, Consumer ${seed.consumerId}"

val result: Future[Purpose] = for {
organizationId <- getOrganizationIdFutureUUID(contexts)
_ <- assertOrganizationIsAConsumer(organizationId, seed.consumerId)
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
_ <- assertOrganizationIsAConsumer(requesterOrgId, seed.consumerId)
eService <- catalogManagementService.getEServiceById(seed.eServiceId)
_ <- if (eService.mode == Receive) Future.unit else Future.failed(EServiceNotInReceiveMode(eService.id))
riskAnalysis <- eService.riskAnalysis
.find(_.id == seed.riskAnalysisId)
.toFuture(RiskAnalysisNotFound(seed.eServiceId, seed.riskAnalysisId))
_ <- checkFreeOfCharge(seed.isFreeOfCharge, seed.freeOfChargeReason)
tenantKind <- getTenantKind(organizationId)
tenantKind <- getTenantKind(eService.producerId)
purposeSeed = seed.toManagement(seed.eServiceId, riskAnalysis.riskAnalysisForm.toManagement(seed.riskAnalysisId))
_ <- checkAgreements(seed.eServiceId, seed.consumerId, seed.title)
purpose <- purposeManagementService.createPurpose(purposeSeed)
Expand All @@ -160,10 +160,10 @@ final case class PurposeApiServiceImpl(
logger.info(operationLabel)

val result: Future[Purpose] = for {
organizationId <- getOrganizationIdFutureUUID(contexts)
_ <- assertOrganizationIsAConsumer(organizationId, seed.consumerId)
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
_ <- assertOrganizationIsAConsumer(requesterOrgId, seed.consumerId)
_ <- checkFreeOfCharge(seed.isFreeOfCharge, seed.freeOfChargeReason)
tenantKind <- getTenantKind(organizationId)
tenantKind <- getTenantKind(requesterOrgId)
purposeSeed <- seed.toManagement(schemaOnlyValidation = true)(tenantKind).toFuture
_ <- checkAgreements(seed.eserviceId, seed.consumerId, seed.title)
purpose <- purposeManagementService.createPurpose(purposeSeed)
Expand Down Expand Up @@ -273,7 +273,7 @@ final case class PurposeApiServiceImpl(
eService <- catalogManagementService.getEServiceById(purpose.eserviceId)
_ <- eServiceModeCheck(eService)
_ <- checkFreeOfCharge(isFreeOfCharge, freeOfChargeReason)
tenant <- getInvolvedTenantByEServiceMode(eService, requesterOrgId)
tenant <- getInvolvedTenantByEServiceMode(eService, purpose.consumerId)
tenantKind <- tenant.kind.toFuture(TenantKindNotFound(tenant.id))
purposePayload <- payload(purpose, tenantKind)
updatedPurpose <- purposeManagementService.updatePurpose(purposeUUID, purposePayload)
Expand Down Expand Up @@ -435,10 +435,11 @@ final case class PurposeApiServiceImpl(
val result: Future[PurposeVersion] = for {
purposeUUID <- purposeId.toFutureUUID
versionUUID <- versionId.toFutureUUID
organizationId <- getOrganizationIdFutureUUID(contexts)
requesterOrgId <- getOrganizationIdFutureUUID(contexts)
purpose <- purposeManagementService.getPurposeById(purposeUUID)
consumer <- tenantManagementService.getTenantById(purpose.consumerId)
tenantKind <- consumer.kind.toFuture(TenantKindNotFound(consumer.id))
eService <- catalogManagementService.getEServiceById(purpose.eserviceId)
tenant <- getInvolvedTenantByEServiceMode(eService, purpose.consumerId)
tenantKind <- tenant.kind.toFuture(TenantKindNotFound(tenant.id))
version <- getVersion(purpose, versionUUID)
riskAnalysisForm <- purpose.riskAnalysisForm.toFuture(MissingRiskAnalysis(purposeUUID))
_ <-
Expand All @@ -448,15 +449,14 @@ final case class PurposeApiServiceImpl(
.toEither
.whenA(version.state == Draft)
.toFuture
eService <- catalogManagementService.getEServiceById(purpose.eserviceId)
ownership <- Ownership
.getOrganizationRole(organizationId, eService.producerId, purpose.consumerId)
.getOrganizationRole(requesterOrgId, eService.producerId, purpose.consumerId)
.toFuture
updatedVersion <- purposeVersionActivation.activateOrWaitForApproval(
eService,
purpose,
version,
organizationId,
requesterOrgId,
ownership
)
} yield updatedVersion.toApi
Expand Down Expand Up @@ -685,9 +685,9 @@ final case class PurposeApiServiceImpl(
}
}

private def getInvolvedTenantByEServiceMode(eservice: CatalogItem, requesterId: UUID): Future[PersistentTenant] =
private def getInvolvedTenantByEServiceMode(eservice: CatalogItem, consumerId: UUID): Future[PersistentTenant] =
eservice.mode match {
case Deliver => tenantManagementService.getTenantById(requesterId)
case Deliver => tenantManagementService.getTenantById(consumerId)
case Receive => tenantManagementService.getTenantById(eservice.producerId)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ object ResponseHandlers extends AkkaResponses {
case Failure(ex: OrganizationNotAllowed) => forbidden(ex, logMessage)
case Failure(ex: PurposeNotFound) => notFound(ex, logMessage)
case Failure(ex: PurposeVersionNotFound) => notFound(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
case Failure(ex) =>
ex.printStackTrace()
internalServerError(ex, logMessage)
}

def suspendPurposeVersionResponse[T](logMessage: String)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
"succeed" in {
val eServiceId = UUID.randomUUID()
val consumerId = UUID.randomUUID()
val producerId = UUID.randomUUID()
val riskAnalysisId = UUID.randomUUID()
val purposeId = UUID.randomUUID()

Expand Down Expand Up @@ -909,12 +910,13 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
mockEServiceRetrieve(
purpose.eserviceId,
SpecData.eService.copy(
producerId = producerId,
descriptors = Seq(SpecData.descriptor.copy(id = SpecData.agreement.descriptorId)),
mode = Receive,
riskAnalysis = Seq(SpecData.riskAnalysis.copy(id = riskAnalysisId))
)
)
mockTenantRetrieve(consumerId, SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PA.some))
mockTenantRetrieve(producerId, SpecData.tenant.copy(id = producerId, kind = PersistentTenantKind.PA.some))

mockAgreementsRetrieve(eServiceId, consumerId, Seq(AgreementActive))

Expand Down Expand Up @@ -1077,6 +1079,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
"fail on SUSPENDED agreement " in {
val eServiceId = UUID.randomUUID()
val consumerId = UUID.randomUUID()
val producerId = UUID.randomUUID()
val purposeId = UUID.randomUUID()
val riskAnalysisId = UUID.randomUUID()

Expand Down Expand Up @@ -1112,13 +1115,14 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
mockEServiceRetrieve(
purpose.eserviceId,
SpecData.eService.copy(
producerId = producerId,
descriptors = Seq(SpecData.descriptor.copy(id = SpecData.agreement.descriptorId)),
mode = Receive,
riskAnalysis = Seq(SpecData.riskAnalysis.copy(id = riskAnalysisId))
)
)

mockTenantRetrieve(consumerId, SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PA.some))
mockTenantRetrieve(producerId, SpecData.tenant.copy(id = producerId, kind = PersistentTenantKind.PA.some))

mockAgreementsRetrieve(eServiceId, consumerId, Seq(AgreementActive), Seq.empty)

Expand Down Expand Up @@ -1187,6 +1191,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate

val eServiceId = UUID.randomUUID()
val consumerId = UUID.randomUUID()
val producerId = UUID.randomUUID()
val riskAnalysisId = UUID.randomUUID()
val purposeId = UUID.randomUUID()

Expand Down Expand Up @@ -1222,12 +1227,13 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
mockEServiceRetrieve(
purpose.eserviceId,
SpecData.eService.copy(
producerId = producerId,
descriptors = Seq(SpecData.descriptor.copy(id = SpecData.agreement.descriptorId)),
mode = Receive,
riskAnalysis = Seq(SpecData.riskAnalysis.copy(id = riskAnalysisId))
)
)
mockTenantRetrieve(consumerId, SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PA.some))
mockTenantRetrieve(producerId, SpecData.tenant.copy(id = producerId, kind = PersistentTenantKind.PA.some))

mockAgreementsRetrieve(eServiceId, consumerId, Seq(AgreementActive), Seq.empty)

Expand Down Expand Up @@ -1270,6 +1276,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate

val eServiceId = UUID.randomUUID()
val consumerId = UUID.randomUUID()
val producerId = UUID.randomUUID()
val riskAnalysisId = UUID.randomUUID()

implicit val context: Seq[(String, String)] =
Expand All @@ -1291,17 +1298,14 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
mockEServiceRetrieve(
purpose.eserviceId,
SpecData.eService.copy(
producerId = producerId,
descriptors = Seq(SpecData.descriptor.copy(id = SpecData.agreement.descriptorId)),
mode = Receive,
riskAnalysis = Seq(SpecData.riskAnalysis.copy(id = riskAnalysisId))
)
)

(mockTenantManagementService
.getTenantById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(consumerId, *, *)
.once()
.returns(Future.successful(SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PA.some)))
mockTenantRetrieve(producerId, SpecData.tenant.copy(id = producerId, kind = PersistentTenantKind.PA.some))

mockAgreementsRetrieve(eServiceId, consumerId, Seq(AgreementActive))

Expand All @@ -1317,6 +1321,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate

val eServiceId = UUID.randomUUID()
val consumerId = UUID.randomUUID()
val producerId = UUID.randomUUID()
val riskAnalysisId = UUID.randomUUID()

implicit val context: Seq[(String, String)] =
Expand Down Expand Up @@ -1348,17 +1353,14 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate
mockEServiceRetrieve(
purpose.eserviceId,
SpecData.eService.copy(
producerId = producerId,
descriptors = Seq(SpecData.descriptor.copy(id = SpecData.agreement.descriptorId)),
mode = Receive,
riskAnalysis = Seq(SpecData.riskAnalysisOnlySchema.copy(id = riskAnalysisId))
)
)

(mockTenantManagementService
.getTenantById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(consumerId, *, *)
.once()
.returns(Future.successful(SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PA.some)))
mockTenantRetrieve(producerId, SpecData.tenant.copy(id = producerId, kind = PersistentTenantKind.PA.some))

mockAgreementsRetrieve(eServiceId, consumerId, Seq(AgreementActive))

Expand Down Expand Up @@ -1578,7 +1580,7 @@ class PurposeApiServiceSpec extends AnyWordSpecLike with SpecHelper with Scalate

mockEServiceRetrieve(eserviceId, SpecData.eService.copy(id = eserviceId, mode = Receive, producerId = producerId))

mockTenantRetrieve(producerId, SpecData.tenant.copy(id = producerId, kind = PersistentTenantKind.PA.some))
mockTenantRetrieve(producerId, SpecData.tenant.copy(id = producerId, kind = PersistentTenantKind.PRIVATE.some))

mockPurposeUpdate(purposeId, seed, SpecData.dependencyPurpose.copy(id = purpose.id))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ class PurposeVersionStateSpec extends AnyWordSpecLike with SpecHelper with Scala
.once()
.returns(Future.successful(purpose))

mockEServiceRetrieve(eServiceId, SpecData.eService.copy(id = eServiceId))
mockTenantRetrieve(consumerId, SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PRIVATE.some))

Get() ~> service.activatePurposeVersion(purposeId.toString, versionId.toString) ~> check {
Expand Down Expand Up @@ -1173,8 +1174,8 @@ class PurposeVersionStateSpec extends AnyWordSpecLike with SpecHelper with Scala
val eService = SpecData.eService.copy(id = eServiceId, descriptors = Seq(descriptor), producerId = producerId)

mockPurposeRetrieve(purposeId, purpose)
mockTenantRetrieve(consumerId, SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PRIVATE.some))
mockEServiceRetrieve(eServiceId, eService)
mockTenantRetrieve(consumerId, SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PRIVATE.some))

Get() ~> service.activatePurposeVersion(purposeId.toString, versionId.toString) ~> check {
status shouldEqual StatusCodes.Forbidden
Expand Down Expand Up @@ -1208,6 +1209,7 @@ class PurposeVersionStateSpec extends AnyWordSpecLike with SpecHelper with Scala
.once()
.returns(Future.successful(purpose))

mockEServiceRetrieve(eServiceId, SpecData.eService.copy(id = eServiceId))
mockTenantRetrieve(consumerId, SpecData.tenant.copy(id = consumerId, kind = PersistentTenantKind.PRIVATE.some))

Get() ~> service.activatePurposeVersion(purposeId.toString, versionId.toString) ~> check {
Expand Down

0 comments on commit 70170ef

Please sign in to comment.