Search in sources :

Example 1 with WithResponse

use of org.alfresco.rest.framework.webscripts.WithResponse in project alfresco-remote-api by Alfresco.

the class WithResponseTest method testSetHeader.

@Test
public void testSetHeader() throws Exception {
    WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
    callBack.setHeader("king", "can");
    callBack.setHeader("king", "kong");
    assertTrue(callBack.getHeaders().size() == 1);
    List<String> vals = callBack.getHeaders().get("king");
    assertTrue(vals.size() == 1);
    assertEquals("kong", vals.get(0));
}
Also used : WithResponse(org.alfresco.rest.framework.webscripts.WithResponse) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 2 with WithResponse

use of org.alfresco.rest.framework.webscripts.WithResponse in project alfresco-remote-api by Alfresco.

the class WithResponseTest method testAddHeader.

@Test
public void testAddHeader() throws Exception {
    WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
    callBack.addHeader("king", "can");
    callBack.addHeader("king", "kong");
    assertTrue(callBack.getHeaders().size() == 1);
    List<String> vals = callBack.getHeaders().get("king");
    assertTrue(vals.size() == 2);
    assertEquals(vals, Arrays.asList("can", "kong"));
}
Also used : WithResponse(org.alfresco.rest.framework.webscripts.WithResponse) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 3 with WithResponse

use of org.alfresco.rest.framework.webscripts.WithResponse 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 4 with WithResponse

use of org.alfresco.rest.framework.webscripts.WithResponse in project alfresco-remote-api by Alfresco.

the class WithResponseTest method testDefaults.

@Test
public void testDefaults() throws Exception {
    WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
    assertEquals(Status.STATUS_OK, callBack.getStatus());
    assertEquals(ResponseWriter.DEFAULT_JSON_CONTENT, callBack.getContentInfo());
    assertEquals(ResponseWriter.CACHE_NEVER, callBack.getCache());
    assertTrue(callBack.getHeaders().isEmpty());
}
Also used : WithResponse(org.alfresco.rest.framework.webscripts.WithResponse) Test(org.junit.Test)

Example 5 with WithResponse

use of org.alfresco.rest.framework.webscripts.WithResponse in project alfresco-remote-api by Alfresco.

the class WithResponseTest method testSetters.

@Test
public void testSetters() throws Exception {
    WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
    callBack.setStatus(Status.STATUS_GONE);
    Cache myCache = new Cache(new Description.RequiredCache() {

        @Override
        public boolean getNeverCache() {
            return false;
        }

        @Override
        public boolean getIsPublic() {
            return true;
        }

        @Override
        public boolean getMustRevalidate() {
            return true;
        }
    });
    callBack.setCache(myCache);
    ContentInfo myContent = new ContentInfoImpl(Format.HTML.mimetype(), "UTF-16", 12, Locale.FRENCH);
    callBack.setContentInfo(myContent);
    assertEquals(Status.STATUS_GONE, callBack.getStatus());
    assertEquals(myCache, callBack.getCache());
    assertEquals(myContent, callBack.getContentInfo());
}
Also used : Description(org.springframework.extensions.webscripts.Description) WithResponse(org.alfresco.rest.framework.webscripts.WithResponse) ContentInfo(org.alfresco.rest.framework.resource.content.ContentInfo) ContentInfoImpl(org.alfresco.rest.framework.resource.content.ContentInfoImpl) Cache(org.springframework.extensions.webscripts.Cache) Test(org.junit.Test)

Aggregations

WithResponse (org.alfresco.rest.framework.webscripts.WithResponse)6 Test (org.junit.Test)6 Matchers.anyString (org.mockito.Matchers.anyString)2 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 ContentInfo (org.alfresco.rest.framework.resource.content.ContentInfo)1 ContentInfoImpl (org.alfresco.rest.framework.resource.content.ContentInfoImpl)1 GrassEntityResource (org.alfresco.rest.framework.tests.api.mocks.GrassEntityResource)1 ApiAssistant (org.alfresco.rest.framework.tools.ApiAssistant)1 AbstractResourceWebScript (org.alfresco.rest.framework.webscripts.AbstractResourceWebScript)1 ResourceWebScriptDelete (org.alfresco.rest.framework.webscripts.ResourceWebScriptDelete)1 Cache (org.springframework.extensions.webscripts.Cache)1 Description (org.springframework.extensions.webscripts.Description)1 WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)1 HttpMethod (org.springframework.http.HttpMethod)1