Initial work for ALF-4131 - Update Replication Definition Webscript

Stubs out webscript and unit tests


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21589 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2010-08-04 11:13:15 +00:00
parent 40a6a8caf6
commit 19a026e3a4
6 changed files with 168 additions and 2 deletions

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Updates a persisted Replication Definition</shortname>
<description>Updates a previously persisted replication defintion with the supplied details</description>
<url>/api/replication-definition/{replication_definition_name}</url>
<format default="json"/>
<authentication>admin</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,2 @@
<#import "replication-definition.lib.ftl" as replicationDefLib />
<@replicationDefLib.replicationDefinitionJSON replicationDefinition=replicationDefinition />

View File

@@ -870,4 +870,10 @@
parent="abstractReplicationWebScript"> parent="abstractReplicationWebScript">
</bean> </bean>
<!-- Updates a replication definition -->
<bean id="webscript.org.alfresco.repository.replication.replication-definition.put"
class="org.alfresco.repo.web.scripts.replication.ReplicationDefinitionPut"
parent="abstractReplicationWebScript">
</bean>
</beans> </beans>

View File

@@ -18,8 +18,6 @@
*/ */
package org.alfresco.repo.web.scripts.replication; package org.alfresco.repo.web.scripts.replication;
import java.util.Comparator;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.alfresco.service.cmr.replication.ReplicationDefinition; import org.alfresco.service.cmr.replication.ReplicationDefinition;

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.web.scripts.replication;
import java.io.IOException;
import java.util.Map;
import org.alfresco.service.cmr.replication.ReplicationDefinition;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* @author Nick Burch
* @since 3.4
*/
public class ReplicationDefinitionPut extends AbstractReplicationWebscript
{
@Override
protected Map<String, Object> buildModel(ReplicationModelBuilder modelBuilder,
WebScriptRequest req, Status status, Cache cache)
{
// Which definition did they ask for?
String replicationDefinitionName =
req.getServiceMatch().getTemplateVars().get("replication_definition_name");
ReplicationDefinition replicationDefinition =
replicationService.loadReplicationDefinition(replicationDefinitionName);
// Does it exist?
if(replicationDefinition == null) {
throw new WebScriptException(
Status.STATUS_NOT_FOUND,
"No Replication Definition found with that name"
);
}
// Grab the JSON, and prepare to update
try
{
JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent()));
// Are they trying to rename?
if(json.has("name")) {
String jsonName = json.getString("name");
if(! jsonName.equals(replicationDefinitionName)) {
// Name has changed, rename it
replicationService.renameReplicationDefinition(
replicationDefinitionName,
jsonName
);
}
}
// Update everything else
updateDefinitionProperties(replicationDefinition, json);
// Save the changes
replicationService.saveReplicationDefinition(replicationDefinition);
}
catch (IOException iox)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from request.", iox);
}
catch (JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from request.", je);
}
// Return the new details on it
return modelBuilder.buildDetails(replicationDefinition);
}
}

View File

@@ -42,6 +42,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response; import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
/** /**
@@ -677,8 +678,66 @@ public class ReplicationRestApiTest extends BaseWebScriptTest
// Check the database for these // Check the database for these
// TODO // TODO
// Ensure we can't create with a duplicate name
// TODO
} }
public void testReplicationDefinitionPut() throws Exception
{
Response response;
// Not allowed if you're not an admin
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
response = sendRequest(new PutRequest(URL_DEFINITION + "MadeUp", "", JSON), Status.STATUS_UNAUTHORIZED);
assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
response = sendRequest(new PutRequest(URL_DEFINITION + "MadeUp", "", JSON), Status.STATUS_UNAUTHORIZED);
assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
// Ensure there aren't any to start with
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
assertEquals(0, replicationService.loadReplicationDefinitions().size());
// You need to specify a real definition
response = sendRequest(new PutRequest(URL_DEFINITION + "MadeUp", "", JSON), Status.STATUS_NOT_FOUND);
assertEquals(Status.STATUS_NOT_FOUND, response.getStatus());
// Create one, and change it
ReplicationDefinition rd = replicationService.createReplicationDefinition("Test", "Testing");
replicationService.saveReplicationDefinition(rd);
response = sendRequest(new PutRequest(URL_DEFINITION + "Test", "{}", JSON), Status.STATUS_OK);
assertEquals(Status.STATUS_OK, response.getStatus());
// Check we got the right information back on it
// TODO
// Change some details, and see them updated in both
// the JSON and on the object in the repo
// TODO
// Create a 2nd definition, and check that the correct
// one gets updated
// TODO
// Change the payload, and see the right information in
// the response JSON for it
// TODO
// Rename to a taken name, won't be allowed
// TODO
// Rename to a spare name, will be changed
// TODO
}
@Override @Override
protected void setUp() throws Exception protected void setUp() throws Exception
{ {