Skip to content

Commit

Permalink
test: serenity-rest implemented tests to check how recording of rest …
Browse files Browse the repository at this point in the history
…requests and reponses works
  • Loading branch information
YamStranger committed Apr 16, 2016
1 parent a4b5c70 commit d869ad2
Show file tree
Hide file tree
Showing 9 changed files with 1,032 additions and 37 deletions.
@@ -0,0 +1,134 @@
package net.serenitybdd.rest.staging

import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.junit.WireMockRule
import com.google.gson.FieldNamingPolicy
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import net.serenity.test.utils.rules.TestCase
import net.serenitybdd.core.rest.RestQuery
import net.thucydides.core.steps.BaseStepListener
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching
import static net.serenitybdd.core.rest.RestMethod.DELETE
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 net.serenitybdd.rest.staging.JsonConverter.*;


/**
* User: YamStranger
* Date: 11/29/15
* Time: 5:58 PM
*/
class WhenRecordDeleteRequestsWithSerenityRest extends Specification {

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

@Rule
def TestCase<BaseStepListener> test = new TestCase({
Mock(BaseStepListener);
}.call());

@Rule
TemporaryFolder temporaryFolder

def Gson gson = new GsonBuilder().setPrettyPrinting().
serializeNulls().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();

def "Should record RestAssured delete() method calls"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Number", "9999")
json.addProperty("Price", "100")
def body = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/number"
def url = "$base$path"

stubFor(WireMock.delete(urlEqualTo(path))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(body)));
when:
def result = delete(url).then()
then: "The JSON request should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "DELETE $url"
assert query.method == DELETE
assert "${query.path}" == url
assert query.statusCode == 200
}
and:
result.statusCode(200)
}

def "Should record RestAssured delete() method calls with parameters"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Exists", true)
json.addProperty("label", "UI")
def body = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/label"
def url = "$base$path"

stubFor(WireMock.delete(urlPathMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(body)));
when:
def result = delete("$url?status={status}", ["status": "available"]).then()
then: "The JSON request should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "DELETE $url?status=available"
assert query.method == DELETE
assert query.statusCode == 200
}
and:
result.statusCode(200)
}

def "Should record RestAssured delete() method calls with parameter provided as a list"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Weather", "rain")
json.addProperty("temperature", "+2")
def body = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/weather"
def url = "$base$path"

stubFor(WireMock.delete(urlPathMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(body)));
when:
def result = delete("$url?status={status}", "available").then()
then: "The JSON request should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "DELETE $url?status=available"
assert query.method == DELETE
assert query.statusCode == 200
}
and:
result.statusCode(200)
}
}
@@ -0,0 +1,137 @@
package net.serenitybdd.rest.staging

import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.junit.WireMockRule
import com.google.gson.FieldNamingPolicy
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import net.serenity.test.utils.rules.TestCase
import net.serenitybdd.core.rest.RestQuery
import net.thucydides.core.annotations.Step
import net.thucydides.core.model.TestResult
import net.thucydides.core.steps.BaseStepListener
import net.thucydides.core.steps.StepFactory
import org.hamcrest.Matchers
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching
import static net.serenitybdd.core.rest.RestMethod.GET
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 net.serenitybdd.rest.staging.JsonConverter.*;

/**
* User: YamStranger
* Date: 11/29/15
* Time: 5:58 PM
*/
class WhenRecordGetRequestsWithSerenityRest extends Specification {

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

@Rule
def TestCase<BaseStepListener> test = new TestCase({
Mock(BaseStepListener);
}.call());

@Rule
TemporaryFolder temporaryFolder

def Gson gson = new GsonBuilder().setPrettyPrinting().
serializeNulls().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();

def "Should record RestAssured get() method calls"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Number", "9999")
json.addProperty("Price", "100")
def body = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/number"
def url = "$base$path"

stubFor(WireMock.get(urlEqualTo(path))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(body)));
when:
def result = get(url).then()
then: "The JSON request should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "GET $url"
assert query.method == GET
assert "${query.path}" == url
assert query.statusCode == 200
}
and:
result.statusCode(200)
}

def "Should record RestAssured get() method calls with parameters"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Exists", true)
json.addProperty("label", "UI")
def body = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/label"
def url = "$base$path"

stubFor(WireMock.get(urlPathMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(body)));
when:
def result = get("$url?status={status}", ["status": "available"]).then()
then: "The JSON request should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "GET $url?status=available"
assert query.method == GET
assert query.statusCode == 200
}
and:
result.statusCode(200)
}

def "Should record RestAssured get() method calls with parameter provided as a list"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Weather", "rain")
json.addProperty("temperature", "+2")
def body = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/weather"
def url = "$base$path"

stubFor(WireMock.get(urlPathMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(body)));
when:
def result = get("$url?status={status}", "available").then()
then: "The JSON request should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "GET $url?status=available"
assert query.method == GET
assert query.statusCode == 200
}
and:
result.statusCode(200)
}
}

0 comments on commit d869ad2

Please sign in to comment.