Search in sources :

Example 1 with ParentModel

use of org.apache.sling.models.testmodels.interfaces.ParentModel in project sling by apache.

the class ResourceModelInterfacesTest method testChildModel.

@Test
public void testChildModel() {
    Object value = RandomStringUtils.randomAlphabetic(10);
    Map<String, Object> props = Collections.singletonMap("property", value);
    ValueMap map = new ValueMapDecorator(props);
    final Resource firstChild = mock(Resource.class);
    when(firstChild.adaptTo(ValueMap.class)).thenReturn(map);
    when(firstChild.adaptTo(ChildModel.class)).thenAnswer(new AdaptToChildModel());
    Object firstGrandChildValue = RandomStringUtils.randomAlphabetic(10);
    ValueMap firstGrandChildMap = new ValueMapDecorator(Collections.singletonMap("property", firstGrandChildValue));
    Object secondGrandChildValue = RandomStringUtils.randomAlphabetic(10);
    ValueMap secondGrandChildMap = new ValueMapDecorator(Collections.singletonMap("property", secondGrandChildValue));
    final Resource firstGrandChild = mock(Resource.class);
    when(firstGrandChild.adaptTo(ValueMap.class)).thenReturn(firstGrandChildMap);
    when(firstGrandChild.adaptTo(ChildModel.class)).thenAnswer(new AdaptToChildModel());
    final Resource secondGrandChild = mock(Resource.class);
    when(secondGrandChild.adaptTo(ValueMap.class)).thenReturn(secondGrandChildMap);
    when(secondGrandChild.adaptTo(ChildModel.class)).thenAnswer(new AdaptToChildModel());
    Resource secondChild = mock(Resource.class);
    when(secondChild.listChildren()).thenReturn(Arrays.asList(firstGrandChild, secondGrandChild).iterator());
    Resource emptyChild = mock(Resource.class);
    when(emptyChild.listChildren()).thenReturn(Collections.<Resource>emptySet().iterator());
    Resource res = mock(Resource.class);
    when(res.getChild("firstChild")).thenReturn(firstChild);
    when(res.getChild("secondChild")).thenReturn(secondChild);
    when(res.getChild("emptyChild")).thenReturn(emptyChild);
    ParentModel model = factory.getAdapter(res, ParentModel.class);
    assertNotNull(model);
    ChildModel childModel = model.getFirstChild();
    assertNotNull(childModel);
    assertEquals(value, childModel.getProperty());
    assertEquals(2, model.getGrandChildren().size());
    assertEquals(firstGrandChildValue, model.getGrandChildren().get(0).getProperty());
    assertEquals(secondGrandChildValue, model.getGrandChildren().get(1).getProperty());
    assertEquals(0, model.getEmptyGrandChildren().size());
}
Also used : ParentModel(org.apache.sling.models.testmodels.interfaces.ParentModel) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) ChildModel(org.apache.sling.models.testmodels.interfaces.ChildModel) Test(org.junit.Test)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)1 ChildModel (org.apache.sling.models.testmodels.interfaces.ChildModel)1 ParentModel (org.apache.sling.models.testmodels.interfaces.ParentModel)1 Test (org.junit.Test)1