diff --git a/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java b/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java index 80d4f4437c..3b5dced5f8 100644 --- a/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java +++ b/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java @@ -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 testNodePropertiesAfterCreation; + protected Map 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 docProps = new HashMap(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 docProps = new HashMap(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(); + } } \ No newline at end of file diff --git a/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiJsonPost_Test.java b/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiJsonPost_Test.java index f3c2f7bee4..1f88d1a131 100644 --- a/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiJsonPost_Test.java +++ b/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiJsonPost_Test.java @@ -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 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 modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL); + assertEquals(5, modifiedAssocs.size()); + + // Extract the target nodeRefs to make them easier to examine + associatedNodes = new ArrayList(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 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 modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL); + assertEquals(1, modifiedAssocs.size()); + + // Extract the target nodeRefs to make them easier to examine + associatedNodes = new ArrayList(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 originalAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL); + assertEquals(2, originalAssocs.size()); + + List associatedNodes = new ArrayList(2); + associatedNodes.add(originalAssocs.get(0).getTargetRef()); + associatedNodes.add(originalAssocs.get(1).getTargetRef()); + + assertTrue(associatedNodes.contains(associatedDoc_A)); + assertTrue(associatedNodes.contains(associatedDoc_B)); + } }