Skip to content

Commit

Permalink
API-7899 - Integrate latest lib and fixtures into TPA (#524)
Browse files Browse the repository at this point in the history
* WIP

* WIP 2

* API-7899 - Clocknow and new lib

* API-7899 - Lining up userIds and emails in test data

* API-7899 - Remove more custom test data

* API-7899 - Removing more custom test data

* API-7899 - More test data simplification

* API-7899 - More fixture shuffling

* API-7899 - More fixture work

* API-7899 - More fixtures

* API-7899 - More of the same

* API-7899 - Add StoredApplicationFixtures

* API-7899 - More stuff

* API-7899 - More fixtures

* API-7899 - More fixtures

* API-7899 - withAccess

* API-7899 - WIP

* API-7899 - WIP

* API-7899 - WIP

* API-7899 - Remove direct creations of StoredApp from 99.9% of sites

* API-7899 - Last tidy

* API-7899 - Restore line removed due to println

* API-7899 - Remove println and transitive deps

* API-7899 - PR comments

* API-7899 - Merge latest main

---------

Co-authored-by: Matt Clark <[email protected]>
  • Loading branch information
AndySpaven and mattclark-zerogravit authored Nov 15, 2024
1 parent 6f73f4b commit 2492c98
Show file tree
Hide file tree
Showing 222 changed files with 2,086 additions and 2,842 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import uk.gov.hmrc.apiplatform.modules.approvals.controllers.actions.{ApprovalsA
import uk.gov.hmrc.apiplatform.modules.approvals.services.GrantApprovalsService
import uk.gov.hmrc.apiplatform.modules.submissions.services.SubmissionsService
import uk.gov.hmrc.thirdpartyapplication.controllers.{ExtraHeadersController, JsonUtils}
import uk.gov.hmrc.thirdpartyapplication.models.Application
import uk.gov.hmrc.thirdpartyapplication.models.JsonFormatters._
import uk.gov.hmrc.thirdpartyapplication.models.db.StoredApplication
import uk.gov.hmrc.thirdpartyapplication.services.ApplicationDataService

object ApprovalsController {
Expand Down Expand Up @@ -60,7 +60,7 @@ class ApprovalsController @Inject() (
withJsonBodyFromAnyContent[TouUpliftRequest] { upliftRequest =>
grantApprovalService.grantWithWarningsForTouUplift(request.application, request.submission, upliftRequest.gatekeeperUserName, upliftRequest.reasons)
.map(_ match {
case Actioned(application) => Ok(Json.toJson(Application(application)))
case Actioned(application) => Ok(Json.toJson(StoredApplication.asApplication(application)))
case RejectedDueToIncorrectSubmissionState => PreconditionFailed(asJsonError("NOT_IN_WARNINGS_STATE", s"Submission for $applicationId was not in a warnings state"))
case RejectedDueToIncorrectApplicationState =>
PreconditionFailed(asJsonError("APPLICATION_IN_INCORRECT_STATE", s"Application is not in state '${State.PRODUCTION}'"))
Expand All @@ -76,7 +76,7 @@ class ApprovalsController @Inject() (
withJsonBodyFromAnyContent[TouUpliftRequest] { upliftRequest =>
grantApprovalService.declineForTouUplift(request.application, request.submission, upliftRequest.gatekeeperUserName, upliftRequest.reasons)
.map(_ match {
case Actioned(application) => Ok(Json.toJson(Application(application)))
case Actioned(application) => Ok(Json.toJson(StoredApplication.asApplication(application)))
case RejectedDueToIncorrectSubmissionState => PreconditionFailed(asJsonError("NOT_IN_WARNINGS_STATE", s"Submission for $applicationId was not in a warnings state"))
case RejectedDueToIncorrectApplicationState =>
PreconditionFailed(asJsonError("APPLICATION_IN_INCORRECT_STATE", s"Application is not in state '${State.PRODUCTION}'"))
Expand All @@ -92,7 +92,7 @@ class ApprovalsController @Inject() (
withJsonBodyFromAnyContent[TouUpliftRequest] { upliftRequest =>
grantApprovalService.resetForTouUplift(request.application, request.submission, upliftRequest.gatekeeperUserName, upliftRequest.reasons)
.map(_ match {
case Actioned(application) => Ok(Json.toJson(Application(application)))
case Actioned(application) => Ok(Json.toJson(StoredApplication.asApplication(application)))
case RejectedDueToIncorrectSubmissionState =>
PreconditionFailed(asJsonError("SUBMISSION_IN_INCORRECT_STATE", s"Submission for $applicationId was not in the expected state"))
case RejectedDueToIncorrectApplicationState =>
Expand All @@ -109,7 +109,7 @@ class ApprovalsController @Inject() (
withJsonBodyFromAnyContent[TouDeleteRequest] { deleteRequest =>
grantApprovalService.deleteTouUplift(request.application, request.submission, deleteRequest.gatekeeperUserName)
.map(_ match {
case Actioned(application) => Ok(Json.toJson(Application(application)))
case Actioned(application) => Ok(Json.toJson(StoredApplication.asApplication(application)))
case RejectedDueToIncorrectApplicationState =>
PreconditionFailed(asJsonError("APPLICATION_IN_INCORRECT_STATE", s"Application is not in state '${State.PRODUCTION}'"))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ trait JsonErrorResponse {
)

def applicationNotFound(applicationId: ApplicationId) =
NotFound(asBody("APPLICATION_NOT_FOUND", s"Application ${applicationId.value} doesn't exist"))
NotFound(asBody("APPLICATION_NOT_FOUND", s"Application ${applicationId} doesn't exist"))

def submissionNotFound(applicationId: ApplicationId) =
NotFound(asBody("SUBMISSION_NOT_FOUND", s"No submission found for application ${applicationId.value}"))
NotFound(asBody("SUBMISSION_NOT_FOUND", s"No submission found for application ${applicationId}"))

def applicationInIncorrectState(applicationId: ApplicationId, state: String) =
PreconditionFailed(asBody("APPLICATION_IN_INCORRECT_STATE", s"Application is not in state #'${state}'"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import uk.gov.hmrc.mongo.play.json.formats.MongoJavatimeFormats
import uk.gov.hmrc.play.json.Union

import uk.gov.hmrc.apiplatform.modules.common.domain.models.{ApplicationId, LaxEmailAddress}
import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models.ApplicationName
import uk.gov.hmrc.apiplatform.modules.applications.submissions.domain.models.{ResponsibleIndividual, SubmissionId}
import uk.gov.hmrc.apiplatform.modules.approvals.domain.models.ResponsibleIndividualVerificationState.{INITIAL, ResponsibleIndividualVerificationState}

Expand All @@ -31,7 +32,7 @@ sealed trait ResponsibleIndividualVerification {
def applicationId: ApplicationId
def submissionId: SubmissionId
def submissionInstance: Int
def applicationName: String
def applicationName: ApplicationName
def createdOn: Instant
def state: ResponsibleIndividualVerificationState
}
Expand All @@ -58,8 +59,8 @@ case class ResponsibleIndividualToUVerification(
applicationId: ApplicationId,
submissionId: SubmissionId,
submissionInstance: Int,
applicationName: String,
createdOn: Instant = Instant.now(),
applicationName: ApplicationName,
createdOn: Instant,
state: ResponsibleIndividualVerificationState = INITIAL
) extends ResponsibleIndividualVerification

Expand All @@ -68,8 +69,8 @@ case class ResponsibleIndividualTouUpliftVerification(
applicationId: ApplicationId,
submissionId: SubmissionId,
submissionInstance: Int,
applicationName: String,
createdOn: Instant = Instant.now(),
applicationName: ApplicationName,
createdOn: Instant,
requestingAdminName: String,
requestingAdminEmail: LaxEmailAddress,
state: ResponsibleIndividualVerificationState = INITIAL
Expand All @@ -80,8 +81,8 @@ case class ResponsibleIndividualUpdateVerification(
applicationId: ApplicationId,
submissionId: SubmissionId,
submissionInstance: Int,
applicationName: String,
createdOn: Instant = Instant.now(),
applicationName: ApplicationName,
createdOn: Instant,
responsibleIndividual: ResponsibleIndividual,
requestingAdminName: String,
requestingAdminEmail: LaxEmailAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import uk.gov.hmrc.mongo.MongoComponent
import uk.gov.hmrc.mongo.play.json.{Codecs, PlayMongoRepository}

import uk.gov.hmrc.apiplatform.modules.common.domain.models.ApplicationId
import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models.ApplicationName
import uk.gov.hmrc.apiplatform.modules.applications.submissions.domain.models.{ResponsibleIndividual, SubmissionId}
import uk.gov.hmrc.apiplatform.modules.approvals.domain.models.ResponsibleIndividualVerificationState.ResponsibleIndividualVerificationState
import uk.gov.hmrc.apiplatform.modules.approvals.domain.models.{
Expand Down Expand Up @@ -178,7 +179,7 @@ class ResponsibleIndividualVerificationRepository @Inject() (mongo: MongoCompone
evt.applicationId,
SubmissionId(evt.submissionId.value),
evt.submissionIndex,
evt.applicationName,
ApplicationName(evt.applicationName),
evt.eventDateTime,
ResponsibleIndividual.build(evt.responsibleIndividualName, evt.responsibleIndividualEmail.text),
evt.requestingAdminName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package uk.gov.hmrc.apiplatform.modules.approvals.services

import java.time.{Clock, Instant}
import java.time.Clock
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Failure

import uk.gov.hmrc.apiplatform.modules.common.domain.models.Actor
import uk.gov.hmrc.apiplatform.modules.common.services.ClockNow
import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models._
import uk.gov.hmrc.thirdpartyapplication.models.db.StoredApplication
import uk.gov.hmrc.thirdpartyapplication.repository.StateHistoryRepository

abstract class BaseService(stateHistoryRepository: StateHistoryRepository, clock: Clock)(implicit ec: ExecutionContext) {
abstract class BaseService(stateHistoryRepository: StateHistoryRepository, val clock: Clock)(implicit ec: ExecutionContext) extends ClockNow {

def insertStateHistory(
snapshotApp: StoredApplication,
Expand All @@ -34,7 +35,7 @@ abstract class BaseService(stateHistoryRepository: StateHistoryRepository, clock
actor: Actor,
rollback: StoredApplication => Any
): Future[StateHistory] = {
val stateHistory = StateHistory(snapshotApp.id, newState, actor, oldState, changedAt = Instant.now(clock))
val stateHistory = StateHistory(snapshotApp.id, newState, actor, oldState, changedAt = instant())
stateHistoryRepository.insert(stateHistory)
.andThen {
case e: Failure[_] =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package uk.gov.hmrc.apiplatform.modules.approvals.services

import java.time.{Clock, Instant}
import java.time.Clock
import javax.inject.{Inject, Singleton}
import scala.concurrent.Future.successful
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -54,7 +54,7 @@ class GrantApprovalsService @Inject() (
responsibleIndividualVerificationRepository: ResponsibleIndividualVerificationRepository,
submissionService: SubmissionsService,
emailConnector: EmailConnector,
val clock: Clock
clock: Clock
)(implicit ec: ExecutionContext
) extends BaseService(stateHistoryRepository, clock)
with ApplicationLogger
Expand Down Expand Up @@ -87,7 +87,7 @@ class GrantApprovalsService @Inject() (
_ <- ET.cond(originalApp.isInProduction, (), RejectedDueToIncorrectApplicationState)
_ <- ET.cond(submission.status.isWarnings, (), RejectedDueToIncorrectSubmissionState)

updatedSubmission = Submission.grantWithWarnings(Instant.now(clock), gatekeeperUserName, reasons, None)(submission)
updatedSubmission = Submission.grantWithWarnings(instant(), gatekeeperUserName, reasons, None)(submission)
savedSubmission <- ET.liftF(submissionService.store(updatedSubmission))
_ <- ET.liftF(setTermsOfUseInvitationStatus(originalApp.id, savedSubmission))
} yield Actioned(originalApp)
Expand All @@ -109,7 +109,7 @@ class GrantApprovalsService @Inject() (
_ <- ET.cond(originalApp.isInProduction, (), RejectedDueToIncorrectApplicationState)
_ <- ET.cond(submission.status.isFailed, (), RejectedDueToIncorrectSubmissionState)

updatedSubmission = Submission.decline(Instant.now(clock), gatekeeperUserName, reasons)(submission)
updatedSubmission = Submission.decline(instant(), gatekeeperUserName, reasons)(submission)
savedSubmission <- ET.liftF(submissionService.store(updatedSubmission))
_ <- ET.liftF(setTermsOfUseInvitationStatus(originalApp.id, savedSubmission))
} yield Actioned(originalApp)
Expand All @@ -130,7 +130,7 @@ class GrantApprovalsService @Inject() (
for {
_ <- ET.cond(originalApp.isInProduction, (), RejectedDueToIncorrectApplicationState)

updatedSubmission = Submission.decline(Instant.now(clock), gatekeeperUserName, "RESET: " + reasons)(submission)
updatedSubmission = Submission.decline(instant(), gatekeeperUserName, "RESET: " + reasons)(submission)
savedSubmission <- ET.liftF(submissionService.store(updatedSubmission))
_ <- ET.liftF(responsibleIndividualVerificationRepository.deleteSubmissionInstance(submission.id, submission.latestInstance.index))
_ <- ET.liftF(setTermsOfUseInvitationStatus(originalApp.id, savedSubmission))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package uk.gov.hmrc.apiplatform.modules.approvals.services

import java.time.{Clock, Instant}
import java.time.Clock
import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

import uk.gov.hmrc.apiplatform.modules.common.domain.models.LaxEmailAddress
import uk.gov.hmrc.apiplatform.modules.common.services.ApplicationLogger
import uk.gov.hmrc.apiplatform.modules.common.services.{ApplicationLogger, ClockNow}
import uk.gov.hmrc.apiplatform.modules.applications.submissions.domain.models.SubmissionId
import uk.gov.hmrc.apiplatform.modules.approvals.domain.models.{
ResponsibleIndividualToUVerification,
Expand All @@ -38,15 +38,15 @@ class ResponsibleIndividualVerificationService @Inject() (
stateHistoryRepository: StateHistoryRepository,
clock: Clock
)(implicit ec: ExecutionContext
) extends BaseService(stateHistoryRepository, clock) with ApplicationLogger {
) extends BaseService(stateHistoryRepository, clock) with ApplicationLogger with ClockNow {

def createNewToUVerification(applicationData: StoredApplication, submissionId: SubmissionId, submissionInstance: Int): Future[ResponsibleIndividualVerification] = {
val verification = ResponsibleIndividualToUVerification(
applicationId = applicationData.id,
submissionId = submissionId,
submissionInstance = submissionInstance,
applicationName = applicationData.name,
createdOn = Instant.now(clock)
createdOn = instant()
)
responsibleIndividualVerificationRepository.save(verification)
}
Expand All @@ -63,7 +63,7 @@ class ResponsibleIndividualVerificationService @Inject() (
submissionId = submissionId,
submissionInstance = submissionInstance,
applicationName = applicationData.name,
createdOn = Instant.now(clock),
createdOn = instant(),
requestingAdminName = requestedByName,
requestingAdminEmail = requestedByEmailAddress
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package uk.gov.hmrc.apiplatform.modules.submissions.domain.services

import java.time.Instant
import java.time.temporal.ChronoUnit

import cats.data.NonEmptyList

Expand Down Expand Up @@ -61,7 +62,7 @@ object AnswerQuestion {
def updateSubmissionState(answers: Submission.AnswersToQuestions, areQuestionsAnswered: Boolean, submission: Submission): Submission = {
import Submission._

val addAnsweringStatus = addStatusHistory(Submission.Status.Answering(Instant.now(), areQuestionsAnswered))
val addAnsweringStatus = addStatusHistory(Submission.Status.Answering(Instant.now().truncatedTo(ChronoUnit.MILLIS), areQuestionsAnswered))
(addAnsweringStatus andThen updateLatestAnswersTo(answers))(submission)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object DeriveContext {

def yesNoFromBoolean(b: Boolean) = if (b) "Yes" else "No"

def deriveFraudPrevention(newUplift: String, subscriptions: List[ApiIdentifier]): String = {
def deriveFraudPrevention(newUplift: String, subscriptions: Set[ApiIdentifier]): String = {
if ("Yes".equalsIgnoreCase(newUplift)) {
// If a new terms of use uplift, then don't want fraud prevention questions
"No"
Expand All @@ -37,7 +37,7 @@ object DeriveContext {
}
}

def deriveFor(application: StoredApplication, subscriptions: List[ApiIdentifier]): Context = {
def deriveFor(application: StoredApplication, subscriptions: Set[ApiIdentifier]): Context = {

val resell = application.sellResellOrDistribute.fold("No")(s => s.answer)
val inHouse = if (resell == "Yes") "No" else "Yes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package uk.gov.hmrc.apiplatform.modules.submissions.services

import java.time.{Clock, Instant}
import java.time.Clock
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

import cats.data.NonEmptyList

import uk.gov.hmrc.apiplatform.modules.common.domain.models.ApplicationId
import uk.gov.hmrc.apiplatform.modules.common.services.EitherTHelper
import uk.gov.hmrc.apiplatform.modules.common.services.{ClockNow, EitherTHelper}
import uk.gov.hmrc.apiplatform.modules.applications.submissions.domain.models.SubmissionId
import uk.gov.hmrc.apiplatform.modules.events.applications.domain.models.ApplicationEvents
import uk.gov.hmrc.apiplatform.modules.submissions.domain.models._
Expand All @@ -37,7 +37,7 @@ class SubmissionsService @Inject() (
contextService: ContextService,
val clock: Clock
)(implicit val ec: ExecutionContext
) extends EitherTHelper[String] {
) extends EitherTHelper[String] with ClockNow {
import cats.instances.future.catsStdInstancesForFuture

private val emptyAnswers = Map.empty[Question.Id, ActualAnswer]
Expand Down Expand Up @@ -67,8 +67,8 @@ class SubmissionsService @Inject() (
allQuestionnaires = groups.flatMap(_.links)
submissionId = SubmissionId.random
context <- contextService.deriveContext(applicationId)
newInstance = Submission.Instance(0, emptyAnswers, NonEmptyList.of(Submission.Status.Created(Instant.now(clock), requestedBy)))
submission = Submission(submissionId, applicationId, Instant.now(clock), groups, QuestionnaireDAO.questionIdsOfInterest, NonEmptyList.of(newInstance), context)
newInstance = Submission.Instance(0, emptyAnswers, NonEmptyList.of(Submission.Status.Created(instant(), requestedBy)))
submission = Submission(submissionId, applicationId, instant(), groups, QuestionnaireDAO.questionIdsOfInterest, NonEmptyList.of(newInstance), context)
savedSubmission <- liftF(submissionsDAO.save(submission))
} yield savedSubmission
)
Expand Down Expand Up @@ -134,7 +134,7 @@ class SubmissionsService @Inject() (
(
for {
submission <- fromOptionF(fetchLatest(appId), "submission not found")
updatedSubmission = Submission.decline(Instant.now(clock), requestedByEmailAddress, reasons)(submission)
updatedSubmission = Submission.decline(instant(), requestedByEmailAddress, reasons)(submission)
savedSubmission <- liftF(store(updatedSubmission))
} yield savedSubmission
)
Expand All @@ -146,7 +146,7 @@ class SubmissionsService @Inject() (
(
for {
submission <- fromOptionF(fetchLatest(appId), "submission not found")
updatedSubmission = Submission.automaticallyMark(Instant.now(clock), requestedByEmailAddress)(submission)
updatedSubmission = Submission.automaticallyMark(instant(), requestedByEmailAddress)(submission)
savedSubmission <- liftF(store(updatedSubmission))
} yield savedSubmission
)
Expand Down
Loading

0 comments on commit 2492c98

Please sign in to comment.