Search in sources :

Example 1 with OperationResourceMetaData

use of org.alfresco.rest.framework.core.OperationResourceMetaData 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)

Aggregations

Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Api (org.alfresco.rest.framework.Api)1 OperationResourceMetaData (org.alfresco.rest.framework.core.OperationResourceMetaData)1 ResourceMetadata (org.alfresco.rest.framework.core.ResourceMetadata)1 ResourceOperation (org.alfresco.rest.framework.core.ResourceOperation)1 GrassEntityResource (org.alfresco.rest.framework.tests.api.mocks.GrassEntityResource)1 WithResponse (org.alfresco.rest.framework.webscripts.WithResponse)1 Test (org.junit.Test)1 HttpMethod (org.springframework.http.HttpMethod)1