generated from ministryofjustice/hmpps-template-kotlin
-
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; JB API client, retriever #15
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 43 additions & 0 deletions
43
...n/uk/gov/justice/digital/hmpps/jobsboardintegrationapi/employers/domain/EmployerMother.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,43 @@ | ||
package uk.gov.justice.digital.hmpps.jobsboardintegrationapi.employers.domain | ||
|
||
object EmployerMother { | ||
val tesco = Employer( | ||
id = "89de6c84-3372-4546-bbc1-9d1dc9ceb354", | ||
name = "Tesco", | ||
description = "Tesco plc is a British multinational groceries and general merchandise retailer headquartered in Welwyn Garden City, England. The company was founded by Jack Cohen in Hackney, London in 1919.", | ||
sector = "RETAIL", | ||
status = "SILVER", | ||
) | ||
|
||
val tescoLogistics = Employer( | ||
id = "2c8032bf-e583-4ae9-bcec-968a1c4881f9", | ||
name = "Tesco", | ||
description = "This is another Tesco employer that provides logistic services.", | ||
sector = "LOGISTICS", | ||
status = "GOLD", | ||
) | ||
|
||
val sainsburys = Employer( | ||
id = "f4fbdbf3-823c-4877-aafc-35a7fa74a15a", | ||
name = "Sainsbury's", | ||
description = "J Sainsbury plc, trading as Sainsbury's, is a British supermarket and the second-largest chain of supermarkets in the United Kingdom. Founded in 1869 by John James Sainsbury with a shop in Drury Lane, London, the company was the largest UK retailer of groceries for most of the 20th century.", | ||
sector = "RETAIL", | ||
status = "GOLD", | ||
) | ||
|
||
val amazon = Employer( | ||
id = "bf392249-b360-4e3e-81a0-8497047987e8", | ||
name = "Amazon", | ||
description = "Amazon.com, Inc., doing business as Amazon, is an American multinational technology company, engaged in e-commerce, cloud computing, online advertising, digital streaming, and artificial intelligence.", | ||
sector = "LOGISTICS", | ||
status = "KEY_PARTNER", | ||
) | ||
|
||
val abcConstruction = Employer( | ||
id = "182e9a24-6edb-48a6-a84f-b7061f004a97", | ||
name = "ABC Construction", | ||
description = "This is a description", | ||
sector = "CONSTRUCTION", | ||
status = "SILVER", | ||
) | ||
} |
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
46 changes: 46 additions & 0 deletions
46
.../jobsboardintegrationapi/integration/shared/infrastructure/JobsBoardApiWebClientShould.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,46 @@ | ||
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.EmployerMother.sainsburys | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.integration.IntegrationTestBase | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.integration.wiremock.HmppsAuthApiExtension.Companion.hmppsAuth | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.integration.wiremock.JobsBoardApiExtension.Companion.jobsBoardApi | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.JobsBoardApiWebClient | ||
|
||
class JobsBoardApiWebClientShould : IntegrationTestBase() { | ||
|
||
@Autowired | ||
private lateinit var jobsBoardApiWebClient: JobsBoardApiWebClient | ||
|
||
@Nested | ||
@DisplayName("JobsBoard `GET` /employers/{id}") | ||
inner class EmployersGetDetailsEndpoint { | ||
@Test | ||
fun `return employer details, given valid employer ID`() { | ||
val employer = sainsburys.copy(createdAt = timeProvider.nowAsInstant()) | ||
|
||
hmppsAuth.stubGrantToken() | ||
jobsBoardApi.stubRetrieveEmployer(employer) | ||
|
||
val actualEmployer = jobsBoardApiWebClient.getEmployer(employer.id) | ||
|
||
assertThat(actualEmployer).isEqualTo(employer) | ||
} | ||
|
||
@Test | ||
fun `return nothing, given invalid employer ID`() { | ||
val employerId = randomUUID() | ||
|
||
hmppsAuth.stubGrantToken() | ||
jobsBoardApi.stubRetrieveEmployerNotFound() | ||
|
||
val actualEmployer = jobsBoardApiWebClient.getEmployer(employerId) | ||
|
||
assertThat(actualEmployer).isNull() | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...tice/digital/hmpps/jobsboardintegrationapi/integration/wiremock/JobsBoardApiMockServer.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,58 @@ | ||
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.get | ||
import com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching | ||
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.employers.domain.Employer | ||
|
||
class JobsBoardApiMockServer : WireMockServer(8092) { | ||
private val retrieveEmployerPathRegex = "/employers/[a-zA-Z0-9\\-]*" | ||
|
||
fun stubRetrieveEmployer(employer: Employer) { | ||
stubFor( | ||
get(urlPathMatching(retrieveEmployerPathRegex)) | ||
.willReturn( | ||
aResponse() | ||
.withHeader("Content-Type", "application/json") | ||
.withBody(employer.response()), | ||
), | ||
) | ||
} | ||
|
||
fun stubRetrieveEmployerNotFound() { | ||
stubFor( | ||
get(urlPathMatching(retrieveEmployerPathRegex)) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(404), | ||
), | ||
) | ||
} | ||
} | ||
|
||
class JobsBoardApiExtension : BeforeAllCallback, AfterAllCallback, BeforeEachCallback { | ||
companion object { | ||
@JvmField | ||
val jobsBoardApi = JobsBoardApiMockServer() | ||
} | ||
|
||
override fun beforeAll(context: ExtensionContext): Unit = jobsBoardApi.start() | ||
override fun beforeEach(context: ExtensionContext): Unit = jobsBoardApi.resetAll() | ||
override fun afterAll(context: ExtensionContext): Unit = jobsBoardApi.stop() | ||
} | ||
|
||
private fun Employer.response() = """ | ||
{ | ||
"id": "$id", | ||
"name": "$name", | ||
"description": "$description", | ||
"sector": "$sector", | ||
"status": "$status", | ||
"createdAt": ${createdAt?.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
12 changes: 12 additions & 0 deletions
12
...ov/justice/digital/hmpps/jobsboardintegrationapi/employers/application/EmployerService.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,12 @@ | ||
package uk.gov.justice.digital.hmpps.jobsboardintegrationapi.employers.application | ||
|
||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.employers.domain.Employer | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.domain.JobsBoardApiClient | ||
|
||
@Service | ||
class EmployerService( | ||
private val jobsBoardApiClient: JobsBoardApiClient, | ||
) { | ||
fun retrieveById(id: String): Employer? = jobsBoardApiClient.getEmployer(id) | ||
} |
7 changes: 7 additions & 0 deletions
7
.../uk/gov/justice/digital/hmpps/jobsboardintegrationapi/shared/domain/JobsBoardApiClient.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,7 @@ | ||
package uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.domain | ||
|
||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.employers.domain.Employer | ||
|
||
interface JobsBoardApiClient { | ||
fun getEmployer(id: String): Employer? | ||
} |
65 changes: 65 additions & 0 deletions
65
...tice/digital/hmpps/jobsboardintegrationapi/shared/infrastructure/JobsBoardApiWebClient.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,65 @@ | ||
package uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.http.MediaType.APPLICATION_JSON | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.springframework.web.reactive.function.client.WebClientResponseException | ||
import reactor.core.publisher.Mono | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.employers.domain.Employer | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.domain.JobsBoardApiClient | ||
import java.time.Instant | ||
|
||
@Service | ||
class JobsBoardApiWebClient( | ||
@Qualifier("jobsBoardWebClient") private val jobsBoardWebClient: WebClient, | ||
) : JobsBoardApiClient { | ||
|
||
companion object { | ||
val log: Logger = LoggerFactory.getLogger(this::class.java) | ||
} | ||
|
||
override fun getEmployer(id: String): Employer? { | ||
log.debug("Getting employer details with id={}", id) | ||
return jobsBoardWebClient | ||
.get().uri("/employers/{id}", id).accept(APPLICATION_JSON).retrieve() | ||
.bodyToMono(GetEmployerResponse::class.java) | ||
.onErrorResume(WebClientResponseException.NotFound::class.java) { | ||
log.debug("Employer not found. employerId={}", id) | ||
Mono.empty() | ||
}.block()?.employer() | ||
} | ||
} | ||
|
||
data class GetEmployerResponse( | ||
val id: String, | ||
val name: String, | ||
val description: String, | ||
val sector: String, | ||
val status: String, | ||
val createdAt: String, | ||
) { | ||
companion object { | ||
fun from(employer: Employer): GetEmployerResponse { | ||
return GetEmployerResponse( | ||
id = employer.id, | ||
name = employer.name, | ||
description = employer.description, | ||
sector = employer.sector, | ||
status = employer.status, | ||
createdAt = employer.createdAt.toString(), | ||
) | ||
} | ||
} | ||
|
||
fun employer() = Employer( | ||
id = id, | ||
name = name, | ||
description = description, | ||
sector = sector, | ||
status = status, | ||
createdAt = Instant.parse(createdAt), | ||
) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
copied from jobs-board backend
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.
The naming and idea was taken from this pattern: https://martinfowler.com/bliki/ObjectMother.html
Thanks for asking @gbmojo
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.
Let's rename it later