Search in sources :

Example 11 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class MocoServer method start.

public int start(final int port, final ChannelInitializer<? extends Channel> pipelineFactory) {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(group).channel(NioServerSocketChannel.class).childHandler(pipelineFactory);
    try {
        future = bootstrap.bind(port).sync();
        SocketAddress socketAddress = future.channel().localAddress();
        return ((InetSocketAddress) socketAddress).getPort();
    } catch (InterruptedException e) {
        throw new MocoException(e);
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) MocoException(com.github.dreamhead.moco.MocoException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 12 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class JsonRequestMatcher method doMatch.

private boolean doMatch(final Request request, final byte[] content) {
    try {
        JsonNode requestNode = mapper.readTree(content);
        JsonNode resourceNode = mapper.readTree(expected.readFor(of(request)).getContent());
        return requestNode.equals(resourceNode);
    } catch (JsonProcessingException jpe) {
        return false;
    } catch (IOException e) {
        throw new MocoException(e);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) MocoException(com.github.dreamhead.moco.MocoException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 13 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class XmlRequestMatcher method extractDocument.

private Document extractDocument(final InputSource inputSource, final DocumentBuilder builder) throws SAXException {
    try {
        Document document = builder.parse(inputSource);
        document.normalizeDocument();
        trimNode(document);
        return document;
    } catch (IOException e) {
        throw new MocoException(e);
    }
}
Also used : IOException(java.io.IOException) MocoException(com.github.dreamhead.moco.MocoException) Document(org.w3c.dom.Document)

Example 14 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class Globs method doGlob.

private static ImmutableList<String> doGlob(final Path path, final Path searchPath) {
    final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + path);
    try {
        final ImmutableList.Builder<String> builder = ImmutableList.builder();
        Files.walkFileTree(searchPath, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) {
                if (matcher.matches(file)) {
                    builder.add(file.toString());
                }
                return FileVisitResult.CONTINUE;
            }
        });
        return builder.build();
    } catch (IOException e) {
        throw new MocoException(e);
    }
}
Also used : Path(java.nio.file.Path) PathMatcher(java.nio.file.PathMatcher) ImmutableList(com.google.common.collect.ImmutableList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) MocoException(com.github.dreamhead.moco.MocoException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 15 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class MocoRequestAction method execute.

@Override
public final void execute(final Request request) {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        final HttpUriRequest actionRequest = prepareRequest(request);
        dump(actionRequest);
        final CloseableHttpResponse response = client.execute(actionRequest);
        dump(response);
    } catch (IOException e) {
        throw new MocoException(e);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) MocoException(com.github.dreamhead.moco.MocoException)

Aggregations

MocoException (com.github.dreamhead.moco.MocoException)18 IOException (java.io.IOException)13 MessageContent (com.github.dreamhead.moco.model.MessageContent)3 ImmutableList (com.google.common.collect.ImmutableList)2 ParseException (freemarker.core.ParseException)2 Template (freemarker.template.Template)2 TemplateException (freemarker.template.TemplateException)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 Document (org.w3c.dom.Document)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Response (com.github.dreamhead.moco.Response)1