- Infrastructure for integration tests in ccm-shortcuts now works (may serve as an example of other modules)
- Integration tests for ShortcutRepository and ShortcutManager
- Fixed some bugs in the database migration scripts


git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4148 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2016-06-09 13:54:23 +00:00
parent e4f8ca3b97
commit e7f4e49a15
15 changed files with 645 additions and 75 deletions

View File

@ -130,14 +130,30 @@
<build> <build>
<finalName>ccm-shortcuts</finalName> <finalName>ccm-shortcuts</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>${project.build.directory}/generated-resources</directory>
</testResource>
</testResources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version> <version>3.3</version>
<configuration> <configuration>
<source>1.7</source> <source>1.8</source>
<target>1.7</target> <target>1.8</target>
<optimize>true</optimize> <optimize>true</optimize>
<debug>true</debug> <debug>true</debug>
<encoding>${project.build.sourceEncoding}</encoding> <encoding>${project.build.sourceEncoding}</encoding>
@ -219,13 +235,6 @@
</plugins> </plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build> </build>
<reporting> <reporting>
@ -347,7 +356,7 @@
<profile> <profile>
<id>wildfly-remote-h2-mem</id> <id>wildfly-remote-h2-mem</id>
<dependencies> <dependencies>
<!-- <dependency> <!-- <dependency>
<groupId>org.libreccm</groupId> <groupId>org.libreccm</groupId>
<artifactId>ccm-core</artifactId> <artifactId>ccm-core</artifactId>
<version>${project.parent.version}</version> <version>${project.parent.version}</version>
@ -359,6 +368,11 @@
<!--<version>8.2.1.Final</version>--> <!--<version>8.2.1.Final</version>-->
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.jacoco</groupId> <groupId>org.jacoco</groupId>
<artifactId>org.jacoco.core</artifactId> <artifactId>org.jacoco.core</artifactId>

View File

