generated from ministryofjustice/hmpps-template-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#20) - implemented `MNJobBoardApiClient`, with integration tests - implemented `EmployerRegistrar` - rename test objects' file to `EmployerObjects` - `EmployerService` is incomplete - revise tests of JobsBoardApiClient
- Loading branch information
1 parent
c02d16f
commit e7373a2
Showing
23 changed files
with
303 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...jobsboardintegrationapi/integration/shared/infrastructure/MNJobBoardApiWebClientShould.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package uk.gov.justice.digital.hmpps.jobsboardintegrationapi.integration.shared.infrastructure | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.employers.domain.EmployerObjects.sainsburys | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.employers.domain.mnEmployer | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.integration.IntegrationTestBase | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.integration.wiremock.MNJobBoardApiExtension.Companion.mnJobBoardApi | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.CreatEmployerRequest | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.MNEmployer | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.MNJobBoardApiWebClient | ||
import kotlin.test.assertFailsWith | ||
|
||
class MNJobBoardApiWebClientShould : IntegrationTestBase() { | ||
|
||
@Autowired | ||
private lateinit var apiWebClient: MNJobBoardApiWebClient | ||
|
||
@Nested | ||
@DisplayName("MN JobBoard `POST` /employers") | ||
inner class EmployersPostEndpoint { | ||
private val employer = sainsburys | ||
private val mnEmployer: MNEmployer get() = employer.copy(createdAt = timeProvider.nowAsInstant()).mnEmployer() | ||
|
||
@Test | ||
fun `create employer, with valid details`() { | ||
val expectedEmployer = mnEmployer.copy(id = 1L) | ||
mnJobBoardApi.stubCreateEmployer(mnEmployer, expectedEmployer.id!!) | ||
|
||
val actualEmployer = CreatEmployerRequest.from(mnEmployer).let { apiWebClient.createEmployer(it) } | ||
|
||
assertThat(actualEmployer).isEqualTo(expectedEmployer) | ||
} | ||
|
||
@Test | ||
fun `receive unauthorised error, if API access token is invalid`() { | ||
mnJobBoardApi.stubCreateEmployerUnauthorised() | ||
|
||
val exception = assertFailsWith<Exception> { | ||
CreatEmployerRequest.from(mnEmployer).let { apiWebClient.createEmployer(it) } | ||
} | ||
|
||
with(exception) { | ||
assertThat(message).contains("Fail to create employer") // reactive throw (ReactiveException) | ||
with(cause!!) { | ||
assertThat(message).contains("Fail to create employer") // actual throw (Exception) | ||
with(cause!!) { | ||
assertThat(message).contains("401 Unauthorized") // cause (401) (WebClientResponseException$Unauthorized) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ice/digital/hmpps/jobsboardintegrationapi/integration/wiremock/MNJobBoardApiMockServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package uk.gov.justice.digital.hmpps.jobsboardintegrationapi.integration.wiremock | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.client.WireMock.aResponse | ||
import com.github.tomakehurst.wiremock.client.WireMock.matching | ||
import com.github.tomakehurst.wiremock.client.WireMock.post | ||
import org.junit.jupiter.api.extension.AfterAllCallback | ||
import org.junit.jupiter.api.extension.BeforeAllCallback | ||
import org.junit.jupiter.api.extension.BeforeEachCallback | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.MNEmployer | ||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
private const val EMPLOYERS_ENDPOINT = "/employers" | ||
|
||
class MNJobBoardApiMockServer( | ||
private val nextId: AtomicLong = AtomicLong(1), | ||
) : WireMockServer(8093) { | ||
fun stubCreateEmployer(mnEmployer: MNEmployer) = stubCreateEmployer(mnEmployer, nextId.getAndIncrement()) | ||
fun stubCreateEmployer(mnEmployer: MNEmployer, newId: Long) { | ||
val employerCreated = mnEmployer.copy(id = newId) | ||
stubFor( | ||
post(EMPLOYERS_ENDPOINT) | ||
.withHeader("Authorization", matching("^Bearer .+\$")) | ||
.willReturn( | ||
aResponse() | ||
.withHeader("Content-Type", "application/json") | ||
.withBody(employerCreated.response()), | ||
), | ||
) | ||
} | ||
|
||
fun stubCreateEmployerUnauthorised() { | ||
stubFor( | ||
post(EMPLOYERS_ENDPOINT) | ||
.withHeader("Authorization", matching("^Bearer .+\$")) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(401), | ||
), | ||
) | ||
} | ||
} | ||
|
||
class MNJobBoardApiExtension : BeforeAllCallback, AfterAllCallback, BeforeEachCallback { | ||
companion object { | ||
@JvmField | ||
val mnJobBoardApi = MNJobBoardApiMockServer() | ||
} | ||
|
||
override fun beforeAll(context: ExtensionContext): Unit = mnJobBoardApi.start() | ||
override fun beforeEach(context: ExtensionContext): Unit = mnJobBoardApi.resetAll() | ||
override fun afterAll(context: ExtensionContext): Unit = mnJobBoardApi.stop() | ||
} | ||
|
||
private fun MNEmployer.response() = """ | ||
{ | ||
"message": { | ||
"successCode": "J2047", | ||
"successMessage": "Successfully added employer", | ||
"httpStatusCode": 201 | ||
}, | ||
"responseObject": { | ||
"id": $id, | ||
"employerName": "$employerName", | ||
"employerBio": "$employerBio", | ||
"sectorId": $sectorId, | ||
"partnerId": $partnerId, | ||
"imgName": ${imgName?.let { "\"$it\"" }}, | ||
"path": ${path?.let { "\"$it\"" }} | ||
} | ||
} | ||
""".trimIndent() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...uk/gov/justice/digital/hmpps/jobsboardintegrationapi/shared/domain/MNJobBoardApiClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.domain | ||
|
||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.CreatEmployerRequest | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.CreatEmployerResponse | ||
|
||
interface MNJobBoardApiClient { | ||
fun createEmployer(request: CreatEmployerRequest): CreatEmployerResponse | ||
} |
Oops, something went wrong.