Skip to content

Commit

Permalink
Repoint back to real search (#48)
Browse files Browse the repository at this point in the history
* restored content of /orders endpoint to be the real athena query; fake one is now /orders-old

* removed unused old endpoints, and renamed functions to appropriate names matching endpoints. Still some cleanup to do
  • Loading branch information
JazJax authored Dec 12, 2024
1 parent 9ebab25 commit 674fb47
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 232 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.justice.digital.hmpps.electronicmonitoringdatastoreapi.controllers

import net.minidev.json.JSONObject
import org.springframework.http.ResponseEntity
import org.springframework.security.access.AccessDeniedException
import org.springframework.security.access.prepost.PreAuthorize
Expand All @@ -20,35 +19,35 @@ import uk.gov.justice.digital.hmpps.electronicmonitoringdatastoreapi.repository.
@RequestMapping(value = ["/orders"], produces = ["application/json"])
class OrderController {

@GetMapping("/{orderID}")
fun getOrder(
@PathVariable(
required = true,
name = "orderID",
) orderId: String,
@RequestHeader(
required = false,
name = "User-Token",
) userToken: String = "no-token-supplied",
): JSONObject {
if (!checkValidUser(userToken)) {
return JSONObject(
mapOf("data" to "Unauthorised request with user token $userToken"),
)
}

if (orderId == "invalid-order") {
return JSONObject(
mapOf("data" to "No order with ID $orderId could be found"),
)
}

val response: JSONObject = JSONObject(
mapOf("data" to "This is the data for order $orderId"),
)

return response
}
// @GetMapping("/{orderID}")
// fun getOrder(
// @PathVariable(
// required = true,
// name = "orderID",
// ) orderId: String,
// @RequestHeader(
// required = false,
// name = "User-Token",
// ) userToken: String = "no-token-supplied",
// ): JSONObject {
// if (!checkValidUser(userToken)) {
// return JSONObject(
// mapOf("data" to "Unauthorised request with user token $userToken"),
// )
// }
//
// if (orderId == "invalid-order") {
// return JSONObject(
// mapOf("data" to "No order with ID $orderId could be found"),
// )
// }
//
// val response: JSONObject = JSONObject(
// mapOf("data" to "This is the data for order $orderId"),
// )
//
// return response
// }

@GetMapping("/getMockOrderSummary/{orderId}")
fun getMockOrderSummary(
Expand All @@ -62,7 +61,9 @@ class OrderController {

@GetMapping("/getOrderSummary/{orderId}")
fun getOrderSummary(
@PathVariable orderId: String,
@PathVariable(
required = true,
) orderId: String,
@RequestHeader(name = "X-User-Token", required = true) userToken: String,
): ResponseEntity<OrderInformation> {
if (!checkValidUser(userToken)) {
Expand All @@ -89,8 +90,8 @@ class OrderController {
}

fun checkValidUser(userToken: String): Boolean {
val validTokenValue: String = "real-token"
val invalidTokenValue: String = "invalid-token"

return userToken == validTokenValue
return userToken != invalidTokenValue
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package uk.gov.justice.digital.hmpps.electronicmonitoringdatastoreapi.controllers

import org.json.JSONObject
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
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.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestHeader
Expand All @@ -25,18 +23,18 @@ import uk.gov.justice.digital.hmpps.electronicmonitoringdatastoreapi.service.Ath
@RequestMapping(value = ["/search"], produces = ["application/json"])
class SearchController {

@GetMapping("/cases/{caseID}")
fun getCases(
@PathVariable(
required = true,
name = "caseID",
) caseId: String,
): JSONObject {
val response: JSONObject = JSONObject()
response.put("data", "You have successfully queried case $caseId")

return response
}
// @GetMapping("/cases/{caseID}")
// fun getCases(
// @PathVariable(
// required = true,
// name = "caseID",
// ) caseId: String,
// ): JSONObject {
// val response: JSONObject = JSONObject()
// response.put("data", "You have successfully queried case $caseId")
//
// return response
// }

@GetMapping("/testEndpoint")
fun confirmAthenaAccess(): ResponseEntity<ResultSet> {
Expand Down Expand Up @@ -99,14 +97,14 @@ class SearchController {
)
}

@PostMapping("/orders")
fun searchOrders(
@PostMapping("/orders-old")
fun searchOrdersFake(
@RequestHeader("X-User-Token", required = true) userToken: String,
@RequestBody searchCriteria: SearchCriteria,
): List<Order> = OrderRepository.getFakeOrders()

@PostMapping("/orders-temp")
fun searchOrdersTemp(
@PostMapping("/orders")
fun searchOrders(
@RequestHeader("X-User-Token", required = true) userToken: String,
@RequestBody searchCriteria: SearchCriteria,
): ResponseEntity<List<Order>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.gov.justice.digital.hmpps.electronicmonitoringdatastoreapi.controllers

import com.fasterxml.jackson.databind.ObjectMapper
import net.minidev.json.JSONObject
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
Expand All @@ -14,59 +13,59 @@ class OrderControllerTest {

private val objectMapper = ObjectMapper()

@Nested
inner class GetOrder {

private val sut: OrderController = OrderController()

@Test
fun `Returns data if correct params supplied`() {
val orderId = "obviously-real-id"
val userToken = "real-token"
val expected = JSONObject(
mapOf("data" to "This is the data for order $orderId"),
)

val result: JSONObject = sut.getOrder(orderId, userToken)
Assertions.assertThat(result).isEqualTo(expected)
}

@Test
fun `Returns 'Order not found' if incorrect orderId`() {
val orderId = "invalid-order"
val userToken = "real-token"
val expected = JSONObject(
mapOf("data" to "No order with ID $orderId could be found"),
)

val result: JSONObject = sut.getOrder(orderId, userToken)
Assertions.assertThat(result).isEqualTo(expected)
}

@Test
fun `returns "Unauthorised request" if userToken is invalid`() {
val orderId = "obviously-real-id"
val userToken = "nonsense-token"
val expected = JSONObject(
mapOf("data" to "Unauthorised request with user token $userToken"),
)

val result: JSONObject = sut.getOrder(orderId, userToken)
Assertions.assertThat(result).isEqualTo(expected)
}

@Test
fun `returns "Unauthorised request" if userToken is not supplied`() {
val orderId = "obviously-real-id"
val userToken = "no-token-supplied" // Default value used explicitly
val expected = JSONObject(
mapOf("data" to "Unauthorised request with user token $userToken"),
)

val result: JSONObject = sut.getOrder(orderId, userToken)
Assertions.assertThat(result).isEqualTo(expected)
}
}
// @Nested
// inner class GetOrder {
//
// private val sut: OrderController = OrderController()
//
// @Test
// fun `Returns data if correct params supplied`() {
// val orderId = "obviously-real-id"
// val userToken = "real-token"
// val expected = JSONObject(
// mapOf("data" to "This is the data for order $orderId"),
// )
//
// val result: JSONObject = sut.getOrder(orderId, userToken)
// Assertions.assertThat(result).isEqualTo(expected)
// }
//
// @Test
// fun `Returns 'Order not found' if incorrect orderId`() {
// val orderId = "invalid-order"
// val userToken = "real-token"
// val expected = JSONObject(
// mapOf("data" to "No order with ID $orderId could be found"),
// )
//
// val result: JSONObject = sut.getOrder(orderId, userToken)
// Assertions.assertThat(result).isEqualTo(expected)
// }
//
// @Test
// fun `returns "Unauthorised request" if userToken is invalid`() {
// val orderId = "obviously-real-id"
// val userToken = "invalid-token"
// val expected = JSONObject(
// mapOf("data" to "Unauthorised request with user token $userToken"),
// )
//
// val result: JSONObject = sut.getOrder(orderId, userToken)
// Assertions.assertThat(result).isEqualTo(expected)
// }
//
// @Test
// fun `returns "Unauthorised request" if userToken is not supplied`() {
// val orderId = "obviously-real-id"
// val userToken = "no-token-supplied" // Default value used explicitly
// val expected = JSONObject(
// mapOf("data" to "Unauthorised request with user token $userToken"),
// )
//
// val result: JSONObject = sut.getOrder(orderId, userToken)
// Assertions.assertThat(result).isEqualTo(expected)
// }
// }

@Nested
inner class GetOrderSummary {
Expand Down Expand Up @@ -103,7 +102,7 @@ class OrderControllerTest {
@Test
fun `Throws AccessDeniedException if userToken is invalid`() {
val orderId = "obviously-real-id"
val userToken = "fake-token"
val userToken = "invalid-token"

Assertions.assertThatExceptionOfType(AccessDeniedException::class.java)
.isThrownBy { sut.getOrderSummary(orderId, userToken) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package uk.gov.justice.digital.hmpps.electronicmonitoringdatastoreapi.controllers

import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.json.JSONObject
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import uk.gov.justice.digital.hmpps.electronicmonitoringdatastoreapi.model.AthenaQuery
Expand All @@ -14,23 +12,23 @@ class SearchControllerTest {

private val sut: SearchController = SearchController()

@Nested
inner class GetSearchResult {

@Test
fun `Accepts caseId as parameter`() {
val caseId = "obviously-fake-data"
val expected = JSONObject(
mapOf("data" to "You have successfully queried case obviously-fake-data"),
)

val result: JSONObject = sut.getCases(caseId)
Assertions.assertThat(result.toString()).isEqualTo(expected.toString())
}
}
// @Nested
// inner class GetSearchResult {
//
// @Test
// fun `Accepts caseId as parameter`() {
// val caseId = "obviously-fake-data"
// val expected = JSONObject(
// mapOf("data" to "You have successfully queried case obviously-fake-data"),
// )
//
// val result: JSONObject = sut.getCases(caseId)
// Assertions.assertThat(result.toString()).isEqualTo(expected.toString())
// }
// }

@Nested
inner class SearchOrders {
inner class SearchOrdersFake {

@Test
fun `Returns a list of orders`() {
Expand All @@ -47,7 +45,7 @@ class SearchControllerTest {
dobYear = "1970",
)

val result: List<Order> = sut.searchOrders(userToken, searchCriteria)
val result: List<Order> = sut.searchOrdersFake(userToken, searchCriteria)

// Assert: Verify the result is a list of Order objects
assertThat(result).isNotEmpty // Ensure the result is not empty
Expand Down
Loading

0 comments on commit 674fb47

Please sign in to comment.