Search in sources :

Example 1 with ResourceModelWithRequiredFieldOptionalStrategy

use of org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredFieldOptionalStrategy 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 2 with ResourceModelWithRequiredFieldOptionalStrategy

use of org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredFieldOptionalStrategy 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)

Aggregations

HashMap (java.util.HashMap)2 Resource (org.apache.sling.api.resource.Resource)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)2 ResourceModelWithRequiredFieldOptionalStrategy (org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredFieldOptionalStrategy)2 Test (org.junit.Test)2