Search in sources :

Example 1 with ResourceMetadata

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

the class ParamsExtractorTests method testPostExtractor.

@SuppressWarnings("unchecked")
@Test
public void testPostExtractor() throws IOException {
    // Put together the stubs
    ResourceWebScriptPost extractor = new ResourceWebScriptPost();
    extractor.setAssistant(assistant);
    extractor.setLocator(locator);
    Map<String, String> templateVars = new HashMap<String, String>();
    Content content = mock(Content.class);
    when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
    WebScriptRequest request = mock(WebScriptRequest.class);
    when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
    when(request.getContent()).thenReturn(content);
    Params params = extractor.extractParams(mockEntity(), request);
    assertNotNull(params);
    assertNotNull(params.getFilter());
    assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
    Object passed = params.getPassedIn();
    assertNotNull(passed);
    assertTrue(List.class.isAssignableFrom(passed.getClass()));
    List<Object> passedObjs = (List<Object>) passed;
    assertTrue(passedObjs.size() == 1);
    assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
    // No entity id for POST
    templateVars.put(ResourceLocator.ENTITY_ID, "1234");
    try {
        params = extractor.extractParams(mockEntity(), request);
        fail("Should not get here. No entity id for POST");
    } catch (UnsupportedResourceOperationException uoe) {
        // Must throw this exception
        assertNotNull(uoe);
    }
    // reset the reader
    when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
    params = extractor.extractParams(mockRelationship(), request);
    assertNotNull(params);
    assertEquals("1234", params.getEntityId());
    passed = params.getPassedIn();
    assertNotNull(passed);
    passedObjs = (List<Object>) passed;
    assertTrue(passedObjs.size() == 1);
    assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
    try {
        // reset the reader
        when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
        templateVars.put(ResourceLocator.RELATIONSHIP_ID, "45678");
        params = extractor.extractParams(mockRelationship(), request);
        fail("Should not get here.");
    } catch (UnsupportedResourceOperationException iae) {
        // Must throw this exception
        assertNotNull("POSTING to a relationship collection by id is not correct.", iae);
    }
    templateVars.clear();
    // reset the reader
    when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
    templateVars.put(ResourceLocator.ENTITY_ID, "1234");
    templateVars.put(ResourceLocator.RELATIONSHIP_ID, "codfish");
    try {
        // POST does not support addressed parameters.
        params = extractor.extractParams(mockEntity(), request);
        fail("Should not get here.");
    } catch (UnsupportedResourceOperationException uoe) {
        // Must throw this exception
        assertNotNull(uoe);
    }
    testExtractOperationParams(templateVars, request, extractor);
    templateVars.clear();
    Method aMethod = ResourceInspector.findMethod(EntityResourceAction.Create.class, GrassEntityResource.class);
    ResourceOperation op = ResourceInspector.inspectOperation(GrassEntityResource.class, aMethod, HttpMethod.POST);
    List<ResourceMetadata> metainfo = ResourceInspector.inspect(GrassEntityResource.class);
    assertNotNull(op);
    assertTrue("Create method should have two params", op.getParameters().size() == 2);
    ResourceParameter singleParam = op.getParameters().get(0);
    assertTrue(ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(singleParam.getParamType()));
    assertFalse("Create grass does not support multiple grass creations", singleParam.isAllowMultiple());
    assertFalse(singleParam.isRequired());
    // Test context when the request body is null and 'required' webApiParam is false
    when(request.getHeader("content-length")).thenReturn("0");
    params = extractor.extractParams(metainfo.get(0), request);
    assertNotNull(params);
    // Test context when the request body is provided and 'required' property is false
    when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.GRASS_JSON));
    params = extractor.extractParams(metainfo.get(0), request);
    assertNotNull(params);
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) HashMap(java.util.HashMap) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) Params(org.alfresco.rest.framework.resource.parameters.Params) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) Match(org.springframework.extensions.webscripts.Match) ResourceWebScriptPost(org.alfresco.rest.framework.webscripts.ResourceWebScriptPost) ResourceParameter(org.alfresco.rest.framework.core.ResourceParameter) Content(org.springframework.extensions.surf.util.Content) StringReader(java.io.StringReader) List(java.util.List) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) Test(org.junit.Test)

