VirtualAssetService { @Transactional public VirtualAsset createOrUpdateVirtualAsset(VirtualAsset virtualAsset) throws VirtualAssetBadRequestException { if (virtualAsset.getRepositoryId() == null) { throw new VirtualAssetBadRequestException("A repository id must be provided"); } logger.debug("Create or update virtual asset with path: {}", virtualAsset.getPath()); Asset asset = assetRepository.findByPathAndRepositoryId(virtualAsset.getPath(), virtualAsset.getRepositoryId()); if (asset != null && !asset.getVirtual()) { throw new VirtualAssetRequiredException("Standard asset can't be modify"); } else if (asset != null) { logger.debug("Virtual asset exists, update it"); if (virtualAsset.getDeleted() == null) { throw new VirtualAssetRequiredException("Missing deleted attribute for update"); } asset.setDeleted(virtualAsset.getDeleted()); asset = assetRepository.save(asset); } else { logger.debug("No existing virtual asset, create one"); Repository repository = repositoryRepository.findById(virtualAsset.getRepositoryId()).orElse(null); if (repository == null) { throw new VirtualAssetBadRequestException("A valid repository must be provided when creating an asset"); } asset = new Asset(); asset.setRepository(repository); asset.setPath(virtualAsset.getPath()); asset.setVirtual(Boolean.TRUE); logger.debug("Create a default AssetExtraction for virtual asset"); AssetExtraction createAssetExtraction = assetExtractionService.createAssetExtraction(asset, null); asset.setLastSuccessfulAssetExtraction(createAssetExtraction); asset = assetRepository.save(asset); } virtualAsset.setId(asset.getId()); return virtualAsset; }  @Transactional VirtualAsset createOrUpdateVirtualAsset(VirtualAsset virtualAsset); List<VirtualAssetTextUnit> getTextUnits(long assetId, Boolean doNotTranslateFilter); @Transactional List<VirtualAssetTextUnit> getLocalizedTextUnits(
            long assetId,
            long localeId,
            InheritanceMode inheritanceMode); PollableFuture<Void> addTextUnits(long assetId, List<VirtualAssetTextUnit> virtualAssetTextUnits); PollableFuture<Void> replaceTextUnits(long assetId, List<VirtualAssetTextUnit> virtualAssetTextUnits); PollableFuture<Void> importLocalizedTextUnits(
            long assetId,
            long localeId,
            List<VirtualAssetTextUnit> textUnitForVirtualAssets); @Transactional void deleteTextUnit(Long assetId, String name); void deleteTextUnits(Asset asset, List<String> names); TMTextUnitVariant addTextUnitVariant(long assetId, long localeId, String name, String content, String comment);  }
@Test public void testCreateVirtualAsset() throws Exception { Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("testCreateVirtualAsset")); VirtualAsset virtualAsset = new VirtualAsset(); virtualAsset.setRepositoryId(repository.getId()); virtualAsset.setPath("default"); virtualAssetService.createOrUpdateVirtualAsset(virtualAsset); Asset asset = assetRepository.findByPathAndRepositoryId("default", repository.getId()); assertEquals("default", asset.getPath()); assertEquals(false, asset.getDeleted()); }
@Test public void testUpdateVirtualAsset() throws Exception { Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("testUpdateVirtualAsset")); VirtualAsset virtualAsset = new VirtualAsset(); virtualAsset.setRepositoryId(repository.getId()); virtualAsset.setPath("default"); virtualAssetService.createOrUpdateVirtualAsset(virtualAsset); Asset asset = assetRepository.findByPathAndRepositoryId("default", repository.getId()); assertEquals("default", asset.getPath()); assertEquals(false, asset.getDeleted()); virtualAsset.setDeleted(Boolean.TRUE); virtualAssetService.createOrUpdateVirtualAsset(virtualAsset); asset = assetRepository.findByPathAndRepositoryId("default", repository.getId()); assertEquals("default", asset.getPath()); assertEquals(true, asset.getDeleted()); }
@Test(expected = VirtualAssetRequiredException.class) public void testCantUpdateAssetAsVirtual() throws Exception { Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("testCantUpdateAssetAsVirtual")); assetService.createAssetWithContent(repository.getId(), "default", ""); VirtualAsset virtualAsset = new VirtualAsset(); virtualAsset.setRepositoryId(repository.getId()); virtualAsset.setPath("default"); virtualAssetService.createOrUpdateVirtualAsset(virtualAsset); }