Skip to content

Commit

Permalink
Merge pull request #116 from hmrc/APIS-7344
Browse files Browse the repository at this point in the history
APIS-7344 Add Create Date Time to the API Approval Record
  • Loading branch information
christopher-rocks authored Oct 30, 2024
2 parents b9afda5 + 152eada commit 0d3f81e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
11 changes: 10 additions & 1 deletion app/uk/gov/hmrc/apipublisher/models/APIApproval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 9 additions & 3 deletions app/uk/gov/hmrc/apipublisher/services/ApprovalService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
58 changes: 35 additions & 23 deletions test/uk/gov/hmrc/apipublisher/services/ApprovalServiceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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")
}

Expand Down

0 comments on commit 0d3f81e

Please sign in to comment.