CCM NG: Test for importing GroupMemberships and some fixes for import procdure

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5764 8810af33-2d31-482b-a856-94f89814c4df
jensp 2018-12-02 08:21:16 +00:00
parent 34150d5349
commit 6f90f3acfb
10 changed files with 479 additions and 12 deletions

View File

@ -25,6 +25,7 @@ import java.io.IOException;
import java.util.Set;
import javax.enterprise.context.RequestScoped;
import javax.transaction.Transactional;
/**
* Base class for importers/exporters. Implementations must be annotated with
@ -69,6 +70,7 @@ public abstract class AbstractEntityImExporter<T extends Exportable> {
*/
protected abstract Set<Class<? extends Exportable>> getRequiredEntities();
@Transactional(Transactional.TxType.REQUIRED)
public T importEntity(final String data) throws ImportExpection {
try {
@ -81,6 +83,7 @@ public abstract class AbstractEntityImExporter<T extends Exportable> {
}
@Transactional(Transactional.TxType.REQUIRED)
public String exportEntity(final Exportable entity) throws ExportException {
try {

View File

@ -49,6 +49,7 @@ public class GroupImExporter extends AbstractEntityImExporter<Group>{
@Transactional(Transactional.TxType.REQUIRED)
protected void saveImportedEntity(final Group entity) {
entity.setPartyId(0);
groupRepository.save(entity);
}

View File

@ -59,6 +59,7 @@ public class GroupMembershipImExporter extends AbstractEntityImExporter<GroupMem
@Transactional(Transactional.TxType.REQUIRED)
protected void saveImportedEntity(final GroupMembership entity) {
entity.setMembershipId(0);
entityManager.persist(entity);
}

View File

@ -80,7 +80,7 @@ public class GroupImportTest {
private static final String IMPORT_MANIFEST_SOURCE = "/imports"
+ "/org.libreccm.imexport.GroupImportTest"
+ "/ccm-export.json";
private static final String IMPORT_USERS_TOC_SOURCE = "/imports"
private static final String IMPORT_GROUPS_TOC_SOURCE = "/imports"
+ "/org.libreccm.imexport.GroupImportTest"
+ "/org.libreccm.security.Group"
+ "/org.libreccm.security.Group.json";
@ -92,9 +92,9 @@ public class GroupImportTest {
private static final String TMP_DIR = System.getProperty("java.io.tmpdir");
private static final String CCM_TESTS_DIR = TMP_DIR + "/ccm-tests";
private static final String IMPORTS_DIR = CCM_TESTS_DIR + "/imports";
private static final String USER_IMPORT_TEST_DIR = IMPORTS_DIR
private static final String GROUP_IMPORT_TEST_DIR = IMPORTS_DIR
+ "/org.libreccm.imexport.GroupImportTest";
private static final String IMPORT_DATA_DIR = USER_IMPORT_TEST_DIR
private static final String IMPORT_DATA_DIR = GROUP_IMPORT_TEST_DIR
+ "/org.libreccm.security.Group";
@Inject
@ -128,7 +128,7 @@ public class GroupImportTest {
// final Path tmpDirPath = Paths.get(TMP_DIR);
final Path ccmTestsDirPath = Paths.get(CCM_TESTS_DIR);
final Path importsPath = Paths.get(IMPORTS_DIR);
final Path userImportsTestDirPath = Paths.get(USER_IMPORT_TEST_DIR);
final Path groupImportsTestDirPath = Paths.get(GROUP_IMPORT_TEST_DIR);
final Path importDataPath = Paths.get(IMPORT_DATA_DIR);
if (Files.exists(ccmTestsDirPath)) {
@ -138,26 +138,26 @@ public class GroupImportTest {
Files.createDirectory(ccmTestsDirPath);
Files.createDirectory(importsPath);
Files.createDirectory(userImportsTestDirPath);
Files.createDirectory(groupImportsTestDirPath);
Files.createDirectory(importDataPath);
final InputStream manifestInputStream = getClass()
.getResourceAsStream(IMPORT_MANIFEST_SOURCE);
final InputStream usersTocInputStream = getClass()
.getResourceAsStream(IMPORT_USERS_TOC_SOURCE);
final InputStream groupsTocInputStream = getClass()
.getResourceAsStream(IMPORT_GROUPS_TOC_SOURCE);
final InputStream group1DataInputStream = getClass()
.getResourceAsStream(IMPORT_DATA_SOURCE);
final Path manifestTargetPath = userImportsTestDirPath
final Path manifestTargetPath = groupImportsTestDirPath
.resolve("ccm-export.json");
final Path usersTocTargetPath = importDataPath
final Path groupsTocTargetPath = importDataPath
.resolve("org.libreccm.security.Group.json");
final Path user1DataTargetPath = importDataPath
final Path group1DataTargetPath = importDataPath
.resolve("696592cd-db19-4aca-bb14-06490cfde243.json");
copy(manifestInputStream, manifestTargetPath);
copy(usersTocInputStream, usersTocTargetPath);
copy(group1DataInputStream, user1DataTargetPath);
copy(groupsTocInputStream, groupsTocTargetPath);
copy(group1DataInputStream, group1DataTargetPath);
}
private void copy(final InputStream source, final Path destination) {

View File

@ -0,0 +1,304 @@
/*
* Copyright (C) 2018 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.imexport;
import static org.libreccm.testutils.DependenciesHelpers.*;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.jboss.arquillian.persistence.CleanupUsingScript;
import org.jboss.arquillian.persistence.CreateSchema;
import org.jboss.arquillian.persistence.PersistenceTest;
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
import org.jboss.arquillian.persistence.TestExecutionPhase;
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.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.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.libreccm.configuration.ConfigurationManager;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.files.CcmFilesConfiguration;
import org.libreccm.security.Shiro;
import org.libreccm.tests.categories.IntegrationTest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import javax.inject.Inject;
import static org.hamcrest.Matchers.*;
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_core_schema.sql")
@CleanupUsingScript(value = {"cleanup.sql"},
phase = TestExecutionPhase.BEFORE)
public class GroupMembershipImportTest {
private static final String IMPORT_MANIFEST_SOURCE = "/imports"
+ "/org.libreccm.imexport.GroupMembershipImportTest"
+ "/ccm-export.json";
private static final String IMPORT_GROUPMEMBERSHIP_TOC_SOURCE = "/imports"
+ "/org.libreccm.imexport.GroupMembershipImportTest"
+ "/org.libreccm.security.GroupMembership"
+ "/org.libreccm.security.GroupMembership.json";
private static final String IMPORT_GROUPMEMBERSHIP_DATA_SOURCE = "/imports"
+ "/org.libreccm.imexport.GroupMembershipImportTest"
+ "/org.libreccm.security.GroupMembership"
+ "/f8ac4073-447e-4bd6-ac96-3bf92bdc8ce7.json";
private static final String TMP_DIR = System.getProperty("java.io.tmpdir");
private static final String CCM_TESTS_DIR = TMP_DIR + "/ccm-tests";
private static final String IMPORTS_DIR = CCM_TESTS_DIR + "/imports";
private static final String GROUPMEMBERSHIP_IMPORT_TEST_DIR = IMPORTS_DIR
+ "/org.libreccm.imexport.GroupMembershipImportTest";
private static final String IMPORT_GROUPMEMBERSHIP_DATA_DIR
= GROUPMEMBERSHIP_IMPORT_TEST_DIR
+ "/org.libreccm.security.GroupMembership";
@Inject
private ConfigurationManager confManager;
@Inject
private ImportExport importExport;
@Inject
private Shiro shiro;
public GroupMembershipImportTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() throws IOException, URISyntaxException {
final CcmFilesConfiguration filesConf = confManager
.findConfiguration(CcmFilesConfiguration.class);
filesConf.setDataPath(CCM_TESTS_DIR);
final Path ccmTestsDirPath = Paths.get(CCM_TESTS_DIR);
final Path importsPath = Paths.get(IMPORTS_DIR);
final Path groupMembershipsImportsTestDirPath = Paths.get(
GROUPMEMBERSHIP_IMPORT_TEST_DIR);
final Path importGroupMembershipDataPath = Paths.get(
IMPORT_GROUPMEMBERSHIP_DATA_DIR);
if (Files.exists(ccmTestsDirPath)) {
Files.walkFileTree(ccmTestsDirPath,
new DeleteDirectoryVisitor());
}
Files.createDirectory(ccmTestsDirPath);
Files.createDirectory(importsPath);
Files.createDirectory(groupMembershipsImportsTestDirPath);
Files.createDirectory(importGroupMembershipDataPath);
final InputStream manifestInputStream = getClass()
.getResourceAsStream(IMPORT_MANIFEST_SOURCE);
final InputStream groupMembershipsTocInputStream = getClass()
.getResourceAsStream(IMPORT_GROUPMEMBERSHIP_TOC_SOURCE);
final InputStream groupMembership1DataInputStream = getClass()
.getResourceAsStream(IMPORT_GROUPMEMBERSHIP_DATA_SOURCE);
final Path manifestTargetPath = groupMembershipsImportsTestDirPath
.resolve("ccm-export.json");
final Path groupMembershipsTocTargetPath = importGroupMembershipDataPath
.resolve("org.libreccm.security.GroupMembership.json");
final Path groupMembership1DataTargetPath
= importGroupMembershipDataPath
.resolve("f8ac4073-447e-4bd6-ac96-3bf92bdc8ce7.json");
copy(manifestInputStream, manifestTargetPath);
copy(groupMembershipsTocInputStream, groupMembershipsTocTargetPath);
copy(groupMembership1DataInputStream, groupMembership1DataTargetPath);
}
private void copy(final InputStream source, final Path destination) {
try (final OutputStream outputStream = new FileOutputStream(
destination.toFile())) {
int data = source.read();
while (data != -1) {
outputStream.write(data);
data = source.read();
}
} catch (IOException ex) {
throw new UnexpectedErrorException(ex);
}
}
@After
public void tearDown() throws IOException {
// final Path ccmTestsDirPath = Paths.get(CCM_TESTS_DIR);
// Files.walkFileTree(ccmTestsDirPath, new DeleteDirectoryVisitor());
}
private class DeleteDirectoryVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(
final Path file, final BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(
final Path dir, final IOException exc)
throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
}
@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap
.create(WebArchive.class,
"LibreCCM-org.libreccm.imexport.GroupImportTest.war")
.addPackage(org.libreccm.cdi.utils.CdiUtil.class.getPackage())
.addPackage(org.libreccm.core.CcmObject.class.getPackage())
.addPackage(org.libreccm.categorization.Categorization.class
.getPackage())
.addPackage(org.libreccm.configuration.Configuration.class
.getPackage())
.addPackage(org.libreccm.files.CcmFiles.class.getPackage())
.addPackage(org.libreccm.l10n.LocalizedString.class.getPackage())
.addPackage(org.libreccm.imexport.Exportable.class.getPackage())
.addPackage(org.libreccm.jpa.EntityManagerProducer.class
.getPackage())
.addPackage(org.libreccm.jpa.utils.MimeTypeConverter.class
.getPackage())
.addPackage(org.libreccm.security.Group.class.getPackage())
.addPackage(org.libreccm.web.CcmApplication.class.getPackage())
.addPackage(org.libreccm.workflow.Workflow.class.getPackage())
.addPackage(org.libreccm.testutils.EqualsVerifier.class
.getPackage())
.addPackage(org.libreccm.tests.categories.IntegrationTest.class
.getPackage())
.addClass(com.arsdigita.kernel.security.SecurityConfig.class)
.addClass(com.arsdigita.kernel.KernelConfig.class)
.addAsLibraries(getModuleDependencies())
.addAsResource("configs/shiro.ini", "shiro.ini")
.addAsResource("imports", "imports")
.addAsResource("test-persistence.xml",
"META-INF/persistence.xml")
.addAsWebInfResource("test-web.xml", "web.xml")
.addAsWebInfResource("META-INF/beans.xml", "beans.xml");
}
@Test
@InSequence(100)
public void checkIfFilesAreAvailable() {
final Path importDataPath = Paths.get(
TMP_DIR,
"ccm-tests",
"imports",
"org.libreccm.imexport.GroupMembershipImportTest");
final Path manifestPath = importDataPath.resolve("ccm-export.json");
final Path groupMembershipTypeDirPath = importDataPath
.resolve("org.libreccm.security.GroupMembership");
final Path groupMembershipDataFile1Path = groupMembershipTypeDirPath
.resolve("f8ac4073-447e-4bd6-ac96-3bf92bdc8ce7.json");
assertThat(String.format("Path %s does not exist.",
manifestPath.toString()),
Files.exists(manifestPath),
is(true));
assertThat(String.format("Path %s does not exist.",
groupMembershipTypeDirPath.toString()),
Files.exists(groupMembershipTypeDirPath),
is(true));
assertThat(String.format("Path %s does not exist.",
groupMembershipDataFile1Path.toString()),
Files.exists(groupMembershipDataFile1Path),
is(true));
}
@Test
@InSequence(150)
public void importsAvailable() {
final List<ImportManifest> imports = importExport
.listAvailableImportArchivies();
assertThat(imports.size(), is(1));
}
@Test
@UsingDataSet(
"datasets/org/libreccm/imexport/GroupMembershipImportTest/data.yml")
@ShouldMatchDataSet(value = "datasets/org/libreccm/imexport/"
+ "GroupMembershipImportTest"
+ "/after-import-single-membership.yml",
excludeColumns = {"party_id", "membership_id"})
@InSequence(200)
public void importSingleGroupMembership() {
shiro.getSystemUser().execute(()
-> importExport.importEntities(
"org.libreccm.imexport.GroupMembershipImportTest")
);
}
}

View File

@ -0,0 +1,70 @@
ccm_core.parties:
# John Doe
- party_id: -10
uuid: 631be113-7e86-453d-9f8b-8cb6cb6df268
name: jdoe
# Max Muster
- party_id: -20
uuid: 3a61d302-97a5-4e46-bbc9-8d716f7c54c4
name: mmuster
# Joe Public
- party_id: -30
uuid: 7d5ad4a7-c2bd-4e49-8716-0bfb40413c75
name: joe
# Group 1
- party_id: -100
uuid: 7200c286-5045-4305-8f40-86cc078b5943
name: group-1
# Group 2
- party_id: -200
uuid: ebdc9a12-8f4e-48e8-92ec-900c65189895
name: group-2
ccm_core.users:
# John Doe
- banned: false
bouncing: false
email_address: john.doe@example.com
family_name: Doe
given_name: John
party_id: -10
# foo123
password: $shiro1$SHA-512$500000$7xkDcZUN0/whJInHIvGsDw==$WhelBVmJU/cLV7lAkMOrE5B/mqCW0bUuid1WX+xBwzzAaekC5bYn9eeOFGJWhiDgmaC50ZCUmM96/iGsRoc4uA==
password_reset_required: false
verified: true
# Max Mustermann
- banned: false
bouncing: false
email_address: max.mustermann@example.org
family_name: Mustermann
given_name: Max
party_id: -20
# foo123
password: $shiro1$SHA-512$500000$Y7CnccN1h25sR7KCElMOXg==$CVLWBhetodaEzzhDfGjRcCFZtSW02xOnjH7xhBx0lbxO66grKIt6LWmXoUhLEydce1JZ7cbzNLYOxIwwTeqi5Q==
password_reset_required: false
verified: true
# Joe Public
- banned: false
bouncing: false
email_address: joe.public@example.com
family_name: Public
given_name: Joe
party_id: -30
password: $shiro1$SHA-512$500000$RUCYXAQt+XzUmj3x8oG5gw==$qU+lX160Jc6sNUOI9X85wlf2lzn4/hLJNURtjmw9LOYJ7vAqUFFmhyNCMxpzuHIpzeMELr+A0XReoSmtcZnOOw==
password_reset_required: false
verified: true
ccm_core.groups:
# Group 1
- party_id: -100
# Group 2
- party_id: -200
ccm_core.group_memberships:
- membership_id: -1000
uuid: b32bd614-00b7-4f1e-b1a3-4fc53f230082
group_id: -200
member_id: -30
- membership_id: -2000
uuid: f8ac4073-447e-4bd6-ac96-3bf92bdc8ce7
group_id: -100
member_id: -10

View File

@ -0,0 +1,65 @@
ccm_core.parties:
# John Doe
- party_id: -10
uuid: 631be113-7e86-453d-9f8b-8cb6cb6df268
name: jdoe
# Max Muster
- party_id: -20
uuid: 3a61d302-97a5-4e46-bbc9-8d716f7c54c4
name: mmuster
# Joe Public
- party_id: -30
uuid: 7d5ad4a7-c2bd-4e49-8716-0bfb40413c75
name: joe
# Group 1
- party_id: -100
uuid: 7200c286-5045-4305-8f40-86cc078b5943
name: group-1
# Group 2
- party_id: -200
uuid: ebdc9a12-8f4e-48e8-92ec-900c65189895
name: group-2
ccm_core.users:
# John Doe
- banned: false
bouncing: false
email_address: john.doe@example.com
family_name: Doe
given_name: John
party_id: -10
# foo123
password: $shiro1$SHA-512$500000$7xkDcZUN0/whJInHIvGsDw==$WhelBVmJU/cLV7lAkMOrE5B/mqCW0bUuid1WX+xBwzzAaekC5bYn9eeOFGJWhiDgmaC50ZCUmM96/iGsRoc4uA==
password_reset_required: false
verified: true
# Max Mustermann
- banned: false
bouncing: false
email_address: max.mustermann@example.org
family_name: Mustermann
given_name: Max
party_id: -20
# foo123
password: $shiro1$SHA-512$500000$Y7CnccN1h25sR7KCElMOXg==$CVLWBhetodaEzzhDfGjRcCFZtSW02xOnjH7xhBx0lbxO66grKIt6LWmXoUhLEydce1JZ7cbzNLYOxIwwTeqi5Q==
password_reset_required: false
verified: true
# Joe Public
- banned: false
bouncing: false
email_address: joe.public@example.com
family_name: Public
given_name: Joe
party_id: -30
password: $shiro1$SHA-512$500000$RUCYXAQt+XzUmj3x8oG5gw==$qU+lX160Jc6sNUOI9X85wlf2lzn4/hLJNURtjmw9LOYJ7vAqUFFmhyNCMxpzuHIpzeMELr+A0XReoSmtcZnOOw==
password_reset_required: false
verified: true
ccm_core.groups:
# Group 1
- party_id: -100
# Group 2
- party_id: -200
ccm_core.group_memberships:
- membership_id: -1000
uuid: b32bd614-00b7-4f1e-b1a3-4fc53f230082
group_id: -200
member_id: -30

View File

@ -0,0 +1,12 @@
{
"created": "2018-12-01T12:00:00",
"onServer": "tests.libreccm.example",
"types": [
"org.libreccm.security.GroupMembership"
],
"entities": {
"org.libreccm.security.GroupMembership": [
"f8ac4073-447e-4bd6-ac96-3bf92bdc8ce7"
]
}
}

View File

@ -0,0 +1,6 @@
{
"membershipId": 123456,
"uuid": "f8ac4073-447e-4bd6-ac96-3bf92bdc8ce7",
"group": "7200c286-5045-4305-8f40-86cc078b5943",
"member": "631be113-7e86-453d-9f8b-8cb6cb6df268"
}