Search in sources :

Example 11 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class InvalidAdaptationsTest method testWrongAdaptableClassException.

@Test(expected = InvalidAdaptableException.class)
public void testWrongAdaptableClassException() {
    Map<String, Object> emptyMap = Collections.<String, Object>emptyMap();
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(emptyMap));
    assertNull(factory.createModel(res, RequestModel.class));
}
Also used : Resource(org.apache.sling.api.resource.Resource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Test(org.junit.Test)

Example 12 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class InvalidAdaptationsTest method testNonModelClassException.

@Test(expected = ModelClassException.class)
public void testNonModelClassException() {
    Map<String, Object> emptyMap = Collections.<String, Object>emptyMap();
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(emptyMap));
    assertNull(factory.createModel(res, NonModel.class));
}
Also used : Resource(org.apache.sling.api.resource.Resource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Test(org.junit.Test)

Example 13 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class AdapterFactoryTest method testCreatedNestedModelWithMissingElements.

@Test(expected = MissingElementsException.class)
public void testCreatedNestedModelWithMissingElements() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("invalid", "required");
    ValueMap vm = new ValueMapDecorator(map);
    when(resource.adaptTo(ValueMap.class)).thenReturn(vm);
    factory.createModel(resource, NestedModel.class);
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Test(org.junit.Test)

Example 14 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class ResourceModelClassesTest method testRequiredPropertyModelOptionalStrategy.

@Test
public void testRequiredPropertyModelOptionalStrategy() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("required1", "required value");
    map.put("required2", "required value");
    map.put("required3", "required value");
    ValueMap vm = spy(new ValueMapDecorator(map));
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    ResourceModelWithRequiredFieldOptionalStrategy model = factory.getAdapter(res, ResourceModelWithRequiredFieldOptionalStrategy.class);
    assertNotNull(model);
    assertEquals("required value", model.getRequired1());
    assertEquals("required value", model.getRequired2());
    verify(vm).get("optional1", String.class);
    verify(vm).get("required1", String.class);
}
Also used : ResourceModelWithRequiredFieldOptionalStrategy(org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredFieldOptionalStrategy) 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 15 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class ResourceModelClassesTest 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("intProperty", new Integer(3));
    map.put("arrayProperty", new String[] { "three", "four" });
    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());
    assertEquals(3, model.getIntProperty());
    String[] array = model.getArrayProperty();
    assertEquals(2, array.length);
    assertEquals("three", array[0]);
    assertTrue(model.isPostConstructCalled());
    verify(res, times(1)).adaptTo(ValueMap.class);
}
Also used : SimplePropertyModel(org.apache.sling.models.testmodels.classes.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)

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