Add mocks configuration dump and restore
This commit is contained in:
parent
c02e93edc3
commit
44f44ee392
6 changed files with 285 additions and 32 deletions
|
@ -3,6 +3,7 @@ package pl.touk.mockserver.server
|
|||
import com.sun.net.httpserver.HttpExchange
|
||||
import groovy.util.logging.Slf4j
|
||||
import pl.touk.mockserver.api.common.ImportAlias
|
||||
import pl.touk.mockserver.api.common.Method
|
||||
import pl.touk.mockserver.api.request.AddMock
|
||||
import pl.touk.mockserver.api.request.MockServerRequest
|
||||
import pl.touk.mockserver.api.request.PeekMock
|
||||
|
@ -30,18 +31,27 @@ class HttpMockServer {
|
|||
private final HttpServerWraper httpServerWraper
|
||||
private final Map<Integer, HttpServerWraper> childServers = new ConcurrentHashMap<>()
|
||||
private final Set<String> mockNames = new CopyOnWriteArraySet<>()
|
||||
private final ConfigObject configuration = new ConfigObject()
|
||||
|
||||
private static
|
||||
final JAXBContext requestJaxbContext = JAXBContext.newInstance(AddMock.package.name, AddMock.classLoader)
|
||||
|
||||
HttpMockServer(int port = 9999) {
|
||||
HttpMockServer(int port = 9999, ConfigObject initialConfiguration = new ConfigObject()) {
|
||||
httpServerWraper = new HttpServerWraper(port)
|
||||
|
||||
initialConfiguration.values()?.each { ConfigObject co ->
|
||||
addMock(co)
|
||||
}
|
||||
|
||||
httpServerWraper.createContext('/serverControl', {
|
||||
HttpExchange ex ->
|
||||
try {
|
||||
if (ex.requestMethod == 'GET') {
|
||||
listMocks(ex)
|
||||
if (ex.requestURI.path == '/serverControl/configuration') {
|
||||
createResponse(ex, configuration.prettyPrint(), 200)
|
||||
} else {
|
||||
listMocks(ex)
|
||||
}
|
||||
} else if (ex.requestMethod == 'POST') {
|
||||
MockServerRequest request = requestJaxbContext.createUnmarshaller().unmarshal(ex.requestBody) as MockServerRequest
|
||||
if (request instanceof AddMock) {
|
||||
|
@ -95,10 +105,41 @@ class HttpMockServer {
|
|||
Mock mock = mockFromRequest(request)
|
||||
HttpServerWraper child = getOrCreateChildServer(mock.port)
|
||||
child.addMock(mock)
|
||||
saveConfiguration(request)
|
||||
mockNames << name
|
||||
createResponse(ex, new MockAdded(), 200)
|
||||
}
|
||||
|
||||
private void addMock(ConfigObject co) {
|
||||
String name = co.name
|
||||
if (name in mockNames) {
|
||||
throw new RuntimeException('mock already registered')
|
||||
}
|
||||
Mock mock = mockFromConfig(co)
|
||||
HttpServerWraper child = getOrCreateChildServer(mock.port)
|
||||
child.addMock(mock)
|
||||
configuration.put(name, co)
|
||||
mockNames << name
|
||||
}
|
||||
|
||||
private void saveConfiguration(AddMock request) {
|
||||
ConfigObject mockDefinition = new ConfigObject()
|
||||
request.metaPropertyValues.findAll { it.name != 'class' && it.value }.each {
|
||||
if (it.name == 'imports') {
|
||||
ConfigObject configObject = new ConfigObject()
|
||||
it.value.each { ImportAlias imp ->
|
||||
configObject.put(imp.alias, imp.fullClassName)
|
||||
}
|
||||
mockDefinition.put(it.name, configObject)
|
||||
} else if (it.name == 'method') {
|
||||
mockDefinition.put(it.name, it.value.name())
|
||||
} else {
|
||||
mockDefinition.put(it.name, it.value)
|
||||
}
|
||||
}
|
||||
configuration.put(request.name, mockDefinition)
|
||||
}
|
||||
|
||||
private static Mock mockFromRequest(AddMock request) {
|
||||
Mock mock = new Mock(request.name, request.path, request.port)
|
||||
mock.imports = request.imports?.collectEntries { [(it.alias): it.fullClassName] } ?: [:]
|
||||
|
@ -112,6 +153,19 @@ class HttpMockServer {
|
|||
return mock
|
||||
}
|
||||
|
||||
private static Mock mockFromConfig(ConfigObject co) {
|
||||
Mock mock = new Mock(co.name, co.path, co.port)
|
||||
mock.imports = co.imports
|
||||
mock.predicate = co.predicate ?: null
|
||||
mock.response = co.response ?: null
|
||||
mock.soap = co.soap ?: null
|
||||
mock.statusCode = co.statusCode ?: null
|
||||
mock.method = co.method ? Method.valueOf(co.method) : null
|
||||
mock.responseHeaders = co.responseHeaders ?: null
|
||||
mock.schema = co.schema ?: null
|
||||
return mock
|
||||
}
|
||||
|
||||
private HttpServerWraper getOrCreateChildServer(int mockPort) {
|
||||
HttpServerWraper child = childServers[mockPort]
|
||||
if (!child) {
|
||||
|
@ -132,6 +186,7 @@ class HttpMockServer {
|
|||
it.removeMock(name)
|
||||
}.flatten() as List<MockEvent>
|
||||
mockNames.remove(name)
|
||||
configuration.remove(name)
|
||||
MockRemoved mockRemoved = new MockRemoved(
|
||||
mockEvents: createMockEventReports(mockEvents)
|
||||
)
|
||||
|
|
|
@ -5,7 +5,7 @@ import groovy.util.logging.Slf4j
|
|||
@Slf4j
|
||||
class Main {
|
||||
static void main(String[] args) {
|
||||
HttpMockServer httpMockServer = args.length == 1 ? new HttpMockServer(args[0] as int) : new HttpMockServer()
|
||||
HttpMockServer httpMockServer = startMockServer(args)
|
||||
|
||||
Runtime.runtime.addShutdownHook(new Thread({
|
||||
log.info('Http server is stopping...')
|
||||
|
@ -17,4 +17,15 @@ class Main {
|
|||
Thread.sleep(10000)
|
||||
}
|
||||
}
|
||||
|
||||
private static HttpMockServer startMockServer(String... args) {
|
||||
switch (args.length) {
|
||||
case 1:
|
||||
return new HttpMockServer(args[0] as int)
|
||||
case 2:
|
||||
return new HttpMockServer(args[0] as int, new ConfigSlurper().parse(new File(args[1]).toURI().toURL()))
|
||||
default:
|
||||
return new HttpMockServer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue