Merge master into RM-4159_EnterpriseReSTTests.

Update the RMRestWrapper with a couple more passthrough methods.
This commit is contained in:
Tom Page
2017-02-24 12:18:21 +00:00
244 changed files with 1241 additions and 694 deletions

View File

@@ -26,9 +26,13 @@
*/
package org.alfresco.rest.core;
import com.jayway.restassured.builder.RequestSpecBuilder;
import org.alfresco.rest.exception.EmptyJsonResponseException;
import org.alfresco.rest.model.RestHtmlResponse;
import org.alfresco.rest.model.RestSiteModel;
import org.alfresco.rest.model.RestSiteModelsCollection;
import org.alfresco.rest.requests.coreAPI.RestCoreAPI;
import org.alfresco.rest.rm.community.requests.igCoreAPI.RestIGCoreAPI;
import org.alfresco.utility.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
@@ -143,4 +147,44 @@ public class RMRestWrapper
{
return restWrapper.getTestUser();
}
/** Get the Alfresco Core API. */
public RestCoreAPI withCoreAPI()
{
return restWrapper.withCoreAPI();
}
/**
* You can handle the request sent to server by calling this method.
* If for example you want to sent multipart form data you can use: <pre>
* restClient.configureRequestSpec()
* .addMultiPart("filedata", Utility.getResourceTestDataFile("restapi-resource"))
* .addFormParam("renditions", "doclib")
* .addFormParam("autoRename", true);
*
* restClient.withCoreAPI().usingNode(ContentModel.my()).createNode();
* </pre> This will create the node using the multipart data defined.
*/
public RequestSpecBuilder configureRequestSpec()
{
return restWrapper.configureRequestSpec();
}
/**
* Process a response that returns a html
*
* @throws EmptyJsonResponseException If there is no response from the server.
*/
public RestHtmlResponse processHtmlResponse(RestRequest simpleRequest)
{
return restWrapper.processHtmlResponse(simpleRequest);
}
/**
* @return the rmRestProperties
*/
public RMRestProperties getRmRestProperties()
{
return this.rmRestProperties;
}
}

View File

@@ -28,12 +28,16 @@ package org.alfresco.rest.core;
import javax.annotation.Resource;
import org.alfresco.rest.requests.Node;
import org.alfresco.rest.requests.coreAPI.RestCoreAPI;
import org.alfresco.rest.rm.community.requests.igCoreAPI.FilePlanComponentAPI;
import org.alfresco.rest.rm.community.requests.igCoreAPI.FilesAPI;
import org.alfresco.rest.rm.community.requests.igCoreAPI.RMSiteAPI;
import org.alfresco.rest.rm.community.requests.igCoreAPI.RMUserAPI;
import org.alfresco.rest.rm.community.requests.igCoreAPI.RecordsAPI;
import org.alfresco.rest.rm.community.requests.igCoreAPI.RestIGCoreAPI;
import org.alfresco.utility.data.DataUser;
import org.alfresco.utility.model.RepoTestModel;
import org.alfresco.utility.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
@@ -69,6 +73,22 @@ public class RestAPIFactory
return getRmRestWrapper().withIGCoreAPI();
}
private RestCoreAPI getRestCoreAPI(UserModel userModel)
{
getRmRestWrapper().authenticateUser(userModel != null ? userModel : dataUser.getAdminUser());
return getRmRestWrapper().withCoreAPI();
}
public Node getNodeAPI(RepoTestModel model) throws Exception
{
return getRestCoreAPI(null).usingNode(model);
}
public Node getNodeAPI(UserModel userModel, RepoTestModel model) throws Exception
{
return getRestCoreAPI(userModel).usingNode(model);
}
public RMSiteAPI getRMSiteAPI()
{
return getRestIGCoreAPI(null).usingRMSite();
@@ -108,4 +128,14 @@ public class RestAPIFactory
{
return getRestIGCoreAPI(userModel).usingFiles();
}
public RMUserAPI getRMUserAPI()
{
return getRestIGCoreAPI(null).usingRMUser();
}
public RMUserAPI getRMUserAPI(UserModel userModel)
{
return getRestIGCoreAPI(userModel).usingRMUser();
}
}

View File

@@ -107,7 +107,7 @@ public class FilePlanComponentProperties
@JsonProperty(value = PROPERTIES_PHYSICAL_SIZE, required = false)
private Integer physicalSize;
@JsonProperty(value = PROPERTIES_RECORD_ID, required = false)
private String recordId;
private String rmIdentifier;
}

View File

