Search in sources :

Example 1 with Sheep

use of org.alfresco.rest.framework.tests.api.mocks.Sheep 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 2 with Sheep

use of org.alfresco.rest.framework.tests.api.mocks.Sheep 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 3 with Sheep

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

the class SerializeTests method testSerializeList.

@Test
public void testSerializeList() throws IOException {
    assertNotNull(helper);
    Object res = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, Params.valueOf("notUsed", null, null), Arrays.asList(new Goat(), new Sheep("ABCD"), new Sheep("XYZ")));
    String out = writeResponse(res);
    assertTrue("There must be json output", StringUtils.isNotBlank(out));
}
Also used : WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) JSONObject(org.json.JSONObject) Goat(org.alfresco.rest.framework.tests.api.mocks.Goat) SlimGoat(org.alfresco.rest.framework.tests.api.mocks3.SlimGoat) Test(org.junit.Test)

Example 4 with Sheep

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

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

Sheep (org.alfresco.rest.framework.tests.api.mocks.Sheep)13 Test (org.junit.Test)13 JSONObject (org.json.JSONObject)8 WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)8 Goat (org.alfresco.rest.framework.tests.api.mocks.Goat)7 SlimGoat (org.alfresco.rest.framework.tests.api.mocks3.SlimGoat)6 ExecutionResult (org.alfresco.rest.framework.jacksonextensions.ExecutionResult)4 HashMap (java.util.HashMap)3 Farmer (org.alfresco.rest.framework.tests.api.mocks.Farmer)3 ResourceMetadata (org.alfresco.rest.framework.core.ResourceMetadata)2 ResourceOperation (org.alfresco.rest.framework.core.ResourceOperation)2 ResourceParameter (org.alfresco.rest.framework.core.ResourceParameter)2 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)2 Grass (org.alfresco.rest.framework.tests.api.mocks.Grass)2 MultiPartTestResponse (org.alfresco.rest.framework.tests.api.mocks.MultiPartTestResponse)2 AbstractResourceWebScript (org.alfresco.rest.framework.webscripts.AbstractResourceWebScript)2 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)2 HashSet (java.util.HashSet)1 Api (org.alfresco.rest.framework.Api)1 BeanPropertiesFilter (org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter)1