-
Notifications
You must be signed in to change notification settings - Fork 73
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
Normation-Quality-Assistant
merged 1 commit into
Normation:branches/rudder/3.1
from
fanf:arch_9201/add_basic_infrastructure_to_test_rest_api
Nov 22, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||
|
||
|
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,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' |
61 changes: 61 additions & 0 deletions
61
rudder-web/src/test/scala/com/normation/rudder/web/rest/RestStatusTest.scala
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,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"))) | ||
} | ||
} | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
rudder-web/src/test/scala/com/normation/rudder/web/rest/RestTestSetUp.scala
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,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") | ||
mockReq.method = "GET" | ||
mockReq.path = path | ||
mockReq | ||
} | ||
|
||
def testGET[T](path: String)(tests: Req => MatchResult[T]) = { | ||
doReq(GET(path))(tests) | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha ok !