use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class DefaultConfigurationInheritanceStrategy method getResource.
@Override
public Resource getResource(Iterator<Resource> configResources) {
if (!config.enabled()) {
return null;
}
if (!configResources.hasNext()) {
return null;
}
Resource primary = configResources.next();
if (!isPropertyInheritance(primary) || !configResources.hasNext()) {
return primary;
}
Map<String, Object> mergedProps = getInheritedProperties(primary.getValueMap(), configResources);
return new ConfigurationResourceWrapper(primary, new ValueMapDecorator(mergedProps));
}
use of org.apache.sling.api.wrappers.ValueMapDecorator 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());
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class ResourceModelInterfacesTest method testRequiredPropertyModel.
@Test
public void testRequiredPropertyModel() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("first", "first-value");
map.put("third", "third-value");
ValueMap vm = spy(new ValueMapDecorator(map));
Resource res = mock(Resource.class);
when(res.adaptTo(ValueMap.class)).thenReturn(vm);
ResourceModelWithRequiredField model = factory.getAdapter(res, ResourceModelWithRequiredField.class);
assertNull(model);
verify(vm).get("required", String.class);
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class ResourceModelInterfacesTest method testSimplePropertyModel.
@Test
public void testSimplePropertyModel() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("first", "first-value");
map.put("third", "third-value");
map.put("fourth", true);
ValueMap vm = new ValueMapDecorator(map);
Resource res = mock(Resource.class);
when(res.adaptTo(ValueMap.class)).thenReturn(vm);
SimplePropertyModel model = factory.getAdapter(res, SimplePropertyModel.class);
assertNotNull(model);
assertEquals("first-value", model.getFirst());
assertNull(model.getSecond());
assertEquals("third-value", model.getThirdProperty());
assertTrue(model.isFourth());
verify(res, times(1)).adaptTo(ValueMap.class);
}
use of org.apache.sling.api.wrappers.ValueMapDecorator 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());
}
Aggregations