You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/01/15 18:30:14 UTC
svn commit: r1652180 -
/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java
Author: bdelacretaz
Date: Thu Jan 15 17:30:14 2015
New Revision: 1652180
URL: http://svn.apache.org/r1652180
Log:
SLING-4258 - test child resources
Modified:
sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java
Modified: sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java?rev=1652180&r1=1652179&r2=1652180&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java (original)
+++ sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java Thu Jan 15 17:30:14 2015
@@ -40,6 +40,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
+import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
@@ -51,17 +52,36 @@ public class JsonObjectCreatorTest {
@Mock
private ResourceResolver resourceResolver;
+ @Mock
+ private ResourceResolver childResourceResolver;
+
private Map<String, Object> props;
private static final String RESOURCE_NAME = "testResource";
private static final String PATH = "/" + RESOURCE_NAME;
private static final Object SAME = new Object();
+ private static final int NCHILDREN = 3;
@Before
public void setup() {
props = new HashMap<String, Object>();
+ props.put("defaultProperty", "defaultValue");
+
+ final List<Resource> empty = new ArrayList<Resource>();
+ when(childResourceResolver.listChildren(any(Resource.class))).thenReturn(empty.iterator());
final List<Resource> children = new ArrayList<Resource>();
+ for(int i=0; i < NCHILDREN; i++) {
+ final Map<String, Object> childProps = new HashMap<String, Object>();
+ final String id = "child" + i;
+ childProps.put("id", id);
+ final Resource r = Mockito.mock(Resource.class);
+ when(r.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(childProps));
+ when(r.getResourceResolver()).thenReturn(childResourceResolver);
+ when(r.getPath()).thenReturn(PATH + "/" + id);
+ children.add(r);
+ }
+
when(resourceResolver.listChildren(any(Resource.class))).thenReturn(children.iterator());
when(resource.getResourceResolver()).thenReturn(resourceResolver);
when(resource.getPath()).thenReturn(PATH);
@@ -73,6 +93,7 @@ public class JsonObjectCreatorTest {
private void assertGet(Object data, Object expected) throws JSONException {
final String key = UUID.randomUUID().toString();
+ props.clear();
props.put(key, data);
when(resource.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(props));
final JSONObject j = JsonObjectCreator.create(resource, 1);
@@ -123,4 +144,14 @@ public class JsonObjectCreatorTest {
assertGet(stream, -1L);
}
+ @Test
+ public void testChildren() throws JSONException {
+ when(resource.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(props));
+ final JSONObject j = JsonObjectCreator.create(resource, 2);
+ for(int i=0 ; i < NCHILDREN; i++) {
+ final String id = "child" + i;
+ assertEquals(id, j.getJSONObject(id).get("id"));
+ }
+ }
+
}