Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up wiremock #887

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,10 @@ class IntegrationTestBase {
)
}

internal fun stubDeletePersonMatch(scenario: String = BASE_SCENARIO, currentScenarioState: String = STARTED, nextScenarioState: String = STARTED, status: Int = 200, body: String = "{}") {
internal fun stubDeletePersonMatch(status: Int = 200) {
stubDeleteRequest(
scenario,
currentScenarioState,
nextScenarioState,
"/person",
url = "/person",
status = status,
body = body,
)
}

Expand Down Expand Up @@ -239,7 +235,7 @@ class IntegrationTestBase {
)
}

internal fun stubDeleteRequest(scenarioName: String? = BASE_SCENARIO, currentScenarioState: String? = STARTED, nextScenarioState: String? = STARTED, url: String, body: String, status: Int = 200) {
internal fun stubDeleteRequest(scenarioName: String? = BASE_SCENARIO, currentScenarioState: String? = STARTED, nextScenarioState: String? = STARTED, url: String, body: String = "{}", status: Int = 200) {
wiremock.stubFor(
WireMock.delete(url)
.inScenario(scenarioName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ abstract class MessagingMultiNodeTestBase : IntegrationTestBase() {

private fun stubSingleProbationResponse(probationCase: ApiResponseSetup, scenarioName: String, currentScenarioState: String, nextScenarioState: String) = stubGetRequest(scenarioName, currentScenarioState, nextScenarioState, "/probation-cases/${probationCase.crn}", probationCaseResponse(probationCase))

fun stub500Response(url: String, nextScenarioState: String = "Next request will succeed", scenarioName: String, currentScenarioState: String = STARTED) = stubGetRequest(scenarioName, currentScenarioState, nextScenarioState, url, body = "", status = 500)
fun stub500Response(url: String, nextScenarioState: String = "Next request will succeed", scenarioName: String? = BASE_SCENARIO, currentScenarioState: String = STARTED) = stubGetRequest(scenarioName, currentScenarioState, nextScenarioState, url, body = "", status = 500)

fun stubPrisonResponse(
apiResponseSetup: ApiResponseSetup,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.justice.digital.hmpps.personrecord.message.listeners.prison

import com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -243,7 +242,7 @@ class PrisonEventListenerIntTest : MessagingMultiNodeTestBase() {
@Test
fun `should retry on retryable error`() {
val prisonNumber = randomPrisonNumber()
stub500Response(prisonNumber, "next request will succeed")
stub500Response("/prisoner/$prisonNumber", nextScenarioState = "next request will succeed", scenarioName = "retry")
stubPrisonResponse(ApiResponseSetup(prisonNumber = prisonNumber), scenarioName = "retry", currentScenarioState = "next request will succeed")
val additionalInformation = AdditionalInformation(prisonNumber = prisonNumber, categoriesChanged = listOf("SENTENCE"))
val domainEvent = DomainEvent(eventType = PRISONER_CREATED, personReference = null, additionalInformation = additionalInformation)
Expand All @@ -264,9 +263,9 @@ class PrisonEventListenerIntTest : MessagingMultiNodeTestBase() {
@Test
fun `should create message processing failed telemetry event when exception thrown`() {
val prisonNumber = randomPrisonNumber()
stub500Response(prisonNumber, STARTED)
stub500Response(prisonNumber, STARTED)
stub500Response(prisonNumber, STARTED)
stub500Response("/prisoner/$prisonNumber", currentScenarioState = "next request will fail", nextScenarioState = "next request will fail", scenarioName = "processing fail")
stub500Response("/prisoner/$prisonNumber", "next request will fail", scenarioName = "processing fail")
stub500Response("/prisoner/$prisonNumber", "next request will fail", scenarioName = "processing fail")
val additionalInformation = AdditionalInformation(prisonNumber = prisonNumber, categoriesChanged = listOf("SENTENCE"))
val domainEvent = DomainEvent(eventType = PRISONER_CREATED, personReference = null, additionalInformation = additionalInformation)
val messageId = publishDomainEvent(PRISONER_CREATED, domainEvent)
Expand Down Expand Up @@ -391,11 +390,6 @@ class PrisonEventListenerIntTest : MessagingMultiNodeTestBase() {
),
)

private fun stub500Response(
prisonNumber: String,
nextScenarioState: String,
) = stubGetRequest(url = "/prisoner/$prisonNumber", scenarioName = "retry", currentScenarioState = STARTED, nextScenarioState = nextScenarioState, body = "", status = 500)

companion object {
@JvmStatic
private fun currentlyManagedParameters(): Stream<Arguments> = Stream.of(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.justice.digital.hmpps.personrecord.message.listeners.probation

import com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED
import org.assertj.core.api.Assertions.assertThat
import org.awaitility.kotlin.await
import org.awaitility.kotlin.untilAsserted
Expand Down Expand Up @@ -418,12 +417,12 @@ class ProbationUnmergeEventListenerIntTest : MessagingMultiNodeTestBase() {
fun `should log when message processing fails`() {
val reactivatedCrn = randomCrn()
val unmergedCrn = randomCrn()
stub500Response(probationUrl(unmergedCrn), STARTED, "failure")
stub500Response(probationUrl(unmergedCrn), STARTED, "failure")
stub500Response(probationUrl(unmergedCrn), STARTED, "failure")
stub500Response(probationUrl(reactivatedCrn), STARTED, "failure")
stub500Response(probationUrl(reactivatedCrn), STARTED, "failure")
stub500Response(probationUrl(reactivatedCrn), STARTED, "failure")
stub500Response(probationUrl(unmergedCrn), "next request will fail", "failure")
stub500Response(probationUrl(unmergedCrn), "next request will fail", "failure", "next request will fail")
stub500Response(probationUrl(unmergedCrn), "next request will fail", "failure", "next request will fail")
stub500Response(probationUrl(reactivatedCrn), "next request will fail", "failure", "next request will fail")
stub500Response(probationUrl(reactivatedCrn), "next request will fail", "failure", "next request will fail")
stub500Response(probationUrl(reactivatedCrn), "next request will fail", "failure", "next request will fail")

val messageId = publishDomainEvent(
OFFENDER_UNMERGED,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.justice.digital.hmpps.personrecord.seeding

import com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import uk.gov.justice.digital.hmpps.personrecord.config.WebTestBase
Expand All @@ -20,7 +19,6 @@ class UpdateFromProbationIntTest : WebTestBase() {

@Test
fun `update from probation`() {
val scenarioName = "update"
val crnOne: String = randomCrn()
val crnTwo: String = randomCrn()
val crnThree: String = randomCrn()
Expand All @@ -38,10 +36,10 @@ class UpdateFromProbationIntTest : WebTestBase() {
crn = crnSeven,
),
)
stubResponse(crnOne, "POPOne", crnTwo, "POPTwo", 0, scenarioName, STARTED)
stubResponse(crnThree, "POPThree", crnFour, "POPFour", 1, scenarioName, STARTED)
stubResponse(crnFive, "POPFive", crnSix, "POPSix", 2, scenarioName, STARTED)
stubSingleResponse(crnSeven, "POPSeven", 3, scenarioName)
stubResponse(crnOne, "POPOne", crnTwo, "POPTwo", 0)
stubResponse(crnThree, "POPThree", crnFour, "POPFour", 1)
stubResponse(crnFive, "POPFive", crnSix, "POPSix", 2)
stubSingleResponse(crnSeven, "POPSeven", 3)

webTestClient.post()
.uri("/updatefromprobation")
Expand Down Expand Up @@ -79,7 +77,6 @@ class UpdateFromProbationIntTest : WebTestBase() {

@Test
fun `start on page 2`() {
val scenarioName = "update"
val crnOne: String = randomCrn()
val crnTwo: String = randomCrn()
val crnThree: String = randomCrn()
Expand All @@ -88,8 +85,8 @@ class UpdateFromProbationIntTest : WebTestBase() {
val crnSix: String = randomCrn()
val crnSeven: String = randomCrn()

stubResponse(crnFive, "POPFive", crnSix, "POPSix", 2, scenarioName, STARTED)
stubSingleResponse(crnSeven, "POPSeven", 3, scenarioName)
stubResponse(crnFive, "POPFive", crnSix, "POPSix", 2)
stubSingleResponse(crnSeven, "POPSeven", 3)

webTestClient.post()
.uri("/updatefromprobation?startPage=2")
Expand All @@ -113,17 +110,13 @@ class UpdateFromProbationIntTest : WebTestBase() {
assertThat(popSeven.sentenceInfo[0].sentenceDate).isEqualTo(newSentenceDate)
}

private fun stubResponse(firstCrn: String, firstPrefix: String, secondCrn: String, secondPrefix: String, page: Int, scenarioName: String, scenarioState: String, totalPages: Int = 4) = stubGetRequest(
private fun stubResponse(firstCrn: String, firstPrefix: String, secondCrn: String, secondPrefix: String, page: Int) = stubGetRequest(
url = "/all-probation-cases?size=2&page=$page&sort=id%2Casc",
scenarioName = scenarioName,
currentScenarioState = scenarioState,
body = allProbationCasesResponse(firstCrn, firstPrefix, secondCrn, secondPrefix, totalPages),
body = allProbationCasesResponse(firstCrn, firstPrefix, secondCrn, secondPrefix, 4),
)

private fun stubSingleResponse(firstCrn: String, firstPrefix: String, page: Int, scenarioName: String) = stubGetRequest(
private fun stubSingleResponse(firstCrn: String, firstPrefix: String, page: Int) = stubGetRequest(
url = "/all-probation-cases?size=2&page=$page&sort=id%2Casc",
scenarioName = scenarioName,
currentScenarioState = STARTED,
body = allProbationCasesSingleResponse(firstCrn, firstPrefix),
)
}