Refactor tests
This commit is contained in:
parent
5db690c434
commit
dfc0332d5a
8 changed files with 233 additions and 2 deletions
14
pom.xml
14
pom.xml
|
@ -16,6 +16,8 @@
|
||||||
<groovy-eclipse-batch.version>2.1.8-01</groovy-eclipse-batch.version>
|
<groovy-eclipse-batch.version>2.1.8-01</groovy-eclipse-batch.version>
|
||||||
<groovy.version>2.3.3</groovy.version>
|
<groovy.version>2.3.3</groovy.version>
|
||||||
<httpclient.version>4.3.5</httpclient.version>
|
<httpclient.version>4.3.5</httpclient.version>
|
||||||
|
<spock-core.version>1.0-groovy-2.0-SNAPSHOT</spock-core.version>
|
||||||
|
<commons-lang3.version>3.3.2</commons-lang3.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,6 +31,18 @@
|
||||||
<artifactId>httpclient</artifactId>
|
<artifactId>httpclient</artifactId>
|
||||||
<version>${httpclient.version}</version>
|
<version>${httpclient.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.spockframework</groupId>
|
||||||
|
<artifactId>spock-core</artifactId>
|
||||||
|
<version>${spock-core.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.util.concurrent.CopyOnWriteArrayList
|
||||||
|
|
||||||
class ContextExecutor {
|
class ContextExecutor {
|
||||||
private final HttpServerWraper httpServerWraper
|
private final HttpServerWraper httpServerWraper
|
||||||
private final String path
|
final String path
|
||||||
private final List<Mock> mocks
|
private final List<Mock> mocks
|
||||||
|
|
||||||
ContextExecutor(HttpServerWraper httpServerWraper, String path, Mock initialMock) {
|
ContextExecutor(HttpServerWraper httpServerWraper, String path, Mock initialMock) {
|
||||||
|
@ -59,6 +59,10 @@ class ContextExecutor {
|
||||||
return mock.counter
|
return mock.counter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addMock(Mock mock){
|
||||||
|
mocks << mock
|
||||||
|
}
|
||||||
|
|
||||||
private static String wrapSoap(String request) {
|
private static String wrapSoap(String request) {
|
||||||
"""<?xml version='1.0' encoding='UTF-8'?>
|
"""<?xml version='1.0' encoding='UTF-8'?>
|
||||||
<soap-env:Envelope xmlns:soap-env='http://schemas.xmlsoap.org/soap/envelope/' xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
|
<soap-env:Envelope xmlns:soap-env='http://schemas.xmlsoap.org/soap/envelope/' xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
|
||||||
|
|
|
@ -28,7 +28,7 @@ class HttpServerWraper {
|
||||||
void addMock(String path, Mock mock) {
|
void addMock(String path, Mock mock) {
|
||||||
ContextExecutor executor = executors.find { it.path == path }
|
ContextExecutor executor = executors.find { it.path == path }
|
||||||
if (executor) {
|
if (executor) {
|
||||||
executor.mocks << mock
|
executor.addMock(mock)
|
||||||
} else {
|
} else {
|
||||||
executors << new ContextExecutor(this, path, mock)
|
executors << new ContextExecutor(this, path, mock)
|
||||||
}
|
}
|
||||||
|
|
105
src/test/groovy/MockServerTest.groovy
Normal file
105
src/test/groovy/MockServerTest.groovy
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
import com.blogspot.przybyszd.mockserver.HttpMockServer
|
||||||
|
import groovy.util.slurpersupport.GPathResult
|
||||||
|
import org.apache.http.HttpEntity
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse
|
||||||
|
import org.apache.http.client.methods.HttpPost
|
||||||
|
import org.apache.http.entity.ContentType
|
||||||
|
import org.apache.http.entity.StringEntity
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient
|
||||||
|
import org.apache.http.impl.client.HttpClients
|
||||||
|
import org.apache.http.util.EntityUtils
|
||||||
|
import pl.touk.mockserver.client.AddMockRequestData
|
||||||
|
import pl.touk.mockserver.client.ControlServerClient
|
||||||
|
import pl.touk.mockserver.client.Util
|
||||||
|
import spock.lang.Shared
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class MockServerTest extends Specification{
|
||||||
|
|
||||||
|
private static final String controlServerUrl = 'http://localhost:9000/serverControl'
|
||||||
|
|
||||||
|
ControlServerClient controlServerClient
|
||||||
|
|
||||||
|
HttpMockServer httpMockServer
|
||||||
|
|
||||||
|
@Shared
|
||||||
|
CloseableHttpClient client = HttpClients.createDefault()
|
||||||
|
|
||||||
|
def setup(){
|
||||||
|
httpMockServer = new HttpMockServer(9000)
|
||||||
|
controlServerClient = new ControlServerClient('localhost', 9000)
|
||||||
|
}
|
||||||
|
|
||||||
|
def cleanup(){
|
||||||
|
httpMockServer.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should add working rest mock on endpoint"(){
|
||||||
|
expect:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
predicate: '''{xml -> xml.name() == 'request'}''',
|
||||||
|
response: '''{xml -> "<goodResponseRest-${xml.name()}/>"}''',
|
||||||
|
soap: false
|
||||||
|
))
|
||||||
|
when:
|
||||||
|
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
|
||||||
|
restPost.entity = new StringEntity('<request/>',ContentType.create("text/xml", "UTF-8"))
|
||||||
|
CloseableHttpResponse response = client.execute(restPost)
|
||||||
|
then:
|
||||||
|
GPathResult restPostResponse = Util.extractXmlResponse(response)
|
||||||
|
restPostResponse.name() == 'goodResponseRest-request'
|
||||||
|
expect:
|
||||||
|
controlServerClient.removeMock('testRest') == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should add soap mock on endpoint"(){
|
||||||
|
expect:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testSoap',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
predicate: '''{xml -> xml.name() == 'request'}''',
|
||||||
|
response: '''{xml -> "<goodResponseSoap-${xml.name()}/>"}''',
|
||||||
|
soap: true
|
||||||
|
))
|
||||||
|
when:
|
||||||
|
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
|
||||||
|
restPost.entity = new StringEntity(Util.soap('<request/>'),ContentType.create("text/xml", "UTF-8"))
|
||||||
|
CloseableHttpResponse response = client.execute(restPost)
|
||||||
|
then:
|
||||||
|
GPathResult restPostResponse = Util.extractXmlResponse(response)
|
||||||
|
restPostResponse.name() == 'Envelope'
|
||||||
|
restPostResponse.Body.'goodResponseSoap-request'.size() == 1
|
||||||
|
expect:
|
||||||
|
controlServerClient.removeMock('testSoap') == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO def "should add simultanously working post and rest mocks with the same predicate and endpoint nad port"(){}"
|
||||||
|
//TODO def "should add mock minimal(){}"
|
||||||
|
//TODO def "should dispatch mocks with the same predicates on another ports"
|
||||||
|
//TODO def "should not add mock with existing name"(){}
|
||||||
|
//TODO def "should not remove the same mock two times"(){}
|
||||||
|
//TODO def "should add mock after deleteing old mock with the same name"(){}
|
||||||
|
//TODO def "should dispatch rest mocks with the another predicates"(){}
|
||||||
|
//TODO def "should dispatch soap mocks with the another predicates"(){}
|
||||||
|
//TODO def "should dispatch rest and soap mocks with the same predicates"(){}
|
||||||
|
//TODO def "should get mock report"(){}
|
||||||
|
//TODO def "should get list mocks"(){}
|
||||||
|
//TODO def "should dispatch rest mock with response code"(){}
|
||||||
|
//TODO def "should dispatch rest mock with post method"(){}
|
||||||
|
//TODO def "should dispatch rest mock with post method and request headers"(){}
|
||||||
|
//TODO def "should dispatch rest mock with post method and response headers"(){}
|
||||||
|
//TODO def "should dispatch rest mock with get method"(){}
|
||||||
|
//TODO def "should dispatch rest mock with get method and request headers"(){}
|
||||||
|
//TODO def "should dispatch rest mock with get method and response headers"(){}
|
||||||
|
//TODO def "should dispatch rest mock with put method"(){}
|
||||||
|
//TODO def "should dispatch rest mock with put method and request headers"(){}
|
||||||
|
//TODO def "should dispatch rest mock with put method and response headers"(){}
|
||||||
|
//TODO def "should dispatch rest mock with delete method"(){}
|
||||||
|
//TODO def "should dispatch rest mock with delete method and request headers"(){}
|
||||||
|
//TODO def "should dispatch rest mock with delete method and response headers"(){}
|
||||||
|
//TODO def "should dispatch rest mocks with all methods"(){}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package pl.touk.mockserver.client
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringEscapeUtils
|
||||||
|
|
||||||
|
class AddMockRequestData {
|
||||||
|
String name
|
||||||
|
String path
|
||||||
|
Integer port
|
||||||
|
String predicate
|
||||||
|
String response
|
||||||
|
Boolean soap
|
||||||
|
|
||||||
|
void setPredicate(String predicate){
|
||||||
|
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
||||||
|
}
|
||||||
|
|
||||||
|
void setResponse(String response){
|
||||||
|
this.response = StringEscapeUtils.escapeXml11(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package pl.touk.mockserver.client
|
||||||
|
|
||||||
|
import groovy.util.slurpersupport.GPathResult
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse
|
||||||
|
import org.apache.http.client.methods.HttpPost
|
||||||
|
import org.apache.http.entity.ContentType
|
||||||
|
import org.apache.http.entity.StringEntity
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient
|
||||||
|
import org.apache.http.impl.client.HttpClients
|
||||||
|
|
||||||
|
class ControlServerClient {
|
||||||
|
private final String address
|
||||||
|
private final CloseableHttpClient client = HttpClients.createDefault()
|
||||||
|
|
||||||
|
ControlServerClient(String host, int port) {
|
||||||
|
address = "http://$host:$port/serverControl"
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean addMock(AddMockRequestData addMockRequestData){
|
||||||
|
HttpPost addMockPost = new HttpPost(address)
|
||||||
|
addMockPost.entity = buildAddMockRequest(addMockRequestData)
|
||||||
|
CloseableHttpResponse response = client.execute(addMockPost)
|
||||||
|
GPathResult responseXml = Util.extractXmlResponse(response)
|
||||||
|
return responseXml.name() == 'mockAdded'
|
||||||
|
}
|
||||||
|
|
||||||
|
int removeMock(String name){
|
||||||
|
HttpPost removeMockPost = new HttpPost(address)
|
||||||
|
removeMockPost.entity = buildRemoveMockRequest(new RemoveMockRequestData(name:name))
|
||||||
|
CloseableHttpResponse response = client.execute(removeMockPost)
|
||||||
|
GPathResult responseXml = Util.extractXmlResponse(response)
|
||||||
|
return responseXml.name() == 'mockRemoved' ? responseXml.text() as int:-1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private StringEntity buildRemoveMockRequest(RemoveMockRequestData data){
|
||||||
|
return new StringEntity("""\
|
||||||
|
<removeMock>
|
||||||
|
<name>${data.name}</name>
|
||||||
|
</removeMock>
|
||||||
|
""",ContentType.create("text/xml", "UTF-8"))
|
||||||
|
}
|
||||||
|
|
||||||
|
private StringEntity buildAddMockRequest(AddMockRequestData data){
|
||||||
|
return new StringEntity("""\
|
||||||
|
<addMock>
|
||||||
|
<name>${data.name}</name>
|
||||||
|
<path>${data.path}</path>
|
||||||
|
<port>${data.port}</port>
|
||||||
|
<predicate>${data.predicate}</predicate>
|
||||||
|
<response>${data.response}</response>
|
||||||
|
<soap>${data.soap}</soap>
|
||||||
|
</addMock>
|
||||||
|
""",ContentType.create("text/xml", "UTF-8"))
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package pl.touk.mockserver.client
|
||||||
|
|
||||||
|
class RemoveMockRequestData {
|
||||||
|
String name
|
||||||
|
}
|
25
src/test/groovy/pl/touk/mockserver/client/Util.groovy
Normal file
25
src/test/groovy/pl/touk/mockserver/client/Util.groovy
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package pl.touk.mockserver.client
|
||||||
|
|
||||||
|
import groovy.transform.PackageScope
|
||||||
|
import groovy.util.slurpersupport.GPathResult
|
||||||
|
import org.apache.http.HttpEntity
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse
|
||||||
|
import org.apache.http.util.EntityUtils
|
||||||
|
|
||||||
|
class Util {
|
||||||
|
@PackageScope
|
||||||
|
static GPathResult extractXmlResponse(CloseableHttpResponse response){
|
||||||
|
HttpEntity entity = response.entity
|
||||||
|
GPathResult xml = new XmlSlurper().parseText(EntityUtils.toString(entity))
|
||||||
|
EntityUtils.consumeQuietly(entity)
|
||||||
|
return xml
|
||||||
|
}
|
||||||
|
|
||||||
|
static String soap(String request) {
|
||||||
|
return """<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<soap-env:Envelope xmlns:soap-env='http://schemas.xmlsoap.org/soap/envelope/' xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
|
||||||
|
<soap-env:Body>$request</soap-env:Body>
|
||||||
|
</soap-env:Envelope>"""
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue