AssetRepository { public void save(Asset asset, byte[] content) throws IOException { checkNotNull(asset.getComponentId(), COMPONENT_ID_REQUIRED); Path parent = resolveComponentPath(asset.getComponentId()); if (!exists(parent.resolve("assets").resolve(asset.getType().getPrefix()))) { createDirectories(parent.resolve("assets").resolve(asset.getType().getPrefix())); } write(resolveAssetPath(asset), content); } @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_save_asset() throws Exception { Page page = aPage().withId("page-id").build(); Asset asset = aFilledAsset(page); Path fileExpected = pagesPath.resolve("assets").resolve("js").resolve(asset.getName()); assertThat(fileExpected.toFile()).doesNotExist(); when(pageRepository.resolvePathFolder("page-id")).thenReturn(pagesPath); when(pageRepository.get(asset.getComponentId())).thenReturn(page); assetRepository.save(asset, "My example with special characters réè@# \ntest".getBytes(Charset.forName("UTF-8"))); assertThat(fileExpected.toFile()).exists(); assertThat(Files.readFirstLine(fileExpected.toFile(), Charset.forName("UTF-8"))).isEqualTo("My example with special characters réè@# "); }