Refactor tests
This commit is contained in:
parent
5db690c434
commit
dfc0332d5a
8 changed files with 233 additions and 2 deletions
|
@ -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