@@ -35,7 +35,7 @@ import org.alfresco.rest.requests.ModelRequest;
* @author Tuna Aksoy
* @since 2.6
*/
public abstract class RMModelRequest extends ModelRequest
public abstract class RMModelRequest extends ModelRequest<RMModelRequest>
{
private RMRestWrapper rmRestWrapper;

View File

@@ -34,6 +34,7 @@ import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanCo
import static org.alfresco.rest.rm.community.util.ParameterCheck.mandatoryObject;
import static org.alfresco.rest.rm.community.util.ParameterCheck.mandatoryString;
import static org.alfresco.rest.rm.community.util.PojoUtility.toJson;
import static org.alfresco.rest.rm.community.util.PojoUtility.toJsonElectronicRecord;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.springframework.http.HttpMethod.DELETE;
import static org.springframework.http.HttpMethod.GET;
@@ -49,13 +50,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Resources;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;
import org.alfresco.rest.core.RMRestWrapper;
import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent;
import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentsCollection;
import org.alfresco.rest.rm.community.requests.RMModelRequest;
import org.alfresco.utility.model.UserModel;
/**
* File plan component REST API Wrapper
@@ -235,13 +234,15 @@ public class FilePlanComponentAPI extends RMModelRequest
/**
* Create electronic record from file resource
*
* @param electronicRecordModel {@link FilePlanComponent} for electronic record to be created
* @param recordContent {@link File} pointing to the content of the electronic record to be created
* @param parentId parent container id
* @return newly created {@link FilePlanComponent}
* @throws Exception if operation failed
* @throws Exception for invalid electronicRecordModel JSON strings
*/
public FilePlanComponent createElectronicRecord(FilePlanComponent electronicRecordModel, File recordContent, String parentId) throws Exception
public FilePlanComponent createElectronicRecord(FilePlanComponent electronicRecordModel, File recordContent,
String parentId) throws Exception
{
mandatoryObject("electronicRecordModel", electronicRecordModel);
mandatoryString("parentId", parentId);
@@ -250,40 +251,23 @@ public class FilePlanComponentAPI extends RMModelRequest
fail("Only electronic records are supported");
}
UserModel currentUser = getRMRestWrapper().getTestUser();
/*
* For file uploads nodeBodyCreate is ignored hence can't be used. Append all FilePlanComponent fields
* to the request.
*/
RequestSpecBuilder builder = new RequestSpecBuilder();
builder.setAuth(basic(currentUser.getUsername(), currentUser.getPassword()));
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(toJson(electronicRecordModel));
RequestSpecBuilder builder = getRMRestWrapper().configureRequestSpec();
JsonNode root = new ObjectMapper().readTree(toJsonElectronicRecord(electronicRecordModel));
// add request fields
Iterator<String> fieldNames = root.fieldNames();
while (fieldNames.hasNext())
{
String fieldName = fieldNames.next();
builder.addMultiPart(fieldName, root.get(fieldName).asText(), ContentType.JSON.name());
}
builder.addMultiPart("filedata", recordContent, ContentType.BINARY.name());
/*
* RestWrapper adds some headers which break multipart/form-data uploads and also assumes json POST requests.
* Upload the file using RestAssured library.
*/
Response response = given()
.spec(builder.build())
.when()
.post("fileplan-components/{fileplanComponentId}/children?{parameters}", parentId, getRMRestWrapper().getParameters())
.andReturn();
getRMRestWrapper().setStatusCode(Integer.toString(response.getStatusCode()));
/* return a FilePlanComponent object representing Response */
return response.jsonPath().getObject("entry", FilePlanComponent.class);
// create node with given content
return createFilePlanComponent(electronicRecordModel, parentId);
}
/**

View File

@@ -26,9 +26,11 @@
*/
package org.alfresco.rest.rm.community.requests.igCoreAPI;
import static com.jayway.restassured.RestAssured.basic;
import static com.jayway.restassured.RestAssured.given;
import static org.jglue.fluentjson.JsonBuilderFactory.buildObject;
import static org.springframework.http.HttpStatus.OK;
import com.google.gson.JsonObject;
import com.jayway.restassured.builder.RequestSpecBuilder;
@@ -38,12 +40,13 @@ import com.jayway.restassured.specification.RequestSpecification;
import org.alfresco.dataprep.AlfrescoHttpClient;
import org.alfresco.dataprep.AlfrescoHttpClientFactory;
import org.alfresco.dataprep.UserService;
import org.alfresco.rest.core.RestAPI;
import org.alfresco.rest.core.RMRestProperties;
import org.alfresco.rest.core.RMRestWrapper;
import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent;
import org.alfresco.utility.data.DataUser;
import org.alfresco.rest.rm.community.model.user.UserPermissions;
import org.alfresco.rest.rm.community.model.user.UserRoles;
import org.alfresco.rest.rm.community.requests.RMModelRequest;
import org.alfresco.utility.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@@ -58,21 +61,44 @@ import org.springframework.stereotype.Component;
// "old-style" API calls.
@Component
@Scope (value = "prototype")
public class RMUserAPI extends RestAPI<RMUserAPI>
public class RMUserAPI extends RMModelRequest
{
@Autowired
private UserService userService;
@Autowired
private DataUser dataUser;
@Autowired
private AlfrescoHttpClientFactory alfrescoHttpClientFactory;
/**
* @param rmRestWrapper
*/
public RMUserAPI(RMRestWrapper rmRestWrapper)
{
super(rmRestWrapper);
}
/**
* Helper method to obtain {@link AlfrescoHttpClient}
* @return Initialized {@link AlfrescoHttpClient} instance
*/
private AlfrescoHttpClient getAlfrescoHttpClient()
{
RMRestProperties properties = getRMRestWrapper().getRmRestProperties();
AlfrescoHttpClientFactory factory = new AlfrescoHttpClientFactory();
factory.setHost(properties.getServer());
factory.setPort(Integer.parseInt(properties.getPort()));
factory.setScheme(properties.getScheme());
return factory.getObject();
}
/**
* Assign RM role to user
* @param userName User's username
* @param userRole User's RM role, one of {@link UserRoles} roles
* @throws Exception for failed requests
*/
public void assignRoleToUser(String userName, String userRole) throws Exception
{
UserModel adminUser = getRMRestWrapper().getTestUser();
// get an "old-style" REST API client
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
AlfrescoHttpClient client = getAlfrescoHttpClient();
// override v1 baseURI and basePath
RequestSpecification spec = new RequestSpecBuilder()
@@ -85,13 +111,13 @@ public class RMUserAPI extends RestAPI<RMUserAPI>
.log().all()
.pathParam("role", userRole)
.pathParam("authority", userName)
.param("alf_ticket", client.getAlfTicket(
dataUser.getAdminUser().getUsername(), dataUser.getAdminUser().getPassword()))
.param("alf_ticket", client.getAlfTicket(adminUser.getUsername(),
adminUser.getPassword()))
.when()
.post("/rm/roles/{role}/authorities/{authority}")
.prettyPeek()
.andReturn();
usingRestWrapper().setStatusCode(Integer.toString(response.getStatusCode()));
getRMRestWrapper().setStatusCode(Integer.toString(response.getStatusCode()));
}
/**
@@ -102,8 +128,10 @@ public class RMUserAPI extends RestAPI<RMUserAPI>
*/
public void addUserPermission(FilePlanComponent component, UserModel user, String permission)
{
UserModel adminUser = getRMRestWrapper().getTestUser();
// get an "old-style" REST API client
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
AlfrescoHttpClient client = getAlfrescoHttpClient();
JsonObject bodyJson = buildObject()
.addArray("permissions")
@@ -118,11 +146,11 @@ public class RMUserAPI extends RestAPI<RMUserAPI>
.setBaseUri(client.getApiUrl())
.setBasePath("/")
.build();
// execute an "old-style" API call
Response response = given()
.spec(spec)
.auth().basic(dataUser.getAdminUser().getUsername(), dataUser.getAdminUser().getPassword())
.auth().basic(adminUser.getUsername(), adminUser.getPassword())
.contentType(ContentType.JSON)
.body(bodyJson.toString())
.pathParam("nodeId", component.getId())
@@ -131,23 +159,47 @@ public class RMUserAPI extends RestAPI<RMUserAPI>
.post("/node/workspace/SpacesStore/{nodeId}/rmpermissions")
.prettyPeek()
.andReturn();
usingRestWrapper().setStatusCode(Integer.toString(response.getStatusCode()));
getRMRestWrapper().setStatusCode(Integer.toString(response.getStatusCode()));
}
/**
* Creates a user with the given name using the old APIs
*
* @param userName The user name
* @param userPassword The user's password
* @param userEmail The user's e-mail address
* @return <code>true</code> if the user was created successfully, <code>false</code> otherwise.
*/
public boolean createUser(String userName)
public boolean createUser(String userName, String userPassword, String userEmail)
{
return userService.create(dataUser.getAdminUser().getUsername(),
dataUser.getAdminUser().getPassword(),
userName,
"password",
"default@alfresco.com",
userName,
userName);
UserModel adminUser = getRMRestWrapper().getTestUser();
AlfrescoHttpClient client = getAlfrescoHttpClient();
JsonObject body = buildObject()
.add("userName", userName)
.add("firstName", userName)
.add("lastName", userName)
.add("password", userPassword)
.add("email", userEmail)
.getJson();
RequestSpecification spec = new RequestSpecBuilder()
.setBaseUri(client.getApiUrl())
.setBasePath("/")
.setAuth(basic(adminUser.getUsername(), adminUser.getPassword()))
.setContentType(ContentType.JSON)
.setBody(body.toString())
.build();
// create POST request to "people" endpoint
Response response = given()
.spec(spec)
.log().all()
.when()
.post("people")
.prettyPeek()
.andReturn();
return (response.getStatusCode() == OK.value());
}
}

