From d5cbefa436d96d0615e06d47401b7183620cca24 Mon Sep 17 00:00:00 2001 From: hongwei Date: Thu, 23 Jun 2022 11:43:29 +0200 Subject: [PATCH] feature/OBPv500 added the headAtms --- .../scala/code/api/v4_0_0/APIMethods400.scala | 3 +- .../scala/code/api/v5_0_0/APIMethods500.scala | 47 +++++++++++- .../test/scala/code/api/v5_0_0/ATMTest.scala | 75 +++++++++++++++++++ .../api/v5_0_0/V500ServerSetupAsync.scala | 15 +++- .../scala/code/setup/SendServerRequests.scala | 10 +++ 5 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 obp-api/src/test/scala/code/api/v5_0_0/ATMTest.scala diff --git a/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala b/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala index f8ed37700..6d0d5bbbd 100644 --- a/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala +++ b/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala @@ -11471,8 +11471,7 @@ trait APIMethods400 { EmptyBody, atmsJsonV400, List( - $UserNotLoggedIn, - InvalidJsonFormat, + $BankNotFound, UnknownError ), List(apiTagATM, apiTagNewStyle) diff --git a/obp-api/src/main/scala/code/api/v5_0_0/APIMethods500.scala b/obp-api/src/main/scala/code/api/v5_0_0/APIMethods500.scala index 9bfd1ef54..269463dde 100644 --- a/obp-api/src/main/scala/code/api/v5_0_0/APIMethods500.scala +++ b/obp-api/src/main/scala/code/api/v5_0_0/APIMethods500.scala @@ -15,7 +15,8 @@ import com.openbankproject.commons.ExecutionContext.Implicits.global import com.openbankproject.commons.model.{BankId, UserAuthContextUpdateStatus} import com.openbankproject.commons.model.enums.StrongCustomerAuthentication import com.openbankproject.commons.util.ApiVersion -import net.liftweb.common.Full +import net.liftweb.common.{Full} +import net.liftweb.http.{Req} import net.liftweb.http.rest.RestHelper import scala.collection.immutable.{List, Nil} @@ -27,6 +28,20 @@ trait APIMethods500 { val Implementations5_0_0 = new Implementations500() + protected trait TestHead { + /** + * Test to see if the request is a GET and expecting JSON in the response. + * The path and the Req instance are extracted. + */ + def unapply(r: Req): Option[(List[String], Req)] = + if (r.requestType.head_? && testResponse_?(r)) + Some(r.path.partPath -> r) else None + + def testResponse_?(r: Req): Boolean + } + + lazy val JsonHead = new TestHead with JsonTest + class Implementations500 { val implementedInApiVersion = ApiVersion.v5_0_0 @@ -226,6 +241,36 @@ trait APIMethods500 { } + staticResourceDocs += ResourceDoc( + headAtms, + implementedInApiVersion, + nameOf(headAtms), + "HEAD", + "/banks/BANK_ID/atms", + "Head Bank ATMS", + s"""Head Bank ATMS.""", + EmptyBody, + atmsJsonV400, + List( + $BankNotFound, + UnknownError + ), + List(apiTagATM, apiTagNewStyle) + ) + lazy val headAtms : OBPEndpoint = { + case "banks" :: BankId(bankId) :: "atms" :: Nil JsonHead _ => { + cc => + for { + (_, callContext) <- getAtmsIsPublic match { + case false => authenticatedAccess(cc) + case true => anonymousAccess(cc) + } + } yield { + ("", HttpCode.`200`(callContext)) + } + } + } + } } diff --git a/obp-api/src/test/scala/code/api/v5_0_0/ATMTest.scala b/obp-api/src/test/scala/code/api/v5_0_0/ATMTest.scala new file mode 100644 index 000000000..2506d75e1 --- /dev/null +++ b/obp-api/src/test/scala/code/api/v5_0_0/ATMTest.scala @@ -0,0 +1,75 @@ +/** +Open Bank Project - API +Copyright (C) 2011-2019, TESOBE GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +Email: contact@tesobe.com +TESOBE GmbH +Osloerstrasse 16/17 +Berlin 13359, Germany + +This product includes software developed at +TESOBE (http://www.tesobe.com/) + */ +package code.api.v5_0_0 + +import code.api.ResourceDocs1_4_0.SwaggerDefinitionsJSON +import code.api.ResourceDocs1_4_0.SwaggerDefinitionsJSON.{postUserAuthContextJson, postUserAuthContextUpdateJsonV310} +import code.api.util.APIUtil.OAuth._ +import code.api.util.ApiRole._ +import code.api.util.ErrorMessages._ +import code.api.v3_1_0.CustomerJsonV310 +import code.api.v5_0_0.OBPAPI5_0_0.Implementations5_0_0 +import code.entitlement.Entitlement +import com.github.dwickern.macros.NameOf.nameOf +import com.openbankproject.commons.model.ErrorMessage +import com.openbankproject.commons.util.ApiVersion +import net.liftweb.json.Serialization.write +import org.scalatest.Tag + +import scala.language.postfixOps + +class ATMTest extends V500ServerSetupAsync { + + /** + * Test tags + * Example: To run tests with tag "getPermissions": + * mvn test -D tagsToInclude + * + * This is made possible by the scalatest maven plugin + */ + object VersionOfApi extends Tag(ApiVersion.v5_0_0.toString) + object ApiEndpoint1 extends Tag(nameOf(Implementations5_0_0.headAtms)) + + + feature("Head Bank ATMS v5.0.0") { + scenario("We will call the Add endpoint properly", ApiEndpoint1, VersionOfApi) { + When("We make a request v5.0.0") + lazy val bankId = randomBankId + val request500 = (v5_0_0_Request / "banks" / bankId / "atms").HEAD + val response500 = makeHeadRequest(request500) + Then("We should get a 200") + response500.code should equal(200) + } + scenario("We will call the Add endpoint with wrong BankId", ApiEndpoint1, VersionOfApi) { + When("We make a request v5.0.0") + val request500 = (v5_0_0_Request / "banks" / "xx_non_existing_bank_id" / "atms").HEAD + val response500 = makeHeadRequest(request500) + Then("We should get a 404") + response500.code should equal(404) + } + + } +} diff --git a/obp-api/src/test/scala/code/api/v5_0_0/V500ServerSetupAsync.scala b/obp-api/src/test/scala/code/api/v5_0_0/V500ServerSetupAsync.scala index 4fa849504..d2598753e 100644 --- a/obp-api/src/test/scala/code/api/v5_0_0/V500ServerSetupAsync.scala +++ b/obp-api/src/test/scala/code/api/v5_0_0/V500ServerSetupAsync.scala @@ -1,10 +1,23 @@ package code.api.v5_0_0 +import code.api.v4_0_0.BanksJson400 import code.setup._ import dispatch.Req +import scala.util.Random.nextInt + trait V500ServerSetupAsync extends ServerSetupWithTestDataAsync with DefaultUsers { def v5_0_0_Request: Req = baseRequest / "obp" / "v5.0.0" - + + def randomBankId : String = { + def getBanksInfo : APIResponse = { + val request = v5_0_0_Request / "banks" + makeGetRequest(request) + } + val banksJson = getBanksInfo.body.extract[BanksJson400] + val randomPosition = nextInt(banksJson.banks.size) + val bank = banksJson.banks(randomPosition) + bank.id + } } \ No newline at end of file diff --git a/obp-api/src/test/scala/code/setup/SendServerRequests.scala b/obp-api/src/test/scala/code/setup/SendServerRequests.scala index 965ed87d3..72ee32d56 100644 --- a/obp-api/src/test/scala/code/setup/SendServerRequests.scala +++ b/obp-api/src/test/scala/code/setup/SendServerRequests.scala @@ -258,6 +258,16 @@ trait SendServerRequests { val jsonReq = createRequest(reqData) getAPIResponse(jsonReq) } + + /** + * this method does a HEAD request given a URL + */ + def makeHeadRequest(req: Req, params: List[(String, String)] = Nil) : APIResponse = { + val extra_headers = Map.empty ++ params + val reqData = extractParamsAndHeaders(req.HEAD, "", "UTF-8", extra_headers) + val jsonReq = createRequest(reqData) + getAPIResponse(jsonReq) + } /** * this method does a GET request given a URL */