From 93ae630f411ccea694748936057e7c9c784df5be Mon Sep 17 00:00:00 2001 From: jensp Date: Sat, 8 Oct 2016 17:17:05 +0000 Subject: [PATCH] CCM NG/ccm-cms: Repository for ContentType objects and accompanying test git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4365 8810af33-2d31-482b-a856-94f89814c4df --- .../contentsection/ContentItemRepository.java | 2 +- .../contentsection/ContentSectionManager.java | 162 ++++--- .../librecms/contentsection/ContentType.java | 12 +- .../contentsection/ContentTypeRepository.java | 57 ++- .../ContentItemRepositoryTest.java | 8 +- .../ContentTypeRepositoryTest.java | 395 ++++++++++++++++++ .../librecms/contentsection/DatasetsTest.java | 3 + .../after-delete.xml | 338 +++++++++++++++ .../ContentTypeRepositoryTest/data.xml | 347 +++++++++++++++ ccm-docrepo/pom.xml | 4 +- ccm-shortcuts/pom.xml | 4 +- 11 files changed, 1269 insertions(+), 63 deletions(-) create mode 100644 ccm-cms/src/test/java/org/librecms/contentsection/ContentTypeRepositoryTest.java create mode 100644 ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/after-delete.xml create mode 100644 ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/data.xml diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java index 840ec86c0..fe4acd8e6 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java @@ -232,7 +232,7 @@ public class ContentItemRepository } /** - * Counts a items in a specfic folder whose {@link CcmObject#displayName} + * Counts a items in a specific folder whose {@link CcmObject#displayName} * starts with the provided pattern. * * @param folder The folder/category to use. diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionManager.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionManager.java index de62b0d2c..5cae4b79d 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionManager.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionManager.java @@ -91,11 +91,11 @@ public class ContentSectionManager { public ContentSection createContentSection(final String name) { if (name == null || name.isEmpty()) { throw new IllegalArgumentException( - "The name of a ContentSection can't be blank."); + "The name of a ContentSection can't be blank."); } final KernelConfig kernelConfig = confManager.findConfiguration( - KernelConfig.class); + KernelConfig.class); final Locale defautLocale = kernelConfig.getDefaultLocale(); final ContentSection section = new ContentSection(); @@ -221,17 +221,17 @@ public class ContentSectionManager { * {@link CmsConstants#PRIVILEGE_ADMINISTER_ROLES} for the provided content * section. * - * @param section The {@link ContentSection} to which the role is added. - * @param roleName The name of the new role. + * @param section The {@link ContentSection} to which the role is added. + * @param roleName The name of the new role. * @param privileges The privileges of the new role. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void addRoleToContentSection( - @RequiresPrivilege(PRIVILEGE_ADMINISTER_ROLES) - final ContentSection section, - final String roleName, - final String... privileges) { + @RequiresPrivilege(PRIVILEGE_ADMINISTER_ROLES) + final ContentSection section, + final String roleName, + final String... privileges) { final Role role = new Role(); role.setName(String.join("_", section.getLabel(), roleName)); @@ -251,15 +251,15 @@ public class ContentSectionManager { * requires {@link CmsConstants#PRIVILEGE_ADMINISTER_ROLES} for the provided * content section. * - * @param role The role to add. + * @param role The role to add. * @param section The section the role is associated with. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void addRoleToContentSection( - final Role role, - @RequiresPrivilege(PRIVILEGE_ADMINISTER_ROLES) - final ContentSection section) { + final Role role, + @RequiresPrivilege(PRIVILEGE_ADMINISTER_ROLES) + final ContentSection section) { section.addRole(role); sectionRepo.save(section); @@ -274,18 +274,18 @@ public class ContentSectionManager { * section. * * @param contentSection The section from which the role is removed. - * @param role The role to remove from the content section. + * @param role The role to remove from the content section. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void removeRoleFromContentSection( - @RequiresPrivilege(PRIVILEGE_ADMINISTER_ROLES) - final ContentSection contentSection, - final Role role) { + @RequiresPrivilege(PRIVILEGE_ADMINISTER_ROLES) + final ContentSection contentSection, + final Role role) { if (contentSection == null) { throw new IllegalArgumentException( - "Can't remove role from ContentSection null"); + "Can't remove role from ContentSection null"); } if (role == null) { @@ -296,8 +296,8 @@ public class ContentSectionManager { sectionRepo.save(contentSection); final TypedQuery query = entityManager - .createNamedQuery("ContentSection.findPermissions", - Permission.class); + .createNamedQuery("ContentSection.findPermissions", + Permission.class); query.setParameter("section", contentSection); query.setParameter("rootDocumentsFolder", contentSection.getRootDocumentsFolder()); @@ -313,15 +313,16 @@ public class ContentSectionManager { * {@link CmsConstants#PRIVILEGE_ADMINISTER_CONTENT_TYPES} for the provided * content section. * - * @param type The {@link ContentItem} class representing the type to add. + * @param type The {@link ContentItem} class representing the type to + * add. * @param section The section to which to type is added. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void addTypeToSection( - final Class type, - @RequiresPrivilege(PRIVILEGE_ADMINISTER_CONTENT_TYPES) - final ContentSection section) { + final Class type, + @RequiresPrivilege(PRIVILEGE_ADMINISTER_CONTENT_TYPES) + final ContentSection section) { throw new UnsupportedOperationException(); } @@ -333,15 +334,15 @@ public class ContentSectionManager { * {@link CmsConstants#PRIVILEGE_ADMINISTER_CONTENT_TYPES} for the provided * content section. * - * @param type The type to remove. + * @param type The type to remove. * @param section The section from which the type is removed. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void removeTypeFromSection( - final ContentType type, - @RequiresPrivilege(PRIVILEGE_ADMINISTER_CONTENT_TYPES) - final ContentSection section) { + final ContentType type, + @RequiresPrivilege(PRIVILEGE_ADMINISTER_CONTENT_TYPES) + final ContentSection section) { throw new UnsupportedOperationException(); } @@ -352,14 +353,14 @@ public class ContentSectionManager { * content section. * * @param definition The lifecycle definition to add. - * @param section The section to which the definition is added. + * @param section The section to which the definition is added. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void addLifecycleDefinitionToContentSection( - final LifecycleDefinition definition, - @RequiresPrivilege(PRIVILEGE_ADMINISTER_LIFECYLES) - final ContentSection section) { + final LifecycleDefinition definition, + @RequiresPrivilege(PRIVILEGE_ADMINISTER_LIFECYLES) + final ContentSection section) { section.addLifecycleDefinition(definition); sectionRepo.save(section); @@ -371,14 +372,14 @@ public class ContentSectionManager { * provided content section. * * @param definition The definition to remove. - * @param section The section from which the definition is removed. + * @param section The section from which the definition is removed. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void removeLifecycleDefinitionFromContentSection( - final LifecycleDefinition definition, - @RequiresPrivilege(PRIVILEGE_ADMINISTER_LIFECYLES) - final ContentSection section) { + final LifecycleDefinition definition, + @RequiresPrivilege(PRIVILEGE_ADMINISTER_LIFECYLES) + final ContentSection section) { section.removeLifecycleDefinition(definition); sectionRepo.save(section); @@ -390,14 +391,14 @@ public class ContentSectionManager { * content section. * * @param template The template to add. - * @param section The content section to which the template is added. + * @param section The content section to which the template is added. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void addWorkflowTemplateToContentSection( - final WorkflowTemplate template, - @RequiresPrivilege(PRIVILEGE_ADMINISTER_WORKFLOW) - final ContentSection section) { + final WorkflowTemplate template, + @RequiresPrivilege(PRIVILEGE_ADMINISTER_WORKFLOW) + final ContentSection section) { section.addWorkflowTemplate(template); sectionRepo.save(section); @@ -409,14 +410,14 @@ public class ContentSectionManager { * provided content section. * * @param template The template to remove. - * @param section The section from which the template is removed. + * @param section The section from which the template is removed. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void removeWorkflowTemplateFromContentSection( - final WorkflowTemplate template, - @RequiresPrivilege(PRIVILEGE_ADMINISTER_WORKFLOW) - final ContentSection section) { + final WorkflowTemplate template, + @RequiresPrivilege(PRIVILEGE_ADMINISTER_WORKFLOW) + final ContentSection section) { section.removeWorkflowTemplate(template); sectionRepo.save(section); @@ -426,20 +427,85 @@ public class ContentSectionManager { * Retrieves the {@link ItemResolver} for the provided content section. * * @param section The section for which the {@link ItemResolver} is - * retrieved. + * retrieved. + * * @return The {@link ItemResolver} for the provided content section. */ public ItemResolver getItemResolver(final ContentSection section) { try { + @SuppressWarnings("unchecked") final Class itemResolverClazz - = (Class) Class. + = (Class) Class. forName(section.getItemResolverClass()); return itemResolverClazz.newInstance(); - } catch (ClassNotFoundException | - IllegalAccessException | - InstantiationException ex) { + } catch (ClassNotFoundException + | IllegalAccessException + | InstantiationException ex) { throw new RuntimeException(ex); } } + /** + * Adds a new {@link ContentType} to a content section, making items of that + * type available in the content section. + * + * @param type The type to add (a subclass of + * {@link ContentItem}. + * @param section The section to which the type is added. + * @param defaultLifecycle The default lifecycle for items of the provided + * type in the provided content section. The + * lifecycle must be part of the provided section. + * Otherwise an {@link IllegalArgumentException} is + * thrown. + * @param defaultWorkflow The default workflow for items of the provided + * type in the provided content section. The + * workflow must be part of the provided section. + * Otherwise an {@link IllegalArgumentException} is + * thrown. + * + * @return The new {@link ContentType} instance. + */ + public ContentType addContentTypeToSection( + final Class type, + final ContentSection section, + final LifecycleDefinition defaultLifecycle, + final WorkflowTemplate defaultWorkflow) { + + throw new UnsupportedOperationException(); + } + + /** + * Checks if a content section has a {@link ContentType} for a specific + * subclass {@link ContentItem}. + * + * @param type The type to check for. + * @param section The section to check for the {@link ContentType}. + * + * @return {@code true} if the section has a {@link ContentType} for + * {@code type}, {@code false} if not. + */ + public boolean hasContentType(final Class type, + final ContentSection section) { + + throw new UnsupportedOperationException(); + } + + /** + * Removes an unused {@link ContentType} from a + * {@link ContentSection}. + * + * @param type The type to remove from the section. + * @param section The section from which the type is removed. + * + * @throws IllegalArgumentException if the provided {@link ContentType} is + * in use or the parameters or otherwise + * illegal. + */ + public void removeContentTypeFromSection( + final Class type, + final ContentSection section) { + + throw new UnsupportedOperationException(); + } + } diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentType.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentType.java index f58f68343..d5f3ebd86 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentType.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentType.java @@ -54,12 +54,20 @@ import javax.persistence.Table; @NamedQuery( name = "ContentType.findByContentSection", query = "SELECT c FROM ContentType c " - + "WHERE c.contentSection = :contentSection"), + + "WHERE c.contentSection = :contentSection " + + "ORDER BY c.contentItemClass") + , @NamedQuery( name = "ContentType.findByContentSectionAndClass", query = "SELECT c FROM ContentType c " + "WHERE c.contentSection = :contentSection " - + "AND c.contentItemClass =:clazz") + + "AND c.contentItemClass = :clazz") + , + @NamedQuery( + name = "ContentType.isInUse", + query = "SELECT COUNT(i) FROM ContentItem i " + + "WHERE i.contentType = :type" + ) }) public class ContentType extends CcmObject implements Serializable { diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentTypeRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentTypeRepository.java index 7f8cd001e..338cc2e1c 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentTypeRepository.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentTypeRepository.java @@ -19,12 +19,17 @@ package org.librecms.contentsection; import org.libreccm.core.AbstractEntityRepository; +import org.libreccm.security.AuthorizationRequired; +import org.libreccm.security.RequiresPrivilege; +import org.librecms.CmsConstants; import java.util.List; import java.util.Optional; import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; import javax.persistence.TypedQuery; +import javax.transaction.Transactional; /** * @@ -34,6 +39,9 @@ import javax.persistence.TypedQuery; public class ContentTypeRepository extends AbstractEntityRepository { + @Inject + private ContentItemRepository itemRepo; + @Override public Class getEntityClass() { return ContentType.class; @@ -50,7 +58,8 @@ public class ContentTypeRepository * @param section The section whose {@link ContentTyp}s are retrieved. Can't * be {@code null}. * - * @return A list of all {@link ContentType}s of the provided section. + * @return A list of all {@link ContentType}s of the provided section + * ordered alphabetically. */ public List findByContentSection(final ContentSection section) { if (section == null) { @@ -167,4 +176,50 @@ public class ContentTypeRepository } } + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + @Override + public void save( + @RequiresPrivilege(CmsConstants.PRIVILEGE_ADMINISTER_CONTENT_TYPES) + final ContentType type) { + + super.save(type); + } + + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + @Override + public void delete( + @RequiresPrivilege(CmsConstants.PRIVILEGE_ADMINISTER_CONTENT_TYPES) + final ContentType type) { + + if (isContentTypeInUse(type)) { + throw new IllegalArgumentException(String.format( + "Contenttype \"%s\" in section \"%s\" is in use and can't be" + + "deleted.", + type.getContentItemClass(), + type.getContentSection().getDisplayName())); + } else { + super.delete(type); + } + } + + /** + * Checks if there is any item of the provided content type and the content + * section to which the type belongs. + * + * @param type The type to check for usage. + * + * @return {@code true} if the type is in use, {@code false} if not. + */ + public boolean isContentTypeInUse(final ContentType type) { + final TypedQuery query = getEntityManager().createNamedQuery( + "ContentType.isInUse", Long.class); + query.setParameter("type", type); + + final long result = query.getSingleResult(); + + return result > 0; + } + } diff --git a/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemRepositoryTest.java b/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemRepositoryTest.java index b8b602586..bf87bbe19 100644 --- a/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemRepositoryTest.java +++ b/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemRepositoryTest.java @@ -30,10 +30,6 @@ import org.jboss.arquillian.transaction.api.annotation.Transactional; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.spec.WebArchive; -import org.jboss.shrinkwrap.resolver.api.maven.Maven; -import org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage; -import org.jboss.shrinkwrap.resolver.api.maven.ScopeType; -import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependencies; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -46,17 +42,15 @@ import org.libreccm.tests.categories.IntegrationTest; import org.librecms.contenttypes.Article; import org.librecms.contenttypes.News; -import java.io.File; -import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Optional; -import java.util.stream.IntStream; import javax.inject.Inject; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; + import static org.libreccm.testutils.DependenciesHelpers.*; /** diff --git a/ccm-cms/src/test/java/org/librecms/contentsection/ContentTypeRepositoryTest.java b/ccm-cms/src/test/java/org/librecms/contentsection/ContentTypeRepositoryTest.java new file mode 100644 index 000000000..c6c9e853a --- /dev/null +++ b/ccm-cms/src/test/java/org/librecms/contentsection/ContentTypeRepositoryTest.java @@ -0,0 +1,395 @@ +/* + * Copyright (C) 2016 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.librecms.contentsection; + +import static org.libreccm.testutils.DependenciesHelpers.*; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.ShouldThrowException; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.arquillian.persistence.CreateSchema; +import org.jboss.arquillian.persistence.PersistenceTest; +import org.jboss.arquillian.persistence.ShouldMatchDataSet; +import org.jboss.arquillian.persistence.UsingDataSet; +import org.jboss.arquillian.transaction.api.annotation.TransactionMode; +import org.jboss.arquillian.transaction.api.annotation.Transactional; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.libreccm.tests.categories.IntegrationTest; +import org.librecms.contenttypes.Article; +import org.librecms.contenttypes.News; +import org.librecms.contenttypes.Event; + +import java.util.List; +import java.util.Optional; + +import javax.inject.Inject; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +/** + * + * @author Jens Pelzetter + */ +@org.junit.experimental.categories.Category(IntegrationTest.class) +@RunWith(Arquillian.class) +@PersistenceTest +@Transactional(TransactionMode.COMMIT) +@CreateSchema({"create_ccm_cms_schema.sql"}) +public class ContentTypeRepositoryTest { + + @Inject + private ContentTypeRepository contentTypeRepo; + + @Inject + private ContentSectionRepository contentSectionRepo; + + public ContentTypeRepositoryTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + @Deployment + public static WebArchive createDeployment() { + return ShrinkWrap + .create(WebArchive.class, + "LibreCCM-org.librecms.contentsection.ContentTypeRepositoryTest.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.Asset.class.getPackage()) + .addPackage(org.librecms.attachments.AttachmentList.class + .getPackage()) + .addPackage(org.librecms.lifecycle.Lifecycle.class.getPackage()) + .addPackage(org.librecms.contentsection.ContentSection.class + .getPackage()) + .addPackage(org.librecms.contenttypes.Article.class.getPackage()) + .addPackage(org.libreccm.tests.categories.IntegrationTest.class + .getPackage()) + // .addAsLibraries(getModuleDependencies()) + .addAsLibraries(getCcmCoreDependencies()) + .addAsResource("test-persistence.xml", + "META-INF/persistence.xml") + .addAsWebInfResource("test-web.xml", "WEB-INF/web.xml") + .addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml"); + } + + /** + * Check if all injected beans are available. + */ + @Test + @InSequence(1) + public void checkInjection() { + assertThat(contentTypeRepo, is(not(nullValue()))); + assertThat(contentSectionRepo, is(not(nullValue()))); + } + + /** + * Checks if + * {@link ContentTypeRepository#findByContentSection(org.librecms.contentsection.ContentSection)} + * returns all content types of the given content section. + */ + @Test + @InSequence(1100) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + public void findByContentSection() { + final ContentSection section = contentSectionRepo.findById(-1001L); + final List types = contentTypeRepo.findByContentSection( + section); + + assertThat(types, is(not(nullValue()))); + assertThat(types.isEmpty(), is(false)); + assertThat(types.size(), is(2)); + + assertThat(types.get(0).getContentItemClass(), + is(equalTo(Article.class.getName()))); + assertThat(types.get(0).getContentSection().getDisplayName(), + is(equalTo("info"))); + + assertThat(types.get(1).getContentItemClass(), + is(equalTo(News.class.getName()))); + assertThat(types.get(1).getContentSection().getDisplayName(), + is(equalTo("info"))); + } + + /** + * Checks if + * {@link ContentTypeRepository#findByContentSection(org.librecms.contentsection.ContentSection)} + * throws all {@link IllegalArgumentException} if called with {@code null} + * for the content section. + */ + @Test(expected = IllegalArgumentException.class) + @InSequence(1110) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + @ShouldThrowException(IllegalArgumentException.class) + public void findByContentSectionNull() { + contentTypeRepo.findByContentSection(null); + } + + /** + * Checks if {@link ContentTypeRepository#findByContentSectionAndClass(org.librecms.contentsection.ContentSection, java.lang.Class) + * returns the expected values. + */ + @Test + @InSequence(1200) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + public void findByContentSectionAndClass() { + final ContentSection section = contentSectionRepo.findById(-1001L); + final Optional articleType = contentTypeRepo + .findByContentSectionAndClass(section, Article.class); + final Optional newsType = contentTypeRepo + .findByContentSectionAndClass(section, News.class); + final Optional eventType = contentTypeRepo + .findByContentSectionAndClass(section, Event.class); + + assertThat(articleType.isPresent(), is(true)); + assertThat(articleType.get().getContentItemClass(), + is(equalTo(Article.class.getName()))); + assertThat(articleType.get().getContentSection().getDisplayName(), + is(equalTo("info"))); + + assertThat(newsType.isPresent(), is(true)); + assertThat(newsType.get().getContentItemClass(), + is(equalTo(News.class.getName()))); + assertThat(newsType.get().getContentSection().getDisplayName(), + is(equalTo("info"))); + + assertThat(eventType.isPresent(), is(false)); + } + + /** + * Checks if + * {@link ContentTypeRepository#findByContentSectionAndClass(org.librecms.contentsection.ContentSection, java.lang.Class)} + * throws a {@link IllegalArgumentException} when called with {@code null} + * for the content section. + */ + @Test(expected = IllegalArgumentException.class) + @InSequence(1210) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + @ShouldThrowException(IllegalArgumentException.class) + public void findByContentSectionNullAndClass() { + + contentTypeRepo.findByContentSectionAndClass(null, Article.class); + } + + /** + * Checks if + * {@link ContentTypeRepository#findByContentSectionAndClass(org.librecms.contentsection.ContentSection, java.lang.Class)} + * throws a {@link IllegalArgumentException} when called with {@code null} + * for the class. + */ + @Test(expected = IllegalArgumentException.class) + @InSequence(1220) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + @ShouldThrowException(IllegalArgumentException.class) + public void findByContentSectionAndClassNull() { + final ContentSection section = contentSectionRepo.findById(-1001L); + final Class type = null; + contentTypeRepo.findByContentSectionAndClass(section, type); + } + + /** + * Checks if + * {@link ContentTypeRepository#findByContentSectionAndClass(org.librecms.contentsection.ContentSection, java.lang.String)} + * returns the expected values. + */ + @Test + @InSequence(1300) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + public void findByContentSectionAndClassName() { + final ContentSection section = contentSectionRepo.findById(-1001L); + final Optional articleType = contentTypeRepo + .findByContentSectionAndClass(section, Article.class.getName()); + final Optional newsType = contentTypeRepo + .findByContentSectionAndClass(section, News.class.getName()); + final Optional eventType = contentTypeRepo + .findByContentSectionAndClass(section, Event.class.getName()); + + assertThat(articleType.isPresent(), is(true)); + assertThat(articleType.get().getContentItemClass(), + is(equalTo(Article.class.getName()))); + assertThat(articleType.get().getContentSection().getDisplayName(), + is(equalTo("info"))); + + assertThat(newsType.isPresent(), is(true)); + assertThat(newsType.get().getContentItemClass(), + is(equalTo(News.class.getName()))); + assertThat(newsType.get().getContentSection().getDisplayName(), + is(equalTo("info"))); + + assertThat(eventType.isPresent(), is(false)); + + } + + /** + * Checks if + * {@link ContentTypeRepository#findByContentSectionAndClass(org.librecms.contentsection.ContentSection, java.lang.String) } + * throws a {@link IllegalArgumentException} when called with {@code null} + * for the content section. + */ + @Test(expected = IllegalArgumentException.class) + @InSequence(1210) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + @ShouldThrowException(IllegalArgumentException.class) + public void findByContentSectionNullAndClassName() { + + contentTypeRepo.findByContentSectionAndClass(null, Article.class + .getName()); + } + + /** + * Checks if + * {@link ContentTypeRepository#findByContentSectionAndClass(org.librecms.contentsection.ContentSection, java.lang.String) } + * throws a {@link IllegalArgumentException} when called with {@code null} + * for the class. + */ + @Test(expected = IllegalArgumentException.class) + @InSequence(1220) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + @ShouldThrowException(IllegalArgumentException.class) + public void findByContentSectionAndClassNameNull() { + final ContentSection section = contentSectionRepo.findById(-1001L); + final String type = null; + contentTypeRepo.findByContentSectionAndClass(section, type); + } + + /** + * Verifies the return value of + * {@link ContentTypeRepository#isContentTypeInUse(org.librecms.contentsection.ContentType)}. + */ + @Test + @InSequence(2000) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + public void verifyIsContentTypeInUse() { + final ContentSection section = contentSectionRepo.findById(-1001L); + final Optional articleType = contentTypeRepo + .findByContentSectionAndClass(section, Article.class); + final Optional newsType = contentTypeRepo + .findByContentSectionAndClass(section, News.class); + + assertThat(articleType.isPresent(), is(true)); + assertThat(newsType.isPresent(), is(true)); + + assertThat(contentTypeRepo.isContentTypeInUse(articleType.get()), + is(true)); + assertThat(contentTypeRepo.isContentTypeInUse(newsType.get()), + is(false)); + } + + /** + * Verifies that an unused content type can be deleted. + */ + @Test + @InSequence(2000) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + @ShouldMatchDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/after-delete.xml") + public void deleteUnusedContentType() { + final ContentSection section = contentSectionRepo.findById(-1001L); + final Optional newsType = contentTypeRepo + .findByContentSectionAndClass(section, News.class); + assertThat(newsType.isPresent(), is(true)); + + contentTypeRepo.delete(newsType.get()); + + } + + /** + * Verifies that content types which are in use can't be deleted. + */ + @Test(expected = IllegalArgumentException.class) + @InSequence(2000) + @UsingDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + @ShouldMatchDataSet("datasets/org/librecms/contentsection/" + + "ContentTypeRepositoryTest/data.xml") + @ShouldThrowException(IllegalArgumentException.class) + public void deleteContentTypeInUse() { + final ContentSection section = contentSectionRepo.findById(-1001L); + final Optional articleType = contentTypeRepo + .findByContentSectionAndClass(section, Article.class); + assertThat(articleType.isPresent(), is(true)); + + contentTypeRepo.delete(articleType.get()); + } + +} diff --git a/ccm-cms/src/test/java/org/librecms/contentsection/DatasetsTest.java b/ccm-cms/src/test/java/org/librecms/contentsection/DatasetsTest.java index 56ddd2b50..5d55e6251 100644 --- a/ccm-cms/src/test/java/org/librecms/contentsection/DatasetsTest.java +++ b/ccm-cms/src/test/java/org/librecms/contentsection/DatasetsTest.java @@ -62,6 +62,9 @@ public class DatasetsTest extends DatasetsVerifier { "/datasets/org/librecms/contentsection/ContentItemManagerTest/after-republish.xml", "/datasets/org/librecms/contentsection/ContentItemManagerTest/after-unpublish.xml", + "/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/data.xml", + "/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/after-delete.xml", + "/datasets/org/librecms/contentsection/FolderManagerTest/data.xml", "/datasets/org/librecms/contentsection/FolderManagerTest/after-create-docs-folder.xml", "/datasets/org/librecms/contentsection/FolderManagerTest/after-create-assets-folder.xml", diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/after-delete.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/after-delete.xml new file mode 100644 index 000000000..b5ac6553b --- /dev/null +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/after-delete.xml @@ -0,0 +1,338 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/data.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/data.xml new file mode 100644 index 000000000..d36057f89 --- /dev/null +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentTypeRepositoryTest/data.xml @@ -0,0 +1,347 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ccm-docrepo/pom.xml b/ccm-docrepo/pom.xml index 87b01a7cb..f3fb21dc0 100644 --- a/ccm-docrepo/pom.xml +++ b/ccm-docrepo/pom.xml @@ -176,7 +176,7 @@ report - + diff --git a/ccm-shortcuts/pom.xml b/ccm-shortcuts/pom.xml index e79bbba63..9ec62496d 100644 --- a/ccm-shortcuts/pom.xml +++ b/ccm-shortcuts/pom.xml @@ -193,7 +193,7 @@ report - +