Search in sources :

Example 1 with MockResource

use of org.apache.sling.testing.resourceresolver.MockResource in project sling by apache.

the class JsonObjectCreatorTest method testCreateArray.

@Test
public void testCreateArray() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("emptyArray", new Object[] {});
    properties.put("stringArray", new String[] { "10", "100" });
    properties.put("intArray", new int[] { 10, 100 });
    properties.put("doubleArray", new double[] { 10d, 100d });
    properties.put("byteArray", new byte[] { 0x0A, 0x64 });
    properties.put("floatArray", new float[] { 10.0f, 100.0f });
    properties.put("shortArray", new short[] { 10, 100 });
    properties.put("longArray", new long[] { 10, 100 });
    properties.put("booleanArray", new boolean[] { true, false });
    properties.put("charArray", new char[] { 'a', 'b' });
    Resource resource = new MockResource("/some/path", properties, resolver);
    JsonObject json = JsonObjectCreator.create(resource).build();
    assertEquals(0, json.getJsonArray("emptyArray").size());
    JsonArray array;
    array = json.getJsonArray("stringArray");
    assertEquals("10", array.getString(0));
    array = json.getJsonArray("intArray");
    assertEquals(10, array.getInt(0));
    array = json.getJsonArray("doubleArray");
    assertEquals("10.0", array.getString(0));
    array = json.getJsonArray("byteArray");
    assertEquals("10", array.getString(0));
    array = json.getJsonArray("floatArray");
    assertEquals("10.0", array.getString(0));
    array = json.getJsonArray("shortArray");
    assertEquals("10", array.getString(0));
    array = json.getJsonArray("longArray");
    assertEquals(10L, array.getJsonNumber(0).longValue());
    array = json.getJsonArray("booleanArray");
    assertEquals(true, array.getBoolean(0));
    array = json.getJsonArray("charArray");
    assertEquals("a", array.getString(0));
}
Also used : JsonArray(javax.json.JsonArray) HashMap(java.util.HashMap) MockResource(org.apache.sling.testing.resourceresolver.MockResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.testing.resourceresolver.MockResource) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 2 with MockResource

use of org.apache.sling.testing.resourceresolver.MockResource in project sling by apache.

the class ResourceTraversorTest method testGetJSONObject.

@Test
public void testGetJSONObject() throws Exception {
    Resource resource = new MockResource("/some/path", Collections.<String, Object>singletonMap("p1", "v1"), resolver);
    JsonObject json = new ResourceTraversor(resource).getJsonObject();
    assertEquals("v1", json.getString("p1"));
}
Also used : MockResource(org.apache.sling.testing.resourceresolver.MockResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.testing.resourceresolver.MockResource) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 3 with MockResource

use of org.apache.sling.testing.resourceresolver.MockResource in project sling by apache.

the class JsonObjectCreatorTest method testCreate.

@Test
public void testCreate() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("byte", 0x0A);
    properties.put("int", 10);
    properties.put("long", 10L);
    properties.put("float", 10.0f);
    properties.put("double", 10.0d);
    properties.put("string", "10");
    properties.put("boolean", false);
    properties.put("object", new Object() {

        public String toString() {
            return "object";
        }
    });
    Resource resource = new MockResource("/some/path", properties, resolver);
    JsonObject json = JsonObjectCreator.create(resource).build();
    assertEquals(10, json.getInt("byte"));
    assertEquals(10, json.getInt("int"));
    assertEquals("10.0", json.getString("float"));
    assertEquals("10.0", json.getString("double"));
    assertEquals("10", json.getString("string"));
    assertEquals(false, json.getBoolean("boolean"));
    assertEquals("object", json.getString("object"));
}
Also used : HashMap(java.util.HashMap) MockResource(org.apache.sling.testing.resourceresolver.MockResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.testing.resourceresolver.MockResource) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 4 with MockResource

use of org.apache.sling.testing.resourceresolver.MockResource in project sling by apache.

the class MergedResourceTest method testResourceTypeByValueMap.

@Test
public void testResourceTypeByValueMap() throws Exception {
    final ValueMap vm1 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vma"));
    final MockResource r1 = new MockResource("/a", vm1, null);
    final ValueMap vm2 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vmb"));
    final MockResource r2 = new MockResource("/b", vm2, null);
    final ValueMap vm3 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vmc"));
    final MockResource r3 = new MockResource("/c", vm3, null);
    final List<Resource> resources = new ArrayList<Resource>();
    resources.add(r1);
    resources.add(r2);
    resources.add(r3);
    final List<ValueMap> valueMaps = new ArrayList<ValueMap>();
    valueMaps.add(vm1);
    valueMaps.add(vm2);
    valueMaps.add(vm3);
    final MergedResource mr = new MergedResource(null, "/a", "a", resources, valueMaps);
    assertEquals("vmc", mr.getResourceType());
    assertNull(mr.getResourceSuperType());
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) MockResource(org.apache.sling.testing.resourceresolver.MockResource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.testing.resourceresolver.MockResource) Test(org.junit.Test)

Example 5 with MockResource

use of org.apache.sling.testing.resourceresolver.MockResource in project sling by apache.

the class MergedResourceTest method testResourceTypeByMethod.

@Test
public void testResourceTypeByMethod() throws Exception {
    final MockResource r1 = new MockResource("/a", null, null) {

        @Override
        public String getResourceType() {
            return "a";
        }
    };
    final MockResource r2 = new MockResource("/b", null, null) {

        @Override
        public String getResourceType() {
            return "b";
        }
    };
    final MockResource r3 = new MockResource("/c", null, null) {

        @Override
        public String getResourceType() {
            return "c";
        }
    };
    final List<Resource> resources = new ArrayList<Resource>();
    resources.add(r1);
    resources.add(r2);
    resources.add(r3);
    final MergedResource mr = new MergedResource(null, "/a", "a", resources, Collections.EMPTY_LIST);
    assertEquals("c", mr.getResourceType());
    assertNull(mr.getResourceSuperType());
}
Also used : MockResource(org.apache.sling.testing.resourceresolver.MockResource) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.testing.resourceresolver.MockResource) Test(org.junit.Test)

Aggregations

Resource (org.apache.sling.api.resource.Resource)7 MockResource (org.apache.sling.testing.resourceresolver.MockResource)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)3 JsonObject (javax.json.JsonObject)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)3 HashMap (java.util.HashMap)2 JsonArray (javax.json.JsonArray)1