use of org.alfresco.repo.transfer.TransferContext in project alfresco-repository by Alfresco.
the class ManifestIntegrationTest method testSnapshot.
@Test
public void testSnapshot() throws Exception {
// Snapshot a transfer node
String CONTENT_STRING = "hello world";
Locale CONTENT_LOCALE = Locale.TAIWAN;
String CONTENT_TITLE = "the title";
// nasty name for XML
String CONTENT_NAME = "&the name <\\*";
String CONTENT_ASSOC_NAME = "&hell+-1we";
String snapshotMe = "snapshotMe";
String title = "title";
String description = "description";
String endpointProtocol = "http";
String endpointHost = "localhost";
int endpointPort = 8080;
String endpointPath = "rhubarb";
String username = "admin";
char[] password = "password".toCharArray();
Map<NodeRef, TransferManifestNode> sentNodes = new HashMap<NodeRef, TransferManifestNode>();
TransferManifestNodeFactoryImpl nodeFactory = new TransferManifestNodeFactoryImpl();
nodeFactory.setNodeService(nodeService);
nodeFactory.setPermissionService(permissionService);
nodeFactory.setDictionaryService(dictionaryService);
nodeFactory.setMlAwareNodeService(mlAwareNodeService);
/**
* Create our transfer target
*/
TransferTarget target = transferService.createAndSaveTransferTarget(snapshotMe, title, description, endpointProtocol, endpointHost, endpointPort, endpointPath, username, password);
File snapshotFile = null;
try {
/**
* Create a test node that we will read and write
*/
ChildAssociationRef child = nodeService.createNode(target.getNodeRef(), ContentModel.ASSOC_CONTAINS, QName.createQName(CONTENT_ASSOC_NAME), ContentModel.TYPE_CONTENT);
NodeRef childNodeRef = child.getChildRef();
ContentWriter writer = contentService.getWriter(childNodeRef, ContentModel.PROP_CONTENT, true);
writer.setLocale(CONTENT_LOCALE);
writer.putContent(CONTENT_STRING);
nodeService.setProperty(childNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
nodeService.setProperty(childNodeRef, ContentModel.PROP_NAME, CONTENT_NAME);
snapshotFile = TempFileProvider.createTempFile("xxx", ".xml");
Writer snapshotWriter = new OutputStreamWriter(new FileOutputStream(snapshotFile), "UTF-8");
Set<NodeRef> nodes = new HashSet<NodeRef>();
/**
* Write three nodes
* a: the root node of the workspace store
* b: the target node
* c: child of the target node
*/
nodes.add(nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE));
nodes.add(target.getNodeRef());
nodes.add(childNodeRef);
TransferManifestWriter formatter = new XMLTransferManifestWriter();
TransferManifestHeader header = new TransferManifestHeader();
header.setNodeCount(nodes.size());
header.setCreatedDate(new Date());
formatter.startTransferManifest(snapshotWriter);
formatter.writeTransferManifestHeader(header);
for (NodeRef nodeRef : nodes) {
TransferManifestNode node = nodeFactory.createTransferManifestNode(nodeRef, null, new TransferContext());
formatter.writeTransferManifestNode(node);
sentNodes.put(nodeRef, node);
}
formatter.endTransferManifest();
snapshotWriter.close();
// Show the snapshot file (For dev purposes)
outputFile(snapshotFile);
/**
* Now read the snapshot file
*/
TestTransferManifestProcessor processor = new TestTransferManifestProcessor();
XMLTransferManifestReader reader = new XMLTransferManifestReader(processor);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser = saxParserFactory.newSAXParser();
parser.parse(snapshotFile, reader);
/**
* Now validate that we read back what we write out
*/
assertEquals("did not get back the same number of nodes", nodes.size(), processor.getNodes().size());
assertNotNull("header is null", processor.getHeader());
for (NodeRef nodeId : nodes) {
System.out.println("Processing node:" + nodeId);
TransferManifestNormalNode readNode = (TransferManifestNormalNode) processor.getNodes().get(nodeId);
TransferManifestNormalNode writeNode = (TransferManifestNormalNode) sentNodes.get(nodeId);
assertNotNull("readNode is null", readNode);
assertNotNull("writeNode is null", writeNode);
assertEquals("type is different", writeNode.getType(), readNode.getType());
assertEquals("nodeRef is different", writeNode.getNodeRef(), readNode.getNodeRef());
assertEquals("parent node ref is different", writeNode.getPrimaryParentAssoc(), readNode.getPrimaryParentAssoc());
if (writeNode.getParentPath() != null) {
assertEquals("parent path is different", writeNode.getParentPath().toString(), readNode.getParentPath().toString());
}
assertEquals("aspects array different size", writeNode.getAspects().size(), readNode.getAspects().size());
for (QName aspect : writeNode.getAspects()) {
assertTrue("missing aspect", readNode.getAspects().contains(aspect));
}
assertEquals("properties array different size", writeNode.getProperties().size(), readNode.getProperties().size());
for (QName prop : writeNode.getProperties().keySet()) {
assertTrue("missing property", readNode.getProperties().containsKey(prop));
}
assertEquals("child assocs different", writeNode.getChildAssocs().size(), readNode.getChildAssocs().size());
assertEquals("parent assocs different", writeNode.getParentAssocs().size(), readNode.getParentAssocs().size());
assertEquals("source assocs different", writeNode.getSourceAssocs().size(), readNode.getSourceAssocs().size());
assertEquals("target assocs different", writeNode.getTargetAssocs().size(), readNode.getTargetAssocs().size());
if (readNode.getNodeRef().equals(childNodeRef)) {
/**
* Check the child node since we created it at the start of this test this test
*/
ContentData data = (ContentData) readNode.getProperties().get(ContentModel.PROP_CONTENT);
assertEquals("content data wrong size", data.getSize(), CONTENT_STRING.length());
assertEquals("content locale wrong", data.getLocale(), CONTENT_LOCALE);
String childTitle = ((MLText) readNode.getProperties().get(ContentModel.PROP_TITLE)).getDefaultValue();
assertEquals("content title wrong", childTitle, CONTENT_TITLE);
String childName = (String) readNode.getProperties().get(ContentModel.PROP_NAME);
assertEquals("content name wrong", childName, CONTENT_NAME);
/**
* Check the parent associations, there should be only one primary
*/
assertTrue("one parent assoc", readNode.getParentAssocs().size() == 1);
assertTrue("isPrimary", readNode.getParentAssocs().get(0).isPrimary());
assertEquals("parent q name", readNode.getParentAssocs().get(0).getQName(), QName.createQName(CONTENT_ASSOC_NAME));
assertEquals("parent type q name", readNode.getParentAssocs().get(0).getTypeQName(), ContentModel.ASSOC_CONTAINS);
assertEquals("child node ref", readNode.getParentAssocs().get(0).getChildRef(), childNodeRef);
assertEquals("parent node ref", readNode.getParentAssocs().get(0).getParentRef(), target.getNodeRef());
assertTrue("zero child assoc", readNode.getChildAssocs().size() == 0);
/**
* Test Node Helper
*/
assertEquals(readNode.getParentAssocs().get(0), TransferManifestNodeHelper.getPrimaryParentAssoc(readNode));
Set<ContentData> content = TransferManifestNodeHelper.getContentData(readNode);
assertEquals("content not found", content.size(), 1);
}
}
} finally {
if (snapshotFile != null) {
snapshotFile.delete();
}
transferService.deleteTransferTarget(snapshotMe);
}
}
Aggregations