Search in sources :

Example 6 with ResourceMetadata

use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.

the class InspectorTests method testInspectOperations.

@Test
public void testInspectOperations() throws IllegalAccessException, InstantiationException, Throwable {
    Api api = Api.valueOf("alfrescomock", "private", "1");
    List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();
    GrassEntityResource grassEntityResource = new GrassEntityResource();
    ResourceInspector.inspectOperations(api, GrassEntityResource.class, "-root-", metainfo);
    assertEquals(3, metainfo.size());
    for (ResourceMetadata resourceMetadata : metainfo) {
        assertEquals(ResourceMetadata.RESOURCE_TYPE.OPERATION, resourceMetadata.getType());
        OperationResourceMetaData operationResourceMetaData = (OperationResourceMetaData) resourceMetadata;
        Method actionMethod = operationResourceMetaData.getOperationMethod();
        String result = null;
        final WithResponse wr = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
        switch(resourceMetadata.getUniqueId()) {
            case "/-root-/{id}/grow":
                assertNotNull("GrassEntityResource supports POST", resourceMetadata.getOperation(HttpMethod.POST));
                assertNull("GrassEntityResource does not support DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
                ResourceOperation op = resourceMetadata.getOperation(HttpMethod.POST);
                assertEquals("grow should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
                Class paramType = resourceMetadata.getObjectType(op);
                Object paramObj = paramType.newInstance();
                result = (String) ResourceInspectorUtil.invokeMethod(actionMethod, grassEntityResource, "xyz", paramObj, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)), wr);
                assertEquals("Growing well", result);
                assertFalse(operationResourceMetaData.isNoAuth(null));
                break;
            case "/-root-/{id}/cut":
                assertNotNull("GrassEntityResource supports POST", resourceMetadata.getOperation(HttpMethod.POST));
                assertNull("GrassEntityResource does not support GET", resourceMetadata.getOperation(HttpMethod.GET));
                op = resourceMetadata.getOperation(HttpMethod.POST);
                assertNull(resourceMetadata.getObjectType(op));
                assertEquals("cut should return ACCEPTED", Status.STATUS_NOT_IMPLEMENTED, op.getSuccessStatus());
                result = (String) ResourceInspectorUtil.invokeMethod(actionMethod, grassEntityResource, "xyz", null, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)), wr);
                assertEquals("All done", result);
                assertFalse(operationResourceMetaData.isNoAuth(null));
                break;
            case "/-root-/{id}/cut-noAuth":
                assertNotNull("GrassEntityResource supports POST", resourceMetadata.getOperation(HttpMethod.POST));
                op = resourceMetadata.getOperation(HttpMethod.POST);
                assertNull(resourceMetadata.getObjectType(op));
                assertEquals("cut should return ACCEPTED", Status.STATUS_NOT_IMPLEMENTED, op.getSuccessStatus());
                result = (String) ResourceInspectorUtil.invokeMethod(actionMethod, grassEntityResource, "xyz", null, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)), wr);
                assertEquals("All done without Auth", result);
                assertTrue(operationResourceMetaData.isNoAuth(null));
                break;
            default:
                fail("Invalid action information.");
        }
    }
}
Also used : GrassEntityResource(org.alfresco.rest.framework.tests.api.mocks.GrassEntityResource) WithResponse(org.alfresco.rest.framework.webscripts.WithResponse) OperationResourceMetaData(org.alfresco.rest.framework.core.OperationResourceMetaData) ArrayList(java.util.ArrayList) Api(org.alfresco.rest.framework.Api) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) Test(org.junit.Test)

Example 7 with ResourceMetadata

use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.

the class InspectorTests method testNodesCommentsRelation.

@Test
public void testNodesCommentsRelation() {
    List<ResourceMetadata> metainfo = ResourceInspector.inspect(NodeCommentsRelation.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    ResourceMetadata metaData = metainfo.get(0);
    assertNotNull(metaData);
    ResourceOperation op = metaData.getOperation(HttpMethod.POST);
    assertTrue("NodeCommentsRelation must support Comment", Comment.class.equals(metaData.getObjectType(op)));
}
Also used : Comment(org.alfresco.rest.api.model.Comment) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) Test(org.junit.Test)

Example 8 with ResourceMetadata

use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.

the class InspectorTests method testInspectRelationshipProperties.

