Search in sources :

Example 36 with ResourceResolverFactory

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

the class SimpleDistributionAgentTest method testDistributionWithFailingDistributionStrategy.

@Test
public void testDistributionWithFailingDistributionStrategy() throws Exception {
    String name = "sample-agent";
    DistributionPackageImporter packageImporter = mock(DistributionPackageImporter.class);
    DistributionPackageExporter packageExporter = mock(DistributionPackageExporter.class);
    DistributionRequestAuthorizationStrategy packageExporterStrategy = mock(DistributionRequestAuthorizationStrategy.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueueDispatchingStrategy distributionHandler = mock(DistributionQueueDispatchingStrategy.class);
    Iterable<DistributionQueueItemStatus> states = Collections.singletonList(new DistributionQueueItemStatus(DistributionQueueItemState.ERROR, DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME));
    when(distributionHandler.add(any(DistributionPackage.class), any(DistributionQueueProvider.class))).thenReturn(states);
    DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "serviceName", packageImporter, packageExporter, packageExporterStrategy, queueProvider, distributionHandler, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, null, 0);
    DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, "/");
    final DistributionPackage distributionPackage = mock(DistributionPackage.class);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    when(distributionPackage.getInfo()).thenReturn(new DistributionPackageInfo("type"));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            ((DistributionPackageProcessor) args[2]).process(distributionPackage);
            return null;
        }
    }).when(packageExporter).exportPackages(any(ResourceResolver.class), any(DistributionRequest.class), any(DistributionPackageProcessor.class));
    when(queueProvider.getQueue(DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME)).thenReturn(new SimpleDistributionQueue(name, "name"));
    DistributionResponse response = agent.execute(resourceResolver, request);
    assertNotNull(response);
    assertEquals("[ERROR]", response.getMessage());
    assertEquals(DistributionRequestState.DROPPED, response.getState());
}
Also used : DistributionQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) SimpleDistributionQueue(org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue) SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionPackageProcessor(org.apache.sling.distribution.packaging.DistributionPackageProcessor) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionEventFactory(org.apache.sling.distribution.event.impl.DistributionEventFactory) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) DistributionResponse(org.apache.sling.distribution.DistributionResponse) Test(org.junit.Test)

Example 37 with ResourceResolverFactory

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

the class SimpleDistributionAgentTest method testDistributionWithWorkingDistributionStrategy.

@Test
public void testDistributionWithWorkingDistributionStrategy() throws Exception {
    String name = "sample-agent";
    DistributionPackageImporter packageImporter = mock(DistributionPackageImporter.class);
    DistributionPackageExporter packageExporter = mock(DistributionPackageExporter.class);
    DistributionRequestAuthorizationStrategy packageExporterStrategy = mock(DistributionRequestAuthorizationStrategy.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueueDispatchingStrategy distributionHandler = mock(DistributionQueueDispatchingStrategy.class);
    DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "subServiceName", packageImporter, packageExporter, packageExporterStrategy, queueProvider, distributionHandler, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, null, 0);
    DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, "/");
    final DistributionPackage distributionPackage = mock(DistributionPackage.class);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    when(distributionPackage.getInfo()).thenReturn(new DistributionPackageInfo("type"));
    Iterable<DistributionQueueItemStatus> states = Collections.singletonList(new DistributionQueueItemStatus(DistributionQueueItemState.QUEUED, DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME));
    when(distributionHandler.add(any(DistributionPackage.class), any(DistributionQueueProvider.class))).thenReturn(states);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            ((DistributionPackageProcessor) args[2]).process(distributionPackage);
            return null;
        }
    }).when(packageExporter).exportPackages(any(ResourceResolver.class), any(DistributionRequest.class), any(DistributionPackageProcessor.class));
    when(queueProvider.getQueue(DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME)).thenReturn(new SimpleDistributionQueue(name, "name"));
    DistributionResponse response = agent.execute(resourceResolver, request);
    assertNotNull(response);
    assertEquals("[QUEUED]", response.getMessage());
    assertEquals(DistributionRequestState.ACCEPTED, response.getState());
}
Also used : DistributionQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) SimpleDistributionQueue(org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue) SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionPackageProcessor(org.apache.sling.distribution.packaging.DistributionPackageProcessor) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionEventFactory(org.apache.sling.distribution.event.impl.DistributionEventFactory) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) DistributionResponse(org.apache.sling.distribution.DistributionResponse) Test(org.junit.Test)

Example 38 with ResourceResolverFactory

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

the class MergedResourceProviderTestForMergingPicker method setup.

