AssetRepository { public Path findAssetPath(String componentId, final String filename, final AssetType assetType) throws IOException { checkNotNull(filename, "Filename is required"); checkNotNull(assetType, format("Asset type is required (filename: %s)", filename)); T component = repository.get(componentId); Asset existingAsset = find(component.getAssets(), asset -> filename.equals(asset.getName()) && assetType.equals(asset.getType())); existingAsset.setComponentId(componentId); if (existingAsset.isExternal()) { throw new NotAllowedException("We can't load an external asset. Use the link " + existingAsset.getName()); } return resolveExistingAssetPath(existingAsset); } @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_find_asset_path_used_by_a_component() throws Exception { Page page = aPage().withId("page-id").build(); Asset asset = aFilledAsset(page); System.out.println(asset.getName()); pagesPath.resolve("assets").resolve("js").resolve(asset.getName()); createDirectories(pagesPath.resolve("assets").resolve("js")); temporaryFolder.newFilePath("assets/js/" + asset.getName()); when(pageRepository.resolvePathFolder("page-id")).thenReturn(pagesPath); when(pageRepository.get("page-id")).thenReturn( aPage().withAsset(asset).build()); assertThat(assetRepository.findAssetPath("page-id", "myasset.js", AssetType.JAVASCRIPT).toFile()).exists(); }
@Test public void should_throw_NotAllowedException_when_find_external_asset() throws Exception { exception.expect(NotAllowedException.class); exception.expectMessage("We can't load an external asset. Use the link http: Page page = aPage().withId("page-id").build(); Asset asset = aFilledAsset(page); asset.setName("http: asset.setExternal(true); when(pageRepository.get("page-id")).thenReturn( aPage().withAsset(asset).build()); assetRepository.findAssetPath( "page-id", "http: }
@Test public void should_throw_NullPointerException_when_find_asset_with_filename_null() throws Exception { exception.expect(NullPointerException.class); exception.expectMessage("Filename is required"); assetRepository.findAssetPath("page-id", null, AssetType.JAVASCRIPT); }
@Test public void should_throw_NullPointerException_when_find_asset_path_with_type_null() throws Exception { exception.expect(NullPointerException.class); exception.expectMessage("Asset type is required"); assetRepository.findAssetPath("page-id", "myfile.js", null); }
@Test(expected = NoSuchElementException.class) public void should_throw_NoSuchElementException_when_finding_inexistant_asset() throws Exception { Page page = aPage().withId("page-id").build(); Asset asset = aFilledAsset(page); pagesPath.resolve(asset.getName()); temporaryFolder.newFilePath(asset.getName()); when(pageRepository.resolvePathFolder("page-id")).thenReturn(pagesPath); when(pageRepository.get("page-id")).thenReturn(aPage().withAsset(asset).build()); assetRepository.findAssetPath("page-id", "inexistant.js", AssetType.JAVASCRIPT); }