Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #9201: Add basic infrastructure to test REST API #1227

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions rudder-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ along with Rudder. If not, see <http://www.gnu.org/licenses/>.
<artifactId>commons-io</artifactId>
<version>${commons-io-version}</version>
</dependency>

<!-- Testing Liftweb -->
<dependency>
<groupId>net.liftweb</groupId>
<artifactId>lift-testkit_${scala-binary-version}</artifactId>
<version>${lift-version}</version>
<scope>test</scope>
</dependency>
<!-- YAML parser, for REST API test -->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.17</version>
<scope>test</scope>
</dependency>

<!-- Authentication -->
<!-- spring dep needed so that the correct version is resolved -->
Expand Down
21 changes: 21 additions & 0 deletions rudder-web/src/test/resources/api/api_v1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
description: Simple test of api/status - just "ok"
method: GET
url: /api/status
response:
type: text #optionnal, by default json
code: 200 #optionnal
content: 'OK'
---
description: reload technique library - just "ok"
method: GET
url: /api/techniqueLibrary/reload
response:
type: text #optionnal, by default json
code: 200
content: 'OK'
# ---
# ^- adding that would lead to a failed test, because
# it would lead to an empty spec


16 changes: 16 additions & 0 deletions rudder-web/src/test/resources/api/api_v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
description: Simple test of api/status - just "ok"
method: GET
url: /api/status
response:
type: text #optionnal, by default json
code: 200
content: 'OK'
# example of a bad spec, because type "plop" is not known
# ---
# method: GET
# url: /api/techniqueLibrary/reload
# response:
# type: plop
# code: 200
# content: 'OK'
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
*************************************************************************************
* Copyright 2016 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.

*
*************************************************************************************
*/

package com.normation.rudder.web.rest

import org.junit._
import org.junit.Assert._
import org.junit.runner._
import org.junit.runner.RunWith
import org.specs2.mutable._
import org.specs2.runner._

import RestTestSetUp._
import net.liftweb.common.Full
import net.liftweb.http.PlainTextResponse

@RunWith(classOf[JUnitRunner])
class RestStatusTest extends Specification {

"testing REST API for directive" should {
"be correct" in {
testGET("/api/status") { req =>
RestStatus(req)() must beEqualTo(Full(PlainTextResponse("OK")))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
*************************************************************************************
* Copyright 2016 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.

*
*************************************************************************************
*/

package com.normation.rudder.web.rest

import org.junit._
import org.junit.Assert._
import org.junit.runner._
import org.junit.runner.RunWith
import org.specs2.matcher.MatchResult
import org.specs2.mutable._
import org.specs2.runner._

import net.liftweb.common.Full
import net.liftweb.http.LiftRules
import net.liftweb.http.LiftRulesMocker
import net.liftweb.http.PlainTextResponse
import net.liftweb.http.Req
import net.liftweb.mocks.MockHttpServletRequest
import net.liftweb.mockweb.MockWeb
import com.normation.utils.StringUuidGeneratorImpl
import com.normation.cfclerk.services.UpdateTechniqueLibrary
import com.normation.eventlog.EventActor
import com.normation.eventlog.ModificationId
import com.normation.cfclerk.services.TechniquesLibraryUpdateNotification
import net.liftweb.common.Box
import com.normation.cfclerk.services.TechniquesLibraryUpdateType
import com.normation.cfclerk.domain.TechniqueName
import net.liftweb.http.LiftResponse
import net.liftweb.util.NamedPF
import net.liftweb.http.S
import net.liftweb.common.EmptyBox


/*
* This file provides all the necessary plumbing to allow test REST API.
*
* Also responsible for setting up datas and mock services.
*/
object RestTestSetUp {

val uuidGen = new StringUuidGeneratorImpl()
val reloadTechniques = new RestTechniqueReload(new UpdateTechniqueLibrary() {
def update(modId: ModificationId, actor:EventActor, reason: Option[String]) : Box[Map[TechniqueName, TechniquesLibraryUpdateType]] = {
Full(Map())
}
def registerCallback(callback:TechniquesLibraryUpdateNotification) : Unit = {}
}, uuidGen)

val liftRules = {
val l = new LiftRules()
l.statelessDispatch.append(RestStatus)
l.statelessDispatch.append(reloadTechniques)
//TODO: add all other rest classes here
l
}

/*
* Correctly build and scope mutable things to use the request in a safe
* way in the context of LiftRules.
*/
def doReq[T](mockReq: MockHttpServletRequest)(tests: Req => MatchResult[T]) = {
LiftRulesMocker.devTestLiftRulesInstance.doWith(liftRules) {
MockWeb.useLiftRules.doWith(true) {
MockWeb.testReq(mockReq)(tests)
}
}
}

/**
* Execute the request and get the response.
* The request must be a stateless one, else a failure
* will follow.
*/
def execRequestResponse[T](mockReq: MockHttpServletRequest)(tests: Box[LiftResponse] => MatchResult[T])= {
doReq(mockReq){ req =>
//the test logic is taken from LiftServlet#doServices.
//perhaps we should call directly that methods, but it need
//much more set-up, and I don't know for sure *how* to set-up things.
NamedPF.applyBox(req, LiftRules.statelessDispatch.toList).map(_.apply() match {
case Full(a) => Full(LiftRules.convertResponse((a, Nil, S.responseCookies, req)))
case r => r
}) match {
case Full(x) => tests(x)
case eb: EmptyBox => tests(eb)
}
}
}


def GET(path: String) = {
val mockReq = new MockHttpServletRequest("http://localhost:8080")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably nitpicking, but if jetty listen on another port, it will fail
we should probably use the defined port for jetty if available.
But that's probably for a future PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, the server/port is only used to build the fake Request/Response, not to actually send anything on that URL. It could be anything, I believe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha ok !

mockReq.method = "GET"
mockReq.path = path
mockReq
}

def testGET[T](path: String)(tests: Req => MatchResult[T]) = {
doReq(GET(path))(tests)
}
}

Loading