Search in sources :

Example 1 with ExecutionResult

use of org.alfresco.rest.framework.jacksonextensions.ExecutionResult 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 2 with ExecutionResult

use of org.alfresco.rest.framework.jacksonextensions.ExecutionResult 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 3 with ExecutionResult

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

Example 4 with ExecutionResult

use of org.alfresco.rest.framework.jacksonextensions.ExecutionResult in project alfresco-remote-api by Alfresco.

the class SerializeTests method testFilter.

// note: exposed as "properties" query param
@Test
public void testFilter() throws IOException, JSONException {
    assertNotNull(helper);
    BeanPropertiesFilter theFilter = getFilter("age");
    Object res = new ExecutionResult(new Sheep("bob"), theFilter);
    String out = writeResponse(res);
    assertTrue("Filter must only return the age.", StringUtils.contains(out, "{\"age\":3}"));
    theFilter = getFilter("age,name");
    res = new ExecutionResult(new Sheep("bob"), theFilter);
    out = writeResponse(res);
    JSONObject jsonRsp = new JSONObject(new JSONTokener(out));
    assertEquals(1, jsonRsp.length());
    JSONObject entry = jsonRsp.getJSONObject("entry");
    assertEquals(2, entry.length());
    assertEquals("The name should be 'Dolly'", "Dolly", entry.getString("name"));
    assertTrue("The age should be 3", entry.getInt("age") == 3);
    // unit test filter with "include" taking precendence over "fields" filter
    List<String> theInclude = getIncludeClause("name");
    theFilter = getFilter("age", theInclude);
    res = new ExecutionResult(new Sheep("bob"), theFilter);
    out = writeResponse(res);
    jsonRsp = new JSONObject(new JSONTokener(out));
    assertEquals(1, jsonRsp.length());
    entry = jsonRsp.getJSONObject("entry");
    assertEquals(2, entry.length());
    assertEquals("The name should be 'Dolly'", "Dolly", entry.getString("name"));
    assertTrue("The age should be 3", entry.getInt("age") == 3);
    Api v3 = Api.valueOf(api.getName(), api.getScope().toString(), "3");
    Map<String, BeanPropertiesFilter> relFiler = getRelationFilter("herd");
    res = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), v3, "goat", ParamsExtender.valueOf(relFiler, "notUsed"), new SlimGoat());
    out = writeResponse(res);
    jsonRsp = new JSONObject(new JSONTokener(out));
    entry = jsonRsp.getJSONObject("relations").getJSONObject("herd").getJSONObject("list").getJSONArray("entries").getJSONObject(0).getJSONObject("entry");
    assertEquals("The name should be 'bigun'", "bigun", entry.getString("name"));
    assertTrue("The quantity should be 56", entry.getInt("quantity") == 56);
    relFiler = getRelationFilter("herd(name)");
    res = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), v3, "goat", ParamsExtender.valueOf(relFiler, "notUsed"), new SlimGoat());
    out = writeResponse(res);
    assertTrue("Must return only the herd name.", StringUtils.contains(out, "{\"name\":\"bigun\"}"));
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) SlimGoat(org.alfresco.rest.framework.tests.api.mocks3.SlimGoat) JSONObject(org.json.JSONObject) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) Api(org.alfresco.rest.framework.Api) BeanPropertiesFilter(org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter) Test(org.junit.Test)

Example 5 with ExecutionResult

use of org.alfresco.rest.framework.jacksonextensions.ExecutionResult in project alfresco-remote-api by Alfresco.

the class SerializeTests method testIncludeSource.

@Test
public void testIncludeSource() throws IOException {
    ExecutionResult exec1 = new ExecutionResult(new Farmer("180"), null);
    ExecutionResult exec2 = new ExecutionResult(new Farmer("456"), null);
    CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1, exec2));
    Object resultCollection = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, "sheep", ParamsExtender.valueOf(true, "1"), coll);
    assertNotNull(resultCollection);
    String out = writeResponse(resultCollection);
    assertTrue("There must 'source' json output", StringUtils.contains(out, "\"source\":{\"name\":\"Dolly\",\"age\":3,\"sheepGuid\":\"1\"}"));
    resultCollection = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, "sheep", ParamsExtender.valueOf(false, "1"), coll);
    assertNotNull(resultCollection);
    out = writeResponse(resultCollection);
    assertFalse("There must not 'source' json output", StringUtils.contains(out, "\"source\":{\"name\":\"Dolly\",\"age\":3,\"sheepGuid\":\"1\"}"));
    coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1, exec2), false, 2, new Sheep("barbie"));
    resultCollection = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, "sheep", ParamsExtender.valueOf(true, "1"), coll);
    assertNotNull(resultCollection);
    out = writeResponse(resultCollection);
    assertTrue("There must 'source' json output", StringUtils.contains(out, "\"source\":{\"name\":\"Dolly\",\"age\":3,\"sheepGuid\":\"barbie\""));
}
Also used : WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) 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

ExecutionResult (org.alfresco.rest.framework.jacksonextensions.ExecutionResult)8 Test (org.junit.Test)6 Farmer (org.alfresco.rest.framework.tests.api.mocks.Farmer)5 JSONObject (org.json.JSONObject)5 Sheep (org.alfresco.rest.framework.tests.api.mocks.Sheep)4 ArrayList (java.util.ArrayList)2 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)2 BeanPropertiesFilter (org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter)2 SlimGoat (org.alfresco.rest.framework.tests.api.mocks3.SlimGoat)2 WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 FacetFieldContext (org.alfresco.rest.api.search.context.FacetFieldContext)1