Search in sources :

Example 16 with KnotContext

use of io.knotx.dataobjects.KnotContext in project knotx by Cognifide.

the class ActionKnotProxyVerticleTest method callGetWithActionFragmentActionHandlerNotExists_expectStatusCode500.

@Test
@KnotxConfiguration("knotx-test.json")
public void callGetWithActionFragmentActionHandlerNotExists_expectStatusCode500(TestContext context) throws Exception {
    KnotContext knotContext = createKnotContext("fragment_form_actionhandler_not_exists_in.txt");
    knotContext.getClientRequest().setMethod(HttpMethod.GET);
    callActionKnotWithAssertions(context, knotContext, clientResponse -> {
        context.assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), clientResponse.getClientResponse().getStatusCode());
        context.assertFalse(clientResponse.getTransition() != null);
        context.assertFalse(clientResponse.getFragments() != null);
    }, error -> context.fail(error.getMessage()));
}
Also used : KnotContext(io.knotx.dataobjects.KnotContext) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Example 17 with KnotContext

use of io.knotx.dataobjects.KnotContext in project knotx by Cognifide.

the class FragmentAssemblerKnotProxyImpl method createSuccessResponse.

private KnotContext createSuccessResponse(KnotContext inputContext, String renderedContent) {
    ClientResponse clientResponse = inputContext.getClientResponse();
    if (StringUtils.isBlank(renderedContent)) {
        clientResponse.setStatusCode(HttpResponseStatus.NO_CONTENT.code());
    } else {
        MultiMap headers = clientResponse.getHeaders();
        headers.add(HttpHeaders.CONTENT_LENGTH.toString().toLowerCase(), Integer.toString(renderedContent.length()));
        clientResponse.setBody(Buffer.buffer(renderedContent)).setHeaders(headers);
        clientResponse.setStatusCode(HttpResponseStatus.OK.code());
    }
    return new KnotContext().setClientRequest(inputContext.getClientRequest()).setClientResponse(clientResponse);
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) MultiMap(io.vertx.reactivex.core.MultiMap) KnotContext(io.knotx.dataobjects.KnotContext)

Example 18 with KnotContext

use of io.knotx.dataobjects.KnotContext in project knotx by Cognifide.

the class KnotxServerRoutingTest method createSimpleGatewayKnot.

private void createSimpleGatewayKnot(final String address, final String transition) {
    Action1<KnotContext> simpleKnot = knotContext -> {
        ClientResponse clientResponse = new ClientResponse();
        clientResponse.setBody(Buffer.buffer());
        clientResponse.setStatusCode(200);
        knotContext.setClientResponse(clientResponse);
        knotContext.setTransition(transition);
    };
    MockKnotProxy.register(vertx.vertx(), address, simpleKnot);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) KnotContext(io.knotx.dataobjects.KnotContext) RunWith(org.junit.runner.RunWith) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Logback(io.knotx.junit.rule.Logback) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Action1(rx.functions.Action1) ClientResponse(io.knotx.dataobjects.ClientResponse) RuleChain(org.junit.rules.RuleChain) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) MultiMap(io.vertx.reactivex.core.MultiMap) Vertx(io.vertx.reactivex.core.Vertx) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) Observable(io.reactivex.Observable) HttpClient(io.vertx.reactivex.core.http.HttpClient) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) ClientResponse(io.knotx.dataobjects.ClientResponse) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) KnotContext(io.knotx.dataobjects.KnotContext)

Example 19 with KnotContext

use of io.knotx.dataobjects.KnotContext in project knotx by Cognifide.

the class KnotxGatewayContextHandler method handle.

@Override
public void handle(RoutingContext context) {
    KnotContext knotContext = context.get(KnotContext.KEY);
    String bodyAsString = context.getBodyAsString();
    if (StringUtils.isNotBlank(bodyAsString)) {
        knotContext.setFragments(Collections.singletonList(Fragment.raw(bodyAsString)));
    }
    LOGGER.debug("CustomFlow: Routing the traffic to '{}'", address);
    proxies.computeIfAbsent(address, adr -> KnotProxy.createProxyWithOptions(vertx, adr, configuration.getDeliveryOptions())).rxProcess(knotContext).doOnSuccess(ctx -> context.put(KnotContext.KEY, ctx)).subscribe(ctx -> {
        context.put(KnotContext.KEY, ctx);
        context.next();
    }, error -> {
        LOGGER.error("Error happened while communicating with {} engine", error, address);
        context.fail(error);
    });
}
Also used : KnotContext(io.knotx.dataobjects.KnotContext) KnotProxy(io.knotx.reactivex.proxy.KnotProxy) Fragment(io.knotx.dataobjects.Fragment) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) LoggerFactory(io.vertx.core.logging.LoggerFactory) Vertx(io.vertx.reactivex.core.Vertx) Map(java.util.Map) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) KnotxServerConfiguration(io.knotx.server.configuration.KnotxServerConfiguration) Collections(java.util.Collections) KnotContext(io.knotx.dataobjects.KnotContext)

Example 20 with KnotContext

use of io.knotx.dataobjects.KnotContext in project knotx by Cognifide.

the class HandlebarsKnotProxyImpl method processError.

@Override
protected KnotContext processError(KnotContext knotContext, Throwable error) {
    LOGGER.error("Error happened during Template processing", error);
    ClientResponse errorResponse = new ClientResponse().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
    return new KnotContext().setClientRequest(knotContext.getClientRequest()).setClientResponse(errorResponse);
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) KnotContext(io.knotx.dataobjects.KnotContext)

Aggregations

KnotContext (io.knotx.dataobjects.KnotContext)21 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)10 Test (org.junit.Test)10 ClientResponse (io.knotx.dataobjects.ClientResponse)6 Vertx (io.vertx.reactivex.core.Vertx)6 Fragment (io.knotx.dataobjects.Fragment)4 KnotProxy (io.knotx.reactivex.proxy.KnotProxy)4 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)4 Logback (io.knotx.junit.rule.Logback)3 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)3 KnotxServerConfiguration (io.knotx.server.configuration.KnotxServerConfiguration)3 Handler (io.vertx.core.Handler)3 Logger (io.vertx.core.logging.Logger)3 LoggerFactory (io.vertx.core.logging.LoggerFactory)3 Async (io.vertx.ext.unit.Async)3 TestContext (io.vertx.ext.unit.TestContext)3 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)3 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)3 MultiMap (io.vertx.reactivex.core.MultiMap)3 RoutingContext (io.vertx.reactivex.ext.web.RoutingContext)3