@ -44,14 +44,16 @@ import javax.persistence.Table;
query = "SELECT s FROM Shortcut s WHERE s.urlKey = :urlKey"), query = "SELECT s FROM Shortcut s WHERE s.urlKey = :urlKey"),
@NamedQuery( @NamedQuery(
name = "Shortcut.findByRedirect", name = "Shortcut.findByRedirect",
query = "SELECT s FROM Shortcut s WHERE s.redirect = :redirect") query = "SELECT s FROM Shortcut s "
+ "WHERE s.redirect = :redirect "
+ "ORDER BY s.urlKey")
}) })
public class Shortcut implements Serializable { public class Shortcut implements Serializable {
private static final long serialVersionUID = -5674633339633714327L; private static final long serialVersionUID = -5674633339633714327L;
@Id @Id
@Column(name = "SHORTCUTS_ID") @Column(name = "SHORTCUT_ID")
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private long shortcutId; private long shortcutId;

View File

@ -32,7 +32,7 @@ import javax.inject.Inject;
*/ */
@RequestScoped @RequestScoped
public class ShortcutManager { public class ShortcutManager {
/** /**
* {@link ShortcutRepository} for interacting with the database. * {@link ShortcutRepository} for interacting with the database.
*/ */
@ -42,11 +42,21 @@ public class ShortcutManager {
/** /**
* Creates a Shortcut * Creates a Shortcut
* *
* @param url * @param url The URL of the Shortcut. Can't be null.
* @param redirect * @param redirect The URL to which the Shortcut redirects. Can't be null.
* @return the new Shortcut * @return the new Shortcut
*/ */
public Shortcut createShortcut(final String url, final String redirect) { public Shortcut createShortcut(final String url, final String redirect) {
if (url == null || url.trim().isEmpty()) {
throw new IllegalArgumentException(
"The URL key of a Shortcut can't be empty");
}
if (redirect == null || redirect.trim().isEmpty()) {
throw new IllegalArgumentException(
"The redirect target of a Shortcut can't be empty");
}
Shortcut shortcut = new Shortcut(); Shortcut shortcut = new Shortcut();
shortcut.setUrlKey(url); shortcut.setUrlKey(url);
shortcut.setRedirect(redirect); shortcut.setRedirect(redirect);
@ -56,30 +66,50 @@ public class ShortcutManager {
/** /**
* Creates a Shortcut * Creates a Shortcut
* *
* @param url * @param url The URL of the Shortcut. Can't be null.
* @param redirect * @param redirect The URL to which the Shortcut redirects. Can't be null.
* @return the new Shortcut * @return the new Shortcut
*/ */
public Shortcut createShortcut(final URL url, final URL redirect) { // public Shortcut createShortcut(final URL url, final URL redirect) {
Shortcut shortcut = new Shortcut(); // if (url == null) {
shortcut.setUrlKey(url.toString()); // throw new IllegalArgumentException(
shortcut.setRedirect(redirect.toString()); // "The URL key of a Shortcut can't be empty");
return shortcut; // }
} //
// if (redirect == null) {
// throw new IllegalArgumentException(
// "The redirect target of a Shortcut can't be empty");
// }
//
// Shortcut shortcut = new Shortcut();
// shortcut.setUrlKey(url.toString());
// shortcut.setRedirect(redirect.toString());
// return shortcut;
// }
/** /**
* Creates a Shortcut * Creates a Shortcut
* *
* @param uri * @param uri The URI of the Shortcut. Can't be null.
* @param redirect * @param redirect The URI to which the Shortcut redirects. Can't be null.
* @return the new Shortcut * @return the new Shortcut
*/ */
public Shortcut createShortcut(final URI uri, final URI redirect) { // public Shortcut createShortcut(final URI uri, final URI redirect) {
Shortcut shortcut = new Shortcut(); // if (uri == null) {
shortcut.setUrlKey(uri.toString()); // throw new IllegalArgumentException(
shortcut.setRedirect(redirect.toString()); // "The URL key of a Shortcut can't be empty");
return shortcut; // }
} //
// if (redirect == null) {
// throw new IllegalArgumentException(
// "The redirect target of a Shortcut can't be empty");
// }
//
// Shortcut shortcut = new Shortcut();
// shortcut.setUrlKey(uri.toString());
// shortcut.setRedirect(redirect.toString());
// return shortcut;
// }
/** /**
* checks if the Shortcut exists. * checks if the Shortcut exists.
@ -87,6 +117,7 @@ public class ShortcutManager {
* @return true if the Shortcut exists * @return true if the Shortcut exists
*/ */
private boolean testShortcut(final Shortcut shortcut) { private boolean testShortcut(final Shortcut shortcut) {
//ToDo
return true; return true;
} }
@ -103,5 +134,4 @@ public class ShortcutManager {
} }
} }

View File

@ -49,7 +49,7 @@ public class ShortcutRepository extends AbstractEntityRepository<Long, Shortcut>
* *
* @param urlKey the wanted urlKey * @param urlKey the wanted urlKey
* *
* @return The shortcut with the specified urlKey if there is any. * @return The shortcut with the specified {@code urlKey} if there is any.
*/ */
public Optional<Shortcut> findByUrlKey(final String urlKey) { public Optional<Shortcut> findByUrlKey(final String urlKey) {
final TypedQuery<Shortcut> query = getEntityManager().createNamedQuery( final TypedQuery<Shortcut> query = getEntityManager().createNamedQuery(
@ -68,7 +68,7 @@ public class ShortcutRepository extends AbstractEntityRepository<Long, Shortcut>
* Finds all shortcuts which redirect to the provided target. * Finds all shortcuts which redirect to the provided target.
* *
* @param redirect the wanted redirect * @param redirect the wanted redirect
* @return List<Shortcut> a List of Shortcuts with the specified redirect * @return a List of Shortcuts with the specified {@code redirect}
*/ */
public List<Shortcut> findByRedirect(final String redirect) { public List<Shortcut> findByRedirect(final String redirect) {
final TypedQuery<Shortcut> query = getEntityManager().createNamedQuery( final TypedQuery<Shortcut> query = getEntityManager().createNamedQuery(

View File

@ -0,0 +1,9 @@
create table SHORTCUTS (
shortcut_id bigint generated by default as identity,
redirect varchar(1024),
url_key varchar(1024),
primary key (shortcut_id)
);
alter table CCM_SHORTCUTS.SHORTCUTS
add constraint UK_4otuwtog6qqdbg4e6p8xdpw8h unique (URL_KEY);

View File

@ -0,0 +1,9 @@
create table SHORTCUTS (
shortcut_id int8 not null,
redirect varchar(1024),
url_key varchar(1024),
primary key (shortcut_id)
);
alter table CCM_SHORTCUTS.SHORTCUTS
add constraint UK_4otuwtog6qqdbg4e6p8xdpw8h unique (URL_KEY);

View File

@ -0,0 +1,81 @@
/*
* 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.libreccm.shortcuts;
import java.util.Arrays;
import java.util.Collection;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.libreccm.tests.categories.UnitTest;
import org.libreccm.testutils.DatasetType;
import org.libreccm.testutils.DatasetsVerifier;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RunWith(Parameterized.class)
@Category(UnitTest.class)
public class DatasetsTest extends DatasetsVerifier {
@Parameterized.Parameters(name = "Dataset {0}")
public static Collection<String> data() {
return Arrays.asList(new String[]{
"/datasets/org/libreccm/shortcuts/ShortcutRepositoryTest/data.xml",
"/datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml",
"/datasets/org/libreccm/shortcuts/ShortcutManagerTest/after-create.xml"
});
}
public DatasetsTest(final String datasetPath) {
super(datasetPath);
}
@Override
public String[] getSchemas() {
return new String[]{"ccm_core", "ccm_shortcuts"};
}
@Override
public DatasetType getDatasetType() {
return DatasetType.FLAT_XML;
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
}

View File

@ -0,0 +1,286 @@
/*
* 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.libreccm.shortcuts;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
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.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;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.libreccm.tests.categories.IntegrationTest;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@Category(IntegrationTest.class)
@RunWith(Arquillian.class)
@PersistenceTest
@Transactional(TransactionMode.COMMIT)
@CreateSchema({"create_ccm_shortcuts_schema.sql"})
public class ShortcutManagerTest {
@Inject
private ShortcutManager shortcutManager;
@PersistenceContext
private EntityManager entityManager;
public ShortcutManagerTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Deployment
public static WebArchive createDeployment() {
final PomEquippedResolveStage pom = Maven
.resolver()
.loadPomFromFile("pom.xml");
final PomEquippedResolveStage dependencies = pom
.importCompileAndRuntimeDependencies();
dependencies.addDependency(MavenDependencies.createDependency(
"org.libreccm:ccm-core", ScopeType.RUNTIME, false));
dependencies.addDependency(MavenDependencies.createDependency(
"org.libreccm:ccm-testutils", ScopeType.RUNTIME, false));
dependencies.addDependency(MavenDependencies.createDependency(
"net.sf.saxon:Saxon-HE", ScopeType.RUNTIME, false));
dependencies.addDependency(MavenDependencies.createDependency(
"org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-impl-maven",
ScopeType.RUNTIME, false));
final File[] libsWithCcmCore = dependencies.resolve().withTransitivity().
asFile();
final List<File> libsList = new ArrayList<>(libsWithCcmCore.length - 1);
IntStream.range(0, libsWithCcmCore.length).forEach(i -> {
final File lib = libsWithCcmCore[i];
if (!lib.getName().startsWith("ccm-core")) {
libsList.add(lib);
}
});
final File[] libs = libsList.toArray(new File[libsList.size()]);
for (File lib : libs) {
System.err.printf("Adding file '%s' to test archive...%n",
lib.getName());
}
return ShrinkWrap.create(
WebArchive.class,
"LibreCCM-org.libreccm.shortcuts.ShortcutTest-web.war")
.addPackage(org.libreccm.categorization.Categorization.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.l10n.LocalizedString.class
.getPackage())
.addPackage(org.libreccm.security.Permission.class.getPackage())
.addPackage(org.libreccm.shortcuts.Shortcuts.class.getPackage())
.addPackage(org.libreccm.web.CcmApplication.class.getPackage())
.addPackage(org.libreccm.workflow.Workflow.class.getPackage())
.addAsLibraries(libs)
.addAsResource("test-persistence.xml",
"META-INF/persistence.xml")
.addAsWebInfResource("test-web.xml", "WEB-INF/web.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml");
}
@Test
@InSequence(1)
public void managerIsInjected() {
assertThat(shortcutManager, is(not(nullValue())));
}
@Test
@InSequence(2)
public void entityManagerIsInjected() {
assertThat(entityManager, is(not((nullValue()))));
}
@Test
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
@ShouldMatchDataSet(
value = "datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml",
excludeColumns = {"shortcut_id"})
@InSequence(100)
public void createShortcutStringParams() {
shortcutManager.createShortcut("datenschutz",
"/ccm/navigation/privacy");
}
@Test(expected = IllegalArgumentException.class)
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
@InSequence(110)
public void createShortcutStringParamsNullUrlKey() {
shortcutManager.createShortcut(null, "http://www.example.org");
}
@Test(expected = IllegalArgumentException.class)
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
@InSequence(120)
public void createShortcutStringParamsNullRedirect() {
shortcutManager.createShortcut("example", null);
}
@Test(expected = IllegalArgumentException.class)
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
@InSequence(140)
public void createShortcutStringParamsEmptyUrlKey() {
shortcutManager.createShortcut(" ", "http://www.example.org");
}
@Test(expected = IllegalArgumentException.class)
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
@InSequence(150)
public void createShortcutStringParamsEmptyRedirect() {
shortcutManager.createShortcut("example", " ");
}
@Test(expected = IllegalArgumentException.class)
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
@InSequence(160)
public void createShortcutStringParamsEmptyParams() {
shortcutManager.createShortcut("", "");
}
// @Test
// @UsingDataSet(
// "datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
// @ShouldMatchDataSet(
// value = "datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml",
// excludeColumns = {"shortcut_id"})
// @InSequence(200)
// public void createShortcutUrlParams() throws MalformedURLException {
// final URL urlKey = new URL("datenschutz");
// final URL redirect = new URL("/ccm/navigation/privacy");
//
// shortcutManager.createShortcut(urlKey, redirect);
// }
//
// @Test(expected = IllegalArgumentException.class)
// @UsingDataSet(
// "datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
// @ShouldThrowException(IllegalArgumentException.class)
// @InSequence(210)
// public void createShortcutUrlParamsNullUrlKey() throws MalformedURLException {
// shortcutManager.createShortcut(null, new URL("http://www.example.org"));
// }
//
// @Test(expected = IllegalArgumentException.class)
// @UsingDataSet(
// "datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
// @ShouldThrowException(IllegalArgumentException.class)
// @InSequence(220)
// public void createShortcutUrlParamsNullRedirect() throws
// MalformedURLException {
// shortcutManager.createShortcut(new URL("example"), null);
// }
//
// @Test
// @UsingDataSet(
// "datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
// @ShouldMatchDataSet(
// value = "datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml",
// excludeColumns = {"shortcut_id"})
// @InSequence(300)
// public void createShortcutUriParams() throws URISyntaxException {
// final URI urlKey = new URI("datenschutz");
// final URI redirect = new URI("/ccm/navigation/privacy");
//
// shortcutManager.createShortcut(urlKey, redirect);
// }
//
// @Test(expected = IllegalArgumentException.class)
// @UsingDataSet(
// "datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
// @ShouldThrowException(IllegalArgumentException.class)
// @InSequence(310)
// public void createShortcutUriParamsNullUrlKey() throws URISyntaxException {
// shortcutManager.createShortcut(null, new URI("http://www.example.org"));
// }
//
// @Test(expected = IllegalArgumentException.class)
// @UsingDataSet(
// "datasets/org/libreccm/shortcuts/ShortcutManagerTest/data.xml")
// @ShouldThrowException(IllegalArgumentException.class)
// @InSequence(320)
// public void createShortcutUriParamsNullRedirect() throws URISyntaxException {
// shortcutManager.createShortcut(new URI("example"), null);
// }
}

View File

@ -27,14 +27,11 @@ import org.jboss.arquillian.transaction.api.annotation.TransactionMode;
import org.jboss.arquillian.transaction.api.annotation.Transactional; import org.jboss.arquillian.transaction.api.annotation.Transactional;
import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive; import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven; import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage; import org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage;
import org.jboss.shrinkwrap.resolver.api.maven.ScopeType; import org.jboss.shrinkwrap.resolver.api.maven.ScopeType;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependencies; import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependencies;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependency;
import org.jboss.shrinkwrap.resolver.impl.maven.coordinate.MavenDependencyImpl;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
@ -45,10 +42,15 @@ import org.junit.runner.RunWith;
import org.libreccm.tests.categories.IntegrationTest; import org.libreccm.tests.categories.IntegrationTest;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import org.jboss.arquillian.persistence.UsingDataSet;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -90,51 +92,59 @@ public class ShortcutRepositoryTest {
} }
@Deployment @Deployment
public static EnterpriseArchive createDeployment() { public static WebArchive createDeployment() {
final PomEquippedResolveStage pom = Maven final PomEquippedResolveStage pom = Maven
.resolver() .resolver()
.loadPomFromFile("pom.xml"); .loadPomFromFile("pom.xml");
final PomEquippedResolveStage dependencies = pom final PomEquippedResolveStage dependencies = pom
.importCompileAndRuntimeDependencies(); .importCompileAndRuntimeDependencies();
dependencies.addDependency(MavenDependencies.createDependency( dependencies.addDependency(MavenDependencies.createDependency(
"org.libreccm:ccm-core", ScopeType.RUNTIME, false)); "org.libreccm:ccm-core", ScopeType.RUNTIME, false));
dependencies.addDependency(MavenDependencies.createDependency( dependencies.addDependency(MavenDependencies.createDependency(
"org.libreccm:ccm-testutils", ScopeType.RUNTIME, false)); "org.libreccm:ccm-testutils", ScopeType.RUNTIME, false));
dependencies.addDependency(MavenDependencies.createDependency( dependencies.addDependency(MavenDependencies.createDependency(
"net.sf.saxon:Saxon-HE", ScopeType.RUNTIME, false)); "net.sf.saxon:Saxon-HE", ScopeType.RUNTIME, false));
final File[] libs = dependencies.resolve().withTransitivity().asFile(); dependencies.addDependency(MavenDependencies.createDependency(
"org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-impl-maven",
ScopeType.RUNTIME, false));
final File[] libsWithCcmCore = dependencies.resolve().withTransitivity().
asFile();
final List<File> libsList = new ArrayList<>(libsWithCcmCore.length - 1);
IntStream.range(0, libsWithCcmCore.length).forEach(i -> {
final File lib = libsWithCcmCore[i];
if (!lib.getName().startsWith("ccm-core")) {
libsList.add(lib);
}
});
final File[] libs = libsList.toArray(new File[libsList.size()]);
for (File lib : libs) { for (File lib : libs) {
System.err.printf("Adding file '%s' to test archive...%n", System.err.printf("Adding file '%s' to test archive...%n",
lib.getName()); lib.getName());
} }
final WebArchive webArchive = ShrinkWrap.create(
WebArchive.class,
"LibreCCM-org.libreccm.shortcuts.ShortcutTest-web.war")
// .addPackage(org.libreccm.categorization.Categorization.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.l10n.LocalizedString.class.getPackage())
// .addPackage(org.libreccm.security.Permission.class.getPackage())
.addPackage(org.libreccm.shortcuts.Shortcuts.class.getPackage())
// .addPackage(org.libreccm.web.CcmApplication.class.getPackage())
// .addPackage(org.libreccm.workflow.Workflow.class.getPackage())
.addAsLibraries(libs)
.addAsResource("test-persistence.xml",
"META-INF/persistence.xml")
.addAsWebInfResource("test-web.xml", "WEB-INF/web.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml");
return ShrinkWrap.create( return ShrinkWrap.create(
EnterpriseArchive.class, WebArchive.class,
"LibreCCM-org.libreccm.shortcuts.ShortcutRepositoryTest.ear") "LibreCCM-org.libreccm.shortcuts.ShortcutTest-web.war")
.addAsModule(webArchive) .addPackage(org.libreccm.categorization.Categorization.class
.addAsResource("application.xml", "application.xml"); .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.l10n.LocalizedString.class
.getPackage())
.addPackage(org.libreccm.security.Permission.class.getPackage())
.addPackage(org.libreccm.shortcuts.Shortcuts.class.getPackage())
.addPackage(org.libreccm.web.CcmApplication.class.getPackage())
.addPackage(org.libreccm.workflow.Workflow.class.getPackage())
.addAsLibraries(libs)
.addAsResource("test-persistence.xml",
"META-INF/persistence.xml")
.addAsWebInfResource("test-web.xml", "WEB-INF/web.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml");
} }
@Test @Test
@ -149,4 +159,80 @@ public class ShortcutRepositoryTest {
assertThat(entityManager, is(not((nullValue())))); assertThat(entityManager, is(not((nullValue()))));
} }
@Test
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutRepositoryTest/data.xml")
@InSequence(10)
public void findByUrlKey() {
final Optional<Shortcut> members = shortcutRepository.findByUrlKey(
"members");
final Optional<Shortcut> mitglieder = shortcutRepository.findByUrlKey(
"mitglieder");
final Optional<Shortcut> shop = shortcutRepository.findByUrlKey("shop");
assertThat(members.isPresent(), is(true));
assertThat(members.get().getUrlKey(), is(equalTo("members")));
assertThat(members.get().getRedirect(),
is(equalTo("/ccm/navigation/members")));
assertThat(mitglieder.isPresent(), is(true));
assertThat(mitglieder.get().getUrlKey(), is(equalTo("mitglieder")));
assertThat(mitglieder.get().getRedirect(),
is(equalTo("/ccm/navigation/members")));
assertThat(shop.isPresent(), is(true));
assertThat(shop.get().getUrlKey(),
is(equalTo("shop")));
assertThat(shop.get().getRedirect(),
is(equalTo("http://www.example.com")));
}
@Test
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutRepositoryTest/data.xml")
@InSequence(10)
public void findByUrlKeyNotExisting() {
final Optional<Shortcut> result = shortcutRepository.findByUrlKey(
"foo");
assertThat(result, is(not(nullValue())));
assertThat(result.isPresent(), is(false));
}
@Test
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutRepositoryTest/data.xml")
@InSequence(30)
public void findByRedirect() {
final List<Shortcut> toMembers = shortcutRepository.findByRedirect(
"/ccm/navigation/members");
assertThat(toMembers.size(), is(2));
assertThat(toMembers.get(0).getUrlKey(), is(equalTo("members")));
assertThat(toMembers.get(0).getRedirect(),
is(equalTo("/ccm/navigation/members")));
assertThat(toMembers.get(1).getUrlKey(), is(equalTo("mitglieder")));
assertThat(toMembers.get(1).getRedirect(),
is(equalTo("/ccm/navigation/members")));
final List<Shortcut> toExampleCom = shortcutRepository.findByRedirect(
"http://www.example.com");
assertThat(toExampleCom.size(), is(1));
assertThat(toExampleCom.get(0).getUrlKey(), is(equalTo("shop")));
assertThat(toExampleCom.get(0).getRedirect(),
is(equalTo("http://www.example.com")));
}
@Test
@UsingDataSet(
"datasets/org/libreccm/shortcuts/ShortcutRepositoryTest/data.xml")
@InSequence(30)
public void findByRedirectNotExisting() {
final List<Shortcut> result = shortcutRepository.findByRedirect(
"http://www.example.org");
assertThat(result, is(not(nullValue())));
assertThat(result.isEmpty(), is(true));
}
} }

View File

@ -624,14 +624,15 @@ CREATE SCHEMA ccm_shortcuts;
add constraint UK5whinfxdaepqs09e5ia9y71uk unique (CONFIGURATION_CLASS, NAME); add constraint UK5whinfxdaepqs09e5ia9y71uk unique (CONFIGURATION_CLASS, NAME);
create table CCM_SHORTCUTS.SHORTCUTS ( create table CCM_SHORTCUTS.SHORTCUTS (
SHORTCUTS_ID bigint not null, SHORTCUT_ID bigint not null,
REDIRECT varchar(1024), REDIRECT varchar(1024),
URL_KEY varchar(1024), URL_KEY varchar(1024),
primary key (SHORTCUTS_ID) primary key (SHORTCUT_ID)
); );
alter table CCM_SHORTCUTS.SHORTCUTS alter table CCM_SHORTCUTS.SHORTCUTS
add constraint UK_4otuwtog6qqdbg4e6p8xdpw8h unique (URL_KEY); add constraint UK_4otuwtog6qqdbg4e6p8xdpw8h unique (URL_KEY);
create sequence hibernate_sequence start with 1 increment by 1; create sequence hibernate_sequence start with 1 increment by 1;
alter table CCM_CORE.APPLICATIONS alter table CCM_CORE.APPLICATIONS

View File

@ -624,10 +624,10 @@ CREATE SCHEMA ccm_shortcuts;
add constraint UK5whinfxdaepqs09e5ia9y71uk unique (CONFIGURATION_CLASS, NAME); add constraint UK5whinfxdaepqs09e5ia9y71uk unique (CONFIGURATION_CLASS, NAME);
create table CCM_SHORTCUTS.SHORTCUTS ( create table CCM_SHORTCUTS.SHORTCUTS (
SHORTCUTS_ID int8 not null, SHORTCUT_ID int8 not null,
REDIRECT varchar(1024), REDIRECT varchar(1024),
URL_KEY varchar(1024), URL_KEY varchar(1024),
primary key (SHORTCUTS_ID) primary key (SHORTCUT_ID)
); );
alter table CCM_SHORTCUTS.SHORTCUTS alter table CCM_SHORTCUTS.SHORTCUTS

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<ccm_shortcuts.shortcuts shortcut_id="-10"
url_key="mitglieder"
redirect="/ccm/navigation/members" />
<ccm_shortcuts.shortcuts shortcut_id="-20"
url_key="members"
redirect="/ccm/navigation/members" />
<ccm_shortcuts.shortcuts shortcut_id="-30"
url_key="privacy"
redirect="/ccm/navigation/privacy" />
<ccm_shortcuts.shortcuts shortcut_id="-40"
url_key="shop"
redirect="http://www.example.com" />
<ccm_shortcuts.shortcuts shortcut_id="-50"
url_key="datenschutz"
redirect="/ccm/navigation/privacy" />
</dataset>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<ccm_shortcuts.shortcuts shortcut_id="-10"
url_key="mitglieder"
redirect="/ccm/navigation/members" />
<ccm_shortcuts.shortcuts shortcut_id="-20"
url_key="members"
redirect="/ccm/navigation/members" />
<ccm_shortcuts.shortcuts shortcut_id="-30"
url_key="privacy"
redirect="/ccm/navigation/privacy" />
<ccm_shortcuts.shortcuts shortcut_id="-40"
url_key="shop"
redirect="http://www.example.com" />
</dataset>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<ccm_shortcuts.shortcuts shortcut_id="-10"
url_key="mitglieder"
redirect="/ccm/navigation/members" />
<ccm_shortcuts.shortcuts shortcut_id="-20"
url_key="members"
redirect="/ccm/navigation/members" />
<ccm_shortcuts.shortcuts shortcut_id="-30"
url_key="privacy"
redirect="/ccm/navigation/privacy" />
<ccm_shortcuts.shortcuts shortcut_id="-40"
url_key="shop"
redirect="http://www.example.com" />
</dataset>

View File

@ -108,7 +108,10 @@ public class DatasetsVerifier {
if (getSchemas().length > 0) { if (getSchemas().length > 0) {
buffer.append(";INIT="); buffer.append(";INIT=");
for (final String schema : getSchemas()) { for (final String schema : getSchemas()) {
buffer.append(String.format("CREATE SCHEMA IF NOT EXISTS %s;", if (buffer.length() > 0) {
buffer.append("\\;");
}
buffer.append(String.format("CREATE SCHEMA IF NOT EXISTS %s",
schema)); schema));
} }
} }