Search in sources :

Example 36 with ValueMapDecorator

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

the class InjectorSpecificAnnotationTest method testOrderForValueAnnotationConstructor.

@Test
public void testOrderForValueAnnotationConstructor() {
    // make sure that that the correct injection is used
    // make sure that log is adapted from value map
    // and not coming from request attribute
    Logger logFromValueMap = LoggerFactory.getLogger(this.getClass());
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("first", "first-value");
    map.put("log", logFromValueMap);
    ValueMap vm = new ValueMapDecorator(map);
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    when(request.getResource()).thenReturn(res);
    org.apache.sling.models.testmodels.classes.constructorinjection.InjectorSpecificAnnotationModel model = factory.getAdapter(request, org.apache.sling.models.testmodels.classes.constructorinjection.InjectorSpecificAnnotationModel.class);
    assertNotNull("Could not instanciate model", model);
    assertEquals("first-value", model.getFirst());
    assertEquals(logFromValueMap, model.getLog());
}
Also used : 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) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 37 with ValueMapDecorator

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

the class InvalidAdaptationsTest method testNonModelClass.

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

Example 38 with ValueMapDecorator

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

the class ResourceModelClassesTest method testListModel.

@Test
public void testListModel() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("intList", new Integer[] { 1, 2, 9, 8 });
    map.put("stringList", new String[] { "hello", "world" });
    ValueMap vm = new ValueMapDecorator(map);
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    ListModel model = factory.getAdapter(res, ListModel.class);
    assertNotNull(model);
    assertEquals(4, model.getIntList().size());
    assertEquals(new Integer(2), model.getIntList().get(1));
    assertEquals(2, model.getStringList().size());
    assertEquals("hello", model.getStringList().get(0));
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ListModel(org.apache.sling.models.testmodels.classes.ListModel) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 39 with ValueMapDecorator

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

the class ResourceModelClassesTest method testArrayWrappersModel.

@Test
public void testArrayWrappersModel() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("intArray", new Integer[] { 1, 2, 9, 8 });
    map.put("secondIntArray", new int[] { 1, 2, 9, 8 });
    ValueMap vm = new ValueMapDecorator(map);
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    ArrayWrappersModel model = factory.getAdapter(res, ArrayWrappersModel.class);
    assertNotNull(model);
    Integer[] intArray = model.getIntArray();
    assertEquals(4, intArray.length);
    assertEquals(new Integer(2), intArray[1]);
    Integer[] secondIntArray = model.getSecondIntArray();
    assertEquals(4, secondIntArray.length);
    assertEquals(new Integer(2), secondIntArray[1]);
}
Also used : ArrayWrappersModel(org.apache.sling.models.testmodels.classes.ArrayWrappersModel) 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 40 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator 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

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