提交 c71f6154 编写于 作者: F Frederik Heremans

Added new deployment-resources REST

上级 174a75ab
......@@ -13,6 +13,8 @@
package org.activiti.rest.api;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Iterator;
......@@ -37,6 +39,36 @@ public class SecuredResource extends ServerResource {
protected String loggedInUser;
/**
* Create a full URL to a rest-resource.
*
* @param urlFragments fragments of the URL, relative to the base of the rest-application.
* @param arguments arguments to replace the placeholders in the urlFragments, using the {@link MessageFormat}
* conventions (eg. <code>{0}</code> is replaced by first argument value).
*/
public String createFullResourceUrl(String[] urlFragments, Object ... arguments) {
Reference url = getRequest().getRootRef().clone();
for(String urlFragment : urlFragments) {
url.addSegment(MessageFormat.format(urlFragment, arguments));
}
return url.toString();
}
/**
* Get a request attribute value, decoded.
*/
protected String getAttribute(String name) {
String value = (String) getRequest().getAttributes().get(name);
if(value != null) {
try {
return URLDecoder.decode(value, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("JVM does not support UTF-8 encoding.", uee);
}
}
return null;
}
protected boolean authenticate() {
return authenticate(null);
}
......@@ -95,19 +127,4 @@ public class SecuredResource extends ServerResource {
}
return variables;
}
/**
* Create a full URL to a rest-resource.
*
* @param urlFragments fragments of the URL, relative to the base of the rest-application.
* @param arguments arguments to replace the placeholders in the urlFragments, using the {@link MessageFormat}
* conventions (eg. <code>{0}</code> is replaced by first argument value).
*/
public String createFullResourceUrl(String[] urlFragments, Object ... arguments) {
Reference url = getRequest().getRootRef().clone();
for(String urlFragment : urlFragments) {
url.addSegment(MessageFormat.format(urlFragment, arguments));
}
return url.toString();
}
}
......@@ -27,10 +27,9 @@ public final class RestUrls {
*/
public static final String SEGMENT_REPOSITORY_RESOURCES = "repository";
/**
* Segment for deployment resources: <i>deployment</i>
*/
public static final String SEGMENT_DEPLOYMENT_RESOURCE = "deployments";
public static final String SEGMENT_DEPLOYMENT_ARTIFACT_RESOURCE = "resources";
public static final String SEGMENT_DEPLOYMENT_ARTIFACT_RESOURCE_CONTENT = "resourcedata";
/**
* URL template for the deployment collection: <i>repository/deployments</i>
......@@ -42,6 +41,24 @@ public final class RestUrls {
*/
public static final String[] URL_DEPLOYMENT = {SEGMENT_REPOSITORY_RESOURCES, SEGMENT_DEPLOYMENT_RESOURCE, "{0}"};
/**
* URL template listing deployment resources: <i>repository/deployments/{0:deploymentId}/resources/{1}:resourceId</i>
*/
public static final String[] URL_DEPLOYMENT_RESOURCES = {SEGMENT_REPOSITORY_RESOURCES, SEGMENT_DEPLOYMENT_RESOURCE,
"{0}", SEGMENT_DEPLOYMENT_ARTIFACT_RESOURCE};
/**
* URL template for a single deployment resource: <i>repository/deployments/{0:deploymentId}/resources/{1}:resourceId</i>
*/
public static final String[] URL_DEPLOYMENT_RESOURCE = {SEGMENT_REPOSITORY_RESOURCES, SEGMENT_DEPLOYMENT_RESOURCE,
"{0}", SEGMENT_DEPLOYMENT_ARTIFACT_RESOURCE, "{1}"};
/**
* URL template for a single deployment resource content: <i>repository/deployments/{0:deploymentId}/resourcedata/{1}:resourceId</i>
*/
public static final String[] URL_DEPLOYMENT_RESOURCE_CONTENT = {SEGMENT_REPOSITORY_RESOURCES, SEGMENT_DEPLOYMENT_RESOURCE,
"{0}", SEGMENT_DEPLOYMENT_ARTIFACT_RESOURCE_CONTENT, "{1}"};
/**
* Creates an url based on the passed fragments and replaces any placeholders with the given arguments. The
......
......@@ -11,7 +11,7 @@
* limitations under the License.
*/
package org.activiti.rest.api.repository;
package org.activiti.rest.api.legacy;
import java.io.InputStream;
......@@ -25,6 +25,7 @@ import org.restlet.resource.Get;
/**
* @author Tijs Rademakers
*/
@Deprecated
public class DeploymentArtifactResource extends SecuredResource {
@Get
......
......@@ -11,7 +11,7 @@
* limitations under the License.
*/
package org.activiti.rest.api.repository;
package org.activiti.rest.api.legacy;
import java.util.List;
......@@ -28,6 +28,7 @@ import org.restlet.resource.Get;
/**
* @author Tijs Rademakers
*/
@Deprecated
public class DeploymentArtifactsResource extends SecuredResource {
@Get
......
......@@ -33,7 +33,7 @@ public class DeploymentResource extends SecuredResource {
public DeploymentResponse getDeployment() {
if(authenticate() == false) return null;
String deploymentId = (String) getRequest().getAttributes().get("deploymentId");
String deploymentId = getAttribute("deploymentId");
if(deploymentId == null) {
throw new ActivitiIllegalArgumentException("The deploymentId cannot be null");
}
......@@ -54,7 +54,7 @@ public class DeploymentResource extends SecuredResource {
public void deleteDeployment() {
if(authenticate() == false) return;
String deploymentId = (String) getRequest().getAttributes().get("deploymentId");
String deploymentId = getAttribute("deploymentId");
Boolean cascade = RequestUtil.getBoolean(getQuery(), "cascade", false);
if (cascade) {
......
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest.api.repository;
import java.util.ArrayList;
import java.util.List;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.repository.Deployment;
import org.activiti.rest.api.ActivitiUtil;
import org.activiti.rest.api.RestUrls;
import org.activiti.rest.api.SecuredResource;
import org.restlet.resource.Get;
/**
* @author Frederik Heremans
*/
public class DeploymentResourceCollectionResource extends SecuredResource {
@Get
public List<DeploymentResourceResponse> getDeploymentResources() {
if(authenticate() == false) return null;
String deploymentId = getAttribute("deploymentId");
if(deploymentId == null) {
throw new ActivitiIllegalArgumentException("No deployment id provided");
}
// Check if deployment exists
Deployment deployment = ActivitiUtil.getRepositoryService().createDeploymentQuery().deploymentId(deploymentId).singleResult();
if(deployment == null) {
throw new ActivitiObjectNotFoundException("Could not find a deployment with id '" + deploymentId + "'.", Deployment.class);
}
List<String> resourceList = ActivitiUtil.getRepositoryService().getDeploymentResourceNames(deploymentId);
// Add additional metadata to the artifact-strings before returning
List<DeploymentResourceResponse> resposeList = new ArrayList<DeploymentResourceResponse>();
String resourceUrl = null;
String resourceContentUrl = null;
for(String resource : resourceList) {
resourceUrl = createFullResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCE, deploymentId, resource);
resourceContentUrl = createFullResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCE_CONTENT, deploymentId, resource);
resposeList.add(new DeploymentResourceResponse(resource, resourceUrl, resourceContentUrl));
}
return resposeList;
}
}
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest.api.repository;
import java.io.InputStream;
import java.util.List;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.repository.Deployment;
import org.activiti.rest.api.ActivitiUtil;
import org.activiti.rest.api.SecuredResource;
import org.restlet.data.MediaType;
import org.restlet.representation.InputRepresentation;
import org.restlet.resource.Get;
/**
* @author Frederik Heremans
*/
public class DeploymentResourceDataResource extends SecuredResource {
@Get
public InputRepresentation getDeploymentResource() {
if(authenticate() == false) return null;
String deploymentId = getAttribute("deploymentId");
if(deploymentId == null) {
throw new ActivitiIllegalArgumentException("No deployment id provided");
}
String resourceId = getAttribute("resourceId");
if(resourceId == null) {
throw new ActivitiIllegalArgumentException("No resource id provided");
}
// Check if deployment exists
Deployment deployment = ActivitiUtil.getRepositoryService().createDeploymentQuery().deploymentId(deploymentId).singleResult();
if(deployment == null) {
throw new ActivitiObjectNotFoundException("Could not find a deployment with id '" + deploymentId + "'.", Deployment.class);
}
List<String> resourceList = ActivitiUtil.getRepositoryService().getDeploymentResourceNames(deploymentId);
if (resourceList.contains(resourceId)) {
final InputStream resourceStream = ActivitiUtil.getRepositoryService().getResourceAsStream(deploymentId, resourceId);
// TODO: Externalize mime-type-mappings?
MediaType mediaType = null;
String lowerResourceName = resourceId.toLowerCase();
if (lowerResourceName.endsWith("png")) {
mediaType = MediaType.IMAGE_PNG;
} else if (lowerResourceName.endsWith("xml") || lowerResourceName.endsWith("bpmn")) {
mediaType = MediaType.TEXT_XML;
} else {
mediaType = MediaType.APPLICATION_ALL;
}
return new InputRepresentation(resourceStream, mediaType);
} else {
// Resource not found in deployment
throw new ActivitiObjectNotFoundException("Could not find a resource with id '" + resourceId + "' in deployment '" + deploymentId + "'.", String.class);
}
}
}
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest.api.repository;
import java.util.List;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.repository.Deployment;
import org.activiti.rest.api.ActivitiUtil;
import org.activiti.rest.api.RestUrls;
import org.activiti.rest.api.SecuredResource;
import org.restlet.resource.Get;
/**
* @author Frederik Heremans
*/
public class DeploymentResourceResource extends SecuredResource {
@Get
public DeploymentResourceResponse getDeploymentResource() {
if(authenticate() == false) return null;
String deploymentId = getAttribute("deploymentId");
if(deploymentId == null) {
throw new ActivitiIllegalArgumentException("No deployment id provided");
}
String resourceId = getAttribute("resourceId");
if(resourceId == null) {
throw new ActivitiIllegalArgumentException("No resource id provided");
}
// Check if deployment exists
Deployment deployment = ActivitiUtil.getRepositoryService().createDeploymentQuery().deploymentId(deploymentId).singleResult();
if(deployment == null) {
throw new ActivitiObjectNotFoundException("Could not find a deployment with id '" + deploymentId + "'.", Deployment.class);
}
List<String> resourceList = ActivitiUtil.getRepositoryService().getDeploymentResourceNames(deploymentId);
if(resourceList.contains(resourceId)) {
// Build resource representation
String resourceUrl = createFullResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCE, deploymentId, resourceId);
String resourceContentUrl = createFullResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCE_CONTENT, deploymentId, resourceId);
return new DeploymentResourceResponse(resourceId, resourceUrl, resourceContentUrl);
} else {
// Resource not found in deployment
throw new ActivitiObjectNotFoundException("Could not find a resource with id '" + resourceId
+ "' in deployment '" + deploymentId + "'.", String.class);
}
}
}
package org.activiti.rest.api.repository;
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Frederik Heremans
*/
public class DeploymentResourceResponse {
private String id;
private String url;
private String contentUrl;
public DeploymentResourceResponse(String resourceId, String url, String contentUrl) {
setId(resourceId);
setUrl(url);
setContentUrl(contentUrl);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setContentUrl(String contentUrl) {
this.contentUrl = contentUrl;
}
public String getContentUrl() {
return contentUrl;
}
}
......@@ -13,6 +13,8 @@ import org.activiti.rest.api.identity.UserGroupsResource;
import org.activiti.rest.api.identity.UserPictureResource;
import org.activiti.rest.api.identity.UserResource;
import org.activiti.rest.api.identity.UserSearchResource;
import org.activiti.rest.api.legacy.DeploymentArtifactResource;
import org.activiti.rest.api.legacy.DeploymentArtifactsResource;
import org.activiti.rest.api.legacy.DeploymentDeleteResource;
import org.activiti.rest.api.legacy.DeploymentUploadResource;
import org.activiti.rest.api.legacy.DeploymentsDeleteResource;
......@@ -35,10 +37,11 @@ import org.activiti.rest.api.process.ProcessInstanceTaskResource;
import org.activiti.rest.api.process.ProcessInstancesResource;
import org.activiti.rest.api.process.SignalEventSubscriptionResource;
import org.activiti.rest.api.process.StartProcessInstanceResource;
import org.activiti.rest.api.repository.DeploymentArtifactResource;
import org.activiti.rest.api.repository.DeploymentArtifactsResource;
import org.activiti.rest.api.repository.DeploymentCollectionResource;
import org.activiti.rest.api.repository.DeploymentResource;
import org.activiti.rest.api.repository.DeploymentResourceCollectionResource;
import org.activiti.rest.api.repository.DeploymentResourceDataResource;
import org.activiti.rest.api.repository.DeploymentResourceResource;
import org.activiti.rest.api.repository.SimpleWorkflowResource;
import org.activiti.rest.api.task.TaskAddResource;
import org.activiti.rest.api.task.TaskAttachmentAddResource;
......@@ -60,6 +63,9 @@ public class RestServicesInit {
// New REST-urls
router.attach("/repository/deployments", DeploymentCollectionResource.class);
router.attach("/repository/deployments/{deploymentId}", DeploymentResource.class);
router.attach("/repository/deployments/{deploymentId}/resources", DeploymentResourceCollectionResource.class);
router.attach("/repository/deployments/{deploymentId}/resources/{resourceId}", DeploymentResourceResource.class);
router.attach("/repository/deployments/{deploymentId}/resourcedata/{resourceId}", DeploymentResourceDataResource.class);
// Old rest-urls
router.attach("/process-engine", ProcessEngineResource.class);
......
......@@ -2,6 +2,8 @@ package org.activiti.rest.api.legacy;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.zip.ZipEntry;
......@@ -27,7 +29,9 @@ import org.restlet.resource.ResourceException;
* POST /deployment,
* DELETE /deployment/{deploymentId},
* GET /deployments,
* POST /deployments/delete
* POST /deployments/delete,
* GET /deployment/{deploymentId}/resources,
* GET /deployment/{deploymentId}/resources/{resourceName}
*
* @author Frederik Heremans
*/
......@@ -275,24 +279,94 @@ public class LegacyDeploymentResourseTest extends BaseRestTestCase {
* Test deleting multiple deployments.
* POST deployments/delete
*/
public void testDeleteMultipleDeploymentsUnexisting() throws Exception {
public void testDeleteMultipleDeploymentsUnexisting() throws Exception {
try {
ClientResource client = getAuthenticatedClient("deployments/delete");
ObjectNode requestNode = objectMapper.createObjectNode();
ArrayNode deploymentIds = objectMapper.createArrayNode();
deploymentIds.add("unexisting");
requestNode.put("deploymentIds", deploymentIds);
try {
client.post(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("Could not find a deployment with id 'unexisting'.", expected.getStatus().getDescription());
}
} finally {
// Always cleanup any created deployments, even if the test failed
List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
for(Deployment deployment : deployments) {
repositoryService.deleteDeployment(deployment.getId(), true);
}
}
}
/**
* Test get all resources in a deployment.
* GET /deployment/{deploymentId}/resources
*/
public void testGetDeploymentResources() throws Exception {
try {
Deployment deployment = repositoryService.createDeployment().name("Deployment 1")
.addClasspathResource("org/activiti/rest/api/repository/oneTaskProcess.bpmn20.xml")
.addInputStream("test.png", new ByteArrayInputStream("test-content".getBytes()))
.deploy();
ClientResource client = getAuthenticatedClient("deployment/" + deployment.getId() + "/resources");
Representation response = client.get();
assertEquals(Status.SUCCESS_OK, client.getResponse().getStatus());
JsonNode resourcesNode = objectMapper.readTree(response.getStream()).get("resources");
assertEquals(2L, resourcesNode.size());
List<String> resources = new ArrayList<String>();
for(int i=0; i < resourcesNode.size(); i++) {
resources.add(resourcesNode.get(i).getTextValue());
}
assertTrue(resources.contains("org/activiti/rest/api/repository/oneTaskProcess.bpmn20.xml"));
assertTrue(resources.contains("test.png"));
} finally {
// Always cleanup any created deployments, even if the test failed
List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
for(Deployment deployment : deployments) {
repositoryService.deleteDeployment(deployment.getId(), true);
}
}
}
/**
* Test get all resources in a deployment.
* GET /deployment/{deploymentId}/resources/{resourceName}
*/
public void testGetDeploymentResource() throws Exception {
try {
ClientResource client = getAuthenticatedClient("deployments/delete");
Deployment deployment = repositoryService.createDeployment().name("Deployment 1")
.addClasspathResource("org/activiti/rest/api/repository/oneTaskProcess.bpmn20.xml")
.addInputStream("test.png", new ByteArrayInputStream("test-content".getBytes()))
.deploy();
ObjectNode requestNode = objectMapper.createObjectNode();
ArrayNode deploymentIds = objectMapper.createArrayNode();
deploymentIds.add("unexisting");
requestNode.put("deploymentIds", deploymentIds);
ClientResource client = getAuthenticatedClient("deployment/" + deployment.getId() + "/resource/org/activiti/rest/api/repository/oneTaskProcess.bpmn20.xml");
try {
client.post(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("Could not find a deployment with id 'unexisting'.", expected.getStatus().getDescription());
}
Representation response = client.get();
assertEquals(Status.SUCCESS_OK, client.getResponse().getStatus());
// Read returned and original content
ByteArrayOutputStream responseContent = new ByteArrayOutputStream();
IOUtils.copy(response.getStream(), responseContent);
ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
IOUtils.copy(ReflectUtil.getResourceAsStream("org/activiti/rest/api/repository/oneTaskProcess.bpmn20.xml"), requestContent);
// Compare response with content that is deployed
assertTrue("Returned content doesn't match deployed content",
Arrays.equals(requestContent.toByteArray(), responseContent.toByteArray()));
} finally {
// Always cleanup any created deployments, even if the test failed
......
......@@ -125,36 +125,4 @@ public class DeploymentCollectionResourceTest extends BaseRestTestCase {
}
}
}
// /**
// * Test deleting deployments.
// * DELETE repository/deployments
// */
// public void testDeleteDeployment() throws Exception {
//
// try {
// repositoryService.createDeployment().name("Deployment 1")
// .addClasspathResource("org/activiti/rest/api/repository/oneTaskProcess.bpmn20.xml")
// .deploy();
// repositoryService.createDeployment().name("Deployment 2")
// .addClasspathResource("org/activiti/rest/api/repository/oneTaskProcess.bpmn20.xml")
// .deploy();
//
// assertEquals(2L, repositoryService.createDeploymentQuery().count());
//
// ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_DEPLOYMENT_COLLECTION));
// client.delete();
//
// assertEquals(Status.SUCCESS_NO_CONTENT, client.getResponse().getStatus());
// assertEquals(0L, repositoryService.createDeploymentQuery().count());
//
// } finally {
// // Always cleanup any created deployments, even if the test failed
// List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
// for(Deployment deployment : deployments) {
// repositoryService.deleteDeployment(deployment.getId(), true);
// }
// }
// }
}
package org.activiti.rest.api.repository;
import java.io.ByteArrayInputStream;
import java.util.List;
import org.activiti.engine.repository.Deployment;
import org.activiti.rest.BaseRestTestCase;
import org.activiti.rest.api.RestUrls;
import org.codehaus.jackson.JsonNode;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
/**
* Test for all REST-operations related to a resources that is part of a deployment.
*
* @author Frederik Heremans
*/
public class DeploymentResourceResourceTest extends BaseRestTestCase {
/**
* Test getting a single resource, deployed in a deployment.
* GET repository/deployments/{deploymentId}/resources/{resourceId}
*/
public void testGetDeploymentResource() throws Exception {
try {
String rawResourceName = "org/activiti/rest/api/repository/oneTaskProcess.bpmn20.xml";
Deployment deployment = repositoryService.createDeployment().name("Deployment 1")
.addClasspathResource(rawResourceName)
.addInputStream("test.txt", new ByteArrayInputStream("Test content".getBytes()))
.deploy();
// Build up the URL manually to make sure resource-id gets encoded correctly as one piece
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCES, deployment.getId()));
client.getReference().addSegment(rawResourceName);
Representation response = client.get();
// Id contains slashes so it will be encoded by the client
String encodedResourceId = client.getReference().getLastSegment(false);
// Check "OK" status
assertEquals(Status.SUCCESS_OK, client.getResponse().getStatus());
JsonNode responseNode = objectMapper.readTree(response.getStream());
// Check URL's for the resource
assertTrue(responseNode.get("url").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(
RestUrls.URL_DEPLOYMENT_RESOURCE, deployment.getId(), encodedResourceId)));
assertTrue(responseNode.get("contentUrl").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(
RestUrls.URL_DEPLOYMENT_RESOURCE_CONTENT, deployment.getId(), encodedResourceId)));
} finally {
// Always cleanup any created deployments, even if the test failed
List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
for(Deployment deployment : deployments) {
repositoryService.deleteDeployment(deployment.getId(), true);
}
}
}
/**
* Test getting a single resource for an unexisting deployment.
* GET repository/deployments/{deploymentId}/resources/{resourceId}
*/
public void testGetDeploymentResourceUnexistingDeployment() throws Exception {
ClientResource client = getAuthenticatedClient(
RestUrls.createRelativeResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCE, "unexisting", "resource.png"));
try {
client.get();
fail("Expected 404 status, but was: " + client.getStatus());
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("Could not find a deployment with id 'unexisting'.", expected.getStatus().getDescription());
}
}
/**
* Test getting an unexisting resource for an existing deployment.
* GET repository/deployments/{deploymentId}/resources/{resourceId}
*/
public void testGetDeploymentResourceUnexistingResource() throws Exception {
try {
Deployment deployment = repositoryService.createDeployment().name("Deployment 1")
.addInputStream("test.txt", new ByteArrayInputStream("Test content".getBytes()))
.deploy();
ClientResource client = getAuthenticatedClient(
RestUrls.createRelativeResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCE, deployment.getId(), "unexisting-resource.png"));
try {
client.get();
fail("Expected 404 status, but was: " + client.getStatus());
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("Could not find a resource with id 'unexisting-resource.png' in deployment '" + deployment.getId() + "'.", expected.getStatus().getDescription());
}
} finally {
// Always cleanup any created deployments, even if the test failed
List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
for(Deployment deployment : deployments) {
repositoryService.deleteDeployment(deployment.getId(), true);
}
}
}
/**
* Test getting a deployment resource content.
* GET repository/deployments/{deploymentId}/resources/{resourceId}
*/
public void testGetDeploymentResourceContent() throws Exception {
try {
Deployment deployment = repositoryService.createDeployment().name("Deployment 1")
.addInputStream("test.txt", new ByteArrayInputStream("Test content".getBytes()))
.deploy();
ClientResource client = getAuthenticatedClient(
RestUrls.createRelativeResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCE_CONTENT, deployment.getId(), "test.txt"));
client.get();
String responseAsString = client.getResponse().getEntityAsText();
assertNotNull(responseAsString);
assertEquals("Test content", responseAsString);
} finally {
// Always cleanup any created deployments, even if the test failed
List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
for(Deployment deployment : deployments) {
repositoryService.deleteDeployment(deployment.getId(), true);
}
}
}
}
package org.activiti.rest.api.repository;
import java.io.ByteArrayInputStream;
import java.util.List;
import org.activiti.engine.repository.Deployment;
import org.activiti.rest.BaseRestTestCase;
import org.activiti.rest.api.RestUrls;
import org.codehaus.jackson.JsonNode;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
/**
* Test for all REST-operations related to listing the resources that are part of a deployment.
*
* @author Frederik Heremans
*/
public class DeploymentResourcesResourceTest extends BaseRestTestCase {
/**
* Test getting all resources for a single deployment.
* GET repository/deployments/{deploymentId}/resources
*/
public void testGetDeploymentResources() throws Exception {
try {
Deployment deployment = repositoryService.createDeployment().name("Deployment 1")
.addClasspathResource("org/activiti/rest/api/repository/oneTaskProcess.bpmn20.xml")
.addInputStream("test.txt", new ByteArrayInputStream("Test content".getBytes()))
.deploy();
ClientResource client = getAuthenticatedClient(
RestUrls.createRelativeResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCES, deployment.getId()));
Representation response = client.get();
// Check "OK" status
assertEquals(Status.SUCCESS_OK, client.getResponse().getStatus());
JsonNode responseNode = objectMapper.readTree(response.getStream());
assertTrue(responseNode.isArray());
assertEquals(2, responseNode.size());
// Since resources can be returned in any arbitrary order, find the right one to check
JsonNode txtNode = null;
for(int i=0; i< responseNode.size(); i++) {
if("test.txt".equals(responseNode.get(i).get("id").getTextValue())) {
txtNode = responseNode.get(i);
break;
}
}
// Check URL's for the resource
assertNotNull(txtNode);
assertTrue(txtNode.get("url").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(
RestUrls.URL_DEPLOYMENT_RESOURCE, deployment.getId(), "test.txt")));
assertTrue(txtNode.get("contentUrl").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(
RestUrls.URL_DEPLOYMENT_RESOURCE_CONTENT, deployment.getId(), "test.txt")));
} finally {
// Always cleanup any created deployments, even if the test failed
List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
for(Deployment deployment : deployments) {
repositoryService.deleteDeployment(deployment.getId(), true);
}
}
}
/**
* Test getting all resources for a single unexisting deployment.
* GET repository/deployments/{deploymentId}/resources
*/
public void testGetDeploymentResourcesUnexistingDeployment() throws Exception {
ClientResource client = getAuthenticatedClient(
RestUrls.createRelativeResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCES, "unexisting"));
try {
client.get();
fail("Expected 404 status, but was: " + client.getStatus());
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("Could not find a deployment with id 'unexisting'.", expected.getStatus().getDescription());
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册