Skip to content

Commit

Permalink
SLING-8853 Keeping SonarQube happy (take apache#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
actinium15 committed Dec 11, 2019
1 parent 9c37354 commit 90c3b4f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class ResourceQueueProvider implements DistributionQueueProvider {

public ResourceQueueProvider(BundleContext context, ResourceResolverFactory resolverFactory,
String serviceName, String agentName, Scheduler scheduler, boolean isActive) {
if (serviceName == null || scheduler == null
if (serviceName == null || (scheduler == null && isActive)
|| context == null || resolverFactory == null || agentName == null) {
throw new IllegalArgumentException("all arguments are required");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.sling.distribution.queue.DistributionQueueState;
import org.apache.sling.distribution.queue.impl.DistributionQueueProcessor;
import org.apache.sling.distribution.queue.impl.DistributionQueueProvider;
import org.apache.sling.distribution.queue.impl.DistributionQueueProviderFactory;
import org.apache.sling.distribution.queue.spi.DistributionQueue;
import org.apache.sling.testing.mock.osgi.MockOsgi;
import org.apache.sling.testing.mock.sling.MockSling;
Expand All @@ -38,6 +39,7 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.osgi.framework.BundleContext;
Expand Down Expand Up @@ -101,12 +103,42 @@ public void testActiveResourceQueue() throws DistributionException, PersistenceE
}
}

@Test
public void testActiveResourceQueueWithoutEnablingProcessing() throws DistributionException {
final String QUEUE_NAME = "testActiveQueue_2";
final int MAX_ENTRIES = 2;
Scheduler tempScheduler = mock(Scheduler.class);
when(tempScheduler.unschedule(Matchers.anyString())).thenReturn(false);

DistributionQueueProvider resourceQueueProvider = new ResourceQueueProvider(bundleContext,
rrf, "test", "testAgent", tempScheduler, true);
DistributionQueue resourceQueue = resourceQueueProvider.getQueue(QUEUE_NAME);

try {
populateDistributionQueue(resourceQueue, MAX_ENTRIES);

assertTrue("Resource Queue state is not RUNNING",
resourceQueue.getStatus().getState().equals(DistributionQueueState.RUNNING));
assertEquals(MAX_ENTRIES, resourceQueue.getStatus().getItemsCount());
} finally {
// should log a WARN for ResourceQueueProvider class
resourceQueueProvider.disableQueueProcessing();
resourceQueue.clear(Integer.MAX_VALUE);
}
}

@Test(expected = DistributionException.class)
public void testPassiveResourceQueueEnableProcessing() throws DistributionException {
final String QUEUE_NAME = "testPassiveQueue_1";
final int MAX_ENTRIES = 4;
DistributionQueueProvider resourceQueueProvider = new ResourceQueueProvider(bundleContext,
rrf, "test", "testAgent", scheduler, false);
DistributionQueueProviderFactory resQueueProviderFactory= new ResourceQueueProviderFactory();
Whitebox.setInternalState(resQueueProviderFactory, "isActive", false);
Whitebox.setInternalState(resQueueProviderFactory, "resourceResolverFactory", rrf);
Whitebox.setInternalState(resQueueProviderFactory, "scheduler", scheduler);
MockOsgi.activate(resQueueProviderFactory, bundleContext);

DistributionQueueProvider resourceQueueProvider = resQueueProviderFactory
.getProvider("test", "testAgent");

DistributionQueue resourceQueue = resourceQueueProvider.getQueue(QUEUE_NAME, null);

Expand All @@ -128,7 +160,7 @@ public void testPassiveResourceQueueDisableProcessing() throws DistributionExcep
final String QUEUE_NAME = "testPassiveQueue_2";
final int MAX_ENTRIES = 2;
DistributionQueueProvider resourceQueueProvider = new ResourceQueueProvider(bundleContext,
rrf, "test", "testAgent", scheduler, false);
rrf, "test", "testAgent", null, false);

DistributionQueue resourceQueue = resourceQueueProvider.getQueue(QUEUE_NAME, null);

Expand All @@ -144,12 +176,66 @@ public void testPassiveResourceQueueDisableProcessing() throws DistributionExcep
}
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidQueueProviderConstruction_1() {
constructIllegalResourceQueueProvider(IllegalQueueProviderType.MISSING_BUNDLE_CONTEXT);
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidQueueProviderConstruction_2() {
constructIllegalResourceQueueProvider(IllegalQueueProviderType.MISSING_RESOLVER_FACTORY);
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidQueueProviderConstruction_3() {
constructIllegalResourceQueueProvider(IllegalQueueProviderType.MISSING_SERVICENAME);
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidQueueProviderConstruction_4() {
constructIllegalResourceQueueProvider(IllegalQueueProviderType.MISSING_AGENTNAME);
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidQueueProviderConstruction_5() {
constructIllegalResourceQueueProvider(IllegalQueueProviderType.MISSING_SCHEDULER_WHEN_ACTIVE);
}

private void populateDistributionQueue(DistributionQueue queue, int maxEntries) {
for (int i = 0; i < maxEntries; i++) {
queue.add(new DistributionQueueItem(PACKAGE_ID, Collections.<String, Object>emptyMap()));
}
}

private ResourceQueueProvider constructIllegalResourceQueueProvider(IllegalQueueProviderType type) {
switch(type) {
case MISSING_BUNDLE_CONTEXT:
return new ResourceQueueProvider(null,
rrf, "test", "testAgent", scheduler, true);
case MISSING_RESOLVER_FACTORY:
return new ResourceQueueProvider(bundleContext,
null, "test", "testAgent", scheduler, true);
case MISSING_SERVICENAME:
return new ResourceQueueProvider(bundleContext,
rrf, null, "testAgent", scheduler, true);
case MISSING_AGENTNAME:
return new ResourceQueueProvider(bundleContext,
rrf, "test", null, scheduler, true);
case MISSING_SCHEDULER_WHEN_ACTIVE:
default:
return new ResourceQueueProvider(bundleContext,
rrf, "test", "testAgent", null, true);
}
}

private enum IllegalQueueProviderType {
MISSING_BUNDLE_CONTEXT,
MISSING_RESOLVER_FACTORY,
MISSING_SERVICENAME,
MISSING_AGENTNAME,
MISSING_SCHEDULER_WHEN_ACTIVE,
}

@BeforeClass
public static void setUp() throws LoginException {
bundleContext = MockOsgi.newBundleContext();
Expand Down

0 comments on commit 90c3b4f

Please sign in to comment.