Skip to content

Commit

Permalink
Merge pull request #38 from hmrc/update-url
Browse files Browse the repository at this point in the history
Fix calls to check if submissions and assumed reports exist
  • Loading branch information
TheNickWilson authored Nov 11, 2024
2 parents 83e3aec + dc1f80d commit 8e8a1bf
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 22 deletions.
23 changes: 19 additions & 4 deletions app/connectors/SubmissionsConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package connectors

import config.Service
import connectors.SubmissionsConnector.SubmissionsExistFailure
import connectors.SubmissionsConnector.{AssumedReportsExistFailure, SubmissionsExistFailure}
import models.submissions.ViewSubmissionsRequest
import play.api.Configuration
import play.api.http.Status.{NOT_FOUND, OK}
Expand All @@ -34,11 +34,11 @@ class SubmissionsConnector @Inject()(configuration: Configuration,

private val digitalPlatformReporting: Service = configuration.get[Service]("microservice.services.digital-platform-reporting")

def submissionsExist(assumedReporting: Boolean)(implicit hc: HeaderCarrier): Future[Boolean] = {
def submissionsExist(implicit hc: HeaderCarrier): Future[Boolean] = {

val request = ViewSubmissionsRequest(assumedReporting)
val request = ViewSubmissionsRequest(assumedReporting = false)

httpClient.post(url"$digitalPlatformReporting/digital-platform-reporting/submission/list")
httpClient.post(url"$digitalPlatformReporting/digital-platform-reporting/submission/delivered/list")
.withBody(Json.toJson(request))
.execute[HttpResponse]
.flatMap { response =>
Expand All @@ -49,11 +49,26 @@ class SubmissionsConnector @Inject()(configuration: Configuration,
}
}
}

def assumedReportsExist(implicit hc: HeaderCarrier): Future[Boolean] =
httpClient.get(url"$digitalPlatformReporting/digital-platform-reporting/submission/assumed")
.execute[HttpResponse]
.flatMap { response =>
response.status match {
case OK => Future.successful(true)
case NOT_FOUND => Future.successful(false)
case status => Future.failed(AssumedReportsExistFailure(status))
}
}
}

object SubmissionsConnector {

final case class SubmissionsExistFailure(status: Int) extends Throwable {
override def getMessage: String = s"Call to check if submissions exist failed with status: $status"
}

final case class AssumedReportsExistFailure(status: Int) extends Throwable {
override def getMessage: String = s"Call to check if manual assumed reports exist failed with status: $status"
}
}
4 changes: 2 additions & 2 deletions app/controllers/IndexController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class IndexController @Inject()(
private def getFileSubmissionsCard(operators: Seq[PlatformOperator], appConfig: FrontendAppConfig)
(implicit request: Request[_]): Future[FileSubmissionsCardViewModel] = {
if (appConfig.fileSubmissionsEnabled) {
submissionsConnector.submissionsExist(assumedReporting = false).map { response =>
submissionsConnector.submissionsExist.map { response =>
FileSubmissionsCardViewModel(response, operators, appConfig)
}
} else {
Expand All @@ -89,7 +89,7 @@ class IndexController @Inject()(
private def getAssumedReportingCard(operators: Seq[PlatformOperator], appConfig: FrontendAppConfig)
(implicit request: Request[_]): Future[AssumedReportingCardViewModel] = {
if (appConfig.assumedReportingEnabled) {
submissionsConnector.submissionsExist(assumedReporting = true).map { response =>
submissionsConnector.assumedReportsExist.map { response =>
AssumedReportingCardViewModel(response, operators, appConfig)
}
} else {
Expand Down
56 changes: 49 additions & 7 deletions it/test/connectors/SubmissionsConnectorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package connectors

import com.github.tomakehurst.wiremock.client.WireMock._
import connectors.SubmissionsConnector.SubmissionsExistFailure
import connectors.SubmissionsConnector.{AssumedReportsExistFailure, SubmissionsExistFailure}
import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.must.Matchers
Expand Down Expand Up @@ -51,40 +51,82 @@ class SubmissionsConnectorSpec extends AnyFreeSpec
"must return true when the server returns OK" in {

wireMockServer.stubFor(
post(urlPathEqualTo("/digital-platform-reporting/submission/list"))
post(urlPathEqualTo("/digital-platform-reporting/submission/delivered/list"))
.withHeader("Authorization", equalTo("authToken"))
.willReturn(ok())
)

val result = connector.submissionsExist(assumedReporting = true).futureValue
val result = connector.submissionsExist.futureValue
result mustEqual true
}

"must return false when the server returns NOT_FOUND" in {

wireMockServer.stubFor(
post(urlPathEqualTo("/digital-platform-reporting/submission/list"))
post(urlPathEqualTo("/digital-platform-reporting/submission/delivered/list"))
.withHeader("Authorization", equalTo("authToken"))
.willReturn(notFound())
)

val result = connector.submissionsExist(assumedReporting = true).futureValue
val result = connector.submissionsExist.futureValue
result mustEqual false
}

"must return a failed future when the server returns an error" in {

wireMockServer.stubFor(
post(urlPathEqualTo("/digital-platform-reporting/submission/list"))
post(urlPathEqualTo("/digital-platform-reporting/submission/delivered/list"))
.withHeader("Authorization", equalTo("authToken"))
.willReturn(serverError())
)

val result = connector.submissionsExist(assumedReporting = true).failed.futureValue
val result = connector.submissionsExist.failed.futureValue
result mustBe a[SubmissionsExistFailure]

val failure = result.asInstanceOf[SubmissionsExistFailure]
failure.status mustEqual INTERNAL_SERVER_ERROR
}
}

"assumedReportsExist" - {

"must return true when the server returns OK" in {

wireMockServer.stubFor(
get(urlPathEqualTo("/digital-platform-reporting/submission/assumed"))
.withHeader("Authorization", equalTo("authToken"))
.willReturn(ok())
)

val result = connector.assumedReportsExist.futureValue
result mustEqual true
}

"must return false when the server returns NOT_FOUND" in {

wireMockServer.stubFor(
get(urlPathEqualTo("/digital-platform-reporting/submission/assumed"))
.withHeader("Authorization", equalTo("authToken"))
.willReturn(notFound())
)

val result = connector.assumedReportsExist.futureValue
result mustEqual false
}

"must return a failed future when the server returns an error" in {

wireMockServer.stubFor(
get(urlPathEqualTo("/digital-platform-reporting/submission/assumed"))
.withHeader("Authorization", equalTo("authToken"))
.willReturn(serverError())
)

val result = connector.assumedReportsExist.failed.futureValue
result mustBe a[AssumedReportsExistFailure]

val failure = result.asInstanceOf[AssumedReportsExistFailure]
failure.status mustEqual INTERNAL_SERVER_ERROR
}
}
}
19 changes: 10 additions & 9 deletions test/controllers/IndexControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class IndexControllerSpec extends SpecBase with MockitoSugar with BeforeAndAfter
"must display the platform operator, reporting notification and file submission cards" in {

when(mockPlatformOperatorConnector.viewPlatformOperators(any())) thenReturn Future.successful(ViewPlatformOperatorsResponse(Nil))
when(mockSubmissionsConnector.submissionsExist(any())(any())) thenReturn Future.successful(false)
when(mockSubmissionsConnector.submissionsExist(any())) thenReturn Future.successful(false)

val application =
applicationBuilder(userAnswers = None)
Expand Down Expand Up @@ -146,8 +146,8 @@ class IndexControllerSpec extends SpecBase with MockitoSugar with BeforeAndAfter

contentAsString(result) mustEqual view(viewModel)(request, messages(application)).toString

verify(mockSubmissionsConnector, times(1)).submissionsExist(eqTo(false))(any())
verify(mockSubmissionsConnector, never()).submissionsExist(eqTo(true))(any())
verify(mockSubmissionsConnector, times(1)).submissionsExist(any())
verify(mockSubmissionsConnector, never()).assumedReportsExist(any())
}
}
}
Expand All @@ -157,7 +157,7 @@ class IndexControllerSpec extends SpecBase with MockitoSugar with BeforeAndAfter
"must display the platform operator, reporting notification and assumed reporting cards" in {

when(mockPlatformOperatorConnector.viewPlatformOperators(any())) thenReturn Future.successful(ViewPlatformOperatorsResponse(Nil))
when(mockSubmissionsConnector.submissionsExist(any())(any())) thenReturn Future.successful(false)
when(mockSubmissionsConnector.assumedReportsExist(any())) thenReturn Future.successful(false)

val application =
applicationBuilder(userAnswers = None)
Expand Down Expand Up @@ -188,8 +188,8 @@ class IndexControllerSpec extends SpecBase with MockitoSugar with BeforeAndAfter

contentAsString(result) mustEqual view(viewModel)(request, messages(application)).toString

verify(mockSubmissionsConnector, times(1)).submissionsExist(eqTo(true))(any())
verify(mockSubmissionsConnector, never()).submissionsExist(eqTo(false))(any())
verify(mockSubmissionsConnector, times(1)).assumedReportsExist(any())
verify(mockSubmissionsConnector, never()).submissionsExist(any())
}
}
}
Expand All @@ -199,7 +199,8 @@ class IndexControllerSpec extends SpecBase with MockitoSugar with BeforeAndAfter
"must display the platform operator, reporting notification, file submission and assumed reporting cards" in {

when(mockPlatformOperatorConnector.viewPlatformOperators(any())) thenReturn Future.successful(ViewPlatformOperatorsResponse(Nil))
when(mockSubmissionsConnector.submissionsExist(any())(any())) thenReturn Future.successful(false)
when(mockSubmissionsConnector.submissionsExist(any())) thenReturn Future.successful(false)
when(mockSubmissionsConnector.assumedReportsExist(any())) thenReturn Future.successful(false)

val application =
applicationBuilder(userAnswers = None)
Expand Down Expand Up @@ -230,8 +231,8 @@ class IndexControllerSpec extends SpecBase with MockitoSugar with BeforeAndAfter

contentAsString(result) mustEqual view(viewModel)(request, messages(application)).toString

verify(mockSubmissionsConnector, times(1)).submissionsExist(eqTo(true))(any())
verify(mockSubmissionsConnector, times(1)).submissionsExist(eqTo(false))(any())
verify(mockSubmissionsConnector, times(1)).submissionsExist(any())
verify(mockSubmissionsConnector, times(1)).assumedReportsExist(any())
}
}
}
Expand Down

0 comments on commit 8e8a1bf

Please sign in to comment.