From 4b9f65d40b593d4ee874dcbd715455332547e839 Mon Sep 17 00:00:00 2001 From: jensp Date: Wed, 2 Nov 2016 08:17:44 +0000 Subject: [PATCH] CCM NG/ccm-cms: AttachmentListManager finished git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4427 8810af33-2d31-482b-a856-94f89814c4df --- .../contentsection/AttachmentListManager.java | 2 +- .../contentsection/ItemAttachmentManager.java | 181 ++++++++-- .../ItemAttachmentManagerTest.java | 324 ++++++++++++------ .../after-move-down.xml | 23 +- .../after-move-up.xml | 23 +- 5 files changed, 411 insertions(+), 142 deletions(-) diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentListManager.java b/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentListManager.java index 87f7bc1f7..6ab9cff31 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentListManager.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentListManager.java @@ -302,7 +302,7 @@ public class AttachmentListManager { * * @param attachmentList The list to move. */ - @Transactional + @Transactional(Transactional.TxType.REQUIRED) @AuthorizationRequired public void moveUp( @RequiresPrivilege(ItemPrivileges.EDIT) diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ItemAttachmentManager.java b/ccm-cms/src/main/java/org/librecms/contentsection/ItemAttachmentManager.java index f4cc5840a..249d31562 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ItemAttachmentManager.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ItemAttachmentManager.java @@ -19,11 +19,15 @@ package org.librecms.contentsection; import java.util.List; + import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.RequiresPrivilege; import org.librecms.contentsection.privileges.ItemPrivileges; +import java.util.Collections; +import java.util.Optional; import java.util.UUID; +import java.util.stream.Collectors; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; @@ -51,15 +55,15 @@ public class ItemAttachmentManager { /** * Adds the provided {@link Asset} to the provided {@link AttachmentList}. * - * @param asset The {@link Asset} to add. + * @param asset The {@link Asset} to add. * @param attachmentList The attachment list to which the asset is added. */ @Transactional(Transactional.TxType.REQUIRED) @AuthorizationRequired public void attachAsset( - final Asset asset, - @RequiresPrivilege(ItemPrivileges.EDIT) - final AttachmentList attachmentList) { + final Asset asset, + @RequiresPrivilege(ItemPrivileges.EDIT) + final AttachmentList attachmentList) { if (asset == null) { throw new IllegalArgumentException("Can't attach asset null."); @@ -67,7 +71,7 @@ public class ItemAttachmentManager { if (attachmentList == null) { throw new IllegalArgumentException( - "Can't attach an asset to attachment list null."); + "Can't attach an asset to attachment list null."); } // For shared assets (we assume that every asset already in the database @@ -76,7 +80,7 @@ public class ItemAttachmentManager { saveNonSharedAsset(asset); } else { final TypedQuery countQuery = entityManager.createNamedQuery( - "ItemAttachment.countByAssetIdAndList", Long.class); + "ItemAttachment.countByAssetIdAndList", Long.class); countQuery.setParameter("asset", asset); countQuery.setParameter("attachmentList", attachmentList); @@ -111,14 +115,23 @@ public class ItemAttachmentManager { * {@link AttachmentList}. If the asset is a non shared asset the asset is * deleted. * - * @param asset The {@link Asset} to remove. + * @param asset The {@link Asset} to remove. * @param attachmentList The attachment list to which the asset is removed - * from. + * from. */ public void unattachAsset(final Asset asset, final AttachmentList attachmentList) { + if (asset == null) { + throw new IllegalArgumentException("Can't unattach null."); + } + + if (attachmentList == null) { + throw new IllegalArgumentException( + "Can't unattach an asset from list null."); + } + final TypedQuery countQuery = entityManager.createNamedQuery( - "ItemAttachment.countByAssetIdAndList", Long.class); + "ItemAttachment.countByAssetIdAndList", Long.class); countQuery.setParameter("asset", asset); countQuery.setParameter("attachmentList", attachmentList); @@ -128,17 +141,18 @@ public class ItemAttachmentManager { return; } + @SuppressWarnings("rawtypes") final TypedQuery query = entityManager - .createNamedQuery("ItemAttachment.findByAssetByAndList", - ItemAttachment.class); + .createNamedQuery("ItemAttachment.findByAssetByAndList", + ItemAttachment.class); query.setParameter("asset", asset); query.setParameter("attachmentList", attachmentList); + @SuppressWarnings("rawtypes") final List attachments = query.getResultList(); attachments.forEach((attachment) -> entityManager.remove(attachment)); if (!assetManager.isShared(asset)) { -// assetRepo.delete(asset); entityManager.remove(asset); } } @@ -147,28 +161,147 @@ public class ItemAttachmentManager { * Moves the {@link Asset} one position up in the provided * {@link AttachmentList}. * - * @param asset The asset to move up. If the asset is not part of the - * provided {@link AttachmentList} an {@link IllegalArgumentException} is - * thrown. + * @param asset The asset to move up. If the asset is not part of + * the provided {@link AttachmentList} an + * {@link IllegalArgumentException} is thrown. * @param attachmentList The attachment list in which the item is moved. */ - public void moveUp(final Asset asset, - final AttachmentList attachmentList) { - throw new UnsupportedOperationException("Not implemented yet"); + @Transactional(Transactional.TxType.REQUIRED) + @AuthorizationRequired + public void moveUp( + final Asset asset, + @RequiresPrivilege(ItemPrivileges.EDIT) + final AttachmentList attachmentList) { + + if (asset == null) { + throw new IllegalArgumentException("Can't move null."); + } + + if (attachmentList == null) { + throw new IllegalArgumentException( + "Can't move up an asset in list null."); + } + + final TypedQuery countQuery = entityManager.createNamedQuery( + "ItemAttachment.countByAssetIdAndList", Long.class); + countQuery.setParameter("asset", asset); + countQuery.setParameter("attachmentList", attachmentList); + final long count = countQuery.getSingleResult(); + + if (count == 0) { + return; + } + + @SuppressWarnings("rawtypes") + final TypedQuery query = entityManager.createNamedQuery( + "ItemAttachment.findByAssetByAndList", ItemAttachment.class); + query.setParameter("asset", asset); + query.setParameter("attachmentList", attachmentList); + final ItemAttachment selected = query.getSingleResult(); + + final Optional> attachment1 = attachmentList + .getAttachments().stream() + .filter(attachment -> { + return attachment.getSortKey() == selected.getSortKey(); + }) + .findFirst(); + final Optional> attachment2 = attachmentList + .getAttachments().stream() + .filter(attachment -> { + return attachment.getSortKey() >= selected.getSortKey() + 1; + }) + .findFirst(); + + if (!attachment2.isPresent()) { + return; + } + + final long sortKey1 = attachment1.get().getSortKey(); + final long sortKey2 = attachment2.get().getSortKey(); + + attachment1.get().setSortKey(sortKey2); + attachment2.get().setSortKey(sortKey1); + + entityManager.merge(attachment1.get()); + entityManager.merge(attachment2.get()); } /** * Moves the {@link Asset} one position down in the provided * {@link AttachmentList}. * - * @param asset The asset to move down. If the asset is not part of the - * provided {@link AttachmentList} an {@link IllegalArgumentException} is - * thrown. + * @param asset The asset to move down. If the asset is not part of + * the provided {@link AttachmentList} an + * {@link IllegalArgumentException} is thrown. * @param attachmentList The attachment list in which the item is moved. */ - public void moveDown(final Asset asset, - final AttachmentList attachmentList) { - throw new UnsupportedOperationException("Not implemented yet"); + @Transactional(Transactional.TxType.REQUIRED) + @AuthorizationRequired + public void moveDown( + final Asset asset, + @RequiresPrivilege(ItemPrivileges.EDIT) + final AttachmentList attachmentList) { + + if (asset == null) { + throw new IllegalArgumentException("Can't move down null."); + } + + if (attachmentList == null) { + throw new IllegalArgumentException( + "Can't move down an asset in list null."); + } + + final TypedQuery countQuery = entityManager.createNamedQuery( + "ItemAttachment.countByAssetIdAndList", Long.class); + countQuery.setParameter("asset", asset); + countQuery.setParameter("attachmentList", attachmentList); + final long count = countQuery.getSingleResult(); + + if (count == 0) { + return; + } + + @SuppressWarnings("rawtypes") + final TypedQuery query = entityManager.createNamedQuery( + "ItemAttachment.findByAssetByAndList", ItemAttachment.class); + query.setParameter("asset", asset); + query.setParameter("attachmentList", attachmentList); + final ItemAttachment selected = query.getSingleResult(); + + final Optional> attachment1 = attachmentList + .getAttachments().stream() + .filter(attachment -> { + return attachment.getSortKey() == selected.getSortKey(); + }) + .findFirst(); + final List> lower = attachmentList + .getAttachments().stream() + .filter(attachment -> { + return attachment.getSortKey() <= selected.getSortKey() - 1; + }) + .collect(Collectors.toList()); + Collections.sort(lower); + + final Optional> attachment2; + if (lower.isEmpty()) { + attachment2 = Optional.empty(); + } else { + attachment2 = Optional.of(lower.get(lower.size() - 1)); + } + + if (!attachment2.isPresent()) { + return; + } + + final long sortKey1 = attachment1.get().getSortKey(); + final long sortKey2 = attachment2.get().getSortKey(); + + attachment1.get().setSortKey(sortKey2); + attachment2.get().setSortKey(sortKey1); + + entityManager.merge(attachment1.get()); + entityManager.merge(attachment2.get()); + } } diff --git a/ccm-cms/src/test/java/org/librecms/contentsection/ItemAttachmentManagerTest.java b/ccm-cms/src/test/java/org/librecms/contentsection/ItemAttachmentManagerTest.java index 89987cc57..26a41927c 100644 --- a/ccm-cms/src/test/java/org/librecms/contentsection/ItemAttachmentManagerTest.java +++ b/ccm-cms/src/test/java/org/librecms/contentsection/ItemAttachmentManagerTest.java @@ -105,60 +105,60 @@ public class ItemAttachmentManagerTest { @Deployment public static WebArchive createDeployment() { return ShrinkWrap - .create(WebArchive.class, - "LibreCCM-org.librecms.assets.AssetManagerTest.war") - .addPackage(org.libreccm.auditing.CcmRevision.class.getPackage()). - addPackage(org.libreccm.categorization.Categorization.class - .getPackage()) - .addPackage(org.libreccm.cdi.utils.CdiUtil.class.getPackage()) - .addPackage(org.libreccm.configuration.Configuration.class - .getPackage()) - .addPackage(org.libreccm.core.CcmCore.class.getPackage()) - .addPackage(org.libreccm.jpa.EntityManagerProducer.class - .getPackage()) - .addPackage(org.libreccm.jpa.utils.MimeTypeConverter.class - .getPackage()) - .addPackage(org.libreccm.l10n.LocalizedString.class - .getPackage()) - .addPackage(org.libreccm.security.Permission.class.getPackage()) - .addPackage(org.libreccm.web.CcmApplication.class.getPackage()) - .addPackage(org.libreccm.workflow.Workflow.class.getPackage()) - .addPackage(com.arsdigita.bebop.Component.class.getPackage()) - .addPackage(com.arsdigita.bebop.util.BebopConstants.class - .getPackage()) - .addClass(com.arsdigita.kernel.KernelConfig.class) - .addClass(com.arsdigita.runtime.CCMResourceManager.class) - .addClass( - com.arsdigita.ui.admin.applications.AbstractAppInstanceForm.class). - addClass( - com.arsdigita.ui.admin.applications.AbstractAppSettingsPane.class). - addClass( - com.arsdigita.ui.admin.applications.DefaultApplicationInstanceForm.class). - addClass( - com.arsdigita.ui.admin.applications.DefaultApplicationSettingsPane.class). - addClass(com.arsdigita.cms.dispatcher.ItemResolver.class) - .addPackage(com.arsdigita.util.Lockable.class.getPackage()) - .addPackage(com.arsdigita.web.BaseServlet.class.getPackage()) - .addPackage(org.librecms.Cms.class.getPackage()) - .addPackage(org.librecms.assets.BinaryAsset.class.getPackage()) - .addPackage(org.librecms.contentsection.Asset.class.getPackage()). - addPackage(org.librecms.lifecycle.Lifecycle.class.getPackage()) - .addPackage(org.librecms.contentsection.ContentSection.class - .getPackage()) - .addPackage(org.librecms.contenttypes.Article.class.getPackage()). - addClass(com.arsdigita.kernel.security.SecurityConfig.class) - .addPackage(org.libreccm.tests.categories.IntegrationTest.class - .getPackage()) - // .addAsLibraries(getModuleDependencies()) - .addAsLibraries(getCcmCoreDependencies()) - .addAsResource("configs/shiro.ini", "shiro.ini") - .addAsResource( - "configs/org/librecms/contentsection/ContentItemManagerTest/log4j2.xml", - "log4j2.xml") - .addAsResource("test-persistence.xml", - "META-INF/persistence.xml") - .addAsWebInfResource("test-web.xml", "web.xml") - .addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml"); + .create(WebArchive.class, + "LibreCCM-org.librecms.assets.AssetManagerTest.war") + .addPackage(org.libreccm.auditing.CcmRevision.class.getPackage()). + addPackage(org.libreccm.categorization.Categorization.class + .getPackage()) + .addPackage(org.libreccm.cdi.utils.CdiUtil.class.getPackage()) + .addPackage(org.libreccm.configuration.Configuration.class + .getPackage()) + .addPackage(org.libreccm.core.CcmCore.class.getPackage()) + .addPackage(org.libreccm.jpa.EntityManagerProducer.class + .getPackage()) + .addPackage(org.libreccm.jpa.utils.MimeTypeConverter.class + .getPackage()) + .addPackage(org.libreccm.l10n.LocalizedString.class + .getPackage()) + .addPackage(org.libreccm.security.Permission.class.getPackage()) + .addPackage(org.libreccm.web.CcmApplication.class.getPackage()) + .addPackage(org.libreccm.workflow.Workflow.class.getPackage()) + .addPackage(com.arsdigita.bebop.Component.class.getPackage()) + .addPackage(com.arsdigita.bebop.util.BebopConstants.class + .getPackage()) + .addClass(com.arsdigita.kernel.KernelConfig.class) + .addClass(com.arsdigita.runtime.CCMResourceManager.class) + .addClass( + com.arsdigita.ui.admin.applications.AbstractAppInstanceForm.class) + .addClass( + com.arsdigita.ui.admin.applications.AbstractAppSettingsPane.class) + .addClass( + com.arsdigita.ui.admin.applications.DefaultApplicationInstanceForm.class) + .addClass( + com.arsdigita.ui.admin.applications.DefaultApplicationSettingsPane.class) + .addClass(com.arsdigita.cms.dispatcher.ItemResolver.class) + .addPackage(com.arsdigita.util.Lockable.class.getPackage()) + .addPackage(com.arsdigita.web.BaseServlet.class.getPackage()) + .addPackage(org.librecms.Cms.class.getPackage()) + .addPackage(org.librecms.assets.BinaryAsset.class.getPackage()) + .addPackage(org.librecms.contentsection.Asset.class.getPackage()). + addPackage(org.librecms.lifecycle.Lifecycle.class.getPackage()) + .addPackage(org.librecms.contentsection.ContentSection.class + .getPackage()) + .addPackage(org.librecms.contenttypes.Article.class.getPackage()). + addClass(com.arsdigita.kernel.security.SecurityConfig.class) + .addPackage(org.libreccm.tests.categories.IntegrationTest.class + .getPackage()) + // .addAsLibraries(getModuleDependencies()) + .addAsLibraries(getCcmCoreDependencies()) + .addAsResource("configs/shiro.ini", "shiro.ini") + .addAsResource( + "configs/org/librecms/contentsection/ContentItemManagerTest/log4j2.xml", + "log4j2.xml") + .addAsResource("test-persistence.xml", + "META-INF/persistence.xml") + .addAsWebInfResource("test-web.xml", "web.xml") + .addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml"); } /** @@ -193,13 +193,13 @@ public class ItemAttachmentManagerTest { @Test @InSequence(100) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet( - value = "datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/after-attach-nonshared.xml", - excludeColumns = {"timestamp", - "uuid", - "attachment_id"}) + value = "datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/after-attach-nonshared.xml", + excludeColumns = {"timestamp", + "uuid", + "attachment_id"}) public void attachNonSharedAsset() throws MimeTypeParseException { final Optional item = itemRepo.findById(-510L); assertThat(item.isPresent(), is(true)); @@ -222,13 +222,13 @@ public class ItemAttachmentManagerTest { @Test @InSequence(100) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet( - value = "datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/after-attach-shared.xml", - excludeColumns = {"timestamp", - "uuid", - "attachment_id"}) + value = "datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/after-attach-shared.xml", + excludeColumns = {"timestamp", + "uuid", + "attachment_id"}) public void attachSharedAsset() throws MimeTypeParseException { final Optional item = itemRepo.findById(-510L); assertThat(item.isPresent(), is(true)); @@ -248,9 +248,9 @@ public class ItemAttachmentManagerTest { @Test @InSequence(110) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") public void attachAssetAlreadyAttached() { final Optional item = itemRepo.findById(-510L); assertThat(item.isPresent(), is(true)); @@ -270,9 +270,9 @@ public class ItemAttachmentManagerTest { @Test(expected = IllegalArgumentException.class) @InSequence(120) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) public void attachAssetNull() { final Optional item = itemRepo.findById(-510L); @@ -293,9 +293,9 @@ public class ItemAttachmentManagerTest { @Test(expected = IllegalArgumentException.class) @InSequence(130) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) public void attachAssetToListNull() { final AttachmentList list = null; @@ -311,12 +311,12 @@ public class ItemAttachmentManagerTest { @Test @InSequence(210) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet( - value = "datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/" - + "after-unattach-shared.xml", - excludeColumns = {"timestamp"}) + value = "datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/" + + "after-unattach-shared.xml", + excludeColumns = {"timestamp"}) public void unattachSharedAsset() { final Asset asset = assetRepo.findById(-610L); final Optional item = itemRepo.findById(-510L); @@ -338,12 +338,12 @@ public class ItemAttachmentManagerTest { @Test @InSequence(220) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet( - value = "datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/" - + "after-unattach-nonshared.xml", - excludeColumns = {"timestamp"}) + value = "datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/" + + "after-unattach-nonshared.xml", + excludeColumns = {"timestamp"}) public void unattachNonSharedAsset() { final Asset asset = assetRepo.findById(-720L); final Optional item = itemRepo.findById(-510L); @@ -365,11 +365,19 @@ public class ItemAttachmentManagerTest { @Test @InSequence(220) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") public void unattachAssetNotAttached() { - fail("Not implemented yet"); + final Asset asset = assetRepo.findById(-720L); + final Optional item = itemRepo.findById(-510L); + + assertThat(asset, is(not(nullValue()))); + assertThat(item.isPresent(), is(true)); + + final AttachmentList list = item.get().getAttachments().get(1); + + attachmentManager.unattachAsset(asset, list); } /** @@ -381,12 +389,19 @@ public class ItemAttachmentManagerTest { @Test(expected = IllegalArgumentException.class) @InSequence(230) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) public void unattachAssetNull() { - fail("Not implemented yet"); + final Asset asset = null; + + final Optional item = itemRepo.findById(-510L); + assertThat(item.isPresent(), is(true)); + + final AttachmentList list = item.get().getAttachments().get(0); + + attachmentManager.unattachAsset(asset, list); } /** @@ -398,12 +413,17 @@ public class ItemAttachmentManagerTest { @Test(expected = IllegalArgumentException.class) @InSequence(240) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) public void unattachAssetFromListNull() { - fail("Not implemented yet"); + final Asset asset = assetRepo.findById(-720L); + assertThat(asset, is(not(nullValue()))); + + final AttachmentList list = null; + + attachmentManager.unattachAsset(asset, list); } /** @@ -414,11 +434,19 @@ public class ItemAttachmentManagerTest { @Test @InSequence(300) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") - @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/after-move-up.xml") + + "ItemAttachmentManagerTest/data.xml") + @ShouldMatchDataSet( + value = "datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/after-move-up.xml", + excludeColumns = {"timestamp"}) public void moveUp() { - fail("Not implemented yet"); + final Optional item = itemRepo.findById(-510L); + assertThat(item.isPresent(), is(true)); + + final AttachmentList list = item.get().getAttachments().get(0); + + attachmentManager.moveUp(list.getAttachments().get(0).getAsset(), + list); } /** @@ -429,11 +457,17 @@ public class ItemAttachmentManagerTest { @Test @InSequence(310) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") public void moveUpLast() { - fail("Not implemented yet"); + final Optional item = itemRepo.findById(-510L); + assertThat(item.isPresent(), is(true)); + + final AttachmentList list = item.get().getAttachments().get(0); + + attachmentManager.moveUp(list.getAttachments().get(2).getAsset(), + list); } /** @@ -445,12 +479,39 @@ public class ItemAttachmentManagerTest { @Test(expected = IllegalArgumentException.class) @InSequence(320) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) public void moveUpNull() { - fail("Not implemented yet"); + final Optional item = itemRepo.findById(-510L); + assertThat(item.isPresent(), is(true)); + + final AttachmentList list = item.get().getAttachments().get(0); + + attachmentManager.moveUp(null, list); + } + + /** + * Verifies that + * {@link ItemAttachmentManager#moveUp(org.librecms.contentsection.Asset, org.librecms.contentsection.AttachmentList)} + * throws an {@link IllegalArgumentException} if called with {@code null} + * for the list. + */ + @Test(expected = IllegalArgumentException.class) + @InSequence(330) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/data.xml") + @ShouldMatchDataSet("datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/data.xml") + @ShouldThrowException(IllegalArgumentException.class) + public void moveUpInListNull() { + final Asset asset = assetRepo.findById(-720L); + assertThat(asset, is(not(nullValue()))); + + final AttachmentList list = null; + + attachmentManager.moveUp(asset, list); } /** @@ -461,11 +522,19 @@ public class ItemAttachmentManagerTest { @Test @InSequence(400) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") - @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/after-move-down.xml") + + "ItemAttachmentManagerTest/data.xml") + @ShouldMatchDataSet( + value = "datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/after-move-down.xml", + excludeColumns = {"timestamp"}) public void moveDown() { - fail("Not implemented yet"); + final Optional item = itemRepo.findById(-510L); + assertThat(item.isPresent(), is(true)); + + final AttachmentList list = item.get().getAttachments().get(0); + + attachmentManager.moveDown(list.getAttachments().get(2).getAsset(), + list); } /** @@ -476,11 +545,17 @@ public class ItemAttachmentManagerTest { @Test @InSequence(410) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") public void moveDownFirst() { - fail("Not implemented yet"); + final Optional item = itemRepo.findById(-510L); + assertThat(item.isPresent(), is(true)); + + final AttachmentList list = item.get().getAttachments().get(0); + + attachmentManager.moveDown(list.getAttachments().get(0).getAsset(), + list); } /** @@ -492,12 +567,39 @@ public class ItemAttachmentManagerTest { @Test(expected = IllegalArgumentException.class) @InSequence(420) @UsingDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ItemAttachmentManagerTest/data.xml") + + "ItemAttachmentManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) public void moveDownNull() { - fail("Not implemented yet"); + final Optional item = itemRepo.findById(-510L); + assertThat(item.isPresent(), is(true)); + + final AttachmentList list = item.get().getAttachments().get(0); + + attachmentManager.moveDown(null, list); + } + + /** + * Verifies that + * {@link ItemAttachmentManager#moveDown(org.librecms.contentsection.Asset, org.librecms.contentsection.AttachmentList)} + * throws an {@link IllegalArgumentException} if called with {@code null} + * for the list. + */ + @Test(expected = IllegalArgumentException.class) + @InSequence(430) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/data.xml") + @ShouldMatchDataSet("datasets/org/librecms/contentsection/" + + "ItemAttachmentManagerTest/data.xml") + @ShouldThrowException(IllegalArgumentException.class) + public void moveDownInListNull() { + final Asset asset = assetRepo.findById(-720L); + assertThat(asset, is(not(nullValue()))); + + final AttachmentList list = null; + + attachmentManager.moveDown(asset, list); } } diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ItemAttachmentManagerTest/after-move-down.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ItemAttachmentManagerTest/after-move-down.xml index 809c88e14..9badb9104 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ItemAttachmentManagerTest/after-move-down.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ItemAttachmentManagerTest/after-move-down.xml @@ -3,6 +3,8 @@ + @@ -345,6 +347,7 @@ + + - diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ItemAttachmentManagerTest/after-move-up.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ItemAttachmentManagerTest/after-move-up.xml index 809c88e14..7c20864cb 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ItemAttachmentManagerTest/after-move-up.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ItemAttachmentManagerTest/after-move-up.xml @@ -3,6 +3,8 @@ + @@ -338,6 +340,7 @@ + + -