View File

@@ -28,13 +28,14 @@ package org.alfresco.rest.rm.community.requests.igCoreAPI;
import static com.jayway.restassured.RestAssured.given;
import static org.alfresco.rest.core.RestRequest.requestWithBody;
import static org.alfresco.rest.core.RestRequest.simpleRequest;
import static org.alfresco.rest.rm.community.util.ParameterCheck.mandatoryObject;
import static org.alfresco.rest.rm.community.util.ParameterCheck.mandatoryString;
import static org.alfresco.rest.rm.community.util.PojoUtility.toJson;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.response.ResponseBody;
import org.alfresco.rest.core.RMRestWrapper;
@@ -78,14 +79,11 @@ public class RecordsAPI extends RMModelRequest
public ResponseBody<?> getRecordContent(String recordId) throws Exception
{
mandatoryString("recordId", recordId);
Response response = given()
.auth().basic(getRMRestWrapper().getTestUser().getUsername(),
getRMRestWrapper().getTestUser().getPassword())
.when()
.get("records/{recordId}/content", recordId)
.andReturn();
getRMRestWrapper().setStatusCode(Integer.toString(response.getStatusCode()));
return response.getBody();
return getRMRestWrapper()
.processHtmlResponse(simpleRequest(GET,"records/{recordId}/content", recordId))
.getBody();
}
/**
@@ -143,6 +141,5 @@ public class RecordsAPI extends RMModelRequest
parameters
));
}
}

View File

@@ -56,6 +56,7 @@ public class RestIGCoreAPI extends RMModelRequest
RestAssured.baseURI = format("%s://%s", rmRestProperties.getScheme(), rmRestProperties.getServer());
RestAssured.port = parseInt(rmRestProperties.getPort());
RestAssured.basePath = rmRestProperties.getRestRmPath();
restWrapper.configureRequestSpec().setBasePath(RestAssured.basePath);
}
/**
@@ -97,4 +98,14 @@ public class RestIGCoreAPI extends RMModelRequest
{
return new FilesAPI(getRMRestWrapper());
}
/**
* Provides DSL for RM user management API
*
* @return {@link RMUserAPI}
*/
public RMUserAPI usingRMUser()
{
return new RMUserAPI(getRMRestWrapper());
}
}

