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()));
}
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()));
}
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()));
}
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()));
}
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;
}
Aggregations