Search in sources :

Example 1 with SlimGoat

use of org.alfresco.rest.framework.tests.api.mocks3.SlimGoat 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)

Aggregations

Api (org.alfresco.rest.framework.Api)1 BeanPropertiesFilter (org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter)1 ExecutionResult (org.alfresco.rest.framework.jacksonextensions.ExecutionResult)1 Sheep (org.alfresco.rest.framework.tests.api.mocks.Sheep)1 SlimGoat (org.alfresco.rest.framework.tests.api.mocks3.SlimGoat)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1 Test (org.junit.Test)1