refactor/Centralise the default value of offset and limit

This commit is contained in:
Marko Milić 2022-10-06 10:50:21 +02:00
parent 618879bf52
commit cdf5e1447b
4 changed files with 21 additions and 14 deletions

View File

@ -9,6 +9,11 @@ import com.openbankproject.commons.util.ApiStandards
object Constant extends MdcLoggable {
logger.info("Instantiating Constants")
object Pagination {
final val offset = 0
final val limit = 500
}
final val h2DatabaseDefaultUrlValue = "jdbc:h2:mem:OBPTest_H2_v2.1.214;NON_KEYWORDS=VALUE;DB_CLOSE_DELAY=10"
final val HostName = APIUtil.getPropsValue("hostname").openOrThrowException(ErrorMessages.HostnameNotSpecified)

View File

@ -899,7 +899,7 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{
}
def getOffset(httpParams: List[HTTPParam]): Box[OBPOffset] = {
(getPaginationParam(httpParams, "offset", None, 0, FilterOffersetError), getPaginationParam(httpParams, "obp_offset", Some(0), 0, FilterOffersetError)) match {
(getPaginationParam(httpParams, "offset", None, 0, FilterOffersetError), getPaginationParam(httpParams, "obp_offset", Some(Constant.Pagination.offset), 0, FilterOffersetError)) match {
case (Full(left), _) =>
Full(OBPOffset(left))
case (Failure(m, e, c), _) =>
@ -908,12 +908,12 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{
Full(OBPOffset(right))
case (_, Failure(m, e, c)) =>
Failure(m, e, c)
case _ => Full(OBPOffset(0))
case _ => Full(OBPOffset(Constant.Pagination.offset))
}
}
def getLimit(httpParams: List[HTTPParam]): Box[OBPLimit] = {
(getPaginationParam(httpParams, "limit", None, 1, FilterLimitError), getPaginationParam(httpParams, "obp_limit", Some(500), 1, FilterLimitError)) match {
(getPaginationParam(httpParams, "limit", None, 1, FilterLimitError), getPaginationParam(httpParams, "obp_limit", Some(Constant.Pagination.limit), 1, FilterLimitError)) match {
case (Full(left), _) =>
Full(OBPLimit(left))
case (Failure(m, e, c), _) =>
@ -922,7 +922,7 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{
Full(OBPLimit(right))
case (_, Failure(m, e, c)) =>
Failure(m, e, c)
case _ => Full(OBPLimit(500))
case _ => Full(OBPLimit(Constant.Pagination.limit))
}
}

View File

@ -942,8 +942,8 @@ object LocalMappedConnector extends Connector with MdcLoggable {
override def getBankAccountsWithAttributes(bankId: BankId, queryParams: List[OBPQueryParam], callContext: Option[CallContext]): OBPReturnType[Box[List[FastFirehoseAccount]]] =
Future{
val limit: Int = queryParams.collect { case OBPLimit(value) => value }.headOption.getOrElse(500)
val offset = queryParams.collect { case OBPOffset(value) => value }.headOption.getOrElse(0)
val limit: Int = queryParams.collect { case OBPLimit(value) => value }.headOption.getOrElse(Constant.Pagination.limit)
val offset = queryParams.collect { case OBPOffset(value) => value }.headOption.getOrElse(Constant.Pagination.offset)
val orderBy = queryParams.collect {
case OBPOrdering(_, OBPDescending) => "DESC"
}.headOption.getOrElse("ASC")

View File

@ -30,7 +30,9 @@ package code.util
import java.time.format.DateTimeFormatter
import java.time.{ZoneId, ZonedDateTime}
import java.util.Date
import code.api.util.APIUtil.{DateWithMsFormat, theEpochTime, DefaultToDate, _}
import code.api.Constant
import code.api.util.APIUtil.{DateWithMsFormat, DefaultToDate, theEpochTime, _}
import code.api.util.ErrorMessages._
import code.api.util._
import code.setup.PropsReset
@ -374,14 +376,14 @@ class APIUtilTest extends FeatureSpec with Matchers with GivenWhenThen with Prop
{
val httpParams: List[HTTPParam] = List(HTTPParam("wrongName", List("100")))
val returnValue = getLimit(httpParams)
returnValue should be (OBPLimit(500))
returnValue should be (OBPLimit(Constant.Pagination.limit))
}
scenario(s"test the wrong case: wrong name (wrongName) and wrong values (wrongValue) in HTTPParam")
{
val httpParams: List[HTTPParam] = List(HTTPParam("wrongName", List("wrongValue")))
val returnValue = getLimit(httpParams)
returnValue should be (OBPLimit(500))
returnValue should be (OBPLimit(Constant.Pagination.limit))
}
}
@ -436,7 +438,7 @@ class APIUtilTest extends FeatureSpec with Matchers with GivenWhenThen with Prop
feature("test APIUtil.getHttpParams method")
{
val RetrunDefaultParams = Full(List(OBPLimit(500),OBPOffset(0),OBPOrdering(None,OBPDescending), OBPFromDate(startDateObject),OBPToDate(endDateObject)))
val RetrunDefaultParams = Full(List(OBPLimit(Constant.Pagination.limit),OBPOffset(0),OBPOrdering(None,OBPDescending), OBPFromDate(startDateObject),OBPToDate(endDateObject)))
scenario(s"test the correct case1: with default parameters")
{
@ -453,7 +455,7 @@ class APIUtilTest extends FeatureSpec with Matchers with GivenWhenThen with Prop
scenario(s"test the correct case2: contains the `anon` ")
{
val ExpectResult =
Full(List(OBPLimit(500),OBPOffset(0),OBPOrdering(None,OBPDescending)
Full(List(OBPLimit(Constant.Pagination.limit),OBPOffset(Constant.Pagination.offset),OBPOrdering(None,OBPDescending)
,OBPFromDate(startDateObject),OBPToDate(endDateObject),
OBPAnon(true)))
val httpParams: List[HTTPParam] = List(
@ -468,7 +470,7 @@ class APIUtilTest extends FeatureSpec with Matchers with GivenWhenThen with Prop
scenario(s"test the correct case3: contains the `anon` and `consumer_id` ")
{
val ExpectResult =
Full(List(OBPLimit(500),OBPOffset(0),OBPOrdering(None,OBPDescending),
Full(List(OBPLimit(Constant.Pagination.limit),OBPOffset(Constant.Pagination.offset),OBPOrdering(None,OBPDescending),
OBPFromDate(startDateObject),OBPToDate(endDateObject),
OBPAnon(true),OBPConsumerId("1")))
val httpParams: List[HTTPParam] = List(
@ -484,7 +486,7 @@ class APIUtilTest extends FeatureSpec with Matchers with GivenWhenThen with Prop
scenario(s"test the correct case4: contains all the fields")
{
val ExpectResult =
Full(List(OBPLimit(500), OBPOffset(0), OBPOrdering(None,OBPDescending),
Full(List(OBPLimit(Constant.Pagination.limit), OBPOffset(Constant.Pagination.offset), OBPOrdering(None,OBPDescending),
OBPFromDate(startDateObject), OBPToDate(endDateObject),
OBPAnon(true), OBPConsumerId("1"), OBPUserId("2"), OBPUrl("obp/v1.2.1/getBanks"),
OBPAppName("PlaneApp"), OBPImplementedByPartialFunction("getBanks"),
@ -573,7 +575,7 @@ class APIUtilTest extends FeatureSpec with Matchers with GivenWhenThen with Prop
feature("test APIUtil.createHttpParamsByUrl method")
{
val RetrunDefaultParams = Full(List(OBPLimit(500),OBPOffset(0),OBPOrdering(None,OBPDescending), OBPFromDate(startDateObject),OBPToDate(endDateObject)))
val RetrunDefaultParams = Full(List(OBPLimit(Constant.Pagination.limit),OBPOffset(Constant.Pagination.offset),OBPOrdering(None,OBPDescending), OBPFromDate(startDateObject),OBPToDate(endDateObject)))
scenario(s"test the correct case1: all the params are in the `URL` ")
{