Search in sources :

Example 1 with Fragment

use of io.knotx.dataobjects.Fragment 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 2 with Fragment

use of io.knotx.dataobjects.Fragment 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 3 with Fragment

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

the class FragmentContext method from.

/**
 * Factory method that creates context from the {@link Fragment}. All services and params are
 * extracted to separate entries.
 *
 * @param fragment - fragment from which the context will be created.
 * @return a FragmentContext that wraps given fragment.
 */
public static FragmentContext from(Fragment fragment) {
    Document document = Jsoup.parseBodyFragment(fragment.content());
    Element scriptTag = document.body().child(0);
    List<Attribute> attributes = scriptTag.attributes().asList();
    Map<String, Attribute> serviceAttributes = attributes.stream().filter(attribute -> attribute.getKey().matches(DATA_SERVICE)).collect(Collectors.toMap(attribute -> ServiceAttributeUtil.extractNamespace(attribute.getKey()), Function.identity()));
    Map<String, Attribute> paramsAttributes = attributes.stream().filter(attribute -> attribute.getKey().matches(DATA_PARAMS)).collect(Collectors.toMap(attribute -> ServiceAttributeUtil.extractNamespace(attribute.getKey()), Function.identity()));
    return new FragmentContext().fragment(fragment).services(serviceAttributes.entrySet().stream().map(entry -> new ServiceEntry(entry.getValue(), paramsAttributes.get(entry.getKey()))).collect(Collectors.toList()));
}
Also used : Fragment(io.knotx.dataobjects.Fragment) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) List(java.util.List) Attribute(org.jsoup.nodes.Attribute) ServiceEntry(io.knotx.knot.service.service.ServiceEntry) Map(java.util.Map) Document(org.jsoup.nodes.Document) Element(org.jsoup.nodes.Element) Observable(io.reactivex.Observable) JsonObject(io.vertx.core.json.JsonObject) ServiceAttributeUtil(io.knotx.knot.service.service.ServiceAttributeUtil) Jsoup(org.jsoup.Jsoup) Attribute(org.jsoup.nodes.Attribute) Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) ServiceEntry(io.knotx.knot.service.service.ServiceEntry)

Example 4 with Fragment

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

the class ActionKnotProxyVerticleTest method callGetWithActionFragmentWithoutIdentifier_expectResponseOkWithOneFragmentChanges.

@Test
@KnotxConfiguration("knotx-test.json")
public void callGetWithActionFragmentWithoutIdentifier_expectResponseOkWithOneFragmentChanges(TestContext context) throws Exception {
    KnotContext knotContext = createKnotContext("fragment_form_no_identifier_in.txt");
    String expectedFragmentHtml = FileReader.readText("fragment_form_no_identifier_out.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(fragments.size(), 1);
        context.assertEquals(clean(expectedFragmentHtml), clean(fragments.get(0).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 5 with Fragment

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

the class HtmlFragmentSplitter method split.

@Override
public List<Fragment> split(String html) {
    List<Fragment> fragments = Lists.newLinkedList();
    if (snippetPatterns.getAnySnippetPattern().matcher(html).matches()) {
        Matcher matcher = snippetPatterns.getSnippetPattern().matcher(html);
        int idx = 0;
        while (matcher.find()) {
            MatchResult matchResult = matcher.toMatchResult();
            if (idx < matchResult.start()) {
                fragments.add(toRaw(html, idx, matchResult.start()));
            }
            fragments.add(toSnippet(matchResult.group(1).intern().split(FragmentConstants.FRAGMENT_IDENTIFIERS_SEPARATOR), html, matchResult.start(), matchResult.end()));
            idx = matchResult.end();
        }
        if (idx < html.length()) {
            fragments.add(toRaw(html, idx, html.length()));
        }
    } else {
        fragments.add(toRaw(html, 0, html.length()));
    }
    return fragments;
}
Also used : Matcher(java.util.regex.Matcher) Fragment(io.knotx.dataobjects.Fragment) MatchResult(java.util.regex.MatchResult)

Aggregations

Fragment (io.knotx.dataobjects.Fragment)7 KnotContext (io.knotx.dataobjects.KnotContext)3 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)3 Test (org.junit.Test)3 Coercion (com.googlecode.zohhak.api.Coercion)1 SnippetPatterns (io.knotx.fragments.SnippetPatterns)1 ServiceAttributeUtil (io.knotx.knot.service.service.ServiceAttributeUtil)1 ServiceEntry (io.knotx.knot.service.service.ServiceEntry)1 Observable (io.reactivex.Observable)1 JsonObject (io.vertx.core.json.JsonObject)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 MatchResult (java.util.regex.MatchResult)1 Matcher (java.util.regex.Matcher)1 Collectors (java.util.stream.Collectors)1 Jsoup (org.jsoup.Jsoup)1 Attribute (org.jsoup.nodes.Attribute)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1