Skip to content

Commit 65a0177

Browse files
committed
create TestUtil as per PR comment
1 parent 5dd3cc6 commit 65a0177

File tree

5 files changed

+98
-111
lines changed

5 files changed

+98
-111
lines changed

test/controllers/manageAgents/AgentOverviewControllerSpec.scala

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,29 @@
1717
package controllers.manageAgents
1818

1919
import base.SpecBase
20-
import models.AgentDetails
2120
import org.mockito.ArgumentMatchers.any
22-
import org.mockito.Mockito._
21+
import org.mockito.Mockito.*
2322
import org.scalatestplus.mockito.MockitoSugar
2423
import play.api.inject.bind
2524
import play.api.test.FakeRequest
26-
import play.api.test.Helpers._
25+
import play.api.test.Helpers.*
2726
import services.StampDutyLandTaxService
27+
import utils.mangeAgents.AgentDetailsTestUtil
2828
import views.html.manageAgents.AgentOverviewView
2929

3030
import scala.concurrent.Future
3131

32-
class AgentOverviewControllerSpec extends SpecBase with MockitoSugar {
33-
34-
val storn = "STN001"
32+
class AgentOverviewControllerSpec extends SpecBase with MockitoSugar with AgentDetailsTestUtil {
3533

3634
private val service = mock[StampDutyLandTaxService]
3735

3836
private def agentOverviewUrl(page: Int) =
39-
controllers.manageAgents.routes.AgentOverviewController.onPageLoad(storn, page).url
37+
controllers.manageAgents.routes.AgentOverviewController.onPageLoad(testStorn, page).url
4038

4139
private def startAddAgentJourneyUrl =
42-
controllers.manageAgents.routes.StartAddAgentController.onSubmit(storn).url
43-
44-
private def agent(i: Int): AgentDetails =
45-
AgentDetails(
46-
storn = "STN001",
47-
name = s"Agent $i",
48-
houseNumber = "64",
49-
addressLine1 = "Zoo Lane",
50-
addressLine2 = None,
51-
addressLine3 = "Lazy Town",
52-
addressLine4 = None,
53-
postcode = Some("SW44GFS"),
54-
phoneNumber = "0543534534543",
55-
emailAddress = "[email protected]",
56-
agentId = "AN001",
57-
isAuthorised = 1
58-
)
59-
60-
private val agents22 = (1 to 22).map(agent)
40+
controllers.manageAgents.routes.StartAddAgentController.onSubmit(testStorn).url
41+
42+
private val agents22 = getAgentList(22)
6143

6244
"AgentOverviewController.onPageLoad" - {
6345

@@ -82,7 +64,7 @@ class AgentOverviewControllerSpec extends SpecBase with MockitoSugar {
8264
maybeSummaryList = None,
8365
pagination = None,
8466
paginationInfoText = None,
85-
postAction = controllers.manageAgents.routes.StartAddAgentController.onSubmit(storn)
67+
postAction = controllers.manageAgents.routes.StartAddAgentController.onSubmit(testStorn)
8668
)(request, messages(application)).toString
8769
}
8870
}
@@ -94,7 +76,7 @@ class AgentOverviewControllerSpec extends SpecBase with MockitoSugar {
9476
.build()
9577

9678
when(service.getAllAgentDetails(any())(any()))
97-
.thenReturn(Future.successful(agents22.toList))
79+
.thenReturn(Future.successful(agents22))
9880

9981
running(application) {
10082
val request = FakeRequest(GET, agentOverviewUrl(page = 1))
@@ -122,7 +104,7 @@ class AgentOverviewControllerSpec extends SpecBase with MockitoSugar {
122104
.build()
123105

124106
when(service.getAllAgentDetails(any())(any()))
125-
.thenReturn(Future.successful(agents22.toList))
107+
.thenReturn(Future.successful(agents22))
126108

127109
running(application) {
128110
val request = FakeRequest(GET, agentOverviewUrl(page = 0))
@@ -140,7 +122,7 @@ class AgentOverviewControllerSpec extends SpecBase with MockitoSugar {
140122
.build()
141123

142124
when(service.getAllAgentDetails(any())(any()))
143-
.thenReturn(Future.successful(agents22.toList))
125+
.thenReturn(Future.successful(agents22))
144126

145127
running(application) {
146128
val request = FakeRequest(GET, agentOverviewUrl(page = 99))

test/controllers/manageAgents/RemoveAgentControllerSpec.scala

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,15 @@ import services.StampDutyLandTaxService
3333
import views.html.manageAgents.RemoveAgentView
3434
import org.mockito.Mockito.*
3535
import org.mockito.ArgumentMatchers.any
36+
import utils.mangeAgents.AgentDetailsTestUtil
3637

3738
import scala.concurrent.Future
3839

39-
class RemoveAgentControllerSpec extends SpecBase with MockitoSugar {
40+
class RemoveAgentControllerSpec extends SpecBase with MockitoSugar with AgentDetailsTestUtil {
4041

4142
def onwardRoute: Call = controllers.routes.HomeController.onPageLoad()
42-
43-
val storn: String = "STN001"
44-
45-
val testAgentDetails: AgentDetails = AgentDetails(
46-
storn = "STN001",
47-
agentReferenceNumber = Some("ARN001"),
48-
name = "Harborview Estates",
49-
houseNumber = "22A",
50-
addressLine1 = "Queensway",
51-
addressLine2 = None,
52-
addressLine3 = "Birmingham",
53-
addressLine4 = None,
54-
postcode = Some("B2 4ND"),
55-
phoneNumber = "01214567890",
56-
emailAddress = "[email protected]",
57-
agentId = "AGT001",
58-
isAuthorised = 1
59-
)
60-
61-
lazy val removeAgentRequestRoute: String = controllers.manageAgents.routes.RemoveAgentController.onPageLoad(storn).url
43+
44+
lazy val removeAgentRequestRoute: String = controllers.manageAgents.routes.RemoveAgentController.onPageLoad(testStorn).url
6245

6346
val formProvider = new RemoveAgentFormProvider()
6447

test/controllers/manageAgents/StartAddAgentControllerSpec.scala

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,22 @@ package controllers.manageAgents
1919
import base.SpecBase
2020
import models.AgentDetails
2121
import org.mockito.ArgumentMatchers.any
22-
import org.mockito.Mockito._
22+
import org.mockito.Mockito.*
2323
import org.scalatestplus.mockito.MockitoSugar
2424
import play.api.inject.bind
2525
import play.api.test.FakeRequest
26-
import play.api.test.Helpers._
26+
import play.api.test.Helpers.*
2727
import services.StampDutyLandTaxService
28+
import utils.mangeAgents.AgentDetailsTestUtil
2829

2930
import scala.concurrent.Future
3031

31-
class StartAddAgentControllerSpec extends SpecBase with MockitoSugar {
32-
33-
private val storn = "STN001"
32+
class StartAddAgentControllerSpec extends SpecBase with MockitoSugar with AgentDetailsTestUtil {
3433

3534
private val service = mock[StampDutyLandTaxService]
3635

37-
private def postUrl: String =
38-
routes.StartAddAgentController.onSubmit(storn).url
39-
40-
private def agent(i: Int): AgentDetails =
41-
AgentDetails(
42-
storn = "STN001",
43-
name = s"Agent $i",
44-
houseNumber = "64",
45-
addressLine1 = "Zoo Lane",
46-
addressLine2 = None,
47-
addressLine3 = "Lazy Town",
48-
addressLine4 = None,
49-
postcode = Some("SW44GFS"),
50-
phoneNumber = "0543534534543",
51-
emailAddress = "[email protected]",
52-
agentId = "AN001",
53-
isAuthorised = 1
54-
)
55-
56-
private def agents(n: Int): List[AgentDetails] = (1 to n).map(agent).toList
57-
58-
private val Max = 25
59-
36+
private def postUrl: String = routes.StartAddAgentController.onSubmit(testStorn).url
37+
6038
"StartAddAgentController.onSubmit" - {
6139

6240
"must redirect to AgentNameController when the number of agents is below the max" in {
@@ -66,7 +44,7 @@ class StartAddAgentControllerSpec extends SpecBase with MockitoSugar {
6644
.build()
6745

6846
when(service.getAllAgentDetails(any())(any()))
69-
.thenReturn(Future.successful(agents(Max - 1)))
47+
.thenReturn(Future.successful(getAgentList(MAX_AGENTS - 1)))
7048

7149
running(application) {
7250
val request = FakeRequest(POST, postUrl)
@@ -85,15 +63,15 @@ class StartAddAgentControllerSpec extends SpecBase with MockitoSugar {
8563
.build()
8664

8765
when(service.getAllAgentDetails(any())(any()))
88-
.thenReturn(Future.successful(agents(Max)))
66+
.thenReturn(Future.successful(getAgentList(MAX_AGENTS)))
8967

9068
running(application) {
9169
val request = FakeRequest(POST, postUrl)
9270
val result = route(application, request).value
9371

9472
status(result) mustEqual SEE_OTHER
9573
redirectLocation(result).value mustEqual
96-
routes.AgentOverviewController.onPageLoad(storn, 1).url
74+
routes.AgentOverviewController.onPageLoad(testStorn, 1).url
9775

9876
flash(result).get("agentsLimitReached") mustBe Some("true")
9977
}

test/utils/PaginationHelperSpec.scala

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import uk.gov.hmrc.govukfrontend.views.viewmodels.pagination.PaginationLink
2323
import uk.gov.hmrc.govukfrontend.views.viewmodels.summarylist.SummaryList
2424
import controllers.manageAgents.routes
2525
import models.AgentDetails
26+
import utils.mangeAgents.AgentDetailsTestUtil
2627

27-
class PaginationHelperSpec extends AnyWordSpec with Matchers {
28+
class PaginationHelperSpec extends AnyWordSpec with Matchers with AgentDetailsTestUtil {
2829

2930
object TestHelper extends PaginationHelper
3031

@@ -42,25 +43,8 @@ class PaginationHelperSpec extends AnyWordSpec with Matchers {
4243

4344
implicit private val messages: Messages = MessagesImpl(Lang("en"), messagesApi)
4445

45-
private def agent(i: Int): AgentDetails =
46-
AgentDetails(
47-
storn = "STN001",
48-
name = s"Agent $i",
49-
houseNumber = "64",
50-
addressLine1 = s"Address $i",
51-
addressLine2 = None,
52-
addressLine3 = "Town",
53-
addressLine4 = None,
54-
postcode = Some("SW24RF"),
55-
phoneNumber = "09543543532",
56-
emailAddress = "[email protected]",
57-
agentId = "AN001",
58-
isAuthorised = 1
59-
)
60-
61-
private val twentyTwoAgents: Seq[AgentDetails] = (1 to 22).map(agent)
62-
private val nineAgents: Seq[AgentDetails] = (1 to 9).map(agent)
63-
private val storn: String = "STN001"
46+
private val twentyTwoAgents: Seq[AgentDetails] = getAgentList(22)
47+
private val nineAgents: Seq[AgentDetails] = getAgentList(9)
6448

6549
"getNumberOfPages" should {
6650
"return 1 when up to 10 items" in {
@@ -123,11 +107,11 @@ class PaginationHelperSpec extends AnyWordSpec with Matchers {
123107

124108
"generatePagination" should {
125109
"return None when only one page" in {
126-
val res = TestHelper.generatePagination(storn, paginationIndex = 1, numberOfPages = 1)
110+
val res = TestHelper.generatePagination(testStorn, paginationIndex = 1, numberOfPages = 1)
127111
res mustBe None
128112
}
129113
"return items and prev/next correctly for middle page" in {
130-
val res = TestHelper.generatePagination(storn, paginationIndex = 2, numberOfPages = 3).get
114+
val res = TestHelper.generatePagination(testStorn, paginationIndex = 2, numberOfPages = 3).get
131115
val items = res.items.get
132116
items.length mustBe 3
133117
items(1).current mustBe Some(true)
@@ -141,19 +125,19 @@ class PaginationHelperSpec extends AnyWordSpec with Matchers {
141125
next.href must include ("paginationIndex=3")
142126
}
143127
"omit previous on first page and next on last page" in {
144-
val first = TestHelper.generatePagination(storn, 1, 3).get
128+
val first = TestHelper.generatePagination(testStorn, 1, 3).get
145129
first.previous mustBe None
146130
first.next.get.href must include ("paginationIndex=2")
147131

148-
val last = TestHelper.generatePagination(storn, 3, 3).get
132+
val last = TestHelper.generatePagination(testStorn, 3, 3).get
149133
last.next mustBe None
150134
last.previous.get.href must include ("paginationIndex=2")
151135
}
152136
}
153137

154138
"generatePaginationItems" should {
155139
"produce one item per page with current flag set correctly" in {
156-
val items = TestHelper.generatePaginationItems(storn, paginationIndex = 2, numberOfPages = 3)
140+
val items = TestHelper.generatePaginationItems(testStorn, paginationIndex = 2, numberOfPages = 3)
157141
items.map(_.number.get.mkString) mustBe Seq("1", "2", "3")
158142
items.map(_.current) mustBe Seq(Some(false), Some(true), Some(false))
159143
items.head.href must include ("paginationIndex=1")
@@ -164,15 +148,15 @@ class PaginationHelperSpec extends AnyWordSpec with Matchers {
164148

165149
"generatePreviousLink / generateNextLink" should {
166150
"return None for previous on page 1, and None for next on the last page" in {
167-
TestHelper.generatePreviousLink(storn, 1, 3) mustBe None
168-
TestHelper.generateNextLink(storn, 3, 3) mustBe None
151+
TestHelper.generatePreviousLink(testStorn, 1, 3) mustBe None
152+
TestHelper.generateNextLink(testStorn, 3, 3) mustBe None
169153
}
170154
"return proper links on middle page" in {
171-
val prev: PaginationLink = TestHelper.generatePreviousLink(storn, 2, 3).get
155+
val prev: PaginationLink = TestHelper.generatePreviousLink(testStorn, 2, 3).get
172156
prev.text.get mustBe "Previous"
173157
prev.href must include ("paginationIndex=1")
174158

175-
val next: PaginationLink = TestHelper.generateNextLink(storn, 2, 3).get
159+
val next: PaginationLink = TestHelper.generateNextLink(testStorn, 2, 3).get
176160
next.text.get mustBe "Next"
177161
next.href must include ("paginationIndex=3")
178162
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2025 HM Revenue & Customs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package utils.mangeAgents
18+
19+
import models.AgentDetails
20+
21+
trait AgentDetailsTestUtil {
22+
23+
val testStorn: String = "STN001"
24+
25+
val MAX_AGENTS = 25
26+
27+
private def agent(i: Int): AgentDetails =
28+
AgentDetails(
29+
storn = "STN001",
30+
name = s"Agent $i",
31+
houseNumber = "64",
32+
addressLine1 = "Zoo Lane",
33+
addressLine2 = None,
34+
addressLine3 = "Lazy Town",
35+
addressLine4 = None,
36+
postcode = Some("SW44GFS"),
37+
phoneNumber = "0543534534543",
38+
emailAddress = "[email protected]",
39+
agentId = "AN001",
40+
isAuthorised = 1
41+
)
42+
43+
def getAgentList(n: Int): List[AgentDetails] = (1 to n).map(agent).toList
44+
45+
val testAgentDetails: AgentDetails = AgentDetails(
46+
storn = "STN001",
47+
agentReferenceNumber = Some("ARN001"),
48+
name = "Harborview Estates",
49+
houseNumber = "22A",
50+
addressLine1 = "Queensway",
51+
addressLine2 = None,
52+
addressLine3 = "Birmingham",
53+
addressLine4 = None,
54+
postcode = Some("B2 4ND"),
55+
phoneNumber = "01214567890",
56+
emailAddress = "[email protected]",
57+
agentId = "AGT001",
58+
isAuthorised = 1
59+
)
60+
}

0 commit comments

Comments
 (0)