rishabhdaim commented on code in PR #25:
URL:
https://github.com/apache/sling-org-apache-sling-event/pull/25#discussion_r1138103248
##########
src/test/java/org/apache/sling/event/impl/jobs/JobManagerImplTest.java:
##########
@@ -19,27 +19,162 @@
package org.apache.sling.event.impl.jobs;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.lang.reflect.InvocationTargetException;
+import java.util.function.Predicate;
+
+import org.apache.commons.lang.reflect.FieldUtils;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration;
+import org.apache.sling.event.impl.jobs.config.JobManagerConfiguration;
+import org.apache.sling.event.impl.jobs.config.QueueConfigurationManager;
+import
org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo;
+import org.apache.sling.event.impl.jobs.config.TopologyCapabilities;
import org.apache.sling.event.jobs.JobManager.QueryType;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
+import org.slf4j.Logger;
public class JobManagerImplTest {
+ private static final String QUERY_ROOT = "/var/eventing/foobar";
+ private static final QueryType QUERY_TYPE = QueryType.ACTIVE;
+
+ private QueueInfo info;
+ private TopologyCapabilities capabilities;
+ private PrintStream originalErr;
+ private ByteArrayOutputStream outContent;
+ private JobManagerConfiguration configuration;
+
+ private static final int LEVEL_DEBUG = 10;
+ private static final int LEVEL_INFO = 20;
+ private static final int LEVEL_WARN = 30;
+
+ @Before
+ public void init() throws IllegalAccessException {
+
+ configuration = mock(JobManagerConfiguration.class);
+ when(configuration.getUniquePath(any(), any(), any(),
any())).thenReturn("/");
+ when(configuration.getUniqueId(any())).thenReturn("1");
+
when(configuration.getAuditLogger()).thenReturn(mock(Logger.class));
+
+ QueueConfigurationManager queueConfigMgr =
mock(QueueConfigurationManager.class);
+ this.info = new QueueInfo();
+ info.queueConfiguration =
mock(InternalQueueConfiguration.class);
+ when(queueConfigMgr.getQueueInfo(anyString())).thenReturn(info);
+
when(configuration.getQueueConfigurationManager()).thenReturn(queueConfigMgr);
+
+ this.capabilities = mock(TopologyCapabilities.class);
+
when(configuration.getTopologyCapabilities()).thenReturn(capabilities);
+
+ when(capabilities.detectTarget(eq("is/assigned"), any(),
any())).thenReturn("assigned");
+
+ ResourceResolver resolver = mock(ResourceResolver.class);
+
when(resolver.getResource(anyString())).thenReturn(mock(Resource.class));
+
when(configuration.createResourceResolver()).thenReturn(resolver);
+
+ this.outContent = new ByteArrayOutputStream();
+ this.originalErr = System.err;
+ System.setErr(new PrintStream(outContent));
+
+ }
+
+ // SLING-8413
+ @Test
+ public void testTopicEscaping() {
+ String baseQuery = JobManagerImpl.buildBaseQuery(QUERY_ROOT,
"randomNonQuotedTopic", QUERY_TYPE, false);
+ assertEquals(
+
"/jcr:root/var/eventing/foobar/element(*,slingevent:Job)[@event.job.topic = "
+ + "'randomNonQuotedTopic' and
not(@slingevent:finishedState) and @event.job.started.time",
+ baseQuery);
+
+ String baseQuery2 = JobManagerImpl.buildBaseQuery(QUERY_ROOT,
"random'Topic", QUERY_TYPE, false);
+
assertEquals("/jcr:root/var/eventing/foobar/element(*,slingevent:Job)[@event.job.topic
= "
+ + "'random''Topic' and
not(@slingevent:finishedState) and @event.job.started.time", baseQuery2);
+
+ }
+
+ @Test
+ public void wontLogAtWarn() throws IllegalAccessException, IOException,
NoSuchMethodException, SecurityException,
+ IllegalArgumentException, InvocationTargetException {
+ JobManagerImpl jobManager = getJobManager(LEVEL_WARN);
+
+ jobManager.addJob("not/assigned", null, null);
+ assertFalse(logContainsLine(line -> line
+ .contains("Persisting job Sling Job
[topic=not/assigned] into queue null with no assigned target")));
+
+ jobManager.addJob("is/assigned", null, null);
+ assertFalse(logContainsLine(line -> line
+ .contains("Persisting job Sling Job
[topic=is/assigned] into queue null with no assigned target")));
+ }
+
+ @Test
+ public void logsInfoForUnassigned() throws IllegalAccessException,
IOException, NoSuchMethodException,
+ SecurityException, IllegalArgumentException,
InvocationTargetException {
+ JobManagerImpl jobManager = getJobManager(LEVEL_INFO);
+ jobManager.addJob("not/assigned", null, null);
+ assertTrue(logContainsLine(line -> line
+ .contains("Persisting job Sling Job
[topic=not/assigned] into queue null with no assigned target")));
+
+ jobManager.addJob("is/assigned", null, null);
+ assertTrue(logContainsLine(line -> !line
+ .contains("Persisting job Sling Job
[topic=is/assigned] into queue null with no assigned target")));
+ }
+
+ @Test
+ public void logsDebugForAssigned() throws IllegalAccessException,
IOException, NoSuchMethodException,
+ SecurityException, IllegalArgumentException,
InvocationTargetException {
+ JobManagerImpl jobManager = getJobManager(LEVEL_DEBUG);
+
+ jobManager.addJob("is/assigned", null, null);
+ assertTrue(logContainsLine(line -> line
+ .contains("Persisting job Sling Job
[topic=is/assigned] into queue null, target=assigned")));
+ }
+
+ @After
+ public void after() throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
+ System.setErr(originalErr);
+ }
+
+ public JobManagerImpl getJobManager(int level) throws
IllegalAccessException, NoSuchMethodException,
+ SecurityException, IllegalArgumentException,
InvocationTargetException {
+
+ JobManagerImpl jobManager = new JobManagerImpl();
+
+ Object logger = FieldUtils.readDeclaredField(jobManager,
"logger", true);
+ FieldUtils.writeDeclaredField(logger, "currentLogLevel", level,
true);
+
+ FieldUtils.writeDeclaredField(jobManager, "configuration",
configuration, true);
+ return jobManager;
+ }
- private static final String QUERY_ROOT = "/var/eventing/foobar";
- private static final QueryType QUERY_TYPE = QueryType.ACTIVE;
-
- // SLING-8413
- @Test
- public void testTopicEscaping() {
- String baseQuery = JobManagerImpl.buildBaseQuery(QUERY_ROOT,
"randomNonQuotedTopic", QUERY_TYPE, false);
-
assertEquals("/jcr:root/var/eventing/foobar/element(*,slingevent:Job)[@event.job.topic
= "
- + "'randomNonQuotedTopic' and not(@slingevent:finishedState)
and @event.job.started.time",baseQuery);
-
- String baseQuery2 = JobManagerImpl.buildBaseQuery(QUERY_ROOT,
"random'Topic", QUERY_TYPE, false);
-
assertEquals("/jcr:root/var/eventing/foobar/element(*,slingevent:Job)[@event.job.topic
= "
- + "'random''Topic' and not(@slingevent:finishedState) and
@event.job.started.time",baseQuery2);
-
- }
-
+ private boolean logContainsLine(Predicate<String> matcher) throws
IOException {
Review Comment:
This method is an exact duplicate of the same method in
`JobhandlingIT.containsLine()`.
I think we can extract them to a common location to avoid code duplication.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]