Example 2 with ResourceMetadata

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

the class ParamsExtractorTests method mockOperation.

/**
 * Mocks an operation
 * @return ResourceMetadata a Entity
 */
private static ResourceMetadata mockOperation() {
    ResourceMetadata resourceMock = mock(ResourceMetadata.class);
    when(resourceMock.getType()).thenReturn(ResourceMetadata.RESOURCE_TYPE.OPERATION);
    return resourceMock;
}
Also used : ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata)

Example 3 with ResourceMetadata

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

the class InspectorTests method testWebDeletedAnnotationOnClass.

@Test
public void testWebDeletedAnnotationOnClass() {
    List<ResourceMetadata> metainfo = ResourceInspector.inspect(GrassEntityResourceNowDeleted.class);
    assertTrue("Must be at least one ResourceMetadata", metainfo.size() > 0);
    ResourceMetadata metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNull("GrassEntityResourceNowDeleted all methods deleted", metaData.getOperation(HttpMethod.GET));
    assertNull("GrassEntityResourceNowDeleted all methods deleted", metaData.getOperation(HttpMethod.PUT));
    assertNull("GrassEntityResourceNowDeleted all methods deleted", metaData.getOperation(HttpMethod.DELETE));
    assertNull("GrassEntityResourceNowDeleted all methods deleted", metaData.getOperation(HttpMethod.POST));
    metainfo = ResourceInspector.inspect(SheepBlackSheepResourceIsNoMore.class);
    assertTrue("Must be at least one ResourceMetadata", metainfo.size() > 0);
    metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNull("SheepBlackSheepResourceIsNoMore all methods deleted", metaData.getOperation(HttpMethod.GET));
    assertNull("SheepBlackSheepResourceIsNoMore all methods deleted", metaData.getOperation(HttpMethod.PUT));
    assertNull("SheepBlackSheepResourceIsNoMore all methods deleted", metaData.getOperation(HttpMethod.DELETE));
    assertNull("SheepBlackSheepResourceIsNoMore all methods deleted", metaData.getOperation(HttpMethod.POST));
}
Also used : ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) SheepBlackSheepResourceIsNoMore(org.alfresco.rest.framework.tests.api.mocks3.SheepBlackSheepResourceIsNoMore) Test(org.junit.Test)

Example 4 with ResourceMetadata

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

the class InspectorTests method testInspectAddressedProperties.

