Search in sources :

Example 1 with GroovyInterceptor

use of com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor in project service-proxy by membrane.

the class GenericJsonParserTest method testMultipleChildElements.

@Test
public void testMultipleChildElements() {
    JSONObject spec = new JSONObject(new HashMap<String, Object>() {

        {
            put("interceptors", Arrays.asList(new HashMap<String, Object>() {

                {
                    put("groovy", new HashMap<String, Object>() {

                        {
                            put("src", "println()");
                        }
                    });
                }
            }, new HashMap<String, Object>() {

                {
                    put("target", new HashMap<String, Object>() {

                        {
                            put("host", "localhost");
                            put("port", 8080);
                        }
                    });
                }
            }));
        }
    });
    ServiceProxy sp = GenericJsonParser.parse(ServiceProxy.class, spec);
    assertEquals(2, sp.getInterceptors().size());
    assertTrue(sp.getInterceptors().get(0) instanceof GroovyInterceptor);
    assertEquals("println()", ((GroovyInterceptor) sp.getInterceptors().get(0)).getSrc());
    assertTrue(sp.getInterceptors().get(1) instanceof AbstractServiceProxy.Target);
    assertEquals("localhost", ((AbstractServiceProxy.Target) sp.getInterceptors().get(1)).getHost());
    assertEquals(8080, ((AbstractServiceProxy.Target) sp.getInterceptors().get(1)).getPort());
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) GroovyInterceptor(com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor) HashMap(java.util.HashMap) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Test(org.junit.Test)

Example 2 with GroovyInterceptor

use of com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor in project service-proxy by membrane.

the class GenericJsonParserTest method testChildElements.

@Test
public void testChildElements() {
    JSONObject spec = new JSONObject(new HashMap<String, Object>() {

        {
            put("interceptors", Collections.singletonList(new HashMap<String, Object>() {

                {
                    put("groovy", new HashMap<String, Object>() {

                        {
                            put("src", "println()");
                        }
                    });
                }
            }));
        }
    });
    ServiceProxy sp = GenericJsonParser.parse(ServiceProxy.class, spec);
    assertFalse(sp.getInterceptors().isEmpty());
    assertEquals("println()", ((GroovyInterceptor) sp.getInterceptors().get(0)).getSrc());
}
Also used : JSONObject(org.jose4j.json.internal.json_simple.JSONObject) AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) HashMap(java.util.HashMap) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Test(org.junit.Test)

Example 3 with GroovyInterceptor

use of com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor in project service-proxy by membrane.

the class GenericJsonParserTest method testTextContent.

@Test
public void testTextContent() {
    String want = "println(\"Hello, World!\")";
    JSONObject spec = new JSONObject(new HashMap<String, Object>() {

        {
            put("src", want);
        }
    });
    GroovyInterceptor gi = GenericJsonParser.parse(GroovyInterceptor.class, spec);
    assertEquals(want, gi.getSrc());
}
Also used : JSONObject(org.jose4j.json.internal.json_simple.JSONObject) GroovyInterceptor(com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Test(org.junit.Test)

Example 4 with GroovyInterceptor

use of com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor in project service-proxy by membrane.

the class GenericJsonParserTest method testCombined.

@Test
public void testCombined() {
    JSONObject spec = new JSONObject(new HashMap<String, Object>() {

        {
            put("port", 2020);
            put("blockRequest", true);
            put("path", new HashMap<String, Object>() {

                {
                    put("isRegExp", true);
                    put("value", "/foo");
                }
            });
            put("interceptors", Arrays.asList(new HashMap<String, Object>() {

                {
                    put("groovy", new HashMap<String, Object>() {

                        {
                            put("src", "println()");
                        }
                    });
                }
            }, new HashMap<String, Object>() {

                {
                    put("target", new HashMap<String, Object>() {

                        {
                            put("host", "localhost");
                            put("port", 8080);
                        }
                    });
                }
            }));
        }
    });
    ServiceProxy sp = GenericJsonParser.parse(ServiceProxy.class, spec);
    assertEquals(2020, sp.getPort());
    assertTrue(sp.isBlockRequest());
    assertTrue(sp.getPath().isRegExp());
    assertEquals("/foo", sp.getPath().getValue());
    assertEquals(2, sp.getInterceptors().size());
    assertTrue(sp.getInterceptors().get(0) instanceof GroovyInterceptor);
    assertEquals("println()", ((GroovyInterceptor) sp.getInterceptors().get(0)).getSrc());
    assertTrue(sp.getInterceptors().get(1) instanceof AbstractServiceProxy.Target);
    assertEquals("localhost", ((AbstractServiceProxy.Target) sp.getInterceptors().get(1)).getHost());
    assertEquals(8080, ((AbstractServiceProxy.Target) sp.getInterceptors().get(1)).getPort());
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) GroovyInterceptor(com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor) HashMap(java.util.HashMap) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Test(org.junit.Test)

Example 5 with GroovyInterceptor

use of com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor in project service-proxy by membrane.

the class ConcurrentConnectionLimitTest method setup.

@Before
public void setup() throws Exception {
    executor = Executors.newFixedThreadPool(concurrency);
    router = new HttpRouter();
    concurrentLimit = router.getTransport().getConcurrentConnectionLimitPerIp();
    ServiceProxy sp = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", port), "", -1);
    GroovyInterceptor gi = new GroovyInterceptor();
    gi.setSrc("exc.setResponse(Response.ok(\"Response\").build())\nRETURN");
    sp.getInterceptors().add(gi);
    router.getRuleManager().addProxyAndOpenPortIfNew(sp);
    router.init();
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) GroovyInterceptor(com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) Before(org.junit.Before)

Aggregations

Test (org.junit.Test)5 GroovyInterceptor (com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor)4 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)4 JSONObject (org.jose4j.json.internal.json_simple.JSONObject)4 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)3 HashMap (java.util.HashMap)3 HttpRouter (com.predic8.membrane.core.HttpRouter)2 Exchange (com.predic8.membrane.core.exchange.Exchange)1 Request (com.predic8.membrane.core.http.Request)1 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)1 Before (org.junit.Before)1