CCM NG: UserImportTest runs successfully

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5747 8810af33-2d31-482b-a856-94f89814c4df
ccm-docs
jensp 2018-11-23 18:51:53 +00:00
parent 52211bba8c
commit 8da6bfa79d
7 changed files with 61 additions and 46 deletions

View File

@ -72,7 +72,9 @@ public abstract class AbstractEntityImExporter<T extends Exportable> {
public T importEntity(final String data) throws ImportExpection { public T importEntity(final String data) throws ImportExpection {
try { try {
return objectMapper.readValue(data, getEntityClass()); final T entity = objectMapper.readValue(data, getEntityClass());
saveImportedEntity(entity);
return entity;
} catch (IOException ex) { } catch (IOException ex) {
throw new ImportExpection(ex); throw new ImportExpection(ex);
} }

View File

@ -76,8 +76,8 @@ final class EntityImExporterTreeManager {
.collect(Collectors.toMap( .collect(Collectors.toMap(
node -> node node -> node
.getEntityImExporter() .getEntityImExporter()
.getClass() .getEntityClass()
.getAnnotation(Processes.class).value().getName(), .getName(),
node -> node)); node -> node));
//Add the dependency relations to the nodes //Add the dependency relations to the nodes
@ -140,7 +140,8 @@ final class EntityImExporterTreeManager {
//Remove the first node from the resolved nodes list //Remove the first node from the resolved nodes list
final EntityImExporterTreeNode current = resolvedNodes.remove(0); final EntityImExporterTreeNode current = resolvedNodes.remove(0);
LOGGER.info("\tProcessing node for EntityImExporter \"{}\"...", LOGGER.info("\tProcessing node for EntityImExporter \"{}\"...",
current.getEntityImExporter().getClass().getName()); current
.getEntityImExporter().getClass().getName());
//Add the node to the ordered modules list. //Add the node to the ordered modules list.
orderedNodes.add(current); orderedNodes.add(current);
@ -197,7 +198,7 @@ final class EntityImExporterTreeManager {
throws DependencyException { throws DependencyException {
//Get the name of the module from the module info. //Get the name of the module from the module info.
final String className = imExporter.getClass().getName(); final String className = imExporter.getEntityClass().getName();
LOGGER LOGGER
.info("Adding dependency relations for EntityImExporter \"{}\"...", .info("Adding dependency relations for EntityImExporter \"{}\"...",
className); className);

View File

@ -57,6 +57,7 @@ import javax.json.JsonArrayBuilder;
import javax.json.JsonObject; import javax.json.JsonObject;
import javax.json.JsonObjectBuilder; import javax.json.JsonObjectBuilder;
import javax.json.JsonReader; import javax.json.JsonReader;
import javax.json.JsonString;
import javax.json.JsonWriter; import javax.json.JsonWriter;
/** /**
@ -276,9 +277,6 @@ public class ImportExport {
.map(EntityImExporterTreeNode::getEntityImExporter) .map(EntityImExporterTreeNode::getEntityImExporter)
.forEach(imExporter -> importEntitiesOfType(importName, .forEach(imExporter -> importEntitiesOfType(importName,
imExporter)); imExporter));
throw new UnsupportedOperationException();
} catch (DependencyException ex) { } catch (DependencyException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
@ -289,9 +287,7 @@ public class ImportExport {
final AbstractEntityImExporter<?> imExporter = node final AbstractEntityImExporter<?> imExporter = node
.getEntityImExporter(); .getEntityImExporter();
final String type = imExporter final String type = imExporter.getEntityClass().getName();
.getClass()
.getAnnotation(Processes.class).value().getName();
return manifest.getTypes().contains(type); return manifest.getTypes().contains(type);
} }
@ -300,9 +296,7 @@ public class ImportExport {
final String importName, final String importName,
final AbstractEntityImExporter<?> entityImExporter) { final AbstractEntityImExporter<?> entityImExporter) {
final String type = entityImExporter final String type = entityImExporter.getEntityClass().getName();
.getClass()
.getAnnotation(Processes.class).value().getName();
try (final InputStream tocInputStream = ccmFiles try (final InputStream tocInputStream = ccmFiles
.createInputStream(String.format("imports/%s/%s/%s.json", .createInputStream(String.format("imports/%s/%s/%s.json",
@ -316,7 +310,7 @@ public class ImportExport {
files.forEach(value -> importEntity(importName, files.forEach(value -> importEntity(importName,
type, type,
value.toString(), ((JsonString) value).getString(),
entityImExporter)); entityImExporter));
} catch (IOException } catch (IOException
@ -429,10 +423,15 @@ public class ImportExport {
final LocalDateTime created = LocalDateTime final LocalDateTime created = LocalDateTime
.parse(manifestJson.getString("created")); .parse(manifestJson.getString("created"));
final String onServer = manifestJson.getString("onServer"); final String onServer = manifestJson.getString("onServer");
final List<String> types = manifestJson.getJsonArray("types") // final List<String> types = manifestJson.getJsonArray("types")
.stream() // .stream()
.map(value -> value.toString()) // .map(value -> value.toString())
.collect(Collectors.toList()); // .collect(Collectors.toList());
final JsonArray typesArray = manifestJson.getJsonArray("types");
final List<String> types = new ArrayList<>();
for(int i = 0; i < typesArray.size(); i++) {
types.add(typesArray.getString(i));
}
return new ImportManifest( return new ImportManifest(
Date.from(created.atZone(ZoneId.of("UTC")).toInstant()), Date.from(created.atZone(ZoneId.of("UTC")).toInstant()),

View File

@ -49,6 +49,8 @@ public class UserImExporter extends AbstractEntityImExporter<User> {
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
protected void saveImportedEntity(final User entity) { protected void saveImportedEntity(final User entity) {
// Reset partyId.
entity.setPartyId(0);
userRepository.save(entity); userRepository.save(entity);
} }

View File

@ -43,7 +43,7 @@ import org.junit.runner.RunWith;
import org.libreccm.configuration.ConfigurationManager; import org.libreccm.configuration.ConfigurationManager;
import org.libreccm.core.UnexpectedErrorException; import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.files.CcmFilesConfiguration; import org.libreccm.files.CcmFilesConfiguration;
import org.libreccm.security.UserRepository; import org.libreccm.security.Shiro;
import org.libreccm.tests.categories.IntegrationTest; import org.libreccm.tests.categories.IntegrationTest;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -60,8 +60,6 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -82,6 +80,10 @@ public class UserImportTest {
private static final String IMPORT_MANIFEST_SOURCE = "/imports" private static final String IMPORT_MANIFEST_SOURCE = "/imports"
+ "/org.libreccm.imexport.UserImportTest" + "/org.libreccm.imexport.UserImportTest"
+ "/ccm-export.json"; + "/ccm-export.json";
private static final String IMPORT_USERS_TOC_SOURCE = "/imports"
+ "/org.libreccm.imexport.UserImportTest"
+ "/org.libreccm.security.User"
+ "/org.libreccm.security.User.json";
private static final String IMPORT_DATA_SOURCE = "/imports" private static final String IMPORT_DATA_SOURCE = "/imports"
+ "/org.libreccm.imexport.UserImportTest" + "/org.libreccm.imexport.UserImportTest"
+ "/org.libreccm.security.User" + "/org.libreccm.security.User"
@ -102,10 +104,7 @@ public class UserImportTest {
private ImportExport importExport; private ImportExport importExport;
@Inject @Inject
private UserRepository userRepository; private Shiro shiro;
@PersistenceContext
private EntityManager entityManager;
public UserImportTest() { public UserImportTest() {
@ -143,15 +142,20 @@ public class UserImportTest {
final InputStream manifestInputStream = getClass() final InputStream manifestInputStream = getClass()
.getResourceAsStream(IMPORT_MANIFEST_SOURCE); .getResourceAsStream(IMPORT_MANIFEST_SOURCE);
final InputStream usersTocInputStream = getClass()
.getResourceAsStream(IMPORT_USERS_TOC_SOURCE);
final InputStream user1DataInputStream = getClass() final InputStream user1DataInputStream = getClass()
.getResourceAsStream(IMPORT_DATA_SOURCE); .getResourceAsStream(IMPORT_DATA_SOURCE);
final Path manifestTargetPath = userImportsTestDirPath final Path manifestTargetPath = userImportsTestDirPath
.resolve("ccm-export.json"); .resolve("ccm-export.json");
final Path usersTocTargetPath = importDataPath
.resolve("org.libreccm.security.User.json");
final Path user1DataTargetPath = importDataPath final Path user1DataTargetPath = importDataPath
.resolve("7cb9aba4-8071-4f27-af19-096e1473d050.json"); .resolve("7cb9aba4-8071-4f27-af19-096e1473d050.json");
copy(manifestInputStream, manifestTargetPath); copy(manifestInputStream, manifestTargetPath);
copy(usersTocInputStream, usersTocTargetPath);
copy(user1DataInputStream, user1DataTargetPath); copy(user1DataInputStream, user1DataTargetPath);
} }
@ -283,7 +287,9 @@ public class UserImportTest {
@InSequence(200) @InSequence(200)
public void importSingleUser() { public void importSingleUser() {
importExport.importEntities("org.libreccm.imexport.UserImportTest"); shiro.getSystemUser().execute(() ->
importExport.importEntities("org.libreccm.imexport.UserImportTest")
);
} }
} }

View File

@ -1,5 +1,5 @@
{ {
"id": 4345, "partyId": 4345,
"uuid": "7cb9aba4-8071-4f27-af19-096e1473d050", "uuid": "7cb9aba4-8071-4f27-af19-096e1473d050",
"name": "janedoe", "name": "janedoe",
"givenName": "Jane", "givenName": "Jane",

View File

@ -0,0 +1,5 @@
{
"files": [
"7cb9aba4-8071-4f27-af19-096e1473d050.json"
]
}