Search in sources :

Example 1 with ServiceEntry

use of io.knotx.knot.service.service.ServiceEntry 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 2 with ServiceEntry

use of io.knotx.knot.service.service.ServiceEntry in project knotx by Cognifide.

the class FragmentContextTest method from_whenFragmentContainsOneService_expectFragmentContextWithExtractedParamsParams.

@TestWith({ "snippet_one_service_no_params.txt;{}", "snippet_one_service_invalid_params_bound.txt;{}", "snippet_one_service_one_param.txt;{\"path\":\"/overridden/path\"}", "snippet_one_service_many_params.txt;{\"path\":\"/overridden/path\",\"anotherParam\":\"someValue\"}" })
public void from_whenFragmentContainsOneService_expectFragmentContextWithExtractedParamsParams(Fragment fragment, String expectedParameters) throws Exception {
    final FragmentContext fragmentContext = FragmentContext.from(fragment);
    final ServiceEntry serviceEntry = fragmentContext.services.get(0);
    assertThat(serviceEntry.getParams().toString(), sameJSONAs(expectedParameters));
}
Also used : ServiceEntry(io.knotx.knot.service.service.ServiceEntry) TestWith(com.googlecode.zohhak.api.TestWith)

Aggregations

ServiceEntry (io.knotx.knot.service.service.ServiceEntry)2 TestWith (com.googlecode.zohhak.api.TestWith)1 Fragment (io.knotx.dataobjects.Fragment)1 ServiceAttributeUtil (io.knotx.knot.service.service.ServiceAttributeUtil)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 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