AssetService { public Asset changeAssetOrderInComponent(T component, String assetId, OrderType ordering) { checkArgument(isNotEmpty(assetId), ASSET_ID_IS_REQUIRED); List<Asset> assets = Ordering.from(Asset.getComparatorByOrder()).sortedCopy(component.getAssets()); Asset previous = null; Asset actual = null; int i = 0; int size = assets.size(); for (Asset a : assets) { if (actual != null) { if (OrderType.INCREMENT.equals(ordering)) { a.setOrder(a.getOrder() - 1); } break; } if (assetId.equals(a.getId())) { actual = a; if ((OrderType.DECREMENT.equals(ordering) && previous == null) || (OrderType.INCREMENT.equals(ordering) && i == size - 1)) { break; } a.setOrder(OrderType.DECREMENT.equals(ordering) ? a.getOrder() - 1 : a.getOrder() + 1); if (previous != null && OrderType.DECREMENT.equals(ordering)) { previous.setOrder(previous.getOrder() + 1); } } else { previous = a; } i++; } repository.updateLastUpdateAndSave(component); return actual; } 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_throw_IllegalArgument_when_sorting_asset_component_with_no_name() throws Exception { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(is("Asset id is required")); assetService.changeAssetOrderInComponent(aPage().build(), null, DECREMENT); }
@Test public void should_increment_asset_order_in_component() throws Exception { Asset[] assets = getSortedAssets(); Page page = aPage().withId("page-id").withName("my-page").withAsset(assets).build(); when(repository.get("page-id")).thenReturn(page); assets[1].setComponentId("page-id"); Asset assetReturned = assetService.changeAssetOrderInComponent(page, "asset2", INCREMENT); assertThat(assetReturned.getName()).isEqualTo("asset2"); assertThat(assets[0].getOrder()).isEqualTo(1); assertThat(assets[1].getOrder()).isEqualTo(3); assertThat(assets[2].getOrder()).isEqualTo(2); verify(repository).updateLastUpdateAndSave(page); }
@Test public void should_decrement_asset_order_in_component() throws Exception { Asset[] assets = getSortedAssets(); Page page = aPage().withId("page-id").withName("my-page").withAsset(assets).build(); when(repository.get("page-id")).thenReturn(page); assets[1].setComponentId("page-id"); Asset assetReturned = assetService.changeAssetOrderInComponent(page, "asset2", DECREMENT); assertThat(assetReturned.getName()).isEqualTo("asset2"); assertThat(assets[0].getOrder()).isEqualTo(2); assertThat(assets[1].getOrder()).isEqualTo(1); assertThat(assets[2].getOrder()).isEqualTo(3); verify(repository).updateLastUpdateAndSave(page); }
@Test public void should_not_increment_asset_order_in_component_when_asset_is_the_last() throws Exception { Asset[] assets = getSortedAssets(); Page page = aPage().withId("page-id").withName("my-page").withAsset(assets).build(); when(repository.get("page-id")).thenReturn(page); assets[2].setComponentId("page-id"); Asset assetReturned = assetService.changeAssetOrderInComponent(page, "asset3", INCREMENT); assertThat(assetReturned.getName()).isEqualTo("asset3"); assertThat(assets[0].getOrder()).isEqualTo(1); assertThat(assets[1].getOrder()).isEqualTo(2); assertThat(assets[2].getOrder()).isEqualTo(3); verify(repository).updateLastUpdateAndSave(page); }
@Test public void should_decrement_asset_order_in_component_when_asset_is_the_last() throws Exception { Asset[] assets = getSortedAssets(); Page page = aPage().withId("page-id").withName("my-page").withAsset(assets).build(); when(repository.get("page-id")).thenReturn(page); assets[2].setComponentId("page-id"); Asset assetReturned = assetService.changeAssetOrderInComponent(page, "asset3", DECREMENT); assertThat(assetReturned.getName()).isEqualTo("asset3"); assertThat(assets[0].getOrder()).isEqualTo(1); assertThat(assets[1].getOrder()).isEqualTo(3); assertThat(assets[2].getOrder()).isEqualTo(2); verify(repository).updateLastUpdateAndSave(page); }
@Test public void should_not_decrement_asset_order_in_component_when_asset_is_the_first() throws Exception { Asset[] assets = getSortedAssets(); Page page = aPage().withId("page-id").withName("my-page").withAsset(assets).build(); when(repository.get("page-id")).thenReturn(page); assets[0].setComponentId("page-id"); Asset assetReturned = assetService.changeAssetOrderInComponent(page, "asset1", DECREMENT); assertThat(assetReturned.getName()).isEqualTo("asset1"); assertThat(assets[0].getOrder()).isEqualTo(1); assertThat(assets[1].getOrder()).isEqualTo(2); assertThat(assets[2].getOrder()).isEqualTo(3); verify(repository).updateLastUpdateAndSave(page); }
@Test public void should_increment_asset_order_in_component_when_asset_is_the_first() throws Exception { Asset[] assets = getSortedAssets(); Page page = aPage().withId("page-id").withName("my-page").withAsset(assets).build(); when(repository.get("page-id")).thenReturn(page); assets[0].setComponentId("page-id"); Asset assetReturned = assetService.changeAssetOrderInComponent(page, "asset1", INCREMENT); assertThat(assetReturned.getName()).isEqualTo("asset1"); assertThat(assets[0].getOrder()).isEqualTo(2); assertThat(assets[1].getOrder()).isEqualTo(1); assertThat(assets[2].getOrder()).isEqualTo(3); verify(repository).updateLastUpdateAndSave(page); }