/** * Gets the Regex pattern to match the namespace and job name from a process definition key. * * @return the Regex pattern */ public Pattern getNamespaceAndJobNameRegexPattern() { // Get the job definition template (e.g. ~namespace~.~jobName~) and Regex escape all periods (e.g. ~namespace~\\.~jobName~). String jobDefinitionTemplate = getActivitiJobDefinitionTemplate().replaceAll("\\.", "\\\\."); // Change the tokens to named capture groups (e.g. (?<namespace>.*)\\.(?<jobName>.*)). jobDefinitionTemplate = jobDefinitionTemplate.replaceAll(getNamespaceToken(), "(?<namespace>.*)"); jobDefinitionTemplate = jobDefinitionTemplate.replaceAll(getJobNameToken(), "(?<jobName>.*)"); // Compile the Regex pattern. return Pattern.compile(jobDefinitionTemplate); }
/** * Gets a job definition key using namespace and name of the the job definition extracted from the specified process definition key using the Regex * pattern. * * @param processDefinitionKey the process definition key * * @return the job definition key */ public JobDefinitionAlternateKeyDto getJobDefinitionKey(String processDefinitionKey) { return getJobDefinitionKey(processDefinitionKey, getNamespaceAndJobNameRegexPattern()); }
/** * Populates the job Object with workflow variables. * * @param job, the Job object * @param variables, the workflow variables */ private void populateWorkflowParameters(Job job, Map<String, Object> variables) { List<Parameter> parameters = new ArrayList<>(); for (Entry<String, Object> paramEntry : variables.entrySet()) { Parameter parameter = new Parameter(paramEntry.getKey(), paramEntry.getValue() == null ? null : paramEntry.getValue().toString()); jobDefinitionHelper.maskPassword(parameter); parameters.add(parameter); } job.setParameters(parameters); }
/** * Gets the Activiti job definition template. * * @return the Activiti job definition template. */ public String getActivitiJobDefinitionTemplate() { // Set the default Activiti Id tokenized template. // ~namespace~.~jobName~ String defaultActivitiIdTemplate = getNamespaceToken() + "." + getJobNameToken(); // Get the Activiti Id template from the environment, but use the default if one isn't configured. // This gives us the ability to customize/change the format post deployment. String template = configurationHelper.getProperty(ConfigurationValue.ACTIVITI_JOB_DEFINITION_ID_TEMPLATE); if (template == null) { template = defaultActivitiIdTemplate; } return template; }
/** * Checks the namespace permissions for the current user for the given process definition key. * * @param processDefinitionKey The process definition key * @param permissions The list of permissions the current user must have */ private void checkPermissions(String processDefinitionKey, NamespacePermissionEnum[] permissions) { // Get the job definition key. JobDefinitionAlternateKeyDto jobDefinitionKey = jobDefinitionHelper.getJobDefinitionKey(processDefinitionKey); // Checks the permissions against the namespace. namespaceSecurityHelper.checkPermission(jobDefinitionKey.getNamespace(), permissions); }
@Test public void testGetActivitiJobDefinitionTemplateUsingDefaultActivitiIdTemplate() { assertEquals("~namespace~.~jobName~", jobDefinitionHelper.getActivitiJobDefinitionTemplate()); }
Assert.isTrue(idInsideActivitiXml.equalsIgnoreCase(jobDefinitionHelper.buildActivitiIdString(namespace, jobName)), "Namespace \"" + namespace + "\" and Job Name \"" + jobName + "\" does not match the ID specified within Activiti XML \"" + idInsideActivitiXml +
/** * Checks the namespace permissions for the current user for the given process definition key. * * @param processDefinitionKey The process definition key * @param permissions The list of permissions the current user must have */ private void checkPermissions(String processDefinitionKey, NamespacePermissionEnum[] permissions) { // Get the job definition key. JobDefinitionAlternateKeyDto jobDefinitionKey = jobDefinitionHelper.getJobDefinitionKey(processDefinitionKey); // Checks the permissions against the namespace. namespaceSecurityHelper.checkPermission(jobDefinitionKey.getNamespace(), permissions); }
@Test public void testGetActivitiJobDefinitionTemplateUsingConfiguredActivitiIdTemplate() throws Exception { // Set up test values. String testActivitiIdTemplate = "testActivitiIdTemplate"; // Override configuration. Map<String, Object> overrideMap = new HashMap<>(); overrideMap.put(ConfigurationValue.ACTIVITI_JOB_DEFINITION_ID_TEMPLATE.getKey(), testActivitiIdTemplate); modifyPropertySourceInEnvironment(overrideMap); try { assertEquals(testActivitiIdTemplate, jobDefinitionHelper.getActivitiJobDefinitionTemplate()); } finally { // Restore the property sources so we don't affect other tests. restorePropertySourceInEnvironment(); } }
/** * Gets the Activiti job definition template. * * @return the Activiti job definition template. */ public String getActivitiJobDefinitionTemplate() { // Set the default Activiti Id tokenized template. // ~namespace~.~jobName~ String defaultActivitiIdTemplate = getNamespaceToken() + "." + getJobNameToken(); // Get the Activiti Id template from the environment, but use the default if one isn't configured. // This gives us the ability to customize/change the format post deployment. String template = configurationHelper.getProperty(ConfigurationValue.ACTIVITI_JOB_DEFINITION_ID_TEMPLATE); if (template == null) { template = defaultActivitiIdTemplate; } return template; }
Assert.isTrue(idInsideActivitiXml.equalsIgnoreCase(jobDefinitionHelper.buildActivitiIdString(namespace, jobName)), "Namespace \"" + namespace + "\" and Job Name \"" + jobName + "\" does not match the ID specified within Activiti XML \"" + idInsideActivitiXml +
/** * Gets a job definition key using namespace and name of the the job definition extracted from the specified process definition key using the Regex * pattern. * * @param processDefinitionKey the process definition key * * @return the job definition key */ public JobDefinitionAlternateKeyDto getJobDefinitionKey(String processDefinitionKey) { return getJobDefinitionKey(processDefinitionKey, getNamespaceAndJobNameRegexPattern()); }
/** * Gets the Regex pattern to match the namespace and job name from a process definition key. * * @return the Regex pattern */ public Pattern getNamespaceAndJobNameRegexPattern() { // Get the job definition template (e.g. ~namespace~.~jobName~) and Regex escape all periods (e.g. ~namespace~\\.~jobName~). String jobDefinitionTemplate = getActivitiJobDefinitionTemplate().replaceAll("\\.", "\\\\."); // Change the tokens to named capture groups (e.g. (?<namespace>.*)\\.(?<jobName>.*)). jobDefinitionTemplate = jobDefinitionTemplate.replaceAll(getNamespaceToken(), "(?<namespace>.*)"); jobDefinitionTemplate = jobDefinitionTemplate.replaceAll(getJobNameToken(), "(?<jobName>.*)"); // Compile the Regex pattern. return Pattern.compile(jobDefinitionTemplate); }
/** * Populates the job Object with workflow variables. * * @param job, the Job object * @param variables, the workflow variables */ private void populateWorkflowParameters(Job job, Map<String, Object> variables) { List<Parameter> parameters = new ArrayList<>(); for (Entry<String, Object> paramEntry : variables.entrySet()) { Parameter parameter = new Parameter(paramEntry.getKey(), paramEntry.getValue() == null ? null : paramEntry.getValue().toString()); jobDefinitionHelper.maskPassword(parameter); parameters.add(parameter); } job.setParameters(parameters); }
@Before public void before() { initMocks(this); when(herdStringHelper.getConfigurationValueAsInteger(ConfigurationValue.JOBS_QUERY_MAX_RESULTS)).thenReturn(1); when(jobDefinitionHelper.getJobDefinitionKey(eq("a.b"), any())).thenReturn(new JobDefinitionAlternateKeyDto("a", "b")); }
/** * Deploys the Activiti XML into Activiti given the specified namespace and job name. If an existing process definition with the specified namespace and job * name already exists, a new process definition with an incremented version will be created by Activiti. * * @param namespace the namespace. * @param jobName the job name. * @param activitiJobXml the Activiti job XML. * * @return the newly created process definition. */ private ProcessDefinition createProcessDefinition(String namespace, String jobName, String activitiJobXml) { // Deploy Activiti XML using Activiti API. String activitiIdString = jobDefinitionHelper.buildActivitiIdString(namespace, jobName); Deployment deployment = activitiRepositoryService.createDeployment().name(activitiIdString).addString(activitiIdString + ACTIVITI_DEPLOY_XML_SUFFIX, activitiJobXml) .deploy(); // Read the created process definition. return activitiRepositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).list().get(0); }
@Test public void testGetJobDefinitionKeyInvalidProcessDefinitionKey() { // Set up test values. String testProcessDefinitionKey = "INVALID_PROCESS_DEFINITION_KEY"; // Get the regex pattern. Pattern pattern = jobDefinitionHelper.getNamespaceAndJobNameRegexPattern(); // Try to get the job definition key when process definition key not match the expected pattern. try { jobDefinitionHelper.getJobDefinitionKey(testProcessDefinitionKey, pattern); fail(); } catch (IllegalArgumentException e) { assertEquals( String.format("Process definition key \"%s\" does not match the expected pattern \"%s\".", testProcessDefinitionKey, pattern.toString()), e.getMessage()); } }
/** * Returns Activiti Id constructed according to the template defined. * * @param namespaceCd the namespace code value * @param jobName the job name value * * @return the Activiti Id */ public String buildActivitiIdString(String namespaceCd, String jobName) { // Populate a map with the tokens mapped to actual database values. Map<String, String> pathToTokenValueMap = new HashMap<>(); pathToTokenValueMap.put(getNamespaceToken(), namespaceCd); pathToTokenValueMap.put(getJobNameToken(), jobName); // The Activiti Id will start as the template. String activitiId = getActivitiJobDefinitionTemplate(); // Substitute the tokens with the actual database values. for (Map.Entry<String, String> mapEntry : pathToTokenValueMap.entrySet()) { activitiId = activitiId.replaceAll(mapEntry.getKey(), mapEntry.getValue()); } // Return the final Activiti Id. return activitiId; }
jobDefinitionHelper.maskPassword(parameter); jobParameters.add(parameter);
@Test public void testGetJobDefinitionKey() { // Set up test values. String testProcessDefinitionKey = String.format("%s.%s", NAMESPACE, JOB_NAME); // Validate the happy path scenario. assertEquals(new JobDefinitionAlternateKeyDto(NAMESPACE, JOB_NAME), jobDefinitionHelper.getJobDefinitionKey(testProcessDefinitionKey)); }