Search in sources :

Example 46 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest 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 47 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest 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 48 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class TriggerAgentRequestHandlerTest method testHandleActive.

@Test
public void testHandleActive() throws Exception {
    DistributionAgent agent = mock(DistributionAgent.class);
    SimpleDistributionAgentAuthenticationInfo authenticationInfo = mock(SimpleDistributionAgentAuthenticationInfo.class);
    DefaultDistributionLog log = mock(DefaultDistributionLog.class);
    TriggerAgentRequestHandler triggerAgentRequestHandler = new TriggerAgentRequestHandler(agent, authenticationInfo, log, true);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    DistributionRequest request = mock(DistributionRequest.class);
    triggerAgentRequestHandler.handle(resourceResolver, request);
}
Also used : DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) Test(org.junit.Test)

Example 49 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class PrivilegeDistributionRequestAuthorizationStrategyTest method testPermissionOnAdd.

@Test
public void testPermissionOnAdd() throws Exception {
    String jcrPrivilege = "somePermission";
    PrivilegeDistributionRequestAuthorizationStrategy strategy = new PrivilegeDistributionRequestAuthorizationStrategy(jcrPrivilege);
    DistributionRequest distributionRequest = mock(DistributionRequest.class);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    Session session = mock(Session.class);
    AccessControlManager acm = mock(AccessControlManager.class);
    Privilege privilege = mock(Privilege.class);
    when(acm.privilegeFromName(jcrPrivilege)).thenReturn(privilege);
    Privilege jcrReadPrivilege = mock(Privilege.class);
    when(acm.privilegeFromName(Privilege.JCR_READ)).thenReturn(jcrReadPrivilege);
    when(session.getAccessControlManager()).thenReturn(acm);
    when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
    String[] paths = new String[] { "/foo" };
    for (String path : paths) {
        when(acm.hasPrivileges(path, new Privilege[] { privilege, jcrReadPrivilege })).thenReturn(true);
    }
    when(distributionRequest.getPaths()).thenReturn(paths);
    when(distributionRequest.getRequestType()).thenReturn(DistributionRequestType.ADD);
    strategy.checkPermission(resourceResolver, distributionRequest);
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Privilege(javax.jcr.security.Privilege) Session(javax.jcr.Session) Test(org.junit.Test)

Example 50 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class PrivilegeDistributionRequestAuthorizationStrategyTest method testNoPermissionOnAdd.

@Test(expected = DistributionException.class)
public void testNoPermissionOnAdd() throws Exception {
    String jcrPrivilege = "somePermission";
    PrivilegeDistributionRequestAuthorizationStrategy strategy = new PrivilegeDistributionRequestAuthorizationStrategy(jcrPrivilege);
    DistributionRequest distributionRequest = mock(DistributionRequest.class);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    Session session = mock(Session.class);
    AccessControlManager acm = mock(AccessControlManager.class);
    Privilege privilege = mock(Privilege.class);
    when(acm.privilegeFromName(jcrPrivilege)).thenReturn(privilege);
    when(session.getAccessControlManager()).thenReturn(acm);
    when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
    String[] paths = new String[] { "/foo" };
    for (String path : paths) {
        when(acm.hasPrivileges(path, new Privilege[] { privilege })).thenReturn(false);
    }
    when(distributionRequest.getPaths()).thenReturn(paths);
    when(distributionRequest.getRequestType()).thenReturn(DistributionRequestType.ADD);
    strategy.checkPermission(resourceResolver, distributionRequest);
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Privilege(javax.jcr.security.Privilege) Session(javax.jcr.Session) Test(org.junit.Test)

Aggregations

DistributionRequest (org.apache.sling.distribution.DistributionRequest)52 Test (org.junit.Test)42 SimpleDistributionRequest (org.apache.sling.distribution.SimpleDistributionRequest)31 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)22 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)16 SlingRepository (org.apache.sling.jcr.api.SlingRepository)14 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)13 Session (javax.jcr.Session)11 List (java.util.List)10 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)10 DistributionPackageBuilder (org.apache.sling.distribution.packaging.DistributionPackageBuilder)10 Event (javax.jcr.observation.Event)9 Scheduler (org.apache.sling.commons.scheduler.Scheduler)9 DistributionPackageProcessor (org.apache.sling.distribution.packaging.DistributionPackageProcessor)9 DistributionResponse (org.apache.sling.distribution.DistributionResponse)7 TreeMap (java.util.TreeMap)6 Resource (org.apache.sling.api.resource.Resource)6 DistributionPackageExporter (org.apache.sling.distribution.packaging.DistributionPackageExporter)6 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)6 FileDistributionPackageBuilder (org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder)6