Component { public Optional<String> renderPage(String pageUri, Model model, Lookup lookup, RequestLookup requestLookup, API api) { Page servingPage = null; for (Page page : pages) { Optional<Map<String, String>> pathParams = page.getUriPatten().match(pageUri); if (pathParams.isPresent()) { requestLookup.setPathParams(pathParams.get()); servingPage = page; break; } } if (servingPage == null) { return Optional.<String>empty(); } requestLookup.tracker().start(this); String html = servingPage.render(model, lookup, requestLookup, api); requestLookup.tracker().finish(); return Optional.of(html); } Component(String name, String version, String contextPath,
                     SortedSet<Page> pages, Set<Fragment> fragments, Set<Layout> layouts,
                     Set<Component> dependencies, String path); String getName(); String getVersion(); String getContextPath(); SortedSet<Page> getPages(); Set<Fragment> getFragments(); Set<Layout> getLayouts(); Set<Component> getDependencies(); String getPath(); Optional<String> renderPage(String pageUri, Model model, Lookup lookup, RequestLookup requestLookup,
                                       API api); boolean hasPage(String pageUri); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); static final String ROOT_COMPONENT_CONTEXT_PATH; }
@Test public void testRenderExistingPage() { Page p1 = createPage("/test/page/one", "Hello world from test page one!"); Page p2 = createPage("/test/page/two", "Hello world from test page two!"); Component component = new Component("test.component-name", null, null, ImmutableSortedSet.of(p1, p2), emptySet(), emptySet(), emptySet(), null); Optional<String> output = component.renderPage("/test/page/one", null, null, createRequestLookup(), null); Assert.assertEquals(output.get(), "Hello world from test page one!"); }
@Test public void testRenderExistingPageWithWildcard() { Page p1 = createPage("/test/page/{wildcard}/one", "Hello world from test page one!"); Page p2 = createPage("/test/page/no-wildcard/two", "Hello world from test page two!"); Component component = new Component("test.component-name", null, null, ImmutableSortedSet.of(p1, p2), emptySet(), emptySet(), emptySet(), null); Optional<String> output = component.renderPage("/test/page/wildcard-value/one", null, null, createRequestLookup(), null); Assert.assertEquals(output.get(), "Hello world from test page one!"); }
@Test public void testRenderNonExistingPage() { Page p1 = createPage("/test/page/one", null); Page p2 = createPage("/test/page/two", null); Component component = new Component("test.component-name", null, null, ImmutableSortedSet.of(p1, p2), emptySet(), emptySet(), emptySet(), null); Optional<String> output = component.renderPage("/test/page/three", null, null, createRequestLookup(), null); Assert.assertFalse(output.isPresent()); }