Skip to content

Commit

Permalink
test: added tests for PUT operations
Browse files Browse the repository at this point in the history
  • Loading branch information
YamStranger committed Mar 30, 2016
1 parent 6ed97f3 commit f1e2cde
Show file tree
Hide file tree
Showing 3 changed files with 356 additions and 0 deletions.
@@ -0,0 +1,192 @@
package net.serenitybdd.rest.staging

import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.junit.WireMockRule
import com.jayway.restassured.specification.RequestSender
import net.serenitybdd.rest.staging.decorators.ResponseDecorated
import net.serenitybdd.rest.staging.decorators.ResponseSpecificationDecorated
import net.serenitybdd.rest.staging.decorators.request.RequestSpecificationDecorated
import net.serenitybdd.rest.staging.rules.RestConfigurationAction
import net.serenitybdd.rest.staging.rules.RestConfigurationRule
import org.junit.Rule
import spock.lang.Specification

import static net.serenitybdd.rest.staging.SerenityRest.*
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
import static com.github.tomakehurst.wiremock.client.WireMock.matching
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching

/**
* User: YamStranger
* Date: 3/14/16
* Time: 9:57 AM
*/
class WhenExecutingPutRequest extends Specification {

@Rule
def WireMockRule wire = new WireMockRule(0);

@Rule
def RestConfigurationRule rule = new RestConfigurationRule(new RestConfigurationAction() {
@Override
void apply() {
reset()
}
},)

def "should use wrapped request and response if they initialised separately"() {
given: "initialised Request and Response and access point"
def request = Mock(RequestSpecificationDecorated)
def response = Mock(ResponseSpecificationDecorated)
def body = "<root>" +
"<value>1</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/levels"
def url = "$base$path"
stubFor(WireMock.put(urlMatching(path))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating new request and making request"
def RequestSender sender = given(request, response)
def generated = sender.put(url)
then: "created response should be decorated"
1 * request.put((String) _, (Object) _)
}

def "should return wrapped response during PUT by URL called from request"() {
given: "configured access point"
def body = "<root>" +
"<value>2</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/keyboard"
def url = "$base$path"
stubFor(WireMock.put(urlMatching(path))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(256)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating new request and making put request"
def response = given().put(url)
then: "created response should be decorated"
response instanceof ResponseDecorated
and: "returned status should be correct"
response.then().statusCode(256)
}

def "should return wrapped response during PUT by URL with MAP parameters called from request"() {
given: "configured access point"
def body = "<root>" +
"<value>3</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/house"
def url = "$base$path"
stubFor(WireMock.put(urlMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(700)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating new request and making put request"
def response = given().put("$url?status={status}", ["status": "available"])
then: "created response should be decorated"
response instanceof ResponseDecorated
and: "returned status should be correct"
response.then().statusCode(700)
}

def "should return wrapped response during PUT by URL with array parameters called from request"() {
given: "configured access point"
def body = "<root>" +
"<value>4</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/pet"
def url = "$base$path"
stubFor(WireMock.put(urlMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(845)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating new request and making put request"
def response = given().put("$url?status={status}", "available")
then: "created response should be decorated"
response instanceof ResponseDecorated
and: "returned status should be correct"
response.then().statusCode(845)
}

def "should return wrapped response during PUT by URL called from response"() {
given: "configured access point"
def body = "<root>" +
"<value>5</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/child"
def url = "$base$path"
stubFor(WireMock.put(urlMatching(path))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(945)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating new request and making put request"
def response = expect().put(url)
then: "created response should be decorated"
response instanceof ResponseDecorated
and: "returned status should be correct"
response.then().statusCode(945)
}

def "should return wrapped response during PUT by URL with MAP parameters called from response"() {
given: "configured access point"
def body = "<root>" +
"<value>6</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/book"
def url = "$base$path"
stubFor(WireMock.put(urlMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(203)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating new request and making put request"
def response = expect().put("$url?status={status}", ["status": "available"])
then: "created response should be decorated"
response instanceof ResponseDecorated
and: "returned status should be correct"
response.then().statusCode(203)
}

def "should return wrapped response during PUT by URL with array parameters called from response"() {
given: "configured access point"
def body = "<root>" +
"<value>7</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/creature"
def url = "$base$path"
stubFor(WireMock.put(urlMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(506)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating new request and making put request"
def response = expect().put("$url?status={status}", "available")
then: "created response should be decorated"
response instanceof ResponseDecorated
and: "returned status should be correct"
response.then().statusCode(506)
}
}
@@ -0,0 +1,85 @@
package net.serenitybdd.rest.staging

import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.junit.WireMockRule
import net.serenitybdd.rest.staging.decorators.ResponseDecorated
import net.serenitybdd.rest.staging.rules.RestConfigurationAction
import net.serenitybdd.rest.staging.rules.RestConfigurationRule
import org.hamcrest.Matchers
import org.junit.Rule
import spock.lang.Specification

import static net.serenitybdd.rest.staging.SerenityRest.*
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
import static com.github.tomakehurst.wiremock.client.WireMock.matching
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching

/**
* User: YamStranger
* Date: 3/14/16
* Time: 9:57 AM
*/
class WhenExecutingPutRequestFromExpectation extends Specification {

@Rule
def WireMockRule wire = new WireMockRule(0);

@Rule
def RestConfigurationRule rule = new RestConfigurationRule(new RestConfigurationAction() {
@Override
void apply() {
reset()
}
},)

def "should return wrapped response during PUT by URL called from expectation"() {
given: "configured access point"
def body = "<root>" +
"<value>7</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/creature"
def url = "$base$path"
stubFor(WireMock.put(urlMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(506)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating expectation"
def expectation = expect().
statusCode(506).
body(Matchers.equalTo(body))
and: "executing expectation"
def response = expectation.when().put(url);
then: "created response should be decorated"
response instanceof ResponseDecorated
}

def "should return wrapped response during PUT by URL called from expectation with parameters"() {
given: "configured access point"
def body = "<root>" +
"<value>7</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/creature"
def url = "$base$path"
stubFor(WireMock.put(urlMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(506)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "executing expectation"
def response = given().
param("x", "y").
expect().
statusCode(506).
body(Matchers.equalTo(body)).
when().
put(url);
then: "created response should be decorated"
response instanceof ResponseDecorated
}
}
@@ -0,0 +1,79 @@
package net.serenitybdd.rest.staging

import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.junit.WireMockRule
import net.serenitybdd.rest.staging.decorators.ResponseDecorated
import net.serenitybdd.rest.staging.rules.RestConfigurationAction
import net.serenitybdd.rest.staging.rules.RestConfigurationRule
import org.hamcrest.Matchers
import org.junit.Rule
import spock.lang.Specification

import static net.serenitybdd.rest.staging.SerenityRest.*
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
import static com.github.tomakehurst.wiremock.client.WireMock.matching
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching

/**
* User: YamStranger
* Date: 3/14/16
* Time: 9:57 AM
*/
class WhenValidatingResponseFromPutOperation extends Specification {

@Rule
def WireMockRule wire = new WireMockRule(0);

@Rule
def RestConfigurationRule rule = new RestConfigurationRule(new RestConfigurationAction() {
@Override
void apply() {
reset()
}
},)

def "should be possible to validate status code"() {
given: "configured access point"
def body = "<root>" +
"<value>7</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/creature"
def url = "$base$path"
stubFor(WireMock.put(urlMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(506)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating new request and making put request"
def response = given().put(url)
then: "created response should be decorated"
response instanceof ResponseDecorated
and: "returned status should be correct"
response.then().statusCode(506)
}

def "should be possible to validate response body"() {
given: "configured access point"
def body = "<root>" +
"<value>7</value>" +
"</root>"
def base = "http://localhost:${wire.port()}"
def path = "/test/creature"
def url = "$base$path"
stubFor(WireMock.put(urlMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(856)
.withHeader("Content-Type", "application/xml")
.withBody(body)));
when: "creating expectation"
def expectation = expect().
statusCode(856).
body(Matchers.equalTo(body))
then: "validation of expectation should be correct"
expectation.when().put(url);
}
}

0 comments on commit f1e2cde

Please sign in to comment.