Persistence of associations

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13773 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2009-03-30 14:49:20 +00:00
parent 509708b273
commit 8e9e0ad860
2 changed files with 206 additions and 31 deletions

View File

@@ -28,7 +28,6 @@ import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.model.ContentModel;
@@ -38,14 +37,12 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.GUID;
import org.alfresco.web.scripts.TestWebScriptServer.GetRequest;
import org.alfresco.web.scripts.TestWebScriptServer.Response;
@@ -56,15 +53,18 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest
protected static final String TEST_FORM_TITLE = "Test form title";
protected String referencingNodeUrl;
protected NodeRef referencingDocNodeRef;
protected NodeRef firstAssociatedDoc;
protected Map<QName, Serializable> testNodePropertiesAfterCreation;
protected Map<QName, Serializable> refNodePropertiesAfterCreation;
protected NodeRef associatedDoc_A;
protected NodeRef associatedDoc_B;
protected NodeRef associatedDoc_C;
protected NodeRef associatedDoc_D;
protected NodeRef associatedDoc_E;
protected NodeRef associatedFolderNodeRef;
protected NodeService nodeService;
private FileFolderService fileFolderService;
private ContentService contentService;
private Repository repositoryHelper;
protected NodeRef secondAssociatedDoc;
protected NodeRef associatedFolderNodeRef;
@Override
protected void setUp() throws Exception
@@ -107,30 +107,19 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest
this.fileFolderService.create(companyHomeNodeRef, "associatedDocsFolder" + guid, ContentModel.TYPE_FOLDER);
associatedFolderNodeRef = associatedDocsFolder.getNodeRef();
// create nodes to use as targets of association
Map<QName, Serializable> docProps = new HashMap<QName, Serializable>(1);
docProps.put(ContentModel.PROP_NAME, "firstAssociatedDoc" + guid + ".txt");
firstAssociatedDoc = this.nodeService.createNode(
associatedFolderNodeRef,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "firstAssociatedDoc" + guid + ".txt"),
ContentModel.TYPE_CONTENT,
docProps).getChildRef();
docProps.clear();
docProps.put(ContentModel.PROP_NAME, "secondAssociatedDoc" + guid + ".txt");
secondAssociatedDoc = this.nodeService.createNode(
associatedFolderNodeRef,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "secondAssociatedDoc" + guid + ".txt"),
ContentModel.TYPE_CONTENT,
docProps).getChildRef();
this.associatedDoc_A = createTestNode("associatedDoc_A" + guid);
this.associatedDoc_B = createTestNode("associatedDoc_B" + guid);
this.associatedDoc_C = createTestNode("associatedDoc_C" + guid);
this.associatedDoc_D = createTestNode("associatedDoc_D" + guid);
this.associatedDoc_E = createTestNode("associatedDoc_E" + guid);
// Now create associations between the referencing and the two node refs.
aspectProps.clear();
this.nodeService.addAspect(this.referencingDocNodeRef, ContentModel.ASPECT_REFERENCING, aspectProps);
this.nodeService.createAssociation(this.referencingDocNodeRef, firstAssociatedDoc, ContentModel.ASSOC_REFERENCES);
this.nodeService.createAssociation(this.referencingDocNodeRef, secondAssociatedDoc, ContentModel.ASSOC_REFERENCES);
this.nodeService.createAssociation(this.referencingDocNodeRef, associatedDoc_A, ContentModel.ASSOC_REFERENCES);
this.nodeService.createAssociation(this.referencingDocNodeRef, associatedDoc_B, ContentModel.ASSOC_REFERENCES);
// Leave the third and 4th nodes without associations as they may be created in
// other test code.
// Create and store the path
StringBuilder builder = new StringBuilder();
@@ -139,17 +128,20 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest
this.referencingNodeUrl = builder.toString();
// Store the original properties of this node
this.testNodePropertiesAfterCreation = nodeService.getProperties(referencingDocNodeRef);
this.refNodePropertiesAfterCreation = nodeService.getProperties(referencingDocNodeRef);
testNodePropertiesAfterCreation.toString();
refNodePropertiesAfterCreation.toString();
}
@Override
public void tearDown()
{
nodeService.deleteNode(this.referencingDocNodeRef);
nodeService.deleteNode(this.firstAssociatedDoc);
nodeService.deleteNode(this.secondAssociatedDoc);
nodeService.deleteNode(this.associatedDoc_A);
nodeService.deleteNode(this.associatedDoc_B);
nodeService.deleteNode(this.associatedDoc_C);
nodeService.deleteNode(this.associatedDoc_D);
nodeService.deleteNode(this.associatedDoc_E);
nodeService.deleteNode(this.associatedFolderNodeRef);
}
@@ -159,4 +151,16 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest
Response result = sendRequest(new GetRequest(url), expectedStatusCode);
return result;
}
protected NodeRef createTestNode(String associatedDocName)
{
Map<QName, Serializable> docProps = new HashMap<QName, Serializable>(1);
docProps.put(ContentModel.PROP_NAME, associatedDocName + ".txt");
return this.nodeService.createNode(
associatedFolderNodeRef,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, associatedDocName + ".txt"),
ContentModel.TYPE_CONTENT,
docProps).getChildRef();
}
}

