Search in sources :

Example 26 with ValueMapDecorator

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));
}
Also used : Resource(org.apache.sling.api.resource.Resource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ConfigurationResourceWrapper(org.apache.sling.caconfig.impl.ConfigurationResourceWrapper)

Example 27 with ValueMapDecorator

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());
}
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 28 with ValueMapDecorator

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);
}
Also used : HashMap(java.util.HashMap) ResourceModelWithRequiredField(org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredField) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 29 with ValueMapDecorator

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);
}
Also used : SimplePropertyModel(org.apache.sling.models.testmodels.interfaces.SimplePropertyModel) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 30 with ValueMapDecorator

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());
}
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

ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)63 Test (org.junit.Test)45 ValueMap (org.apache.sling.api.resource.ValueMap)44 HashMap (java.util.HashMap)36 Resource (org.apache.sling.api.resource.Resource)36 ValidationModel (org.apache.sling.validation.model.ValidationModel)9 ValidationResult (org.apache.sling.validation.ValidationResult)8 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)6 ArrayList (java.util.ArrayList)3 RepositoryException (javax.jcr.RepositoryException)3 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)3 ResourceModelWithRequiredField (org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredField)3 MockResource (org.apache.sling.testing.resourceresolver.MockResource)3 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)3 Calendar (java.util.Calendar)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2