@Test
public void testInspectRelationshipProperties() {
    List<ResourceMetadata> metainfo = ResourceInspector.inspect(GoatRelationshipResource.class);
    assertTrue(metainfo.size() == 2);
    for (ResourceMetadata resourceMetadata : metainfo) {
        switch(resourceMetadata.getUniqueId()) {
            case "/goat/{entityId}/herd":
                assertNotNull("GoatRelationshipResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
                break;
            case "/goat/{entityId}/herd/{id}/content":
                assertNotNull("GoatRelationshipResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
                break;
            default:
                fail("Invalid information.");
        }
    }
}
Also used : ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) Test(org.junit.Test)

Example 9 with ResourceMetadata

use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.

the class InspectorTests method testInspectRelationship.

@Test
public void testInspectRelationship() {
    List<ResourceMetadata> metainfo = ResourceInspector.inspect(SheepBlackSheepResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    ResourceMetadata metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("SheepBlackSheepResource supports GET", metaData.getOperation(HttpMethod.GET));
    ResourceOperation op = metaData.getOperation(HttpMethod.GET);
    List<ResourceParameter> params = op.getParameters();
    assertTrue("readAll method should have 1 url param and 3 query params", params.size() == 4);
    assertNotNull("SheepBlackSheepResource supports PUT", metaData.getOperation(HttpMethod.PUT));
    op = metaData.getOperation(HttpMethod.PUT);
    params = op.getParameters();
    assertTrue("update method should have 2 url params and 1 HTTP_OBJECT param ", params.size() == 3);
    assertNotNull("SheepBlackSheepResource supports POST", metaData.getOperation(HttpMethod.POST));
    op = metaData.getOperation(HttpMethod.POST);
    params = op.getParameters();
    assertTrue("SheepBlackSheepResource must support Sheep", Sheep.class.equals(metaData.getObjectType(op)));
    assertTrue("create method should have 1 url param and 1 HTTP_OBJECT param ", params.size() == 2);
    assertNotNull(params);
    for (ResourceParameter resourceParameter : params) {
        if (ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(resourceParameter.getParamType())) {
            // set to not allow multiple
            assertFalse(resourceParameter.isAllowMultiple());
        }
    }
    assertNotNull("SheepBlackSheepResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    op = metaData.getOperation(HttpMethod.DELETE);
    assertEquals("SheepBlackSheepResource should return STATUS_CONFLICT", Status.STATUS_CONFLICT, op.getSuccessStatus());
    params = op.getParameters();
    assertTrue("DELETE method on a relations should have 2 url params.", params.size() == 2);
    metainfo = ResourceInspector.inspect(MultiPartTestRelationshipResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("MultiPartTestRelationshipResource support POST", metaData.getOperation(HttpMethod.POST));
    assertNull("MultiPartTestRelationshipResource does not supports GET", metaData.getOperation(HttpMethod.GET));
    assertNull("MultiPartTestRelationshipResource does not supports PUT", metaData.getOperation(HttpMethod.PUT));
    assertNull("MultiPartTestRelationshipResource does not supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    op = metaData.getOperation(HttpMethod.POST);
    assertTrue("MultiPartTestRelationshipResource must support MultiPartTestResponse", MultiPartTestResponse.class.equals(metaData.getObjectType(op)));
}
Also used : ResourceParameter(org.alfresco.rest.framework.core.ResourceParameter) MultiPartTestRelationshipResource(org.alfresco.rest.framework.tests.api.mocks.MultiPartTestRelationshipResource) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) MultiPartTestResponse(org.alfresco.rest.framework.tests.api.mocks.MultiPartTestResponse) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) Test(org.junit.Test)

Example 10 with ResourceMetadata

use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.

the class ParamsExtractorTests method mockEntity.

/**
 * Mocks a Entity Resource
 * @return ResourceMetadata a Entity
 */
private static ResourceMetadata mockEntity() {
    ResourceMetadata resourceMock = mock(ResourceMetadata.class);
    ResourceOperation resourceOperation = mock(ResourceOperation.class);
    when(resourceMock.getType()).thenReturn(ResourceMetadata.RESOURCE_TYPE.ENTITY);
    when(resourceMock.getOperation(notNull(HttpMethod.class))).thenReturn(resourceOperation);
    when(resourceMock.getObjectType(notNull(ResourceOperation.class))).thenReturn(Farmer.class);
    return resourceMock;
}
Also used : ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) HttpMethod(org.springframework.http.HttpMethod) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation)

Aggregations

ResourceMetadata (org.alfresco.rest.framework.core.ResourceMetadata)12 Test (org.junit.Test)8 ResourceOperation (org.alfresco.rest.framework.core.ResourceOperation)7 HttpMethod (org.springframework.http.HttpMethod)4 ResourceParameter (org.alfresco.rest.framework.core.ResourceParameter)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Api (org.alfresco.rest.framework.Api)2 MultiPartTestResponse (org.alfresco.rest.framework.tests.api.mocks.MultiPartTestResponse)2 Sheep (org.alfresco.rest.framework.tests.api.mocks.Sheep)2 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Comment (org.alfresco.rest.api.model.Comment)1 OperationResourceMetaData (org.alfresco.rest.framework.core.OperationResourceMetaData)1 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)1 EntityResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction)1 Params (org.alfresco.rest.framework.resource.parameters.Params)1 Farmer (org.alfresco.rest.framework.tests.api.mocks.Farmer)1 GoatEntityResource (org.alfresco.rest.framework.tests.api.mocks.GoatEntityResource)1