Refactor packages
This commit is contained in:
parent
dfc0332d5a
commit
fc4bda11de
6 changed files with 8 additions and 10 deletions
|
@ -0,0 +1,72 @@
|
|||
package pl.touk.mockserver.server
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange
|
||||
import groovy.util.slurpersupport.GPathResult
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
|
||||
class ContextExecutor {
|
||||
private final HttpServerWraper httpServerWraper
|
||||
final String path
|
||||
private final List<Mock> mocks
|
||||
|
||||
ContextExecutor(HttpServerWraper httpServerWraper, String path, Mock initialMock) {
|
||||
this.httpServerWraper = httpServerWraper
|
||||
this.path = path
|
||||
this.mocks = new CopyOnWriteArrayList<>([initialMock])
|
||||
httpServerWraper.createContext(path,{
|
||||
HttpExchange ex ->
|
||||
ex.sendResponseHeaders(200, 0)
|
||||
String input = ex.requestBody.text
|
||||
println "Mock received input"
|
||||
GPathResult inputXml = new XmlSlurper().parseText(input)
|
||||
for (Mock mock : mocks){
|
||||
GPathResult xml = inputXml
|
||||
try {
|
||||
if (mock.soap) {
|
||||
if(xml.name() == 'Envelope' && xml.Body.size() > 0){
|
||||
xml = getSoapBodyContent(xml)
|
||||
}else{
|
||||
continue
|
||||
}
|
||||
}
|
||||
if (xml != null && mock.predicate(xml)) {
|
||||
println "Mock ${mock.name} invoked"
|
||||
++mock.counter
|
||||
String response = mock.responseOk(xml)
|
||||
ex.responseBody << (mock.soap ? wrapSoap(response) : response)
|
||||
ex.responseBody.close()
|
||||
return
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
ex.responseBody << "<invalidInput/>"
|
||||
ex.responseBody.close()
|
||||
})
|
||||
}
|
||||
|
||||
private static GPathResult getSoapBodyContent(GPathResult xml) {
|
||||
return xml.Body.'**'[1]
|
||||
}
|
||||
|
||||
int removeMock(String name) {
|
||||
Mock mock = mocks.find {it.name == name}
|
||||
if(mock){
|
||||
mocks.remove(mock)
|
||||
}
|
||||
return mock.counter
|
||||
}
|
||||
|
||||
void addMock(Mock mock){
|
||||
mocks << mock
|
||||
}
|
||||
|
||||
private static String wrapSoap(String request) {
|
||||
"""<?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>"""
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package pl.touk.mockserver.server
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange
|
||||
import groovy.util.slurpersupport.GPathResult
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
|
||||
class HttpMockServer {
|
||||
|
||||
private final HttpServerWraper httpServerWraper
|
||||
private final List<HttpServerWraper> childServers = new CopyOnWriteArrayList<>()
|
||||
private final Set<String> mockNames = new CopyOnWriteArraySet<>()
|
||||
|
||||
HttpMockServer(int port = 9999){
|
||||
httpServerWraper= new HttpServerWraper(port)
|
||||
|
||||
httpServerWraper.createContext('/serverControl', {
|
||||
HttpExchange ex ->
|
||||
ex.sendResponseHeaders(200, 0)
|
||||
try{
|
||||
GPathResult request = new XmlSlurper().parse(ex.requestBody)
|
||||
if(ex.requestMethod== 'POST' && request.name() == 'addMock'){
|
||||
addMock(request, ex)
|
||||
}else if(ex.requestMethod == 'POST' && request.name() == 'removeMock'){
|
||||
removeMock(request, ex)
|
||||
}
|
||||
//TODO add get mock report
|
||||
//TODO add list mock
|
||||
}catch(Exception e){
|
||||
createErrorResponse(ex, e)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private void addMock(GPathResult request, HttpExchange ex) {
|
||||
String name = request.name
|
||||
if (name in mockNames) {
|
||||
throw new RuntimeException('mock already registered')
|
||||
}
|
||||
println "Adding $name"
|
||||
String mockPath = request.path
|
||||
int mockPort = Integer.valueOf(request.port as String)
|
||||
Closure predicate = Eval.me(request.predicate as String) as Closure
|
||||
Closure okResponse = Eval.me(request.response as String) as Closure
|
||||
boolean soap = Boolean.valueOf(request.soap as String)
|
||||
Mock mock = new Mock(name, predicate, okResponse, soap)
|
||||
HttpServerWraper child = childServers.find { it.port == mockPort }
|
||||
if (!child) {
|
||||
child = new HttpServerWraper(mockPort)
|
||||
childServers << child
|
||||
}
|
||||
child.addMock(mockPath, mock)
|
||||
mockNames << name
|
||||
ex.responseBody << '<mockAdded/>'
|
||||
ex.responseBody.close()
|
||||
}
|
||||
|
||||
private void removeMock(GPathResult request, HttpExchange ex) {
|
||||
String name = request.name
|
||||
if (! (name in mockNames)) {
|
||||
throw new RuntimeException('mock not registered')
|
||||
}
|
||||
println "Removing $name"
|
||||
int used = childServers.inject(0) { int res, HttpServerWraper server-> server.removeMock(name) + res}
|
||||
mockNames.remove(name)
|
||||
ex.responseBody << "<mockRemoved>$used</mockRemoved>"
|
||||
ex.responseBody.close()
|
||||
}
|
||||
|
||||
private static void createErrorResponse(HttpExchange ex, Exception e) {
|
||||
ex.responseBody << """<exceptionOccured>${e.message}</exceptionOccured>"""
|
||||
ex.responseBody.close()
|
||||
}
|
||||
|
||||
void stop(){
|
||||
childServers.each {it.stop()}
|
||||
httpServerWraper.stop()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package pl.touk.mockserver.server
|
||||
|
||||
import com.sun.net.httpserver.HttpHandler
|
||||
import com.sun.net.httpserver.HttpServer
|
||||
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class HttpServerWraper {
|
||||
private final HttpServer httpServer
|
||||
private final int port
|
||||
|
||||
private List<ContextExecutor> executors = []
|
||||
|
||||
HttpServerWraper(int port) {
|
||||
this.port = port
|
||||
InetSocketAddress addr = new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port)
|
||||
httpServer = HttpServer.create(addr, 0)
|
||||
httpServer.executor = Executors.newCachedThreadPool()
|
||||
println "Http server statrting on port $port..."
|
||||
httpServer.start()
|
||||
println 'Http server is started'
|
||||
}
|
||||
|
||||
void createContext(String context, HttpHandler handler) {
|
||||
httpServer.createContext(context, handler)
|
||||
}
|
||||
|
||||
void addMock(String path, Mock mock) {
|
||||
ContextExecutor executor = executors.find { it.path == path }
|
||||
if (executor) {
|
||||
executor.addMock(mock)
|
||||
} else {
|
||||
executors << new ContextExecutor(this, path, mock)
|
||||
}
|
||||
}
|
||||
|
||||
void stop() {
|
||||
executors.each { httpServer.removeContext(it.path) }
|
||||
httpServer.stop(0)
|
||||
}
|
||||
|
||||
int removeMock(String name) {
|
||||
executors.inject(0) { int res, ContextExecutor e -> e.removeMock(name) + res}
|
||||
}
|
||||
}
|
17
src/main/groovy/pl/touk/mockserver/server/Main.groovy
Normal file
17
src/main/groovy/pl/touk/mockserver/server/Main.groovy
Normal file
|
@ -0,0 +1,17 @@
|
|||
package pl.touk.mockserver.server
|
||||
|
||||
class Main {
|
||||
static void main(String [] args) {
|
||||
HttpMockServer httpMockServer = new HttpMockServer()
|
||||
|
||||
Runtime.runtime.addShutdownHook(new Thread({
|
||||
println 'Http server is stopping...'
|
||||
httpMockServer.stop()
|
||||
println 'Http server is stopped'
|
||||
} as Runnable))
|
||||
|
||||
while(true){
|
||||
Thread.sleep(10000)
|
||||
}
|
||||
}
|
||||
}
|
20
src/main/groovy/pl/touk/mockserver/server/Mock.groovy
Normal file
20
src/main/groovy/pl/touk/mockserver/server/Mock.groovy
Normal file
|
@ -0,0 +1,20 @@
|
|||
package pl.touk.mockserver.server
|
||||
|
||||
import groovy.transform.EqualsAndHashCode
|
||||
|
||||
@EqualsAndHashCode(excludes = ["counter"])
|
||||
class Mock {
|
||||
final String name
|
||||
final Closure predicate
|
||||
final Closure responseOk
|
||||
final boolean soap
|
||||
//TODO add http method
|
||||
int counter = 0
|
||||
|
||||
Mock(String name, Closure predicate, Closure responseOk, boolean soap) {
|
||||
this.name = name
|
||||
this.predicate = predicate
|
||||
this.responseOk = responseOk
|
||||
this.soap = soap
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue