Search in sources :

Example 1 with KnotContext

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

the class ActionKnotProxyVerticleTest method callPostWithActionFragmentWithIncorrectSnippetId_expectStatusCode500.

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

Example 2 with KnotContext

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

the class ActionKnotProxyVerticleTest method callGetWithNoActionFragments_expectResponseOkNoFragmentChanges.

@Test
@KnotxConfiguration("knotx-test.json")
public void callGetWithNoActionFragments_expectResponseOkNoFragmentChanges(TestContext context) throws Exception {
    String expectedTemplatingFragment = FileReader.readText("fragment_templating_out.txt");
    KnotContext knotContext = createKnotContext(FIRST_FRAGMENT, LAST_FRAGMENT, "fragment_templating_in.txt");
    knotContext.getClientRequest().setMethod(HttpMethod.GET);
    callActionKnotWithAssertions(context, knotContext, clientResponse -> {
        context.assertEquals(HttpResponseStatus.OK.code(), clientResponse.getClientResponse().getStatusCode());
        context.assertTrue(clientResponse.getTransition() != null);
        context.assertEquals(KNOT_TRANSITION, clientResponse.getTransition());
        context.assertTrue(clientResponse.getFragments() != null);
        List<Fragment> fragments = clientResponse.getFragments();
        context.assertEquals(FIRST_FRAGMENT.content(), fragments.get(0).content());
        context.assertEquals(expectedTemplatingFragment, fragments.get(1).content());
        context.assertEquals(LAST_FRAGMENT.content(), fragments.get(2).content());
    }, error -> context.fail(error.getMessage()));
}
Also used : KnotContext(io.knotx.dataobjects.KnotContext) Fragment(io.knotx.dataobjects.Fragment) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Example 3 with KnotContext

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

the class ActionKnotProxyVerticleTest method callGetWithTwoActionFragments_expectResponseOkTwoFragmentChanges.

@Test
@KnotxConfiguration("knotx-test.json")
public void callGetWithTwoActionFragments_expectResponseOkTwoFragmentChanges(TestContext context) throws Exception {
    String expectedRedirectFormFragment = FileReader.readText("fragment_form_redirect_out.txt");
    String expectedSelfFormFragment = FileReader.readText("fragment_form_self_out.txt");
    KnotContext knotContext = createKnotContext(FIRST_FRAGMENT, LAST_FRAGMENT, "fragment_form_redirect_in.txt", "fragment_form_self_in.txt");
    knotContext.getClientRequest().setMethod(HttpMethod.GET);
    callActionKnotWithAssertions(context, knotContext, clientResponse -> {
        context.assertEquals(HttpResponseStatus.OK.code(), clientResponse.getClientResponse().getStatusCode());
        context.assertTrue(clientResponse.getTransition() != null);
        context.assertEquals(KNOT_TRANSITION, clientResponse.getTransition());
        context.assertTrue(clientResponse.getFragments() != null);
        List<Fragment> fragments = clientResponse.getFragments();
        context.assertEquals(FIRST_FRAGMENT.content(), fragments.get(0).content());
        context.assertEquals(clean(expectedRedirectFormFragment), clean(fragments.get(1).content()));
        context.assertEquals(clean(expectedSelfFormFragment), clean(fragments.get(2).content()));
        context.assertEquals(LAST_FRAGMENT.content(), fragments.get(3).content());
    }, error -> context.fail(error.getMessage()));
}
Also used : KnotContext(io.knotx.dataobjects.KnotContext) Fragment(io.knotx.dataobjects.Fragment) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Example 4 with KnotContext

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

the class KnotxAssemblerHandler method handle.

@Override
public void handle(RoutingContext context) {
    KnotContext knotContext = context.get(KnotContext.KEY);
    if (isOkClientResponse(knotContext.getClientResponse())) {
        assembler.rxProcess(knotContext).doOnSuccess(this::traceMessage).subscribe(ctx -> {
            if (isOkClientResponse(ctx.getClientResponse())) {
                sendResponse(context, ctx.getClientResponse());
            } else {
                context.fail(ctx.getClientResponse().getStatusCode());
            }
        }, error -> {
            LOGGER.error("Error happened while communicating with {} engine", error, configuration.getDefaultFlow().splitterAddress());
            context.fail(error);
        });
    } else {
        sendResponse(context, knotContext.getClientResponse());
    }
}
Also used : KnotContext(io.knotx.dataobjects.KnotContext)

Example 5 with KnotContext

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

the class KnotxEngineHandler method handleRoute.

private void handleRoute(final RoutingContext context, final String address, final Map<String, RoutingEntry> routing) {
    KnotContext knotContext = context.get(KnotContext.KEY);
    proxies.computeIfAbsent(address, adr -> KnotProxy.createProxyWithOptions(vertx, adr, configuration.getDeliveryOptions())).rxProcess(knotContext).doOnSuccess(ctx -> context.put(KnotContext.KEY, ctx)).subscribe(ctx -> OptionalAction.of(Optional.ofNullable(ctx.getTransition())).ifPresent(on -> {
        RoutingEntry entry = routing.get(on);
        if (entry != null) {
            handleRoute(context, entry.address(), entry.onTransition());
        } else {
            LOGGER.debug("Received transition '{}' from '{}'. No further routing available for the transition. Go to the response generation.", on, address);
            // last knot can return default transition
            context.put(KnotContext.KEY, ctx);
            context.next();
        }
    }).ifNotPresent(() -> {
        LOGGER.debug("Request processing finished by {} Knot. Go to the response generation", address);
        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) OptionalAction(io.knotx.util.OptionalAction) KnotProxy(io.knotx.reactivex.proxy.KnotProxy) HashMap(java.util.HashMap) RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) LoggerFactory(io.vertx.core.logging.LoggerFactory) RoutingEntry(io.knotx.server.configuration.RoutingEntry) Vertx(io.vertx.reactivex.core.Vertx) Map(java.util.Map) Optional(java.util.Optional) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) KnotxServerConfiguration(io.knotx.server.configuration.KnotxServerConfiguration) RoutingEntry(io.knotx.server.configuration.RoutingEntry) 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