AssetService { public Asset upload(MultipartFile file, T component, String type) { AssetType assetType = AssetType.getAsset(type); checkArgument(file != null && !file.isEmpty(), "Part named [file] is needed to successfully import a component"); checkArgument(assetType != null, ASSET_TYPE_IS_REQUIRED); try { if (AssetType.JSON.getPrefix().equals(type)) { checkWellFormedJson(file.getBytes()); } final Asset asset = new Asset() .setName(getOriginalFilename(file.getOriginalFilename())) .setType(assetType) .setOrder(getNextOrder(component)); Optional<Asset> existingAsset = Iterables.<Asset>tryFind(component.getAssets(), new Predicate<Asset>() { @Override public boolean apply(Asset element) { return asset.equalsWithoutComponentId(element); } }); if (existingAsset.isPresent()) { asset.setId(existingAsset.get().getId()); } return save(component, asset, file.getBytes()); } catch (IOException e) { logger.error("Asset creation" + e); throw new ServerImportException( format("Error while uploading asset in %s [%s]", file.getOriginalFilename(), repository.getComponentName()), e); } } 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_uploading_file_null() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(is("Part named [file] is needed to successfully import a component")); assetService.upload(null, aPage().build(), "js"); }
@Test public void should_return_error_when_uploading_file_empty() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(is("Part named [file] is needed to successfully import a component")); MockMultipartFile file = new MockMultipartFile("file", "myfile.js", "application/js", "".getBytes()); assetService.upload(file, aPage().build(), "js"); }
@Test public void should_return_error_when_uploadind_type_invalid() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(is("Part named [file] is needed to successfully import a component")); MockMultipartFile file = new MockMultipartFile("file", "myfile.js", "application/js", "".getBytes()); assetService.upload(file, aPage().build(), "INVALID"); }
@Test public void should_return_error_when_uploading_with_error_onsave() throws Exception { expectedException.expect(RepositoryException.class); expectedException.expectMessage(is("Error while saving internal asset")); Page page = aPage().build(); MockMultipartFile file = new MockMultipartFile("file.js", "myfile.inv", "application/javascript", "function(){}".getBytes()); doThrow(IOException.class).when(repository).updateLastUpdateAndSave(page); assetService.upload(file, page, "js"); }
@Test(expected = MalformedJsonException.class) public void should_check_that_json_is_well_formed_while_uploading_a_json_asset() throws Exception { MockMultipartFile file = new MockMultipartFile("asset.json", "asset.json", "application/javascript", "{ not json }".getBytes()); assetService.upload(file, aPage().build(), "json"); }