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());
}
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());
}
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);
}
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);
}
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;
}
Aggregations