New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SLING-11831 - Allow setting job properties for custom job state #26
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
07cba8d
SLING-11831 - Allow setting job properties for custom job state
andrewmkhoury 008e5b1
SLING-11831 - Allow setting job properties for custom job state
andrewmkhoury 8b42a7f
SLING-11831 - Allow setting job properties for custom job state
andrewmkhoury 2a0dd3f
Add NotNull annotations
andrewmkhoury File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
src/test/java/org/apache/sling/event/impl/jobs/queues/JobExecutionContextImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.sling.event.impl.jobs.queues; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
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.math.BigInteger; | ||
import java.util.HashMap; | ||
|
||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ResourceResolver; | ||
import org.apache.sling.api.resource.ValueMap; | ||
import org.apache.sling.commons.scheduler.Scheduler; | ||
import org.apache.sling.commons.threads.ThreadPoolManager; | ||
import org.apache.sling.event.impl.jobs.JobConsumerManager; | ||
import org.apache.sling.event.impl.jobs.JobHandler; | ||
import org.apache.sling.event.impl.jobs.JobImpl; | ||
import org.apache.sling.event.impl.jobs.JobManagerImpl; | ||
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.impl.jobs.stats.StatisticsManager; | ||
import org.apache.sling.event.jobs.Job; | ||
import org.apache.sling.event.jobs.JobManager; | ||
import org.apache.sling.event.jobs.consumer.JobExecutionContext; | ||
import org.apache.sling.event.jobs.consumer.JobExecutionResult; | ||
import org.apache.sling.event.jobs.consumer.JobExecutor; | ||
import org.apache.sling.event.jobs.jmx.QueuesMBean; | ||
import org.apache.sling.testing.mock.sling.junit.SlingContext; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
|
||
public class JobExecutionContextImplTest { | ||
|
||
@Rule | ||
public SlingContext context = new SlingContext(); | ||
|
||
private JobManager jobManager; | ||
private JobManagerConfiguration configuration; | ||
|
||
@Before | ||
public void setUp() { | ||
configuration = createMockJobManagerConfiguration(); | ||
|
||
QueueConfigurationManager queueConfigMgr = mock(QueueConfigurationManager.class); | ||
QueueInfo info = new QueueInfo(); | ||
info.queueConfiguration = new InternalQueueConfiguration(); | ||
when(queueConfigMgr.getQueueInfo(anyString())).thenReturn(info); | ||
when(configuration.getQueueConfigurationManager()).thenReturn(queueConfigMgr); | ||
|
||
TopologyCapabilities capabilities = mock(TopologyCapabilities.class); | ||
JobConsumerManager jobConsumerManager = mock(JobConsumerManager.class); | ||
QueueManager qManager = mock(QueueManager.class); | ||
ThreadPoolManager threadPoolManager = mock(ThreadPoolManager.class); | ||
MetricRegistry metric = mock(MetricRegistry.class); | ||
StatisticsManager statisticsManager = mock(StatisticsManager.class); | ||
QueuesMBean queuesMBean = mock(QueuesMBean.class); | ||
Scheduler scheduler = mock(Scheduler.class); | ||
|
||
context.registerService(JobManagerConfiguration.class, configuration); | ||
context.registerService(TopologyCapabilities.class, capabilities); | ||
context.registerService(QueueConfigurationManager.class, queueConfigMgr); | ||
context.registerService(MetricRegistry.class, metric); | ||
context.registerService(QueueManager.class, qManager); | ||
context.registerService(JobConsumerManager.class, jobConsumerManager); | ||
context.registerService(ThreadPoolManager.class, threadPoolManager); | ||
context.registerService(StatisticsManager.class, statisticsManager); | ||
context.registerService(QueuesMBean.class, queuesMBean); | ||
context.registerService(Scheduler.class, scheduler); | ||
context.registerService(JobExecutor.class, new TestJobExecutor(), new HashMap<String, Object>() {{ | ||
put(JobExecutor.PROPERTY_TOPICS, "test"); | ||
}}); | ||
|
||
jobManager = new JobManagerImpl(); | ||
context.registerInjectActivateService(jobManager, new HashMap<String, Object>()); | ||
} | ||
|
||
@Test | ||
public void testSetProperty() { | ||
// Create a job - it will be written to the mock jcr | ||
Job job = jobManager.addJob("test", null); | ||
|
||
// Process the job | ||
JobExecutor je = new TestJobExecutor(); | ||
je.process(job, new JobExecutionContextImpl(new JobHandler((JobImpl) job, je, configuration), null)); | ||
|
||
// Retrieve the custom property | ||
assertEquals("testValue", job.getProperty("test", String.class)); | ||
|
||
final String testValue; | ||
Iterable<Resource> resources = context.resourceResolver().getResource("/var/eventing/jobs/assigned").getChildren(); | ||
ValueMap props = resources.iterator().next().adaptTo(ValueMap.class); | ||
testValue = props.get("test", String.class); | ||
assertEquals("testValue", testValue); | ||
} | ||
|
||
public class TestJobExecutor implements JobExecutor { | ||
|
||
@Override | ||
public JobExecutionResult process(Job job, JobExecutionContext context) { | ||
context.setProperty("test", "testValue"); | ||
return context.result().message("TEST").succeeded(); | ||
} | ||
} | ||
|
||
private JobManagerConfiguration createMockJobManagerConfiguration() { | ||
JobManagerConfiguration jobManagerConfig = mock(JobManagerConfiguration.class); | ||
|
||
String jobsPath = "/var/eventing"; | ||
|
||
when(jobManagerConfig.getUniqueId(anyString())).then(new Answer<String>() { | ||
@Override | ||
public String answer(InvocationOnMock invocation) throws Throwable { | ||
byte [] digest = java.security.MessageDigest.getInstance("md5").digest(String.valueOf(Math.random()).getBytes("UTF-8")); | ||
BigInteger bigInt = new BigInteger(1, digest); | ||
String hashtext = bigInt.toString(16); | ||
return hashtext + "_" + String.valueOf((int)(Math.random()* 1000000)); | ||
} | ||
}); | ||
when(jobManagerConfig.getScheduledJobsPath(false)).thenReturn(jobsPath + "/scheduled-jobs"); | ||
when(jobManagerConfig.getUniquePath(eq(null), anyString(), anyString(), eq(null))).then( | ||
new Answer<String>() { | ||
@Override | ||
public String answer(InvocationOnMock invocation) throws Throwable { | ||
String jobNodePath = jobsPath + "/jobs/assigned/" + invocation.getArgument(2); | ||
return jobNodePath; | ||
} | ||
} | ||
); | ||
when(jobManagerConfig.getAuditLogger()).thenReturn(LoggerFactory.getLogger("org.apache.sling.event.jobs.audit")); | ||
ResourceResolver resolver = context.resourceResolver(); | ||
when(jobManagerConfig.createResourceResolver()).thenReturn(resolver); | ||
return jobManagerConfig; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional, this could be simplfied with:
Objects.requireNonNull(name, "Name must not be null")
But this would throw a NPE (with the supplied message) rather that an IllegalArgumentMessage.
https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#requireNonNull-T-java.lang.String-
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it is a user facing API, I implemented it opting for IllegalArgumentException as the exception type. Do you agree or should I change it and allow it to be an NPE?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also add a @NotNull annotation to the API to make it more visible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay with keeping the current code to have a more descriptive error, but I agree with @joerghoh regarding the annotations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had thought of using
@NonNull
too, but it meant I need to introduce a new dependency to this project and org.apache.sling.event.api project. WDYT?Also, note that
@Nonnull
isn't perfect at blocking null parameters from getting passed, so it still makes sense to keep the null checks. See here for details:https://stackoverflow.com/questions/13484202/how-to-use-nullable-and-nonnull-annotations-more-effectively/46290602#46290602
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we use other null annotations in Sling: https://sling.apache.org/documentation/development/null-analysis.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kwin, I will use those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, done:
https://github.com/apache/sling-org-apache-sling-event/pull/26/files#diff-6821a1c0362dd00f0e0c3dd812e85e5fc3424c1b63971fe46712d547a3eafe75R89
https://github.com/apache/sling-org-apache-sling-event/pull/26/files#diff-9c5fb3d1b7e3b0f54bc5c4182965c4fe1f9023d449017cece3005d3f90e8e4d8R122-R126