generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAN-28 - add new api to get individual note
- Loading branch information
1 parent
c2f31fc
commit 1f65a94
Showing
6 changed files
with
177 additions
and
38 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
...rc/integrationTest/kotlin/uk/gov/justice/digital/hmpps/LicenceConditionIntegrationTest.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,94 @@ | ||
package uk.gov.justice.digital.hmpps | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceConditionNote | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceConditionNoteDetail | ||
import uk.gov.justice.digital.hmpps.data.generator.LicenceConditionGenerator | ||
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator | ||
import uk.gov.justice.digital.hmpps.service.toSummary | ||
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson | ||
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken | ||
import java.time.LocalDate | ||
|
||
@AutoConfigureMockMvc | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class LicenceConditionIntegrationTest { | ||
@Autowired | ||
lateinit var mockMvc: MockMvc | ||
|
||
@Test | ||
fun `unauthorized status returned`() { | ||
mockMvc | ||
.perform(MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/licence-condition/1/note/1")) | ||
.andExpect(MockMvcResultMatchers.status().isUnauthorized) | ||
} | ||
|
||
@Test | ||
fun `licence condition not found`() { | ||
val response = mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/licence-condition/1/note/6") | ||
.withToken() | ||
) | ||
.andDo(print()) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
.andReturn().response.contentAsJson<LicenceConditionNoteDetail>() | ||
|
||
val expected = LicenceConditionNoteDetail(PersonGenerator.OVERVIEW.toSummary()) | ||
|
||
assertEquals(expected, response) | ||
} | ||
|
||
@Test | ||
fun `note not found`() { | ||
val response = mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/licence-condition/${LicenceConditionGenerator.LC_WITH_NOTES.id}/note/7") | ||
.withToken() | ||
) | ||
.andDo(print()) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
.andReturn().response.contentAsJson<LicenceConditionNoteDetail>() | ||
|
||
val expected = LicenceConditionNoteDetail(PersonGenerator.OVERVIEW.toSummary()) | ||
|
||
assertEquals(expected, response) | ||
} | ||
|
||
@Test | ||
fun `get note for licence condition`() { | ||
val response = mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/licence-condition/${LicenceConditionGenerator.LC_WITH_NOTES.id}/note/1") | ||
.withToken() | ||
) | ||
.andDo(print()) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
.andReturn().response.contentAsJson<LicenceConditionNoteDetail>() | ||
|
||
val expected = LicenceConditionNoteDetail( | ||
PersonGenerator.OVERVIEW.toSummary(), | ||
LicenceConditionNote( | ||
1, | ||
"Joe Root", | ||
LocalDate.of(2024, 4, 23), | ||
""" | ||
You must not drink any alcohol until Wednesday 7th August 2024 unless your | ||
probation officer says you can. You will need to wear an electronic tag all the time so | ||
we can check this. | ||
""".trimIndent(), | ||
false | ||
) | ||
) | ||
|
||
assertEquals(expected, response) | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
...nd-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceConditionService.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,63 @@ | ||
package uk.gov.justice.digital.hmpps.service | ||
|
||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceConditionNote | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceConditionNoteDetail | ||
import uk.gov.justice.digital.hmpps.datetime.DeliusDateFormatter | ||
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonRepository | ||
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.getPerson | ||
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.LicenceConditionRepository | ||
import java.time.LocalDate | ||
import kotlin.jvm.optionals.getOrNull | ||
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.LicenceCondition as EntityLicenceCondition | ||
|
||
@Service | ||
class LicenceConditionService( | ||
private val personRepository: PersonRepository, | ||
private val licenceConditionRepository: LicenceConditionRepository) { | ||
|
||
fun getLicenceConditionNote(crn: String, licenceConditionId: Long, noteId: Int): LicenceConditionNoteDetail { | ||
val person = personRepository.getPerson(crn) | ||
|
||
val licenceCondition = licenceConditionRepository.findById(licenceConditionId).getOrNull() | ||
|
||
return LicenceConditionNoteDetail( | ||
person.toSummary(), | ||
licenceCondition?.toLicenceConditionNote()?.let { | ||
when { | ||
it.size > noteId -> it[noteId] | ||
else -> null | ||
} | ||
} | ||
) | ||
|
||
} | ||
} | ||
|
||
fun EntityLicenceCondition.toLicenceConditionNote(): List<LicenceConditionNote>? { | ||
|
||
return notes?.let { | ||
val splitParam = "---------------------------------------------------------" + System.lineSeparator() | ||
notes.split(splitParam).mapIndexed { index, note -> | ||
val matchResult = Regex( | ||
"^Comment added by (.+?) on (\\d{2}/\\d{2}/\\d{4}) at \\d{2}:\\d{2}" | ||
+ System.lineSeparator() | ||
).find(note) | ||
val commentLine = matchResult?.value | ||
val commentText = commentLine?.let { note.removePrefix(commentLine) } ?: note | ||
|
||
val userCreatedBy = matchResult?.groupValues?.get(1) | ||
val dateCreatedBy = matchResult?.groupValues?.get(2) | ||
?.let { LocalDate.parse(it, DeliusDateFormatter) } | ||
|
||
|
||
LicenceConditionNote( | ||
index, | ||
userCreatedBy, | ||
dateCreatedBy, | ||
commentText.removeSuffix(System.lineSeparator()).chunked(1500)[0], | ||
note.length > 1500 | ||
) | ||
} | ||
} | ||
} |
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