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