Split project to modules
This commit is contained in:
parent
1bbaf72084
commit
73b3630e2f
26 changed files with 145 additions and 34 deletions
28
mockserver-client/pom.xml
Normal file
28
mockserver-client/pom.xml
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>http-mock-server</artifactId>
|
||||
<groupId>pl.touk.mockserver</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mockserver-client</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,42 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.TypeChecked
|
||||
import org.apache.commons.lang3.StringEscapeUtils
|
||||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
class AddMockRequestData {
|
||||
String name
|
||||
String path
|
||||
Integer port
|
||||
String predicate
|
||||
String response
|
||||
Boolean soap
|
||||
Integer statusCode
|
||||
Method method
|
||||
String responseHeaders
|
||||
|
||||
void setPredicate(String predicate) {
|
||||
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
||||
}
|
||||
|
||||
void setResponse(String response) {
|
||||
this.response = StringEscapeUtils.escapeXml11(response)
|
||||
}
|
||||
|
||||
void setResponseHeaders(String responseHeaders) {
|
||||
this.responseHeaders = StringEscapeUtils.escapeXml11(responseHeaders)
|
||||
}
|
||||
|
||||
enum Method {
|
||||
POST,
|
||||
GET,
|
||||
DELETE,
|
||||
PUT,
|
||||
TRACE,
|
||||
HEAD,
|
||||
OPTIONS,
|
||||
PATCH
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.util.slurpersupport.GPathResult
|
||||
import org.apache.http.client.methods.CloseableHttpResponse
|
||||
import org.apache.http.client.methods.HttpGet
|
||||
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"
|
||||
}
|
||||
|
||||
void addMock(AddMockRequestData addMockRequestData) {
|
||||
HttpPost addMockPost = new HttpPost(address)
|
||||
addMockPost.entity = buildAddMockRequest(addMockRequestData)
|
||||
CloseableHttpResponse response = client.execute(addMockPost)
|
||||
GPathResult responseXml = Util.extractXmlResponse(response)
|
||||
if (responseXml.name() != 'mockAdded') {
|
||||
if (responseXml.text() == 'mock already registered') {
|
||||
throw new MockAlreadyExists()
|
||||
|
||||
}
|
||||
throw new InvalidMockDefinition(responseXml.text())
|
||||
}
|
||||
}
|
||||
|
||||
List<MockEvent> 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)
|
||||
if (responseXml.name() == 'mockRemoved') {
|
||||
return responseXml.'mockEvent'.collect {
|
||||
new MockEvent(mockRequestFromXml(it.request), mockResponseFromXml(it.response))
|
||||
}
|
||||
}
|
||||
throw new MockDoesNotExist()
|
||||
}
|
||||
|
||||
private static MockResponse mockResponseFromXml(GPathResult xml) {
|
||||
return new MockResponse(xml.statusCode.text() as int, xml.text.text(), xml.headers.param.collectEntries { [(it.@name.text()):it.text()] })
|
||||
}
|
||||
|
||||
private static MockRequest mockRequestFromXml(GPathResult xml) {
|
||||
return new MockRequest(
|
||||
xml.text.text(),
|
||||
xml.headers.param.collectEntries { [(it.@name.text()):it.text()] },
|
||||
xml.query.param.collectEntries { [(it.@name.text()):it.text()] },
|
||||
xml.path.elem*.text()
|
||||
)
|
||||
}
|
||||
|
||||
private static StringEntity buildRemoveMockRequest(RemoveMockRequestData data) {
|
||||
return new StringEntity("""\
|
||||
<removeMock>
|
||||
<name>${data.name}</name>
|
||||
</removeMock>
|
||||
""", ContentType.create("text/xml", "UTF-8"))
|
||||
}
|
||||
|
||||
private static StringEntity buildAddMockRequest(AddMockRequestData data) {
|
||||
return new StringEntity("""\
|
||||
<addMock>
|
||||
<name>${data.name}</name>
|
||||
<path>${data.path}</path>
|
||||
<port>${data.port}</port>
|
||||
${data.predicate ? "<predicate>${data.predicate}</predicate>" : ''}
|
||||
${data.response ? "<response>${data.response}</response>" : ''}
|
||||
${data.soap != null ? "<soap>${data.soap}</soap>" : ''}
|
||||
${data.statusCode ? "<statusCode>${data.statusCode}</statusCode>" : ''}
|
||||
${data.method ? "<method>${data.method}</method>" : ''}
|
||||
${data.responseHeaders ? "<responseHeaders>${data.responseHeaders}</responseHeaders>" : ''}
|
||||
</addMock>
|
||||
""", ContentType.create("text/xml", "UTF-8"))
|
||||
}
|
||||
|
||||
List<RegisteredMock> listMocks() {
|
||||
HttpGet get = new HttpGet(address)
|
||||
CloseableHttpResponse response = client.execute(get)
|
||||
GPathResult xml = Util.extractXmlResponse(response)
|
||||
if (xml.name() == 'mocks') {
|
||||
return xml.mock.collect { new RegisteredMock(it.name.text(), it.path.text(), it.port.text() as int) }
|
||||
}
|
||||
return []
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
class InvalidMockDefinition extends RuntimeException {
|
||||
InvalidMockDefinition(String s) {
|
||||
super(s)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
class MockAlreadyExists extends RuntimeException {}
|
|
@ -0,0 +1,9 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
class MockDoesNotExist extends RuntimeException {
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.EqualsAndHashCode
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@EqualsAndHashCode
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
class MockEvent {
|
||||
final MockRequest request
|
||||
final MockResponse response
|
||||
|
||||
MockEvent(MockRequest request, MockResponse response) {
|
||||
this.request = request
|
||||
this.response = response
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.EqualsAndHashCode
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
@EqualsAndHashCode
|
||||
class MockRequest {
|
||||
final String text
|
||||
final Map<String, String> headers
|
||||
final Map<String, String> query
|
||||
final List<String> path
|
||||
|
||||
MockRequest(String text, Map<String, String> headers, Map<String, String> query, List<String> path) {
|
||||
this.text = text
|
||||
this.headers = headers
|
||||
this.query = query
|
||||
this.path = path
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.EqualsAndHashCode
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
@EqualsAndHashCode
|
||||
class MockResponse {
|
||||
final int statusCode
|
||||
final String text
|
||||
final Map<String, String> headers
|
||||
|
||||
MockResponse(int statusCode, String text, Map<String, String> headers) {
|
||||
this.statusCode = statusCode
|
||||
this.text = text
|
||||
this.headers = headers
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.EqualsAndHashCode
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
@EqualsAndHashCode
|
||||
class RegisteredMock {
|
||||
final String name
|
||||
final String path
|
||||
final int port
|
||||
|
||||
RegisteredMock(String name, String path, int port) {
|
||||
this.name = name
|
||||
this.path = path
|
||||
this.port = port
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
class RemoveMockRequestData {
|
||||
String name
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package pl.touk.mockserver.client
|
||||
|
||||
import groovy.json.JsonSlurper
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.TypeChecked
|
||||
import groovy.util.slurpersupport.GPathResult
|
||||
import org.apache.http.HttpEntity
|
||||
import org.apache.http.client.methods.CloseableHttpResponse
|
||||
import org.apache.http.util.EntityUtils
|
||||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
class Util {
|
||||
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>"""
|
||||
}
|
||||
|
||||
static Object extractJsonResponse(CloseableHttpResponse response) {
|
||||
HttpEntity entity = response.entity
|
||||
Object json = new JsonSlurper().parseText(EntityUtils.toString(entity))
|
||||
EntityUtils.consumeQuietly(entity)
|
||||
return json
|
||||
}
|
||||
|
||||
static void consumeResponse(CloseableHttpResponse response) {
|
||||
EntityUtils.consumeQuietly(response.entity)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue