Search in sources :

Example 76 with ResourceResolver

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

the class MockedResourceResolverImplTest method testQueryResources.

@SuppressWarnings("unchecked")
@Test
public void testQueryResources() throws LoginException {
    final int n = 3;
    String[] languages = new String[] { FAKE_QUERY_LANGUAGE };
    Mockito.when(queryProvider.getSupportedLanguages(Mockito.any(ResolveContext.class))).thenReturn(languages);
    Mockito.when(queryProvider.queryResources(Mockito.any(ResolveContext.class), Mockito.any(String.class), Mockito.any(String.class))).thenReturn(buildValueMapCollection(n, "A_").iterator());
    final ResourceResolver rr = resourceResolverFactory.getResourceResolver(null);
    buildResource("/search/test/withchildren", buildChildResources("/search/test/withchildren"), rr, resourceProvider);
    final Iterator<Map<String, Object>> it = rr.queryResources("/search", FAKE_QUERY_LANGUAGE);
    final Set<String> toFind = new HashSet<String>();
    for (int i = 0; i < n; i++) {
        toFind.add("A_" + i);
    }
    assertTrue("Expecting non-empty result (" + n + ")", it.hasNext());
    while (it.hasNext()) {
        final Map<String, Object> m = it.next();
        toFind.remove(m.get(PATH));
    }
    assertTrue("Expecting no leftovers (" + n + ") in" + toFind, toFind.isEmpty());
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 77 with ResourceResolver

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

the class ResourceResolverWebConsolePlugin method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final String test = request.getParameter(ATTR_TEST);
    String msg = null;
    if (test != null && test.length() > 0) {
        ResourceResolver resolver = null;
        try {
            // prepare the request for the resource resolver
            HttpServletRequest helper = new ResolverRequest(request, test);
            resolver = resolverFactory.getServiceResourceResolver(this.resolverFactory.getServiceUserAuthenticationInfo("console"));
            // map or resolve as instructed
            Object result;
            if ("Map".equals(request.getParameter(ATTR_SUBMIT))) {
                if (helper.getServerName() == null) {
                    result = resolver.map(helper.getPathInfo());
                } else {
                    result = resolver.map(helper, helper.getPathInfo());
                }
            } else {
                result = resolver.resolve(helper, helper.getPathInfo());
            }
            // set the result to render the result
            msg = result.toString();
        } catch (final Throwable t) {
            // some error occurred, report it as a result
            msg = "Test Failure: " + t;
        } finally {
            if (resolver != null) {
                resolver.close();
            }
        }
    }
    // finally redirect
    final String path = request.getContextPath() + request.getServletPath() + request.getPathInfo();
    final String redirectTo;
    if (msg == null) {
        redirectTo = path;
    } else {
        redirectTo = path + '?' + PAR_MSG + '=' + encodeParam(msg) + '&' + PAR_TEST + '=' + encodeParam(test);
    }
    response.sendRedirect(redirectTo);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Example 78 with ResourceResolver

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

the class MockedResourceResolverImplTest method testResourceResolverListChildren.

/**
     * Test listing children via the resource resolver listChildren call.
     * @throws LoginException
     */
@Test
public void testResourceResolverListChildren() throws LoginException {
    ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
    buildResource("/single/test/withchildren", buildChildResources("/single/test/withchildren"), resourceResolver, resourceProvider);
    Resource resource = resourceResolver.getResource("/single/test/withchildren");
    Assert.assertNotNull(resource);
    // test via the resource list children itself, this really just tests this test case.
    Iterator<Resource> resourceIterator = resourceResolver.listChildren(resource);
    Assert.assertNotNull(resourceResolver);
    int i = 0;
    while (resourceIterator.hasNext()) {
        Assert.assertEquals("m" + i, resourceIterator.next().getName());
        i++;
    }
    Assert.assertEquals(5, i);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 79 with ResourceResolver

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

the class MockedResourceResolverImplTest method testGetAuthenticatedResolve.

/**
     * Test various administrative resource resolvers.
     * @throws LoginException
     */
@Test
public void testGetAuthenticatedResolve() throws LoginException {
    ResourceResolver resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
    Assert.assertNotNull(resourceResolver);
    Map<String, Object> authenticationInfo = new HashMap<String, Object>();
    resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(authenticationInfo);
    Assert.assertNotNull(resourceResolver);
}
Also used : HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 80 with ResourceResolver

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

the class MockedResourceResolverImplTest method testRelativeResource.

/**
     * Test search paths
     * @throws LoginException
     */
@Test
public void testRelativeResource() throws LoginException {
    ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
    Assert.assertNotNull(resourceResolver);
    Resource appResource = buildResource("/apps/store/inventory", EMPTY_RESOURCE_LIST, resourceResolver, appsResourceProvider);
    Resource libResource = buildResource("/libs/store/catalog", EMPTY_RESOURCE_LIST, resourceResolver, appsResourceProvider);
    Resource testResource = resourceResolver.getResource("store/inventory");
    Assert.assertEquals(appResource, testResource);
    testResource = resourceResolver.getResource("store/catalog");
    Assert.assertEquals(libResource, testResource);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) 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