Search in sources :

Example 1 with Farmer

use of org.alfresco.rest.framework.tests.api.mocks.Farmer 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);
    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 Farmer

use of org.alfresco.rest.framework.tests.api.mocks.Farmer in project alfresco-remote-api by Alfresco.

the class ParamsExtractorTests method testSpecialChars.

@Test
public void testSpecialChars() throws IOException {
    String specialChars = new String(new char[] { (char) '香' }) + " 香蕉";
    ResourceWebScriptPost extractor = new ResourceWebScriptPost();
    extractor.setAssistant(assistant);
    Map<String, String> templateVars = new HashMap<String, String>();
    String mockMe = "{\"name\":\"" + specialChars + "\",\"created\":\"2012-03-23T15:56:18.552+0000\",\"age\":54,\"id\":\"1234A3\",\"farm\":\"LARGE\"}";
    Content content = mock(Content.class);
    when(content.getReader()).thenReturn(new StringReader(mockMe));
    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);
    Object passed = params.getPassedIn();
    assertTrue(List.class.isAssignableFrom(passed.getClass()));
    @SuppressWarnings("unchecked") List<Object> passedObjs = (List<Object>) passed;
    assertTrue(passedObjs.size() == 1);
    assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
    Farmer f = (Farmer) passedObjs.get(0);
    assertTrue(f.getName().equals("香 香蕉"));
    // Test passing in special characters as a param.
    ResourceWebScriptGet getExtractor = new ResourceWebScriptGet();
    getExtractor.setAssistant(assistant);
    Map<String, String> getTemplateVars = new HashMap<String, String>();
    WebScriptRequest getRequest = mock(WebScriptRequest.class);
    when(getRequest.getServiceMatch()).thenReturn(new Match(null, getTemplateVars, null));
    when(getRequest.getParameterNames()).thenReturn(new String[] { "aParam" });
    when(getRequest.getParameterValues("aParam")).thenReturn(new String[] { specialChars });
    Params pGet = getExtractor.extractParams(mockEntity(), getRequest);
    assertNotNull(pGet);
    String pVal = pGet.getParameter("aParam");
    assertTrue(pVal.equals("香 香蕉"));
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) ResourceWebScriptGet(org.alfresco.rest.framework.webscripts.ResourceWebScriptGet) HashMap(java.util.HashMap) Params(org.alfresco.rest.framework.resource.parameters.Params) Match(org.springframework.extensions.webscripts.Match) ResourceWebScriptPost(org.alfresco.rest.framework.webscripts.ResourceWebScriptPost) Content(org.springframework.extensions.surf.util.Content) StringReader(java.io.StringReader) List(java.util.List) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) Test(org.junit.Test)

Example 3 with Farmer

use of org.alfresco.rest.framework.tests.api.mocks.Farmer in project alfresco-remote-api by Alfresco.

the class SerializeTests method testExpandRelations.

@Test
public void testExpandRelations() throws IOException {
    assertNotNull(helper);
    Map<String, BeanPropertiesFilter> rFilter = getRelationFilter("blacksheep,baaahh");
    ExecutionResult res = (ExecutionResult) helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, "sheep", ParamsExtender.valueOf(rFilter, "1"), new Farmer("180"));
    assertNotNull(res);
    String out = writeResponse(res);
    assertTrue(Farmer.class.equals(res.getRoot().getClass()));
    assertTrue("There must be json output", StringUtils.isNotBlank(out));
    Paging pageRequest = Paging.valueOf(1, 2);
    Object resultCollection = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, "sheep", ParamsExtender.valueOf(rFilter, "1"), CollectionWithPagingInfo.asPaged(pageRequest, Arrays.asList(new Farmer("180"), new Farmer("190"), new Farmer("280"))));
    assertNotNull(resultCollection);
    out = writeResponse(resultCollection);
    assertTrue("There must be json output", StringUtils.isNotBlank(out));
}
Also used : Paging(org.alfresco.rest.framework.resource.parameters.Paging) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) JSONObject(org.json.JSONObject) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) BeanPropertiesFilter(org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter) Test(org.junit.Test)

