Search in sources :

Example 1 with ChildModel

use of org.apache.sling.models.testmodels.classes.ChildModel in project sling by apache.

the class ResourceModelConstructorTest 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());
}
Also used : ParentModel(org.apache.sling.models.testmodels.classes.constructorinjection.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.classes.ChildModel) Test(org.junit.Test)

Example 2 with ChildModel

use of org.apache.sling.models.testmodels.classes.ChildModel 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());
}
Also used : ParentModel(org.apache.sling.models.testmodels.classes.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.classes.ChildModel) Test(org.junit.Test)

Aggregations

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