Skip to content

Commit

Permalink
APIS-4758 - Removing scalamock from tests (#73)
Browse files Browse the repository at this point in the history
* APIS-4758-FIX - Fixing user

* APIS-4758 - Added metals to git ignore

Co-authored-by: Andy Spaven <[email protected]>

* APIS-4758-- Updated tests to remove scalamock

* APIS-4758 - Removing metals conf file

* APIS-4758 - Refactored tests

Co-authored-by: Andy Spaven <[email protected]>

* APIS-4758 - Removing model change

Co-authored-by: Pete Slater <[email protected]>
Co-authored-by: Andy Spaven <[email protected]>
  • Loading branch information
3 people authored Jun 3, 2020
1 parent 9b42296 commit e307334
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 203 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ precompiled
/.project
project/project
project/target
project/metals.sbt
/RUNNING_PID
server.pid
/.settings
Expand Down
12 changes: 4 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ val compile = Seq(
"com.typesafe.play" %% "play-json" % "2.7.1",
"org.typelevel" %% "cats-core" % "2.1.0",
"eu.timepit" %% "refined" % "0.9.13",
// "eu.timepit" %% "refined-cats" % "0.9.13"
"be.venneborg" %% "play26-refined" % "0.5.0"
)

Expand All @@ -49,14 +48,11 @@ val overrides: Seq[ModuleID] = Seq(
)

def test(scope: String = "test,acceptance") = Seq(
"uk.gov.hmrc" %% "hmrctest" % "3.9.0-play-26" % scope,
"uk.gov.hmrc" %% "reactivemongo-test" % "4.16.0-play-26" % scope,
"org.scalamock" %% "scalamock-scalatest-support" % "3.6.0" % scope,
"org.scalatest" %% "scalatest" % "3.0.5" % scope,
"org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % scope,
"org.scalatestplus.play" %% "scalatestplus-play" % "3.1.3" % scope,
"org.mockito" %% "mockito-scala-scalatest" % "1.7.1" % scope,
"org.pegdown" % "pegdown" % "1.6.0" % scope,
"com.typesafe.play" %% "play-test" % play.core.PlayVersion.current % scope,
"org.mockito" % "mockito-core" % "1.10.19" % "test"
"com.typesafe.play" %% "play-test" % play.core.PlayVersion.current % scope
)

val appName = "api-subscription-fields"
Expand Down Expand Up @@ -122,4 +118,4 @@ lazy val scoverageSettings: Seq[Setting[_]] = Seq(
def onPackageName(rootPackage: String): String => Boolean = { testName => testName startsWith rootPackage }

// Note that this task has to be scoped globally
bloopAggregateSourceDependencies in Global := true
bloopAggregateSourceDependencies in Global := true
Empty file added fixme
Empty file.
4 changes: 0 additions & 4 deletions project/metals.sbt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package uk.gov.hmrc.apisubscriptionfields

import org.scalatest._
import org.mockito.{MockitoSugar, ArgumentMatchersSugar}
import org.scalatest.{Matchers, OptionValues, WordSpec}
import org.scalatestplus.play.WsScalaTestClient
import play.api.test.{DefaultAwaitTimeout,FutureAwaits}
import play.api.test.{DefaultAwaitTimeout, FutureAwaits}

abstract class HmrcPlaySpec
extends WordSpec
with Matchers
with OptionValues
with WsScalaTestClient
with DefaultAwaitTimeout
with FutureAwaits
abstract class HmrcSpec extends WordSpec with Matchers with OptionValues with WsScalaTestClient with MockitoSugar with ArgumentMatchersSugar {
}

abstract class AsyncHmrcSpec
extends HmrcSpec with DefaultAwaitTimeout with FutureAwaits {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import uk.gov.hmrc.apisubscriptionfields.model._
import Types._
import cats.data.NonEmptyList
import eu.timepit.refined.api.Refined
import uk.gov.hmrc.apisubscriptionfields.model.DevhubAccessLevel._
import uk.gov.hmrc.apisubscriptionfields.model.DevhubAccessRequirement._

trait FieldDefinitionTestData extends TestData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,43 @@

package uk.gov.hmrc.apisubscriptionfields.controller

import org.scalamock.scalatest.MockFactory
import play.api.libs.json.{JsDefined, JsString}
import play.api.test.{FakeRequest, StubControllerComponentsFactory}
import play.api.test.Helpers._
import uk.gov.hmrc.apisubscriptionfields.FieldDefinitionTestData
import uk.gov.hmrc.apisubscriptionfields.model.JsonFormatters
import uk.gov.hmrc.apisubscriptionfields.service.ApiFieldDefinitionsService
import uk.gov.hmrc.play.test.UnitSpec
import uk.gov.hmrc.apisubscriptionfields.AsyncHmrcSpec

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future.successful
import play.api.libs.json._
import play.api.test.Helpers._

class ApiFieldDefinitionsControllerDeleteSpec extends UnitSpec with FieldDefinitionTestData with MockFactory with JsonFormatters with StubControllerComponentsFactory {
class ApiFieldDefinitionsControllerDeleteSpec
extends AsyncHmrcSpec
with FieldDefinitionTestData
with JsonFormatters
with StubControllerComponentsFactory {

private val mockFieldsDefinitionService = mock[ApiFieldDefinitionsService]
private val controller = new ApiFieldDefinitionsController(stubControllerComponents(), mockFieldsDefinitionService)

"DELETE /definition/context/:apiContext/version/:apiVersion" should {
"return NO_CONTENT (204) when successfully deleted from repo" in {
(mockFieldsDefinitionService.delete _).expects(FakeContext, FakeVersion).returns(Future.successful(true))
when(mockFieldsDefinitionService.delete(FakeContext, FakeVersion)).thenReturn(successful(true))

val result = await(controller.deleteFieldsDefinition(FakeContext, FakeVersion)(FakeRequest()))
val result = controller.deleteFieldsDefinition(FakeContext, FakeVersion)(FakeRequest())

status(result) shouldBe NO_CONTENT
}

"return NOT_FOUND (404) when failed to delete from repo" in {
(mockFieldsDefinitionService.delete _).expects(FakeContext, FakeVersion).returns(Future.successful(false))
when(mockFieldsDefinitionService.delete(FakeContext, FakeVersion)).thenReturn(successful(false))

val result = await(controller.deleteFieldsDefinition(FakeContext, FakeVersion)(FakeRequest()))
val result = controller.deleteFieldsDefinition(FakeContext, FakeVersion)(FakeRequest())

status(result) shouldBe NOT_FOUND
(contentAsJson(result) \ "code") shouldBe JsDefined(JsString("NOT_FOUND"))
(contentAsJson(result) \ "message") shouldBe JsDefined(JsString(s"Fields definition not found for (${FakeContext.value}, ${FakeVersion.value})"))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@

package uk.gov.hmrc.apisubscriptionfields.controller

import org.scalamock.scalatest.MockFactory
import play.api.libs.json.{JsDefined, JsString, Json}
import play.api.test.{FakeRequest, StubControllerComponentsFactory}
import play.api.test.Helpers._
import play.api.test._
import uk.gov.hmrc.apisubscriptionfields.FieldDefinitionTestData
import uk.gov.hmrc.apisubscriptionfields.model.{BulkApiFieldDefinitionsResponse, ApiFieldDefinitionsResponse, JsonFormatters}
import uk.gov.hmrc.apisubscriptionfields.service.ApiFieldDefinitionsService
import uk.gov.hmrc.play.test.UnitSpec
import uk.gov.hmrc.apisubscriptionfields.AsyncHmrcSpec

import scala.concurrent.Future
import scala.concurrent.Future.{successful,failed}
import scala.concurrent.ExecutionContext.Implicits.global
import uk.gov.hmrc.apisubscriptionfields.model.ApiFieldDefinitionsResponse

class ApiFieldDefinitionsControllerGetSpec extends UnitSpec with FieldDefinitionTestData with MockFactory with JsonFormatters with StubControllerComponentsFactory {
class ApiFieldDefinitionsControllerGetSpec
extends AsyncHmrcSpec
with FieldDefinitionTestData
with JsonFormatters
with StubControllerComponentsFactory {

private val mockFieldsDefinitionService = mock[ApiFieldDefinitionsService]
private val controller = new ApiFieldDefinitionsController(stubControllerComponents(), mockFieldsDefinitionService)
Expand Down Expand Up @@ -154,28 +157,28 @@ class ApiFieldDefinitionsControllerGetSpec extends UnitSpec with FieldDefinition

"GET /definition/context/:apiContext/version/:apiVersion" should {
"return OK when the expected record exists in the repo" in {
mockFieldsDefinitionService.get _ expects (FakeContext, FakeVersion) returns Future.successful(Some(responseModel))
when(mockFieldsDefinitionService.get(FakeContext, FakeVersion)).thenReturn(successful(Some(responseModel)))

val result = await(controller.getFieldsDefinition(FakeContext, FakeVersion)(FakeRequest()))
val result = controller.getFieldsDefinition(FakeContext, FakeVersion)(FakeRequest())

status(result) shouldBe OK
contentAsJson(result) shouldBe responseJson
}

"return NOT_FOUND when entity does not exist in the repo" in {
mockFieldsDefinitionService.get _ expects (FakeContext, FakeVersion) returns Future.successful(None)
when(mockFieldsDefinitionService.get(FakeContext, FakeVersion)).thenReturn(successful(None))

val result = await(controller.getFieldsDefinition(FakeContext, FakeVersion)(FakeRequest()))
val result = controller.getFieldsDefinition(FakeContext, FakeVersion)(FakeRequest())

status(result) shouldBe NOT_FOUND
(contentAsJson(result) \ "code") shouldBe JsDefined(JsString("NOT_FOUND"))
(contentAsJson(result) \ "message") shouldBe JsDefined(JsString(s"Fields definition not found for ($fakeRawContext, $fakeRawVersion)"))
}

"return INTERNAL_SERVER_ERROR when service throws exception" in {
mockFieldsDefinitionService.get _ expects (FakeContext, FakeVersion) returns Future.failed(emulatedFailure)
when(mockFieldsDefinitionService.get(FakeContext, FakeVersion)).thenReturn(failed(emulatedFailure))

val result = await(controller.getFieldsDefinition(FakeContext, FakeVersion)(FakeRequest()))
val result = controller.getFieldsDefinition(FakeContext, FakeVersion)(FakeRequest())

status(result) shouldBe INTERNAL_SERVER_ERROR
(contentAsJson(result) \ "code") shouldBe JsDefined(JsString("UNKNOWN_ERROR"))
Expand All @@ -185,27 +188,27 @@ class ApiFieldDefinitionsControllerGetSpec extends UnitSpec with FieldDefinition

"GET /definition" should {
"return OK with all field definitions" in {
mockFieldsDefinitionService.getAll _ expects () returns Future.successful(allResponseModel)
when(mockFieldsDefinitionService.getAll).thenReturn(successful(allResponseModel))

val result = await(controller.getAllFieldsDefinitions(FakeRequest()))
val result = controller.getAllFieldsDefinitions(FakeRequest())

status(result) shouldBe OK
contentAsJson(result) shouldBe allResponseJson
}

"return OK with an empty list when no field definitions exist in the repo" in {
mockFieldsDefinitionService.getAll _ expects () returns Future.successful(BulkApiFieldDefinitionsResponse(Seq()))
when(mockFieldsDefinitionService.getAll).thenReturn(successful(BulkApiFieldDefinitionsResponse(Seq())))

val result = await(controller.getAllFieldsDefinitions(FakeRequest()))
val result = controller.getAllFieldsDefinitions(FakeRequest())

status(result) shouldBe OK
contentAsJson(result) shouldBe emptyAllResponseJson
}

"return INTERNAL_SERVER_ERROR when service throws exception" in {
mockFieldsDefinitionService.getAll _ expects () returns Future.failed(emulatedFailure)
when(mockFieldsDefinitionService.getAll).thenReturn(failed(emulatedFailure))

val result = await(controller.getAllFieldsDefinitions(FakeRequest()))
val result = controller.getAllFieldsDefinitions(FakeRequest())

status(result) shouldBe INTERNAL_SERVER_ERROR
(contentAsJson(result) \ "code") shouldBe JsDefined(JsString("UNKNOWN_ERROR"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,37 @@

package uk.gov.hmrc.apisubscriptionfields.controller

import org.scalamock.scalatest.MockFactory
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._
import play.api.test.Helpers._
import play.api.test._
import uk.gov.hmrc.apisubscriptionfields.FieldDefinitionTestData
import uk.gov.hmrc.apisubscriptionfields.model.{FieldDefinitionsRequest, JsonFormatters}
import uk.gov.hmrc.apisubscriptionfields.service.ApiFieldDefinitionsService
import uk.gov.hmrc.play.test.UnitSpec
import uk.gov.hmrc.apisubscriptionfields.AsyncHmrcSpec
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class ApiFieldDefinitionsControllerPostSpec extends UnitSpec
with FieldDefinitionTestData
with MockFactory
with JsonFormatters
with StubControllerComponentsFactory {
class ApiFieldDefinitionsControllerPostSpec extends AsyncHmrcSpec
with FieldDefinitionTestData
with JsonFormatters
with StubControllerComponentsFactory {

private val mockFieldDefintionService = mock[ApiFieldDefinitionsService]
private val controller = new ApiFieldDefinitionsController(stubControllerComponents(), mockFieldDefintionService)

"validateFieldsDefinition" should {
"return OK when FieldDefinitionsRequest is valid" in {

val json = mkJson(FakeValidRegexFieldsDefinitionRequest)
println(Json.prettyPrint(json))

testSubmitResult(mkRequest(json)) { result =>
status(result) shouldBe OK
}
}

"return BadRequest when FieldDefinitionsRequest is invalid" in {

val json = Json.parse(jsonInvalidRegexFieldsDefinitionRequest)

testSubmitResult(mkRequest(json)) { result =>
status(result) shouldBe BAD_REQUEST
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@

package uk.gov.hmrc.apisubscriptionfields.controller

import org.scalamock.scalatest.MockFactory
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._
import play.api.test.Helpers._
import play.api.test._
import uk.gov.hmrc.apisubscriptionfields.FieldDefinitionTestData
import uk.gov.hmrc.apisubscriptionfields.model.{FieldDefinitionsRequest, JsonFormatters}
import uk.gov.hmrc.apisubscriptionfields.service.ApiFieldDefinitionsService
import uk.gov.hmrc.play.test.UnitSpec
import uk.gov.hmrc.apisubscriptionfields.AsyncHmrcSpec

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future.successful
import scala.concurrent.Future

class ApiFieldDefinitionsControllerPutSpec extends UnitSpec
class ApiFieldDefinitionsControllerPutSpec extends AsyncHmrcSpec
with FieldDefinitionTestData
with MockFactory
with JsonFormatters
with StubControllerComponentsFactory {

Expand All @@ -40,9 +39,7 @@ class ApiFieldDefinitionsControllerPutSpec extends UnitSpec

"PUT /definition/context/:apiContext/version/:apiVersion" should {
"return CREATED when created in the repo" in {
(mockApiFieldDefinitionsService.upsert _).
expects(FakeContext, FakeVersion, NelOfFieldDefinitions).
returns(Future.successful((FakeApiFieldDefinitionsResponse, true)))
when(mockApiFieldDefinitionsService.upsert(FakeContext, FakeVersion, NelOfFieldDefinitions)).thenReturn(successful((FakeApiFieldDefinitionsResponse, true)))

val json = mkJson(FieldDefinitionsRequest(NelOfFieldDefinitions))
testSubmitResult(mkRequest(json)) { result =>
Expand All @@ -51,9 +48,7 @@ class ApiFieldDefinitionsControllerPutSpec extends UnitSpec
}

"return OK when updated in the repo" in {
(mockApiFieldDefinitionsService.upsert _).
expects(FakeContext, FakeVersion, NelOfFieldDefinitions).
returns(Future.successful((FakeApiFieldDefinitionsResponse, false)))
when(mockApiFieldDefinitionsService.upsert(FakeContext, FakeVersion, NelOfFieldDefinitions)).thenReturn(successful((FakeApiFieldDefinitionsResponse, false)))

val json = mkJson(FieldDefinitionsRequest(NelOfFieldDefinitions))
testSubmitResult(mkRequest(json)) { result =>
Expand Down
Loading

0 comments on commit e307334

Please sign in to comment.