Search in sources :

Example 46 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class DelegateResourceTest method setup.

@Before
public void setup() throws LoginException, PersistenceException {
    ResourceResolver adminResolver = null;
    try {
        adminResolver = rrFactory.getAdministrativeResourceResolver(null);
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("text", "baseTESTValue");
        properties.put("other", "baseOther");
        properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/delegate/base");
        ResourceUtil.getOrCreateResource(adminResolver, baseComponentPath, properties, null, false);
        properties.clear();
        properties.put("text", "extendedTESTValue");
        properties.put("other", "extendedOther");
        properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/delegate/extended");
        ResourceUtil.getOrCreateResource(adminResolver, extendedComponentPath, properties, null, false);
        properties.clear();
        properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, "sling/delegate/base");
        ResourceUtil.getOrCreateResource(adminResolver, "/apps/sling/delegate/extended", properties, null, false);
        properties.clear();
        adminResolver.commit();
    } finally {
        if (adminResolver != null && adminResolver.isLive()) {
            adminResolver.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Before(org.junit.Before)

Example 47 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ExporterTest method testDoubledServlets.

@Test
public void testDoubledServlets() throws Exception {
    ResourceResolver resolver = null;
    try {
        resolver = rrFactory.getAdministrativeResourceResolver(null);
        FakeResponse response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(doubledComponentPath + ".firstmodel.json"), response, resolver);
        JsonObject obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals("first", obj.getString("value"));
        response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(doubledComponentPath + ".secondmodel.json"), response, resolver);
        obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals("second", obj.getString("value"));
    } finally {
        if (resolver != null && resolver.isLive()) {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 48 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ExporterTest method testExportToMap.

@Test
public void testExportToMap() throws Exception {
    ResourceResolver resolver = null;
    try {
        resolver = rrFactory.getAdministrativeResourceResolver(null);
        final Resource baseComponentResource = resolver.getResource(baseComponentPath);
        Assert.assertNotNull(baseComponentResource);
        Map<String, Object> data = modelFactory.exportModelForResource(baseComponentResource, "jackson", Map.class, Collections.<String, String>emptyMap());
        Assert.assertEquals("Should have resource value", "baseTESTValue", data.get("sampleValue"));
        Assert.assertEquals("Should have resource value", "BASETESTVALUE", data.get("UPPER"));
    } finally {
        if (resolver != null && resolver.isLive()) {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 49 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ResourceResolverInjectorTest method testFromResource.

@Test
public void testFromResource() {
    Resource resource = mock(Resource.class);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    when(resource.getResourceResolver()).thenReturn(resourceResolver);
    Object result = injector.getValue(resource, "resourceResolver", ResourceResolver.class, element, registry);
    assertEquals(resourceResolver, result);
}
Also used : Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 50 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ResourceResolverInjectorTest method testFromRequest.

@Test
public void testFromRequest() {
    SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    when(request.getResourceResolver()).thenReturn(resourceResolver);
    Object result = injector.getValue(request, "resourceResolver", ResourceResolver.class, element, registry);
    assertEquals(resourceResolver, result);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)339 Resource (org.apache.sling.api.resource.Resource)168 Test (org.junit.Test)131 HashMap (java.util.HashMap)65 LoginException (org.apache.sling.api.resource.LoginException)53 PersistenceException (org.apache.sling.api.resource.PersistenceException)52 Session (javax.jcr.Session)31 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)29 ValueMap (org.apache.sling.api.resource.ValueMap)27 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 ArrayList (java.util.ArrayList)23 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 Map (java.util.Map)19 Before (org.junit.Before)19 IOException (java.io.IOException)17 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)17 ChildResource (org.apache.sling.validation.model.ChildResource)17 HashSet (java.util.HashSet)16 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)15