use of org.apache.sling.models.testmodels.classes.ParentModel in project sling by apache.
the class ResourceModelClassesTest method testChildModel.
@Test
public void testChildModel() {
Object firstValue = RandomStringUtils.randomAlphabetic(10);
ValueMap firstMap = new ValueMapDecorator(Collections.singletonMap("property", firstValue));
final Resource firstChild = mock(Resource.class);
when(firstChild.adaptTo(ValueMap.class)).thenReturn(firstMap);
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(firstValue, 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());
}
Aggregations