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.
- Loading branch information
1 parent
7cba623
commit 4f942ac
Showing
7 changed files
with
192 additions
and
4 deletions.
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
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
51 changes: 51 additions & 0 deletions
51
...us/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserLocationIntegrationTest.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,51 @@ | ||
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.result.MockMvcResultHandlers.print | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.Address | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.LocationDetails | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.Name | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.UserOfficeLocation | ||
import uk.gov.justice.digital.hmpps.data.generator.OffenderManagerGenerator.DEFAULT_LOCATION | ||
import uk.gov.justice.digital.hmpps.data.generator.OffenderManagerGenerator.STAFF_USER_1 | ||
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson | ||
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken | ||
|
||
@AutoConfigureMockMvc | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class UserLocationIntegrationTest { | ||
@Autowired | ||
lateinit var mockMvc: MockMvc | ||
|
||
@Test | ||
fun `unauthorized status returned`() { | ||
mockMvc | ||
.perform(MockMvcRequestBuilders.get("/user/peter-parker/locations")) | ||
.andExpect(MockMvcResultMatchers.status().isUnauthorized) | ||
} | ||
|
||
@Test | ||
fun `get user location`() { | ||
val response = mockMvc.perform(MockMvcRequestBuilders.get("/user/peter-parker/locations").withToken()) | ||
.andDo(print()) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
.andReturn().response.contentAsJson<UserOfficeLocation>() | ||
|
||
val expected = UserOfficeLocation( | ||
Name(STAFF_USER_1.forename, surname = STAFF_USER_1.surname), | ||
listOf(LocationDetails( | ||
DEFAULT_LOCATION.id, | ||
DEFAULT_LOCATION.description, | ||
Address(DEFAULT_LOCATION.buildingNumber, DEFAULT_LOCATION.streetName, DEFAULT_LOCATION.townCity, DEFAULT_LOCATION.county, DEFAULT_LOCATION.postcode)) | ||
) | ||
) | ||
assertEquals(expected, response) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserLocationController.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,21 @@ | ||
package uk.gov.justice.digital.hmpps.api.controller | ||
|
||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import org.springframework.security.access.prepost.PreAuthorize | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import uk.gov.justice.digital.hmpps.service.UserLocationService | ||
|
||
@RestController | ||
@Tag(name = "Locations") | ||
@RequestMapping("/user/{username}") | ||
@PreAuthorize("hasRole('PROBATION_API__MANAGE_A_SUPERVISION__CASE_DETAIL')") | ||
class UserLocationController(private val userLocationService: UserLocationService) { | ||
|
||
@GetMapping("/locations") | ||
@Operation(summary = "Display user locations") | ||
fun getUserOfficeLocations(@PathVariable username: String) = userLocationService.getUserOfficeLocations(username) | ||
} |
25 changes: 25 additions & 0 deletions
25
...ius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/UserOfficeLocation.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,25 @@ | ||
package uk.gov.justice.digital.hmpps.api.model.sentence | ||
|
||
data class UserOfficeLocation( | ||
val name: Name, | ||
val locations: List<LocationDetails> | ||
) | ||
|
||
data class Name( | ||
val forename: String, | ||
val middleName: String? = null , | ||
val surname: String | ||
) | ||
data class LocationDetails( | ||
val id: Long, | ||
val description: String, | ||
val address: Address | ||
) | ||
|
||
data class Address( | ||
val buildingNumber: String? = null, | ||
val streetName: String? = null, | ||
val town: String? = null, | ||
val county: String? = null, | ||
val postcode: String? = null | ||
) |
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
28 changes: 28 additions & 0 deletions
28
...on-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserLocationService.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,28 @@ | ||
package uk.gov.justice.digital.hmpps.service | ||
|
||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.Address | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.LocationDetails | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.Name | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.UserOfficeLocation | ||
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Location | ||
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.StaffUserRepository | ||
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.getUser | ||
|
||
@Service | ||
class UserLocationService(private val staffUserRepository: StaffUserRepository) { | ||
|
||
fun getUserOfficeLocations(username: String): UserOfficeLocation { | ||
val user = staffUserRepository.getUser(username) | ||
|
||
val userLocations = staffUserRepository.findUserOfficeLocations(user.id) | ||
|
||
return UserOfficeLocation( | ||
Name(user.forename, user.forename2, user.surname), | ||
userLocations.map { it.toLocationDetails()} | ||
) | ||
} | ||
|
||
fun Location.toLocationDetails(): LocationDetails = | ||
LocationDetails(id, description, Address(buildingNumber, streetName, townCity, county, postcode)) | ||
} |