Example 4 with Farmer

use of org.alfresco.rest.framework.tests.api.mocks.Farmer in project alfresco-remote-api by Alfresco.

the class SerializeTests method testExpandEmbedded.

@Test
public void testExpandEmbedded() throws IOException {
    assertNotNull(helper);
    Farmer aFarmer = new Farmer("180");
    aFarmer.setGoatId("1111");
    aFarmer.setSheepId("2222");
    ExecutionResult res = (ExecutionResult) helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, Params.valueOf("notUsed", null, null), aFarmer);
    assertNotNull(res);
    assertTrue(Farmer.class.equals(res.getRoot().getClass()));
    Map<String, Object> embeds = res.getEmbedded();
    assertNotNull(embeds);
    assertTrue(embeds.size() > 0);
    ExecutionResult goat = (ExecutionResult) embeds.get("goat");
    assertTrue(Goat.class.equals(goat.getRoot().getClass()));
    ExecutionResult grassEmbed = (ExecutionResult) goat.getEmbedded().get("grass");
    Grass grass = (Grass) grassEmbed.getRoot();
    assertNotNull(grass);
    assertTrue("Goat1111".equals(grass.getId()));
    ExecutionResult sheep = (ExecutionResult) embeds.get("sheep");
    assertTrue(Sheep.class.equals(sheep.getRoot().getClass()));
    Sheep aSheep = (Sheep) sheep.getRoot();
    assertTrue("2222".equals(aSheep.getId()));
    String out = writeResponse(res);
    assertTrue("There must be json output", StringUtils.isNotBlank(out));
}
Also used : Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) JSONObject(org.json.JSONObject) Goat(org.alfresco.rest.framework.tests.api.mocks.Goat) SlimGoat(org.alfresco.rest.framework.tests.api.mocks3.SlimGoat) Grass(org.alfresco.rest.framework.tests.api.mocks.Grass) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) Test(org.junit.Test)

Example 5 with Farmer

use of org.alfresco.rest.framework.tests.api.mocks.Farmer in project alfresco-remote-api by Alfresco.

the class SerializeTests method testExpandRecursiveRelations.

@Test
public void testExpandRecursiveRelations() throws IOException {
    ExecutionResult exec1 = new ExecutionResult(new Farmer("180"), null);
    ExecutionResult exec2 = new ExecutionResult(new Farmer("456"), getFilter("age"));
    CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1, exec2));
    ExecutionResult execResult = new ExecutionResult(new Sheep("ssheep"), null);
    Map<String, Object> related = new HashMap<String, Object>();
    related.put("farmers", coll);
    execResult.addRelated(related);
    String out = writeResponse(execResult);
    assertTrue("There must be json output", StringUtils.isNotBlank(out));
    assertFalse("collections should be serialized correctly.  There may be a problem with this one.", StringUtils.contains(out, "\"collection\":["));
    assertTrue("collections should be serialized correctly.  There should be embed relations", StringUtils.contains(out, "\"relations\":{\"farmers\":{\"list\":"));
}
Also used : HashMap(java.util.HashMap) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) JSONObject(org.json.JSONObject) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) Test(org.junit.Test)

Aggregations

Farmer (org.alfresco.rest.framework.tests.api.mocks.Farmer)12 Test (org.junit.Test)12 StringReader (java.io.StringReader)5 ExecutionResult (org.alfresco.rest.framework.jacksonextensions.ExecutionResult)5 JSONObject (org.json.JSONObject)5 HashMap (java.util.HashMap)4 Params (org.alfresco.rest.framework.resource.parameters.Params)3 Sheep (org.alfresco.rest.framework.tests.api.mocks.Sheep)3 Content (org.springframework.extensions.surf.util.Content)3 Match (org.springframework.extensions.webscripts.Match)3 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)3 WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)3 List (java.util.List)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)2 Grass (org.alfresco.rest.framework.tests.api.mocks.Grass)2 ResourceWebScriptPost (org.alfresco.rest.framework.webscripts.ResourceWebScriptPost)2 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 FacetFieldContext (org.alfresco.rest.api.search.context.FacetFieldContext)1