AssetService { public void changeAssetStateInPreviewable(T component, String assetId, boolean active) { checkArgument(isNotEmpty(assetId), ASSET_ID_IS_REQUIRED); if (component instanceof Previewable) { Previewable previewable = (Previewable) component; if (previewable.getInactiveAssets().contains(assetId) && active) { previewable.getInactiveAssets().remove(assetId); repository.updateLastUpdateAndSave(component); } else if (!previewable.getInactiveAssets().contains(assetId) && !active) { previewable.getInactiveAssets().add(assetId); repository.updateLastUpdateAndSave(component); } } } 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_not_change_asset_state_in_previewable_when_asset_is_already_inactive() throws Exception { Page page = aPage() .withId("page-id") .withName("my-page") .withInactiveAsset("assetUIID") .build(); when(repository.get("page-id")).thenReturn(page); Asset assetSent = anAsset().withId("assetUIID").withComponentId("page-id").withName("myasset.js").build(); assetService.changeAssetStateInPreviewable(page, "assetUIID", false); assertThat(page.getInactiveAssets()).isNotEmpty().contains("assetUIID"); }
@Test public void should_change_asset_state_in_previewable_when_asset_state_is_inactive() throws Exception { Page page = aPage() .withId("page-id") .withName("my-page") .build(); when(repository.get("page-id")).thenReturn(page); Asset assetSent = anAsset().withId("assetUIID").withComponentId("page-id").withName("myasset.js").build(); assetService.changeAssetStateInPreviewable(page, "assetUIID", false); assertThat(page.getInactiveAssets()).isNotEmpty().contains("assetUIID"); verify(repository).updateLastUpdateAndSave(page); }
@Test public void should_reactive_asset_in_previable_when_asset_is_inactive_in_previewable() throws Exception { Page page = aPage() .withId("page-id") .withName("my-page") .withInactiveAsset("assetUIID") .build(); when(repository.get("page-id")).thenReturn(page); Asset assetSent = anAsset().withId("assetUIID").withComponentId("page-id").withName("myasset.js").build(); assetService.changeAssetStateInPreviewable(page, "assetUIID", true); assertThat(page.getInactiveAssets()).isEmpty(); verify(repository).updateLastUpdateAndSave(page); }