-
Notifications
You must be signed in to change notification settings - Fork 0
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
[ESWE-1181] Employer Creation; MN API client, registrar; revise tests #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ generic-service: | |
# TODO: This should be replaced by a call to a different service, or removed | ||
EXAMPLE_API_URL: "https://jobs-board-integration-api-preprod.hmpps.service.justice.gov.uk" | ||
API_BASE_URL_JOBSBOARD: "https://jobs-board-api-preprod.hmpps.service.justice.gov.uk" | ||
API_BASE_URL_MNJOBBOARD: "https://preprodservices.sequation.net/sequation-job-api" # URL to be confirmed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actual URL is TBD |
||
|
||
serviceAccountName: education-skills-work-employment-preprod | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ generic-service: | |
# TODO: This should be replaced by a call to a different service, or removed | ||
EXAMPLE_API_URL: "https://jobs-board-integration-api.hmpps.service.justice.gov.uk" | ||
API_BASE_URL_JOBSBOARD: "https://jobs-board-api.hmpps.service.justice.gov.uk" | ||
API_BASE_URL_MNJOBBOARD: "https://liveservices.sequation.net/sequation-job-api" # URL to be confirmed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actual URL is TBD |
||
|
||
serviceAccountName: education-skills-work-employment-prod | ||
|
||
|
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) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,37 @@ import org.springframework.stereotype.Service | |
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.config.ConditionalOnIntegrationEnabled | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.employers.domain.Employer | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.domain.JobsBoardApiClient | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.domain.MNJobBoardApiClient | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.CreatEmployerRequest | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.MNEmployer | ||
|
||
@ConditionalOnIntegrationEnabled | ||
@Service | ||
class EmployerService( | ||
private val jobsBoardApiClient: JobsBoardApiClient, | ||
private val mnJobBoardApiClient: MNJobBoardApiClient, | ||
) { | ||
fun retrieveById(id: String): Employer? = jobsBoardApiClient.getEmployer(id) | ||
|
||
fun create(mnEmployer: MNEmployer): MNEmployer { | ||
val request = CreatEmployerRequest.from(mnEmployer) | ||
// TODO cater optional fields for employerStatus = KEY_PARTNER (sectorId==2) | ||
return mnJobBoardApiClient.createEmployer(request) | ||
} | ||
|
||
fun convert(employer: Employer) = employer.run { | ||
MNEmployer( | ||
employerName = name, | ||
employerBio = description, | ||
// FIXME translate from employer.sector | ||
sectorId = 1, | ||
// FIXME translate from employer.status | ||
partnerId = 1, | ||
) | ||
} | ||
Comment on lines
+25
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incomplete. |
||
|
||
fun createIdMapping(mnId: Long, employerId: String) { | ||
// TODO persist ID mapping | ||
throw NotImplementedError("ID Mapping is not yet implemented") | ||
} | ||
} |
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map secret value to env var, for MN APi client's authorisation