@Before
public void setup() throws Exception {
    final MockResourceResolverFactoryOptions options = new MockResourceResolverFactoryOptions();
    options.setSearchPaths(new String[] { "/apps/", "/libs/" });
    final ResourceResolverFactory factory = new MockResourceResolverFactory(options);
    this.resolver = factory.getResourceResolver(null);
    MockHelper.create(this.resolver).resource("/apps").resource("a").p(MergedResourceConstants.PN_HIDE_CHILDREN, new String[] { "Z", "x", "y" }).resource("1").p("a", "1").p("b", "2").resource(".2").p(ResourceResolver.PROPERTY_RESOURCE_TYPE, "apps").resource(".3").p("e", "2").p(MergedResourceConstants.PN_HIDE_PROPERTIES, "*").p("b", "x").p("d", "1").resource(".4").p("e", "2").p(MergedResourceConstants.PN_HIDE_PROPERTIES, new String[] { "a", "c" }).p("b", "x").p("d", "1").resource(".X").resource("/apps/b").resource("c").resource("d").resource("e").resource("f").resource("/libs").resource("deleteTest").resource(".mvmTest").p("a", "1").p("b", "2").resource(".a").resource("1").p("a", "5").p("c", "2").resource(".2").p(ResourceResolver.PROPERTY_RESOURCE_TYPE, "libs").resource(".3").p("a", "1").p("b", "2").p("c", "3").resource(".4").p("a", "1").p("b", "2").p("c", "3").resource(".Y").resource(".Z").resource("/libs/a/Y/a").resource("/libs/a/Y/b").resource("/libs/a/Y/c").resource("/libs/b").resource("c").resource("d").resource("e").resource("f").commit();
    this.provider = new CRUDMergingResourceProvider("/merged", new MergingResourcePicker(), false);
    this.ctx = new BasicResolveContext(resolver);
}
Also used : ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) MockResourceResolverFactory(org.apache.sling.testing.resourceresolver.MockResourceResolverFactory) MockResourceResolverFactoryOptions(org.apache.sling.testing.resourceresolver.MockResourceResolverFactoryOptions) MockResourceResolverFactory(org.apache.sling.testing.resourceresolver.MockResourceResolverFactory) MergingResourcePicker(org.apache.sling.resourcemerger.impl.picker.MergingResourcePicker) Before(org.junit.Before)

Example 39 with ResourceResolverFactory

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

the class ResourceResolverApiTest method setup.

@Before
public void setup() throws LoginException {
    final ResourceResolverFactory rrf = teleporter.getService(ResourceResolverFactory.class);
    resResolver = rrf.getAdministrativeResourceResolver(null);
}
Also used : ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Before(org.junit.Before)

Example 40 with ResourceResolverFactory

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

the class MockedResourceResolverImplTest method before.

