-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathElasticIntegrationBase.kt
123 lines (110 loc) · 4.53 KB
/
ElasticIntegrationBase.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package uk.gov.justice.hmpps.probationsearch.controllers
import com.fasterxml.jackson.databind.ObjectMapper
import io.restassured.RestAssured
import org.junit.jupiter.api.BeforeEach
import org.opensearch.client.RestHighLevelClient
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import uk.gov.justice.hmpps.probationsearch.dto.OffenderAlias
import uk.gov.justice.hmpps.probationsearch.dto.OffenderDetail
import uk.gov.justice.hmpps.probationsearch.dto.PhoneNumber
import uk.gov.justice.hmpps.probationsearch.dto.PhoneNumber.PhoneTypes.MOBILE
import uk.gov.justice.hmpps.probationsearch.dto.PhoneNumber.PhoneTypes.TELEPHONE
import uk.gov.justice.hmpps.probationsearch.dto.ProbationArea
import uk.gov.justice.hmpps.probationsearch.dto.Team
import uk.gov.justice.hmpps.probationsearch.util.JwtAuthenticationHelper
import uk.gov.justice.hmpps.probationsearch.util.PersonSearchHelper
import kotlin.random.Random.Default.nextInt
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(profiles = ["test"])
abstract class ElasticIntegrationBase {
@Autowired
internal lateinit var jwtAuthenticationHelper: JwtAuthenticationHelper
@Value("\${local.server.port}")
private var port: Int = 0
@BeforeEach
internal fun before() {
RestAssured.port = port
}
@Autowired
@Qualifier("openSearchClient")
internal lateinit var openSearchClient: RestHighLevelClient
@Autowired
private lateinit var objectMapper: ObjectMapper
fun loadOffenders(vararg offenders: OffenderReplacement) {
val template = "/searchdata/offender-template.json".readResourceAsText()
val templateOffender = objectMapper.readValue(template, OffenderDetail::class.java)
val offendersToLoad = offenders.map { it ->
templateOffender.copy(
offenderId = it.offenderId,
surname = it.surname,
firstName = it.firstName,
middleNames = it.middleNames,
dateOfBirth = it.dateOfBirth,
softDeleted = it.deleted,
gender = it.gender,
otherIds = templateOffender.otherIds.copy(
crn = it.crn,
nomsNumber = it.nomsNumber,
croNumber = it.croNumber,
pncNumber = it.pncNumber,
niNumber = it.niNumber,
previousCrn = it.previousCrn,
),
offenderAliases = it.aliases.map { alias ->
OffenderAlias(
id = nextInt().toString(),
firstName = alias.firstName,
surname = alias.surname,
dateOfBirth = alias.dateOfBirth,
)
},
contactDetails = templateOffender.contactDetails?.copy(
addresses = templateOffender.contactDetails?.addresses?.map { address ->
if (address.status?.code == "M") {
address.copy(
streetName = it.streetName,
town = it.town,
county = it.county,
postcode = it.postcode,
)
} else {
address
}
},
phoneNumbers = listOf(
PhoneNumber(it.phoneNumber, TELEPHONE),
PhoneNumber(it.mobileNumber, MOBILE),
).filter { it.number != null },
),
offenderManagers = templateOffender.offenderManagers?.map { offenderManager ->
it.offenderManagers.find { replacement -> replacement.active == offenderManager.active }
.let { matchingReplacement ->
offenderManager.copy(
probationArea = ProbationArea(
code = matchingReplacement?.code,
description = matchingReplacement?.description
),
team = Team(
code = matchingReplacement?.team?.code,
localDeliveryUnit = matchingReplacement?.team?.localDeliveryUnit
),
softDeleted = matchingReplacement?.softDeleted,
)
}
},
currentExclusion = it.currentExclusion,
currentRestriction = it.currentRestriction,
)
}.map { objectMapper.writeValueAsString(it) }
PersonSearchHelper(openSearchClient).loadData(offendersToLoad)
}
private fun String.readResourceAsText(): String {
return ElasticIntegrationBase::class.java.getResource(this)!!.readText()
}
}
fun Int.toCrn() = "X%05d".format(this)
fun Int.toNomsNumber() = "G%d0GG".format(this)