Refactoring

This commit is contained in:
Dominik Adam Przybysz 2014-12-07 21:17:33 +01:00
parent 6bd008c5ec
commit 876c29e57d

View file

@ -14,7 +14,7 @@ class HttpMockServer {
HttpServerWraper httpServerWraper HttpServerWraper httpServerWraper
List<HttpServerWraper> childServers = new CopyOnWriteArrayList<>() List<HttpServerWraper> childServers = new CopyOnWriteArrayList<>()
Set<String> actionsNames = new CopyOnWriteArraySet<>() Set<String> mockNames = new CopyOnWriteArraySet<>()
HttpMockServer(int port = 9999){ HttpMockServer(int port = 9999){
httpServerWraper= new HttpServerWraper(port) httpServerWraper= new HttpServerWraper(port)
@ -25,9 +25,9 @@ class HttpMockServer {
try{ try{
GPathResult request = new XmlSlurper().parse(ex.requestBody) GPathResult request = new XmlSlurper().parse(ex.requestBody)
if(ex.requestMethod== 'POST' && request.name() == 'addMock'){ if(ex.requestMethod== 'POST' && request.name() == 'addMock'){
addMockAction(request, ex) addMock(request, ex)
}else if(ex.requestMethod == 'POST' && request.name() == 'removeMock'){ }else if(ex.requestMethod == 'POST' && request.name() == 'removeMock'){
removeMockAction(request, ex) removeMock(request, ex)
} }
//TODO add list mock //TODO add list mock
}catch(Exception e){ }catch(Exception e){
@ -36,9 +36,9 @@ class HttpMockServer {
}) })
} }
private void addMockAction(GPathResult request, HttpExchange ex) { private void addMock(GPathResult request, HttpExchange ex) {
String name = request.name String name = request.name
if (name in actionsNames) { if (name in mockNames) {
throw new RuntimeException('mock already registered') throw new RuntimeException('mock already registered')
} }
println "Adding $name" println "Adding $name"
@ -46,30 +46,25 @@ class HttpMockServer {
int mockPort = Integer.valueOf(request.port as String) int mockPort = Integer.valueOf(request.port as String)
Closure predicate = Eval.me(request.predicate as String) as Closure Closure predicate = Eval.me(request.predicate as String) as Closure
Closure okResponse = Eval.me(request.response as String) as Closure Closure okResponse = Eval.me(request.response as String) as Closure
MockAction action = new MockAction(name, predicate, okResponse) Mock mock = new Mock(name, predicate, okResponse)
HttpServerWraper child = childServers.find { it.port == mockPort } HttpServerWraper child = childServers.find { it.port == mockPort }
if (!child) { if (!child) {
child = new HttpServerWraper(mockPort) child = new HttpServerWraper(mockPort)
childServers << child childServers << child
} }
child.addAction(mockPath, action) child.addMock(mockPath, mock)
actionsNames << name mockNames << name
ex.responseBody << '<mockAdded/>' ex.responseBody << '<mockAdded/>'
ex.responseBody.close() ex.responseBody.close()
} }
private void removeMockAction(GPathResult request, HttpExchange ex) { private void removeMock(GPathResult request, HttpExchange ex) {
String name = request.name String name = request.name
if (! (name in actionsNames)) { if (! (name in mockNames)) {
throw new RuntimeException('mock not registered') throw new RuntimeException('mock not registered')
} }
childServers.each { for (ContextExecutor e : it.executors){ childServers.each {it.removeMock(name)}
MockAction action = e.actions.find {it.name == name} mockNames.remove(name)
if(action){
e.actions.remove(action)
actionsNames.remove(name)
}
}}
ex.responseBody << '<mockRemoved/>' ex.responseBody << '<mockRemoved/>'
ex.responseBody.close() ex.responseBody.close()
} }
@ -104,43 +99,43 @@ class HttpMockServer {
httpServer.createContext(context, handler) httpServer.createContext(context, handler)
} }
void addAction(String path, MockAction action){ 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.actions << action executor.mocks << mock
}else { }else {
executors << new ContextExecutor(this, path, action) executors << new ContextExecutor(this, path, mock)
} }
} }
void removeAction(String name){
//TODO delete action by name
}
void stop(){ void stop(){
executors.each {httpServer.removeContext(it.path)} executors.each {httpServer.removeContext(it.path)}
httpServer.stop(0) httpServer.stop(0)
} }
void removeMock(String name) {
executors.each {it.removeMock(name)}
}
} }
private static final class ContextExecutor{ private static final class ContextExecutor{
final HttpServerWraper httpServerWraper final HttpServerWraper httpServerWraper
final String path final String path
List<MockAction> actions List<Mock> mocks
ContextExecutor(HttpServerWraper httpServerWraper, String path, MockAction initialAction) { ContextExecutor(HttpServerWraper httpServerWraper, String path, Mock initialMock) {
this.httpServerWraper = httpServerWraper this.httpServerWraper = httpServerWraper
this.path = path this.path = path
this.actions = new CopyOnWriteArrayList<>([initialAction]) this.mocks = new CopyOnWriteArrayList<>([initialMock])
httpServerWraper.createContext(path,{ httpServerWraper.createContext(path,{
HttpExchange ex -> HttpExchange ex ->
ex.sendResponseHeaders(200, 0) ex.sendResponseHeaders(200, 0)
String input = ex.requestBody.text String input = ex.requestBody.text
println "Mock received input" println "Mock received input"
GPathResult xml = new XmlSlurper().parseText(input) GPathResult xml = new XmlSlurper().parseText(input)
for (MockAction action : actions){ for (Mock mock : mocks){
if(action.predicate(xml)){ if(mock.predicate(xml)){
ex.responseBody << action.responseOk(xml) ex.responseBody << mock.responseOk(xml)
ex.responseBody.close() ex.responseBody.close()
return return
} }
@ -149,17 +144,24 @@ class HttpMockServer {
ex.responseBody.close() ex.responseBody.close()
}) })
} }
void removeMock(String name) {
Mock mock = mocks.find {it.name == name}
if(mock){
mocks.remove(mock)
}
}
} }
@EqualsAndHashCode @EqualsAndHashCode
private static final class MockAction { private static final class Mock {
final String name final String name
final Closure predicate final Closure predicate
final Closure responseOk final Closure responseOk
//TODO add http method //TODO add http method
//TODO add is soap method //TODO add is soap method
MockAction(String name, Closure predicate, Closure responseOk) { Mock(String name, Closure predicate, Closure responseOk) {
this.name = name this.name = name
this.predicate = predicate this.predicate = predicate
this.responseOk = responseOk this.responseOk = responseOk