View File

@@ -26,10 +26,15 @@ package org.alfresco.repo.web.scripts.forms;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.web.scripts.TestWebScriptServer.GetRequest;
import org.alfresco.web.scripts.TestWebScriptServer.PostRequest;
import org.alfresco.web.scripts.TestWebScriptServer.Response;
@@ -41,6 +46,9 @@ public class FormRestApiJsonPost_Test extends AbstractTestFormRestApi
private static final String PROP_CM_DESCRIPTION = "prop_cm_description";
private static final String PROP_MIMETYPE = "prop_mimetype";
private static final String APPLICATION_JSON = "application/json";
private static final String ASSOC_CM_REFERENCES = "assoc_cm_references";
private static final String ASSOC_CM_REFERENCES_ADDED = "assoc_cm_references_added";
private static final String ASSOC_CM_REFERENCES_REMOVED = "assoc_cm_references_removed";
public void testSimpleJsonPostRequest() throws IOException, JSONException
{
@@ -95,4 +103,167 @@ public class FormRestApiJsonPost_Test extends AbstractTestFormRestApi
String retrievedMimetype = (String)formData.get(PROP_MIMETYPE);
assertEquals(MimetypeMap.MIMETYPE_HTML, modifiedMimetype);
}
/**
* This test method attempts to add new associations between existing nodes.
*/
public void testAddNewAssociationsToNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalAssocsBeforeChanges();
// Add three additional associations
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = associatedDoc_C + "," + associatedDoc_D + "," + associatedDoc_E;
jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated associations via the node service
List<AssociationRef> modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(5, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (AssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getTargetRef());
}
assertTrue(associatedNodes.contains(associatedDoc_A));
assertTrue(associatedNodes.contains(associatedDoc_B));
assertTrue(associatedNodes.contains(associatedDoc_C));
assertTrue(associatedNodes.contains(associatedDoc_D));
assertTrue(associatedNodes.contains(associatedDoc_E));
// The Rest API should also give us the modified assocs.
Response response = sendRequest(new GetRequest(referencingNodeUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_CM_REFERENCES);
// We expect exactly 5 assocs on the test node
assertEquals(5, jsonAssocs.split(",").length);
for (AssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getTargetRef().toString()));
}
}
/**
* This test method attempts to remove an existing association between two existing
* nodes.
*/
public void testRemoveAssociationsFromNode() throws Exception
{
List<NodeRef> associatedNodes;
checkOriginalAssocsBeforeChanges();
// Remove an association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = associatedDoc_B.toString();
jsonPostData.put(ASSOC_CM_REFERENCES_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated associations via the node service
List<AssociationRef> modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(1, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (AssociationRef assocRef : modifiedAssocs)
{
associatedNodes.add(assocRef.getTargetRef());
}
assertTrue(associatedNodes.contains(associatedDoc_A));
// The Rest API should also give us the modified assocs.
Response response = sendRequest(new GetRequest(referencingNodeUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_CM_REFERENCES);
// We expect exactly 1 assoc on the test node
assertEquals(1, jsonAssocs.split(",").length);
for (AssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getTargetRef().toString()));
}
}
/**
* This test method attempts to add the same association twice. This attempt should
* not succeed.
*/
public void off_testAddAssocThatAlreadyExists() throws Exception
{
//TODO This test has been switched off as the current implementation of assoc
// persistence does not handle this -ve test case. Currently a 500 error
// is thrown from within the repo.
checkOriginalAssocsBeforeChanges();
// Add an association
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = associatedDoc_C.toString();
jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
// Try to add the same association again
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
fail("This TC not finished.");
}
/**
* This test method attempts to remove an association that does not exist. This
* attempt should not succeed.
*/
public void off_testRemoveAssocThatDoesNotExist() throws Exception
{
//TODO This test has been switched off as the current implementation of assoc
// persistence does not handle this -ve test case. Currently a 500 error
// is thrown from within the repo.
checkOriginalAssocsBeforeChanges();
// Remove a non-existent association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = associatedDoc_E.toString();
jsonPostData.put(ASSOC_CM_REFERENCES_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUrl, jsonPostString, APPLICATION_JSON), 200);
fail("This TC not finished.");
}
private void checkOriginalAssocsBeforeChanges()
{
List<AssociationRef> originalAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(2, originalAssocs.size());
List<NodeRef> associatedNodes = new ArrayList<NodeRef>(2);
associatedNodes.add(originalAssocs.get(0).getTargetRef());
associatedNodes.add(originalAssocs.get(1).getTargetRef());
assertTrue(associatedNodes.contains(associatedDoc_A));
assertTrue(associatedNodes.contains(associatedDoc_B));
}
}