public static Builder builder(String key) { return new Builder(key); }
public Page build() { if (key == null || key.isEmpty()) { throw new IllegalArgumentException("Key must not be empty"); } if (name == null || name.isEmpty()) { throw new IllegalArgumentException("Name must be defined and not empty"); } if (qualifiers.length > 0 && !COMPONENT.equals(scope)) { throw new IllegalArgumentException(format("The scope must be '%s' when component qualifiers are provided", COMPONENT)); } if (qualifiers.length == 0 && COMPONENT.equals(scope)) { qualifiers = Qualifier.values(); } return new Page(this); } }
@Test public void ordered_by_name() { underTest .addPage(Page.builder("fake/K1").setName("N2").build()) .addPage(Page.builder("fake/K2").setName("N3").build()) .addPage(Page.builder("fake/K3").setName("N1").build()); Collection<Page> result = underTest.getPages(); assertThat(result).extracting(Page::getKey, Page::getName) .containsOnly( tuple("fake/K3", "N1"), tuple("fake/K1", "N2"), tuple("fake/K2", "N3")); }
@Test public void test_page_definition() { PageDefinition underTest = context -> context.addPage(Page.builder("my_plugin/my_page").setName("My Page").build()); Context context = new Context(); underTest.define(context); assertThat(context.getPages()).extracting(Page::getKey).contains("my_plugin/my_page"); }
private Page[] createPages() { Page page1 = Page.builder("my_plugin/first_page") .setName("First Page") .setScope(COMPONENT) .setComponentQualifiers(Qualifier.PROJECT) .build(); Page page2 = Page.builder("my_plugin/second_page") .setName("Second Page") .setScope(COMPONENT) .setComponentQualifiers(Qualifier.PROJECT) .build(); Page adminPage = Page.builder("my_plugin/admin_page") .setName("Admin Page") .setScope(COMPONENT) .setComponentQualifiers(Qualifier.PROJECT) .setAdmin(true) .build(); return new Page[] {page1, page2, adminPage}; }
@Test public void get_organization_admin_pages() { PageDefinition plugin = context -> context .addPage(Page.builder("my_plugin/O1").setName("O1").setScope(ORGANIZATION).build()) .addPage(Page.builder("my_plugin/O2").setName("O2").setScope(ORGANIZATION).setAdmin(true).build()); underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{plugin}); underTest.start(); List<Page> result = underTest.getOrganizationPages(true); assertThat(result) .extracting(Page::getKey) .containsExactly("my_plugin/O2"); }
@Test public void all_qualifiers_when_component_page() { Page result = underTest.setScope(COMPONENT).build(); assertThat(result.getComponentQualifiers()).containsOnly(Qualifier.values()); }
private void checkCoreExtensionExists(Page page) { String coreExtensionName = page.getPluginKey(); checkState(coreExtensionRepository.isInstalled(coreExtensionName), "Page '%s' references Core Extension '%s' which is not installed", page.getName(), coreExtensionName); }
@Test public void filter_pages_without_qualifier() { PageDefinition plugin = context -> context .addPage(Page.builder("my_plugin/K1").setName("N1").build()) .addPage(Page.builder("my_plugin/K2").setName("N2").build()) .addPage(Page.builder("my_plugin/K3").setName("N3").build()); underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{plugin}); underTest.start(); List<Page> result = underTest.getGlobalPages(false); assertThat(result) .extracting(Page::getKey) .containsExactly("my_plugin/K1", "my_plugin/K2", "my_plugin/K3"); }
private Page[] createPages() { Page page = Page.builder("my_plugin/page").setName("My Plugin Page").build(); Page anotherPage = Page.builder("another_plugin/page").setName("My Another Page").build(); Page adminPage = Page.builder("my_plugin/admin_page").setName("Admin Page").setAdmin(true).build(); return new Page[] {page, anotherPage, adminPage}; } }
@Test public void fail_if_page_with_unknown_plugin() { PageDefinition governance = context -> context.addPage(Page.builder("governance/my_key").setName("N1").build()); PageDefinition plugin42 = context -> context.addPage(Page.builder("plugin_42/my_key").setName("N2").build()); pluginRepository = mock(PluginRepository.class); when(pluginRepository.hasPlugin("governance")).thenReturn(true); underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{governance, plugin42}); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Page 'N2' references plugin 'plugin_42' that does not exist"); underTest.start(); } }
@Test public void fail_if_key_does_not_contain_a_slash() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Page key [project_dump] is not valid. It must contain a single slash, for example my_plugin/my_page."); Page.builder("project_dump").setName("Project Dump").build(); }
@Test public void fail_if_key_contains_more_than_one_slash() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Page key [governance/project/dump] is not valid. It must contain a single slash, for example my_plugin/my_page."); Page.builder("governance/project/dump").setName("Project Dump").build(); } }