From 152eadadfff2441c691491652c7ff4da90d810a6 Mon Sep 17 00:00:00 2001 From: kskourtis Date: Tue, 29 Oct 2024 16:26:20 +0000 Subject: [PATCH] APIS-7344 Add Create Date Time to the API Approval Record --- .../apipublisher/models/APIApproval.scala | 11 +++- .../services/ApprovalService.scala | 12 +++- .../services/ApprovalServiceSpec.scala | 58 +++++++++++-------- 3 files changed, 54 insertions(+), 27 deletions(-) diff --git a/app/uk/gov/hmrc/apipublisher/models/APIApproval.scala b/app/uk/gov/hmrc/apipublisher/models/APIApproval.scala index a18faa0..f341b00 100644 --- a/app/uk/gov/hmrc/apipublisher/models/APIApproval.scala +++ b/app/uk/gov/hmrc/apipublisher/models/APIApproval.scala @@ -16,9 +16,18 @@ package uk.gov.hmrc.apipublisher.models +import java.time.Instant + import play.api.libs.json.{Format, Json} -case class APIApproval(serviceName: String, serviceUrl: String, name: String, description: Option[String] = None, approved: Option[Boolean] = Some(false)) { +case class APIApproval( + serviceName: String, + serviceUrl: String, + name: String, + description: Option[String] = None, + approved: Option[Boolean] = Some(false), + createdOn: Option[Instant] = Some(Instant.now()) + ) { def isApproved: Boolean = approved.getOrElse(false) } diff --git a/app/uk/gov/hmrc/apipublisher/services/ApprovalService.scala b/app/uk/gov/hmrc/apipublisher/services/ApprovalService.scala index d352458..e2f540e 100644 --- a/app/uk/gov/hmrc/apipublisher/services/ApprovalService.scala +++ b/app/uk/gov/hmrc/apipublisher/services/ApprovalService.scala @@ -40,10 +40,16 @@ class ApprovalService @Inject() (apiApprovalRepository: APIApprovalRepository, a case (_, _) => false } + def saveApproval(apiApproval: APIApproval, maybeExistingApiApproval: Option[APIApproval], isApproved: Boolean): Future[APIApproval] = + maybeExistingApiApproval match { + case Some(existingApproval) => apiApprovalRepository.save(apiApproval.copy(approved = Some(isApproved), createdOn = existingApproval.createdOn)) + case _ => apiApprovalRepository.save(apiApproval.copy(approved = Some(isApproved))) + } + for { - existingApiApproval <- apiApprovalRepository.fetch(apiApproval.serviceName) - isApproved = calculateApiApprovalStatus(existingApiApproval) - _ <- apiApprovalRepository.save(apiApproval.copy(approved = Some(isApproved))) + maybeExistingApiApproval <- apiApprovalRepository.fetch(apiApproval.serviceName) + isApproved = calculateApiApprovalStatus(maybeExistingApiApproval) + _ <- saveApproval(apiApproval, maybeExistingApiApproval, isApproved) } yield isApproved } diff --git a/test/uk/gov/hmrc/apipublisher/services/ApprovalServiceSpec.scala b/test/uk/gov/hmrc/apipublisher/services/ApprovalServiceSpec.scala index eedc05e..a63dd2b 100644 --- a/test/uk/gov/hmrc/apipublisher/services/ApprovalServiceSpec.scala +++ b/test/uk/gov/hmrc/apipublisher/services/ApprovalServiceSpec.scala @@ -16,6 +16,7 @@ package uk.gov.hmrc.apipublisher.services +import java.time.Duration import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future.successful @@ -66,29 +67,33 @@ class ApprovalServiceSpec extends AsyncHmrcSpec { } "Allow publication of previously disabled service when PreventAutoDeploy is disabled" in new Setup { - val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val existingApiApproval = apiApproval.copy(approved = Some(false), createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5)))) when(mockAppConfig.preventAutoDeploy).thenReturn(false) - when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval.copy(approved = Some(false))))) - when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true)))).thenReturn(successful(apiApproval.copy(approved = Some(true)))) + when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval))) + when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))) + .thenReturn(successful(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))) val result = await(underTest.createOrUpdateServiceApproval(apiApproval)) result shouldBe true - verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true))) + verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)) } "Allow publication of previously enabled service when PreventAutoDeploy is disabled" in new Setup { - val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val existingApiApproval = apiApproval.copy(approved = Some(true), createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5)))) when(mockAppConfig.preventAutoDeploy).thenReturn(false) - when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval.copy(approved = Some(true))))) - when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true)))).thenReturn(successful(apiApproval.copy(approved = Some(true)))) + when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval))) + when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))) + .thenReturn(successful(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))) val result = await(underTest.createOrUpdateServiceApproval(apiApproval)) result shouldBe true - verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true))) + verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)) } "Prevent publication of previously unknown services when PreventAutoDeploy is enabled" in new Setup { @@ -105,40 +110,46 @@ class ApprovalServiceSpec extends AsyncHmrcSpec { } "Prevent publication of previously disabled service when PreventAutoDeploy is enabled" in new Setup { - val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val existingApiApproval = apiApproval.copy(approved = Some(false), createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5)))) when(mockAppConfig.preventAutoDeploy).thenReturn(true) - when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval.copy(approved = Some(false))))) - when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(false)))).thenReturn(successful(apiApproval.copy(approved = Some(false)))) + when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval))) + when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(false), createdOn = existingApiApproval.createdOn))) + .thenReturn(successful(apiApproval.copy(approved = Some(false), createdOn = existingApiApproval.createdOn))) val result = await(underTest.createOrUpdateServiceApproval(apiApproval)) result shouldBe false - verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(false))) + verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(false), createdOn = existingApiApproval.createdOn)) } "Allow publication of previously enabled service when PreventAutoDeploy is enabled" in new Setup { - val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val existingApiApproval = apiApproval.copy(approved = Some(true), createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5)))) when(mockAppConfig.preventAutoDeploy).thenReturn(true) - when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval.copy(approved = Some(true))))) - when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true)))).thenReturn(successful(apiApproval.copy(approved = Some(true)))) + when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval))) + when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))) + .thenReturn(successful(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))) val result = await(underTest.createOrUpdateServiceApproval(apiApproval)) result shouldBe true - verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true))) + verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)) } "Allow an existing Service to be approved" in new Setup { - val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val existingApiApproval = apiApproval.copy(createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5)))) - when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval))) - when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true)))).thenReturn(successful(apiApproval.copy(approved = Some(true)))) + when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval))) + when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))) + .thenReturn(successful(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))) val result = await(underTest.approveService("testService")) result shouldBe ServiceLocation("testService", "http://localhost/myservice") - verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true))) + verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)) } "Raise an exception if an attempt is made to approve an unknown service" in new Setup { @@ -151,12 +162,13 @@ class ApprovalServiceSpec extends AsyncHmrcSpec { } "Return a summary of a service when requested" in new Setup { - val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description")) + val existingApiApproval = apiApproval.copy(createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5)))) - when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval))) + when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval))) val result = await(underTest.fetchServiceApproval("testService")) - result shouldBe apiApproval + result shouldBe apiApproval.copy(createdOn = existingApiApproval.createdOn) verify(mockApiApprovalRepository).fetch("testService") }