Skip to content

Commit

Permalink
PI-2391: Fixed issues with missing test data (#4078)
Browse files Browse the repository at this point in the history
* PI-2391: Fixed issues with missing test data
  • Loading branch information
pmcphee77 authored Jul 29, 2024
1 parent 65eb7bb commit edebd3e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,47 @@ internal class ProxyIntegrationTest {
).andExpect(status().is2xxSuccessful).andReturn().response.contentAsJson<CompareAllReport>()

assertThat(res.totalNumberOfRequests, equalTo(8))
assertThat(res.totalNumberOfCrns, equalTo(1))
assertThat(res.totalNumberOfCrns, equalTo(2))
assertThat(res.currentPageNumber, equalTo(1))
}

@Test
fun `compare when test data is not available`() {
val res = mockMvc.perform(
post("/secure/compareAll")
.contentType("application/json;charset=utf-8")
.content(
"""
{
"pageNumber": 2,
"pageSize": 1,
"crns": [ "C123456", "U123456"],
"uriConfig": {
"CONVICTION_BY_ID": {
"convictionId": "?"
},
"CONVICTION_REQUIREMENTS": {
"convictionId": "?",
"activeOnly": true,
"excludeSoftDeleted": true
},
"CONVICTION_BY_ID_NSIS": {
"convictionId": "?",
"nsiCodes": "?"
},
"CONVICTION_BY_ID_PSS": {
"convictionId": "?"
}
}
}
"""
)
.withToken()
).andExpect(status().is2xxSuccessful).andReturn().response.contentAsJson<CompareAllReport>()

assertThat(res.totalNumberOfRequests, equalTo(0))
assertThat(res.totalNumberOfCrns, equalTo(2))
assertThat(res.currentPageNumber, equalTo(2))
assertThat(res.unableToBeExecuted, equalTo(4))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,18 @@ class CommunityApiController(
.map(CompletableFuture<CompareReport>::join)
.collect(Collectors.toList())
}
val unsuccessful = reports.filter { !it.success }
val unsuccessful = reports.filter { !it.success && it.testExecuted == true }
val executionFailures = reports.filter { it.testExecuted == false }.size

return CompareAllReport(
totalNumberOfCrns = personList.numberOfElements,
totalNumberOfCrns = personList.totalElements.toInt(),
totalPages = pageable.pageSize,
currentPageNumber = compare.pageNumber,
totalNumberOfRequests = reports.size,
totalNumberOfRequests = reports.size - executionFailures,
numberOfSuccessfulRequests = reports.size - unsuccessful.size,
numberOfUnsuccessfulRequests = unsuccessful.size,
failureReports = reports.filter { !it.success }
unableToBeExecuted = executionFailures,
failureReports = reports.filter { !it.success && it.testExecuted == true }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CommunityApiService(
val instance = applicationContext.getBean(uri.ccdInstance)
val function = instance::class.members.firstOrNull { it.name == uri.ccdFunction }
val functionParams = function?.parameters?.drop(1)?.associateBy({ it }, {
generateValue(it, compare.params)
generateValue(it, compare.params, uri.urlParams)
})!!
val paramsFunc = function.parameters
val params = mapOf(paramsFunc[0] to instance) + functionParams
Expand All @@ -38,8 +38,12 @@ class CommunityApiService(
)
}

fun generateValue(param: KParameter, originalValue: Map<*, *>): Any? {
fun generateValue(param: KParameter, originalValue: Map<*, *>, paramNames: List<String>): Any? {
var value = originalValue.values.toList()[param.index - 1]
if (value == "?" || value == "") {
val name = paramNames[param.index - 1]
throw DataNotAvailableException(name)
}
if (param.type.classifier == List::class) {
value = value.toString().split(",")
}
Expand All @@ -50,13 +54,24 @@ class CommunityApiService(
fun compare(compare: Compare, headers: Map<String, String>): CompareReport {

val uri = Uri.valueOf(compare.uri)
val ccdJsonString = getCcdJson(compare)
val comApiUri = compare.params.entries.fold(uri.comApiUrl) { path, (key, value) ->
path.replace(
"{$key}",
value.toString()
)
}.replace(" ", "%20")

val ccdJsonString = try {
getCcdJson(compare)
} catch (ex: DataNotAvailableException) {
return CompareReport(
endPointName = uri.name,
url = comApiUri,
message = ex.message!! + " for ${compare.params["crn"]}",
success = false
)
}

val comApiJsonString = communityApiClient.proxy(URI.create(communityApiUrl + comApiUri), headers).body!!
val ccdJson = Json.createReader(StringReader(ccdJsonString)).readValue() as JsonStructure
val comApiJson = Json.createReader(StringReader(comApiJsonString)).readValue() as JsonStructure
Expand All @@ -68,7 +83,8 @@ class CommunityApiService(
message = "${results.size} differences found between New API and Community API",
issues = results,
url = comApiUri,
success = results.isEmpty()
success = results.isEmpty(),
testExecuted = true
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ data class CompareAllReport(
val totalNumberOfRequests: Int,
val numberOfSuccessfulRequests: Int,
val numberOfUnsuccessfulRequests: Int,
val unableToBeExecuted: Int,
val failureReports: List<CompareReport>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ data class CompareReport(
val endPointName: String,
val message: String,
val url: String? = null,
val testExecuted: Boolean? = false,
val success: Boolean,
val issues: List<String>? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package uk.gov.justice.digital.hmpps.api.proxy

class DataNotAvailableException(param: String) :
RuntimeException("No Data available for param $param")
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Nsi(
val personId: Long,

@Column(name = "event_id")
val eventId: Long,
val eventId: Long?,

@ManyToOne
@JoinColumn(name = "nsi_type_id")
Expand Down

0 comments on commit edebd3e

Please sign in to comment.