@SuppressWarnings("unchecked")
@Before
public void before() throws LoginException {
    activator = new ResourceResolverFactoryActivator();
    // system bundle access
    final Bundle systemBundle = Mockito.mock(Bundle.class);
    Mockito.when(systemBundle.getState()).thenReturn(Bundle.ACTIVE);
    Mockito.when(bundleContext.getBundle(Constants.SYSTEM_BUNDLE_LOCATION)).thenReturn(systemBundle);
    activator.resourceAccessSecurityTracker = new ResourceAccessSecurityTracker();
    activator.resourceProviderTracker = resourceProviderTracker;
    activator.changeListenerWhiteboard = resourceChangeListenerWhiteboard;
    activator.serviceUserMapper = Mockito.mock(ServiceUserMapper.class);
    handlers.add(createRPHandler(resourceProvider, "org.apache.sling.resourceresolver.impl.DummyTestProvider", 10L, "/single"));
    // setup mapping resources at /etc/map to exercise vanity etc.
    // hmm, can't provide the resolver since its not up and ready.
    // mapping almost certainly work properly until this can be setup correctly.
    buildMappingResource("/etc/map", mappingResourceProvider, null);
    handlers.add(createRPHandler(mappingResourceProvider, "org.apache.sling.resourceresolver.impl.MapProvider", 11L, "/etc"));
    handlers.add(createRPHandler(appsResourceProvider, "org.apache.sling.resourceresolver.impl.AppsProvider", 12L, "/libs"));
    handlers.add(createRPHandler(appsResourceProvider, "org.apache.sling.resourceresolver.impl.AppsProvider", 13L, "/apps"));
    handlers.add(createRPHandler(queriableResourceProviderA, "org.apache.sling.resourceresolver.impl.QueriableResourceProviderA", 14L, "/searchA"));
    Mockito.when(queriableResourceProviderA.getQueryLanguageProvider()).thenReturn(queryProvider);
    ResourceProviderStorage storage = new ResourceProviderStorage(handlers);
    Mockito.when(resourceProviderTracker.getResourceProviderStorage()).thenReturn(storage);
    // activate the components.
    activator.activate(bundleContext, new ResourceResolverFactoryConfig() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return null;
        }

        @Override
        public String[] resource_resolver_virtual() {
            return new String[] { "/:/" };
        }

        @Override
        public String[] resource_resolver_vanitypath_whitelist() {
            return null;
        }

        @Override
        public boolean resource_resolver_vanitypath_maxEntries_startup() {
            return true;
        }

        @Override
        public int resource_resolver_vanitypath_maxEntries() {
            return -1;
        }

        @Override
        public int resource_resolver_vanitypath_bloomfilter_maxBytes() {
            return 1024000;
        }

        @Override
        public String[] resource_resolver_vanitypath_blacklist() {
            return null;
        }

        @Override
        public boolean resource_resolver_vanity_precedence() {
            return false;
        }

        @Override
        public String[] resource_resolver_searchpath() {
            return new String[] { "/apps", "/libs" };
        }

        @Override
        public String[] resource_resolver_required_providers() {
            return new String[] { "org.apache.sling.resourceresolver.impl.DummyTestProvider" };
        }

        @Override
        public String[] resource_resolver_required_providernames() {
            return null;
        }

        @Override
        public boolean resource_resolver_providerhandling_paranoid() {
            return false;
        }

        @Override
        public boolean resource_resolver_optimize_alias_resolution() {
            return true;
        }

        @Override
        public String[] resource_resolver_mapping() {
            return new String[] { "/:/", "/content/:/", "/system/docroot/:/", "/content.html-/$" };
        }

        @Override
        public String[] resource_resolver_map_observation() {
            return new String[] { "/" };
        }

        @Override
        public String resource_resolver_map_location() {
            return MapEntries.DEFAULT_MAP_ROOT;
        }

        @Override
        public boolean resource_resolver_manglenamespaces() {
            return true;
        }

        @Override
        public boolean resource_resolver_log_closing() {
            return false;
        }

        @Override
        public boolean resource_resolver_enable_vanitypath() {
            return true;
        }

        @Override
        public int resource_resolver_default_vanity_redirect_status() {
            return 302;
        }

        @Override
        public boolean resource_resolver_allowDirect() {
            return true;
        }

        @Override
        public boolean resource_resolver_log_unclosed() {
            return true;
        }
    });
    // configure using Bundle
    Mockito.when(usingBundle.getBundleContext()).thenReturn(usingBundleContext);
    Mockito.when(usingBundleContext.getBundle()).thenReturn(usingBundle);
    // extract any services that were registered into a map.
    ArgumentCaptor<Class> classesCaptor = ArgumentCaptor.forClass(Class.class);
    ArgumentCaptor<ServiceFactory> serviceCaptor = ArgumentCaptor.forClass(ServiceFactory.class);
    @SuppressWarnings("rawtypes") ArgumentCaptor<Dictionary> propertiesCaptor = ArgumentCaptor.forClass(Dictionary.class);
    Mockito.verify(bundleContext, Mockito.atLeastOnce()).registerService((Class<ResourceResolverFactory>) classesCaptor.capture(), (ServiceFactory<ResourceResolverFactory>) serviceCaptor.capture(), propertiesCaptor.capture());
    int si = 0;
    List<ServiceFactory> serviceList = serviceCaptor.getAllValues();
    @SuppressWarnings({ "unused", "rawtypes" }) List<Dictionary> servicePropertiesList = propertiesCaptor.getAllValues();
    for (Class serviceClass : classesCaptor.getAllValues()) {
        services.put(serviceClass.getName(), serviceList.get(si));
        serviceProperties.put(serviceClass.getName(), serviceProperties.get(si));
        si++;
    }
    // verify that a ResourceResolverFactoryImpl was created and registered.
    Assert.assertNotNull("" + services, services.get(ResourceResolverFactory.class.getName()));
    Object rrf = services.get(ResourceResolverFactory.class.getName());
    if (rrf instanceof ServiceFactory) {
        rrf = ((ServiceFactory) rrf).getService(usingBundle, null);
    }
    Assert.assertTrue(rrf instanceof ResourceResolverFactoryImpl);
    resourceResolverFactory = (ResourceResolverFactoryImpl) rrf;
}
Also used : Dictionary(java.util.Dictionary) ServiceFactory(org.osgi.framework.ServiceFactory) Bundle(org.osgi.framework.Bundle) ServiceUserMapper(org.apache.sling.serviceusermapping.ServiceUserMapper) Annotation(java.lang.annotation.Annotation) ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Before(org.junit.Before)

Aggregations

ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)41 Test (org.junit.Test)26 SlingRepository (org.apache.sling.jcr.api.SlingRepository)22 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)16 DistributionRequest (org.apache.sling.distribution.DistributionRequest)13 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)12 DistributionPackageImporter (org.apache.sling.distribution.packaging.DistributionPackageImporter)12 Before (org.junit.Before)10 Event (javax.jcr.observation.Event)8 Scheduler (org.apache.sling.commons.scheduler.Scheduler)8 DistributionEventFactory (org.apache.sling.distribution.event.impl.DistributionEventFactory)8 DistributionPackageExporter (org.apache.sling.distribution.packaging.DistributionPackageExporter)8 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)8 DistributionQueueDispatchingStrategy (org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy)8 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)7 SimpleDistributionQueue (org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)6 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)6 Resource (org.apache.sling.api.resource.Resource)5