Skip to content

Commit

Permalink
PI-1962 Remove feature flag (#3551)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl authored Mar 28, 2024
1 parent 707a74b commit 706f6be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import org.springframework.jdbc.core.SqlParameter
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource
import org.springframework.jdbc.core.simple.SimpleJdbcCall
import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.flags.FeatureFlags
import uk.gov.justice.digital.hmpps.messaging.RiskAssessment
import java.sql.SQLException
import java.sql.Types
import java.time.ZonedDateTime

@Service
class RiskScoreService(jdbcTemplate: JdbcTemplate, val featureFlags: FeatureFlags) {
class RiskScoreService(jdbcTemplate: JdbcTemplate) {
private val updateRsrAndOspScoresProcedure = SimpleJdbcCall(jdbcTemplate)
.withCatalogName("pkg_triggersupport")
.withProcedureName("procUpdateCAS")
Expand All @@ -28,10 +27,6 @@ class RiskScoreService(jdbcTemplate: JdbcTemplate, val featureFlags: FeatureFlag
SqlParameter("p_osp_score_c", Types.NUMERIC),
SqlParameter("p_osp_level_i_code", Types.VARCHAR),
SqlParameter("p_osp_level_c_code", Types.VARCHAR),
)

private val SimpleJdbcCall.withIndirectIndecentAndDirectContact
get() = declareParameters(
SqlParameter("p_osp_level_iic_code", Types.VARCHAR),
SqlParameter("p_osp_level_dc_code", Types.VARCHAR),
)
Expand All @@ -47,26 +42,20 @@ class RiskScoreService(jdbcTemplate: JdbcTemplate, val featureFlags: FeatureFlag
ospDirectContact: RiskAssessment?,
) {
try {
val params = MapSqlParameterSource()
.addValue("p_crn", crn)
.addValue("p_event_number", eventNumber)
.addValue("p_rsr_assessor_date", assessmentDate)
.addValue("p_rsr_score", rsr.score)
.addValue("p_rsr_level_code", rsr.band)
.addValue("p_osp_score_i", ospIndecent?.score)
.addValue("p_osp_score_c", ospContact?.score)
.addValue("p_osp_level_i_code", ospIndecent?.band)
.addValue("p_osp_level_c_code", ospContact?.band)

if (featureFlags.enabled("osp-indirect-indecent-and-direct-contact")) {
updateRsrAndOspScoresProcedure.withIndirectIndecentAndDirectContact.execute(
params
.addValue("p_osp_level_iic_code", ospIndirectIndecent?.band)
.addValue("p_osp_level_dc_code", ospDirectContact?.band)
)
} else {
updateRsrAndOspScoresProcedure.execute(params)
}
updateRsrAndOspScoresProcedure.execute(
MapSqlParameterSource()
.addValue("p_crn", crn)
.addValue("p_event_number", eventNumber)
.addValue("p_rsr_assessor_date", assessmentDate)
.addValue("p_rsr_score", rsr.score)
.addValue("p_rsr_level_code", rsr.band)
.addValue("p_osp_score_i", ospIndecent?.score)
.addValue("p_osp_score_c", ospContact?.score)
.addValue("p_osp_level_i_code", ospIndecent?.band)
.addValue("p_osp_level_c_code", ospContact?.band)
.addValue("p_osp_level_iic_code", ospIndirectIndecent?.band)
.addValue("p_osp_level_dc_code", ospDirectContact?.band)
)
} catch (e: UncategorizedSQLException) {
e.sqlException.takeIf { it.isValidationError }
?.parsedValidationMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package uk.gov.justice.digital.hmpps.integrations.delius
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
Expand All @@ -20,7 +19,6 @@ import org.springframework.jdbc.core.namedparam.MapSqlParameterSource
import org.springframework.jdbc.core.namedparam.SqlParameterSource
import org.springframework.jdbc.core.simple.SimpleJdbcCall
import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.flags.FeatureFlags
import uk.gov.justice.digital.hmpps.messaging.RiskAssessment
import java.sql.SQLException
import java.time.ZonedDateTime
Expand All @@ -30,17 +28,9 @@ internal class RiskScoreServiceTest {
@Mock
private lateinit var jdbcTemplate: JdbcTemplate

@Mock
private lateinit var featureFlags: FeatureFlags

@Mock
private lateinit var simpleJdbcCall: SimpleJdbcCall

@BeforeEach
fun featureFlag() {
givenTheOspFlagIs(true)
}

@Test
fun `scores are passed to the database procedure`() {
givenTheDatabaseProcedureSucceeds().use {
Expand All @@ -51,17 +41,6 @@ internal class RiskScoreServiceTest {
}
}

@Test
fun `scores are passed to the old database procedure when flag is disabled`() {
givenTheOspFlagIs(false)
givenTheDatabaseProcedureSucceeds().use {
assertDoesNotThrow {
whenUpdatingRsrAndOspScores()
thenTheOldProcedureIsCalled()
}
}
}

@Test
fun `known validation errors are logged to telemetry and wrapped`() {
givenTheDatabaseProcedureThrows(sqlException("CRN/Offender does not exist", 20000)).use {
Expand Down Expand Up @@ -95,7 +74,7 @@ internal class RiskScoreServiceTest {
}

private fun whenUpdatingRsrAndOspScores() {
RiskScoreService(jdbcTemplate, featureFlags).updateRsrAndOspScores(
RiskScoreService(jdbcTemplate).updateRsrAndOspScores(
crn = "A000001",
eventNumber = 123,
assessmentDate = ZonedDateTime.of(2022, 12, 15, 9, 0, 0, 0, EuropeLondon),
Expand Down Expand Up @@ -128,32 +107,10 @@ internal class RiskScoreServiceTest {
)
}

private fun thenTheOldProcedureIsCalled() {
val expectedValues = mapOf(
"p_crn" to "A000001",
"p_event_number" to 123,
"p_rsr_assessor_date" to ZonedDateTime.of(2022, 12, 15, 9, 0, 0, 0, EuropeLondon),
"p_rsr_score" to 1.00,
"p_rsr_level_code" to "A",
"p_osp_score_i" to 2.00,
"p_osp_score_c" to 4.00,
"p_osp_level_i_code" to "B",
"p_osp_level_c_code" to "D"
)
verify(simpleJdbcCall).execute(
check<MapSqlParameterSource> { params ->
assertThat(params.values, equalTo(expectedValues))
}
)
}

private fun givenTheDatabaseProcedureSucceeds(): MockedConstruction<SimpleJdbcCall> {
whenever(simpleJdbcCall.withProcedureName("procUpdateCAS")).thenReturn(simpleJdbcCall)
whenever(simpleJdbcCall.withoutProcedureColumnMetaDataAccess()).thenReturn(simpleJdbcCall)
whenever(simpleJdbcCall.declareParameters(*Array(9) { any() })).thenReturn(simpleJdbcCall)
if (featureFlags.enabled("osp-indirect-indecent-and-direct-contact")) {
whenever(simpleJdbcCall.declareParameters(*Array(2) { any() })).thenReturn(simpleJdbcCall)
}
whenever(simpleJdbcCall.declareParameters(*Array(11) { any() })).thenReturn(simpleJdbcCall)
return mockConstructionWithAnswer(SimpleJdbcCall::class.java, { simpleJdbcCall })
}

Expand All @@ -163,10 +120,6 @@ internal class RiskScoreServiceTest {
return mockedConstruction
}

private fun givenTheOspFlagIs(value: Boolean) {
whenever(featureFlags.enabled("osp-indirect-indecent-and-direct-contact")).thenReturn(value)
}

private fun sqlException(message: String? = null, code: Int = 20000) =
UncategorizedSQLException("error", "sql", SQLException(message, "", code))
}

0 comments on commit 706f6be

Please sign in to comment.