AssetService { public Asset save(T component, final Asset asset) { checkArgument(isNotEmpty(asset.getName()), ASSET_URL_IS_REQUIRED); checkArgument(asset.getType() != null, ASSET_TYPE_IS_REQUIRED); if (asset.getId() != null) { Iterables.<Asset>find(component.getAssets(), new Predicate<Asset>() { @Override public boolean apply(Asset element) { return asset.getId().equals(element.getId()); } }).setName(asset.getName()).setType(asset.getType()).setActive(asset.isActive()); } else { asset.setId(randomUUID().toString()); asset.setOrder(getNextOrder(component)); component.getAssets().add(asset); } repository.updateLastUpdateAndSave(component); return asset; } AssetService(Repository<T> repository, AssetRepository<T> assetRepository, AssetImporter<T> assetImporter, JacksonObjectMapper mapper); Asset upload(MultipartFile file, T component, String type); Path findAssetPath(String id, String filename, String type); Asset save(T component, final Asset asset); Asset save(T component, Asset asset, byte[] content); String getAssetContent(T component, Asset asset); void duplicateAsset(Path artifactSourcePath, Path artifactTargetPath, String sourceArtifactId, String targetArtifactId); void delete(T component, final String assetId); Asset changeAssetOrderInComponent(T component, String assetId, OrderType ordering); void changeAssetStateInPreviewable(T component, String assetId, boolean active); void loadDefaultAssets(T content); static final String ASSET_TYPE_IS_REQUIRED; static final String ASSET_URL_IS_REQUIRED; static final String ASSET_ID_IS_REQUIRED; }
@Test public void should_return_error_when_adding_asset_with_name_null() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(is("Asset URL is required")); assetService.save(aPage().withId("page-id").build(), anAsset().withName(null).build()); }
@Test public void should_return_error_when_adding_asset_with_name_empty() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(is("Asset URL is required")); assetService.save(aPage().withId("page-id").build(), anAsset().withName("").build()); }
@Test public void should_return_error_when_adding_asset_with_type_invalid() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(is("Asset type is required")); assetService.save(aPage().withId("page-id").build(), anAsset().withName("http: }
@Test public void should_save_new_asset_and_populate_its_id() throws Exception { Page page = aPage().build(); Asset asset = assetService.save(page, anAsset().withName("http: verify(repository).updateLastUpdateAndSave(page); assertThat(page.getAssets()).contains(asset); assertThat(asset.getId()).isNotNull(); }
@Test public void should_compute_order_while_saving_a_new_asset() throws Exception { Page page = aPage().build(); Asset firstAsset = assetService.save(page, anAsset().withName("http: Asset secondtAsset = assetService.save(page, anAsset().withName("http: assertThat(firstAsset.getOrder()).isEqualTo(1); assertThat(secondtAsset.getOrder()).isEqualTo(2); }
@Test public void should_update_existing_local_asset() throws Exception { Asset existingAsset = anAsset().withId("existingAsset").withName("http: Asset updatedAsset = anAsset().withId("existingAsset").withName("http: Page page = aPage().withAsset(existingAsset).build(); assetService.save(page, updatedAsset); verify(repository).updateLastUpdateAndSave(page); assertThat(page.getAssets().iterator().next()).isEqualTo(updatedAsset); }
@Test public void should_return_error_when_error_onsave() throws Exception { expectedException.expect(RepositoryException.class); Page page = aPage().build(); doThrow(RepositoryException.class).when(repository).updateLastUpdateAndSave(page); assetService.save(page, anAsset().withName("http: }