Search in sources :

Example 41 with ValueMapDecorator

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

the class ResourceModelClassesTest method testRequiredPropertyMissingModelOptionalStrategy.

@Test
public void testRequiredPropertyMissingModelOptionalStrategy() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("first", "first-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);
    assertNull(model);
    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 42 with ValueMapDecorator

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

the class ResourceModelClassesTest 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 43 with ValueMapDecorator

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

the class ResourceModelClassesTest method testRequiredPropertyModelWithException.

@Test
public void testRequiredPropertyModelWithException() {
    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);
    boolean thrown = false;
    try {
        factory.createModel(res, ResourceModelWithRequiredField.class);
    } catch (MissingElementsException e) {
        assertEquals("required", ((Field) e.getMissingElements().iterator().next().getElement()).getName());
        thrown = true;
    }
    assertTrue(thrown);
    verify(vm).get("required", String.class);
}
Also used : ResourceModelWithRequiredField(org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredField) Field(java.lang.reflect.Field) 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) MissingElementsException(org.apache.sling.models.factory.MissingElementsException) Test(org.junit.Test)

Example 44 with ValueMapDecorator

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

the class ViaTest method testProjectionToChildResource.

@Test
public void testProjectionToChildResource() {
    String value = RandomStringUtils.randomAlphanumeric(10);
    ValueMap map = new ValueMapDecorator(Collections.<String, Object>singletonMap("firstProperty", value));
    when(childResource.adaptTo(ValueMap.class)).thenReturn(map);
    ChildResourceViaModel model = factory.getAdapter(resource, ChildResourceViaModel.class);
    assertNotNull(model);
    assertEquals(value, model.getFirstProperty());
}
Also used : ChildResourceViaModel(org.apache.sling.models.testmodels.classes.ChildResourceViaModel) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Test(org.junit.Test)

Example 45 with ValueMapDecorator

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

the class ViaTest method testProjectionToResource.

@Test
public void testProjectionToResource() {
    String value = RandomStringUtils.randomAlphanumeric(10);
    ValueMap map = new ValueMapDecorator(Collections.<String, Object>singletonMap("firstProperty", value));
    when(resource.adaptTo(ValueMap.class)).thenReturn(map);
    ViaModel model = factory.getAdapter(request, ViaModel.class);
    assertNotNull(model);
    assertEquals(value, model.getFirstProperty());
}
Also used : ChildResourceViaModel(org.apache.sling.models.testmodels.classes.ChildResourceViaModel) ViaModel(org.apache.sling.models.testmodels.classes.ViaModel) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) 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