From 2f8a9f1d58086e37ffd0675e69732de83208fb12 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 4 Aug 2010 14:17:53 +0000 Subject: [PATCH] Finish ALF-4132 & ALF-4131 - edit and create replication definition webscripts, including unit tests git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21598 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../replication/ReplicationDefinitionPut.java | 10 +- .../replication/ReplicationRestApiTest.java | 437 ++++++++++++++++-- 2 files changed, 419 insertions(+), 28 deletions(-) diff --git a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionPut.java b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionPut.java index a5f8a03b8a..1327e9d0cd 100644 --- a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionPut.java +++ b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionPut.java @@ -64,11 +64,19 @@ public class ReplicationDefinitionPut extends AbstractReplicationWebscript if(json.has("name")) { String jsonName = json.getString("name"); if(! jsonName.equals(replicationDefinitionName)) { - // Name has changed, rename it + // Name has changed, ensure the new name is spare + if(replicationService.loadReplicationDefinition(jsonName) != null) { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The specified new name is already in use"); + } + + // Rename it replicationService.renameReplicationDefinition( replicationDefinitionName, jsonName ); + + // And grab the updated version post-rename + replicationDefinition = replicationService.loadReplicationDefinition(jsonName); } } diff --git a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java index 8520137ee1..85f2a22acc 100644 --- a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java +++ b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java @@ -26,6 +26,7 @@ import org.alfresco.repo.model.Repository; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.person.TestPersonManager; import org.alfresco.repo.web.scripts.BaseWebScriptTest; +import org.alfresco.service.cmr.action.ActionStatus; import org.alfresco.service.cmr.action.ActionTrackingService; import org.alfresco.service.cmr.replication.ReplicationDefinition; import org.alfresco.service.cmr.replication.ReplicationService; @@ -66,6 +67,7 @@ public class ReplicationRestApiTest extends BaseWebScriptTest private ActionTrackingService actionTrackingService; private Repository repositoryHelper; + private NodeRef dataDictionary; public void testReplicationDefinitionsGet() throws Exception { @@ -439,7 +441,7 @@ public class ReplicationRestApiTest extends BaseWebScriptTest assertEquals(startedAt, json.get("startedAt")); assertEquals(JSONObject.NULL, json.get("endedAt")); assertEquals(JSONObject.NULL, json.get("failureMessage")); - assertEquals("/api/running-action/replicationActionExecutor="+ + assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor="+ actionId + "=" + instanceId, json.get("executionDetails")); assertEquals(JSONObject.NULL, json.get("transferLocalReport")); assertEquals(JSONObject.NULL, json.get("transferRemoteReport")); @@ -464,7 +466,7 @@ public class ReplicationRestApiTest extends BaseWebScriptTest assertEquals(startedAt, json.get("startedAt")); assertEquals(JSONObject.NULL, json.get("endedAt")); assertEquals(JSONObject.NULL, json.get("failureMessage")); - assertEquals("/api/running-action/replicationActionExecutor="+ + assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor="+ actionId + "=" + instanceId, json.get("executionDetails")); assertEquals(JSONObject.NULL, json.get("transferLocalReport")); assertEquals(JSONObject.NULL, json.get("transferRemoteReport")); @@ -479,12 +481,6 @@ public class ReplicationRestApiTest extends BaseWebScriptTest rd.getPayload().add( repositoryHelper.getCompanyHome() ); - NodeRef dataDictionary = - nodeService.getChildByName( - repositoryHelper.getCompanyHome(), - ContentModel.ASSOC_CONTAINS, - "Data Dictionary" - ); rd.getPayload().add( dataDictionary ); replicationService.saveReplicationDefinition(rd); @@ -500,7 +496,7 @@ public class ReplicationRestApiTest extends BaseWebScriptTest assertEquals(startedAt, json.get("startedAt")); assertEquals(JSONObject.NULL, json.get("endedAt")); assertEquals(JSONObject.NULL, json.get("failureMessage")); - assertEquals("/api/running-action/replicationActionExecutor="+ + assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor="+ actionId + "=" + instanceId, json.get("executionDetails")); assertEquals(JSONObject.NULL, json.get("transferLocalReport")); assertEquals(JSONObject.NULL, json.get("transferRemoteReport")); @@ -556,7 +552,7 @@ public class ReplicationRestApiTest extends BaseWebScriptTest assertEquals(startedAt, json.get("startedAt")); assertEquals(JSONObject.NULL, json.get("endedAt")); assertEquals(JSONObject.NULL, json.get("failureMessage")); - assertEquals("/api/running-action/replicationActionExecutor="+ + assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor="+ actionId + "=" + instanceId, json.get("executionDetails")); assertEquals(JSONObject.NULL, json.get("transferLocalReport")); assertEquals(JSONObject.NULL, json.get("transferRemoteReport")); @@ -648,7 +644,7 @@ public class ReplicationRestApiTest extends BaseWebScriptTest response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_BAD_REQUEST); assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus()); - json.put("name", "NewDefinition"); + json.put("name", "New Definition"); response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_BAD_REQUEST); assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus()); @@ -660,26 +656,123 @@ public class ReplicationRestApiTest extends BaseWebScriptTest // Check we got the right information back - // TODO + String jsonStr = response.getContentAsString(); + json = new JSONObject(jsonStr); + assertNotNull(json); + + assertEquals("New Definition", json.get("name")); + assertEquals("Testing", json.get("description")); + assertEquals("New", json.get("status")); + assertEquals(JSONObject.NULL, json.get("startedAt")); + assertEquals(JSONObject.NULL, json.get("endedAt")); + assertEquals(JSONObject.NULL, json.get("failureMessage")); + assertEquals(JSONObject.NULL, json.get("executionDetails")); + assertEquals(JSONObject.NULL, json.get("transferLocalReport")); + assertEquals(JSONObject.NULL, json.get("transferRemoteReport")); + assertEquals(true, json.get("enabled")); + assertEquals(JSONObject.NULL, json.get("targetName")); + assertEquals(0, json.getJSONArray("payload").length()); - // Check that the right stuff ended up in the database - // TODO + // Check that the right stuff ended up in the repository + ReplicationDefinition rd = replicationService.loadReplicationDefinition("New Definition"); + assertEquals("New Definition", rd.getReplicationName()); + assertEquals("Testing", rd.getDescription()); + assertEquals(ActionStatus.New, rd.getExecutionStatus()); + assertEquals(null, rd.getExecutionStartDate()); + assertEquals(null, rd.getExecutionEndDate()); + assertEquals(null, rd.getExecutionFailureMessage()); + assertEquals(null, rd.getLocalTransferReport()); + assertEquals(null, rd.getRemoteTransferReport()); + assertEquals(null, rd.getTargetName()); + assertEquals(0, rd.getPayload().size()); + assertEquals(true, rd.isEnabled()); // Post with the full set of options - // TODO + json = new JSONObject(); + json.put("name", "Test"); + json.put("description", "Test Description"); + json.put("targetName", "Target"); + json.put("enabled", false); + JSONArray payloadRefs = new JSONArray(); + payloadRefs.put(repositoryHelper.getCompanyHome().toString()); + payloadRefs.put(dataDictionary.toString()); + json.put("payload", payloadRefs); + + response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_OK); + assertEquals(Status.STATUS_OK, response.getStatus()); - // Check the response for these - // TODO + // Check the response for this + jsonStr = response.getContentAsString(); + json = new JSONObject(jsonStr); + assertNotNull(json); + + assertEquals("Test", json.get("name")); + assertEquals("Test Description", json.get("description")); + assertEquals("New", json.get("status")); + assertEquals(JSONObject.NULL, json.get("startedAt")); + assertEquals(JSONObject.NULL, json.get("endedAt")); + assertEquals(JSONObject.NULL, json.get("failureMessage")); + assertEquals(JSONObject.NULL, json.get("executionDetails")); + assertEquals(JSONObject.NULL, json.get("transferLocalReport")); + assertEquals(JSONObject.NULL, json.get("transferRemoteReport")); + assertEquals(false, json.get("enabled")); + assertEquals("Target", json.get("targetName")); + assertEquals(2, json.getJSONArray("payload").length()); + + JSONObject payload = json.getJSONArray("payload").getJSONObject(0); + assertEquals(repositoryHelper.getCompanyHome().toString(), payload.get("nodeRef")); + assertEquals(true, payload.get("isFolder")); + assertEquals("Company Home", payload.get("name")); + assertEquals("/Company Home", payload.get("path")); + + payload = json.getJSONArray("payload").getJSONObject(1); + assertEquals(dataDictionary.toString(), payload.get("nodeRef")); + assertEquals(true, payload.get("isFolder")); + assertEquals("Data Dictionary", payload.get("name")); + assertEquals("/Company Home/Data Dictionary", payload.get("path")); - // Check the database for these - // TODO + // Check the database for this + rd = replicationService.loadReplicationDefinition("Test"); + assertEquals("Test", rd.getReplicationName()); + assertEquals("Test Description", rd.getDescription()); + assertEquals(ActionStatus.New, rd.getExecutionStatus()); + assertEquals(null, rd.getExecutionStartDate()); + assertEquals(null, rd.getExecutionEndDate()); + assertEquals(null, rd.getExecutionFailureMessage()); + assertEquals(null, rd.getLocalTransferReport()); + assertEquals(null, rd.getRemoteTransferReport()); + assertEquals("Target", rd.getTargetName()); + assertEquals(false, rd.isEnabled()); + assertEquals(2, rd.getPayload().size()); + assertEquals(repositoryHelper.getCompanyHome(), rd.getPayload().get(0)); + assertEquals(dataDictionary, rd.getPayload().get(1)); + + + // Ensure that the original one wasn't changed by anything + rd = replicationService.loadReplicationDefinition("New Definition"); + assertEquals("New Definition", rd.getReplicationName()); + assertEquals("Testing", rd.getDescription()); + assertEquals(ActionStatus.New, rd.getExecutionStatus()); + assertEquals(null, rd.getExecutionStartDate()); + assertEquals(null, rd.getExecutionEndDate()); + assertEquals(null, rd.getExecutionFailureMessage()); + assertEquals(null, rd.getLocalTransferReport()); + assertEquals(null, rd.getRemoteTransferReport()); + assertEquals(null, rd.getTargetName()); + assertEquals(0, rd.getPayload().size()); + assertEquals(true, rd.isEnabled()); + // Ensure we can't create with a duplicate name - // TODO + json = new JSONObject(); + json.put("name", "Test"); + + response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_BAD_REQUEST); + assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus()); } public void testReplicationDefinitionPut() throws Exception @@ -716,25 +809,308 @@ public class ReplicationRestApiTest extends BaseWebScriptTest // Check we got the right information back on it - // TODO + String jsonStr = response.getContentAsString(); + JSONObject json = new JSONObject(jsonStr); + assertNotNull(json); + + assertEquals("Test", json.get("name")); + assertEquals("Testing", json.get("description")); + assertEquals("New", json.get("status")); + assertEquals(JSONObject.NULL, json.get("startedAt")); + assertEquals(JSONObject.NULL, json.get("endedAt")); + assertEquals(JSONObject.NULL, json.get("failureMessage")); + assertEquals(JSONObject.NULL, json.get("executionDetails")); + assertEquals(JSONObject.NULL, json.get("transferLocalReport")); + assertEquals(JSONObject.NULL, json.get("transferRemoteReport")); + assertEquals(true, json.get("enabled")); + assertEquals(JSONObject.NULL, json.get("targetName")); + assertEquals(0, json.getJSONArray("payload").length()); + + + // Ensure we didn't get any unexpected data back + JSONArray keys = json.names(); + for(int i=0; i