AssetRepository { public void refreshAssets(final T component) { component.addAssets(newHashSet(concat(transform(newArrayList(AssetType.values()), new Function<AssetType, List<Asset>>() { @Override public List<Asset> apply(AssetType type) { try { Path assetTypePath = repository.resolvePath(component.getId()).resolve(Paths.get("assets", type.getPrefix())); if (exists(assetTypePath)) { return findAssetInPathWhithoutComponentId(component, type, assetTypePath); } } catch (IOException e) { throw new RepositoryException(format("Failed to initialized assets for %s %s", repository.getComponentName(), component.getId()), e); } return emptyList(); } })))); repository.updateLastUpdateAndSave(component); } @Inject  AssetRepository(Repository<T> repository, BeanValidator validator); void save(Asset asset, byte[] content); void save(String componentId, Asset asset, byte[] content); void delete(Asset asset); byte[] readAllBytes(Asset asset); byte[] readAllBytes(String componentId, Asset asset); Path findAssetPath(String componentId, final String filename, final AssetType assetType); List<Asset> findAssetInPath(T component, AssetType type, Path directory); List<Asset> findAssetInPathWhithoutComponentId(T component, AssetType type, Path directory); void refreshAssets(final T component); static final String COMPONENT_ID_REQUIRED; }
@Test public void should_refresh_component_assets_from_disk() throws Exception { Page page = aPage().withId("page-id") .withAsset(anAsset().withName("existing-asset.js")).build(); temporaryFolder.newFolder("page-id", "assets", "css"); write(pagesPath.resolve("page-id/assets/css/file1.css"), "<style>.maclass1{}</style>".getBytes()); write(pagesPath.resolve("page-id/assets/css/file2.css"), "<style>.maclass2{}</style>".getBytes()); when(pageRepository.resolvePath("page-id")).thenReturn(pagesPath.resolve("page-id")); assetRepository.refreshAssets(page); assertThat(page.getAssets()).hasSize(3); assertThat(page.getAssets()).extracting("name").contains("file1.css", "file2.css", "existing-asset.js"); verify(pageRepository).updateLastUpdateAndSave(page); }