AssetVisitor implements ElementVisitor<Set<Asset>> { @Override public Set<Asset> visit(Container container) { Set<Asset> assets = new HashSet<>(); assets.addAll(linkAssetToComponent(container.getId())); assets.addAll(visitRows(container.getRows())); return assets; } AssetVisitor(WidgetRepository widgetRepository); @Override Set<Asset> visit(Container container); @Override Set<Asset> visit(FormContainer formContainer); @Override Set<Asset> visit(TabsContainer tabsContainer); @Override Set<Asset> visit(TabContainer tabContainer); @Override Set<Asset> visit(Component component); Set<Asset> visit(Widget widget); @Override Set<Asset> visit(ModalContainer modalContainer); @Override Set<Asset> visit(P previewable);  }
@Test public void should_return_empty_set_when_components_use_no_asset() throws Exception { Component component = mockComponentFor(aWidget(), null); Page page = aPage().with(component).build(); Set<Asset> assets = assetVisitor.visit(page); assertThat(assets).isEmpty(); }
@Test public void should_return_list_of_asset_used_by_widgets() throws Exception { Component component = mockComponentFor( aWidget(), UUID.randomUUID().toString(), anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT), anAsset().withName("http: ); Page page = aPage().with(component).build(); Set<Asset> assets = assetVisitor.visit(page); assertThat(assets).extracting("name").containsOnlyOnce("myfile.js", "http: }
@Test public void should_return_list_of_asset_used_by_one_page() throws Exception { Page page = aPage().withAsset(anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT).build()).build(); Set<Asset> assets = assetVisitor.visit(page); assertThat(assets).extracting("name").containsOnlyOnce("myfile.js"); }
@Test public void should_return_list_of_distinct_asset_used_by_page_and_widgets() throws Exception { Component component = mockComponentFor( aWidget(), "id1", anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT), anAsset().withName("http: ); Page page = aPage() .with(component) .withAsset(anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT).build()) .build(); Set<Asset> assets = assetVisitor.visit(page); assertThat(assets).extracting("name").contains("myfile.js", "myfile.js", "http: assertThat(assets).extracting("componentId").contains(null, "id1", "id1"); }
@Test public void should_return_list_of_asset_needed_by_widgets_in_container() throws Exception { Component component1 = mockComponentFor(aWidget(), "id1", anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT)); Component component2 = mockComponentFor(aWidget(), "id2", anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT)); when(widgetRepository.get("pbContainer")).thenReturn(aWidget().build()); Set<Asset> assets = assetVisitor.visit(aContainer().with(component1, component2).build()); assertThat(assets).extracting("name").containsExactly("myfile.js", "myfile.js"); assertThat(assets).extracting("componentId").contains("id2", "id1"); }
@Test public void should_return_list_of_asset_needed_by_widgets_in_container_who_have_an_asset() throws Exception { Component component1 = mockComponentFor(aWidget(), "id1", anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT)); Component component2 = mockComponentFor(aWidget(), "id2", anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT)); when(widgetRepository.get("pbContainer")).thenReturn(aWidget().assets(anAsset().withName("container.min.js").withType(AssetType.JAVASCRIPT)).build()); Set<Asset> assets = assetVisitor.visit(aContainer().with(component1, component2).build()); assertThat(assets).extracting("name").containsExactly("container.min.js", "myfile.js", "myfile.js"); assertThat(assets).extracting("componentId").contains("id2", "id1"); }
@Test public void should_return_list_of_asset_needed_by_widgets_in_formcontainer() throws Exception { Component component1 = mockComponentFor(aWidget(), UUID.randomUUID().toString(), anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT)); Component component2 = mockComponentFor(aWidget(), UUID.randomUUID().toString(), anAsset().withName("myfile.css").withType(AssetType.CSS)); when(widgetRepository.get("pbFormContainer")).thenReturn(aWidget().build()); when(widgetRepository.get("pbContainer")).thenReturn(aWidget().build()); Set<Asset> assets = assetVisitor.visit(aFormContainer().with( aContainer().with(component1, component2)).build()); assertThat(assets).extracting("name").containsOnly("myfile.js", "myfile.css"); }
@Test public void should_return_list_of_asset_needed_by_widgets_in_tabscontainer_plus_uibootstrap_which_is_needed_by_tabscontainer() throws Exception { Component component1 = mockComponentFor(aWidget(), UUID.randomUUID().toString(), anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT)); Component component2 = mockComponentFor(aWidget(), UUID.randomUUID().toString(), anAsset().withName("myfile.css").withType(AssetType.CSS)); when(widgetRepository.get("pbTabsContainer")).thenReturn(aWidget().assets(anAsset().withName("bootstrap.min.js").withType(AssetType.JAVASCRIPT)).build()); when(widgetRepository.get("pbContainer")).thenReturn(aWidget().build()); Set<Asset> assets = assetVisitor.visit(aTabsContainer().with( aTabContainer().withId("Tab 1").with(aContainer().with(component1)), aTabContainer().withId("Tab 2").with(aContainer().with(component2))).build()); assertThat(assets).extracting("name").containsOnly("myfile.js", "myfile.css", "bootstrap.min.js"); assertThat(assets.iterator().next().getComponentId()).isNotEmpty(); }
@Test public void should_update_inactive_indicator_when_asset_is_inactive_in_a_page() throws Exception { Page page = aPage() .withAsset( anAsset().withId("assetUIID1").withName("myfile.js").withType(AssetType.JAVASCRIPT).build(), anAsset().withId("assetUIID2").withName("myfile.css").withType(AssetType.CSS).build() ) .withInactiveAsset("assetUIID2") .build(); Set<Asset> assets = assetVisitor.visit(page); assertThat(assets).extracting("name").contains("myfile.js", "myfile.css"); assertThat(assets).extracting("active").contains(true, false); }
@Test public void should_return_list_of_asset_needed_by_widgets_in_modal_container() throws Exception { Component component1 = mockComponentFor(aWidget(), UUID.randomUUID().toString(), anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT)); Component component2 = mockComponentFor(aWidget(), UUID.randomUUID().toString(), anAsset().withName("myfile.js").withType(AssetType.JAVASCRIPT)); when(widgetRepository.get("pbModalContainer")).thenReturn(aWidget().build()); when(widgetRepository.get("pbContainer")).thenReturn(aWidget().build()); Set<Asset> assets = assetVisitor.visit(aModalContainer().with(aContainer().with(component1, component2)).build()); assertThat(assets).extracting("name").containsExactly("myfile.js", "myfile.js"); }
@Test public void should_good_list_widget_asset__if_one_is_inactive() throws Exception { Component component = mockComponentFor( aWidget(), "id1", anAsset().withName("myfileBis.js").withType(AssetType.JAVASCRIPT), anAsset().withName("http: ); Page page = aPage().with(component) .withAsset( anAsset().withId("assetUIID1").withName("myfile.js").withType(AssetType.JAVASCRIPT).build(), anAsset().withId("assetUIID2").withName("myfile.css").withType(AssetType.CSS).build() ) .withInactiveAsset("assetUIID2", "myfileBis.js") .build(); Set<Asset> assets = assetVisitor.visit(page); assertThat(assets).extracting("name").contains("myfile.js", "myfile.css", "myfileBis.js", "http: assertThat(assets).extracting("active").contains(true, false, true, false); }