View File

@@ -0,0 +1,50 @@
/*
* #%L
* Alfresco Records Management Module
* %%
* Copyright (C) 2005 - 2017 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* -
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
* -
* 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/>.
* #L%
*/
package org.alfresco.rest.rm.community.util;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentProperties;
/**
* Mix class for FilePlanComponent POJO class
* Mix-in annotations are: a way to associate annotations with classes
* without modifying (target) classes themselves.
*
* @author Rodica Sutu
* @since 2.6
*/
public abstract class FilePlanComponentMixIn
{
/**
* Annotation used to indicate that a property should be serialized "unwrapped"
* Its properties are instead included as properties of its containing Object
*/
@JsonUnwrapped
abstract FilePlanComponentProperties getProperties();
}

View File

@@ -26,13 +26,12 @@
*/
package org.alfresco.rest.rm.community.util;
import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent;
/**
* Utility class for creating the json object
*
@@ -56,15 +55,35 @@ public class PojoUtility
//return the json object
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(model);
}
catch (JsonGenerationException e)
catch (JsonProcessingException e)
{
return e.toString();
}
catch (JsonMappingException e)
}
/**
* Converting object to JSON string for electronic records
*
* @param model The java object model to convert
* @throws JsonProcessingException Throws exceptions if the given object doesn't match to the POJO class model
*/
public static String toJsonElectronicRecord(Object model)
{
ObjectMapper mapper = new ObjectMapper();
//inject the "mix-in" annotations from FilePlanComponentMix to
// FilePlanComponent POJO class when converting to json
mapper.addMixIn(FilePlanComponent.class, FilePlanComponentMixIn.class);
//include only values that differ from default settings to be included
mapper.setSerializationInclusion(Include.NON_DEFAULT);
try
{
return e.toString();
//return the json object
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(model);
}
catch (IOException e)
catch (JsonProcessingException e)
{
return e.toString();
}