@Test
public void testInspectAddressedProperties() {
    Api api = Api.valueOf("alfrescomock", "private", "1");
    List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();
    ResourceInspector.inspectAddressedProperties(api, FlockEntityResource.class, "myroot", metainfo);
    assertTrue(metainfo.size() == 1);
    ResourceMetadata metaData = metainfo.get(0);
    assertEquals("/myroot/{id}/photo", metaData.getUniqueId());
    assertTrue(metaData.getOperations().size() == 3);
    assertNotNull("FlockEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
    assertNotNull("FlockEntityResource supports PUT", metaData.getOperation(HttpMethod.PUT));
    assertNotNull("FlockEntityResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    metainfo.clear();
    ResourceInspector.inspectAddressedProperties(api, FlocketEntityResource.class, "myroot", metainfo);
    assertTrue(metainfo.size() == 3);
    for (ResourceMetadata resourceMetadata : metainfo) {
        if ("/myroot/{id}/photo".equals(resourceMetadata.getUniqueId())) {
            assertNotNull("FlocketEntityResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
            assertNotNull("FlocketEntityResource supports PUT", resourceMetadata.getOperation(HttpMethod.PUT));
            assertNotNull("FlocketEntityResource supports DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
        } else {
            if ("/myroot/{id}/album".equals(resourceMetadata.getUniqueId())) {
                assertNotNull("FlocketEntityResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
                assertNotNull("FlocketEntityResource supports PUT", resourceMetadata.getOperation(HttpMethod.PUT));
                assertNull("FlocketEntityResource does not support DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
            } else {
                if ("/myroot/{id}/madeUpProp".equals(resourceMetadata.getUniqueId())) {
                    assertNotNull("FlocketEntityResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
                    assertNull("FlocketEntityResource does not supports PUT", resourceMetadata.getOperation(HttpMethod.PUT));
                    assertNull("FlocketEntityResource does not support DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
                } else {
                    fail("Invalid address property information.");
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Api(org.alfresco.rest.framework.Api) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) Test(org.junit.Test)

Example 5 with ResourceMetadata

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

the class InspectorTests method testInspectEntity.

@Test
public void testInspectEntity() {
    List<ResourceMetadata> metainfo = ResourceInspector.inspect(SheepEntityResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    ResourceMetadata metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("SheepEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
    assertNotNull("SheepEntityResource supports PUT", metaData.getOperation(HttpMethod.PUT));
    assertNotNull("SheepEntityResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    assertNull("SheepEntityResource does not support POST", metaData.getOperation(HttpMethod.POST));
    ResourceOperation op = metaData.getOperation(HttpMethod.GET);
    assertEquals("Sheep ReadALL should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
    op = metaData.getOperation(HttpMethod.PUT);
    assertTrue("SheepEntityResource must support Sheep", Sheep.class.equals(metaData.getObjectType(op)));
    metainfo = ResourceInspector.inspect(SheepNoActionEntityResource.class);
    assertTrue("SheepNoActionEntityResource has no actions.", metainfo.isEmpty());
    metainfo = ResourceInspector.inspect(GoatEntityResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("GoatEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
    op = metaData.getOperation(HttpMethod.GET);
    List<ResourceParameter> params = op.getParameters();
    assertTrue("readById method should have 1 url param", params.size() == 1);
    metainfo = ResourceInspector.inspect(FlockEntityResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("FlockEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
    assertNotNull("FlockEntityResource supports PUT", metaData.getOperation(HttpMethod.PUT));
    assertNotNull("FlockEntityResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    assertNull("FlockEntityResource does not support POST", metaData.getOperation(HttpMethod.POST));
    metainfo = ResourceInspector.inspect(MultiPartTestEntityResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("MultiPartTestEntityResource support POST", metaData.getOperation(HttpMethod.POST));
    assertNull("MultiPartTestEntityResource does not supports GET", metaData.getOperation(HttpMethod.GET));
    assertNull("MultiPartTestEntityResource does not supports PUT", metaData.getOperation(HttpMethod.PUT));
    assertNull("MultiPartTestEntityResource does not supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    op = metaData.getOperation(HttpMethod.POST);
    assertTrue("MultiPartTestEntityResource must support MultiPartTestResponse", MultiPartTestResponse.class.equals(metaData.getObjectType(op)));
    assertEquals("MultiPartTestEntityResource should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
}
Also used : SheepNoActionEntityResource(org.alfresco.rest.framework.tests.api.mocks.SheepNoActionEntityResource) ResourceParameter(org.alfresco.rest.framework.core.ResourceParameter) GoatEntityResource(org.alfresco.rest.framework.tests.api.mocks.GoatEntityResource) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) MultiPartTestEntityResource(org.alfresco.rest.framework.tests.api.mocks.MultiPartTestEntityResource) MultiPartTestResponse(org.alfresco.rest.framework.tests.api.mocks.MultiPartTestResponse) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) FlockEntityResource(org.alfresco.rest.framework.tests.api.mocks3.FlockEntityResource) Test(org.junit.Test)

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