Add Job Event Messaging to tasks.

* Added test for sample and cleanup

* Added skipEventsListener

* Added new message format.

* Added integration tests.

* Updated application.properties to add json content type

resolves spring-cloud/spring-cloud-task#119
This commit is contained in:
Michael Minella
2016-04-12 18:05:16 -05:00
parent ced5a3aa87
commit 43d869f726
51 changed files with 3620 additions and 11 deletions

View File

@@ -0,0 +1,111 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed 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.springframework.cloud.task.batch.listener;
import java.util.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.cloud.task.batch.listener.support.JobExecutionEvent;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Glenn Renfro.
*/
public class EventJobExecutionTests {
private static final String JOB_NAME = "FOODJOB";
private static final Long JOB_INSTANCE_ID = 1l;
private static final Long JOB_EXECUTION_ID = 2l;
private static final String JOB_CONFIGURATION_NAME = "FOO_JOB_CONFIG";
private JobParameters jobParameters;
private JobInstance jobInstance;
@Before
public void setup() {
jobInstance = new JobInstance(JOB_INSTANCE_ID, JOB_NAME);
jobParameters = new JobParameters();
}
@Test
public void testBasic() {
JobExecution jobExecution = new JobExecution(jobInstance, JOB_EXECUTION_ID, jobParameters, JOB_CONFIGURATION_NAME);
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent(jobExecution);
assertNotNull("jobInstance should not be null", jobExecutionEvent.getJobInstance());
assertNotNull("jobParameters should not be null", jobExecutionEvent.getJobParameters());
assertEquals("jobConfigurationName did not match expected", JOB_CONFIGURATION_NAME,
jobExecutionEvent.getJobConfigurationName());
assertEquals("jobParameters size did not match", 0, jobExecutionEvent.getJobParameters().getParameters().size());
assertEquals("jobInstance name did not match", JOB_NAME, jobExecutionEvent.getJobInstance().getJobName());
assertEquals("no step executions were expected", 0, jobExecutionEvent.getStepExecutions().size());
assertEquals("exitStatus did not match expected", "UNKNOWN", jobExecutionEvent.getExitStatus().getExitCode());
}
@Test
public void testJobParameters() {
String[] JOB_PARAM_KEYS = { "A", "B", "C", "D" };
Date testDate = new Date();
JobParameter[] PARAMETERS = { new JobParameter("FOO", true), new JobParameter(1L, true),
new JobParameter(1D, true), new JobParameter(testDate, false) };
Map jobParamMap = new LinkedHashMap<String, JobParameter>();
for (int paramCount = 0; paramCount < JOB_PARAM_KEYS.length; paramCount++) {
jobParamMap.put(JOB_PARAM_KEYS[paramCount], PARAMETERS[paramCount]);
}
jobParameters = new JobParameters(jobParamMap);
JobExecution jobExecution = new JobExecution(jobInstance, JOB_EXECUTION_ID, jobParameters, JOB_CONFIGURATION_NAME);
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent(jobExecution);
assertNotNull("Job Parameter A was expected", jobExecutionEvent.getJobParameters().getString("A"));
assertNotNull("Job Parameter B was expected", jobExecutionEvent.getJobParameters().getLong("B"));
assertNotNull("Job Parameter C was expected", jobExecutionEvent.getJobParameters().getDouble("C"));
assertNotNull("Job Parameter D was expected", jobExecutionEvent.getJobParameters().getDate("D"));
assertEquals("Job Parameter A value was not correct", "FOO", jobExecutionEvent.getJobParameters().getString("A"));
assertEquals("Job Parameter B value was not correct", new Long(1), jobExecutionEvent.getJobParameters().getLong("B"));
assertEquals("Job Parameter C value was not correct", new Double(1), jobExecutionEvent.getJobParameters().getDouble("C"));
assertEquals("Job Parameter D value was not correct", testDate, jobExecutionEvent.getJobParameters().getDate("D"));
}
@Test
public void testStepExecutions() {
JobExecution jobExecution = new JobExecution(jobInstance, JOB_EXECUTION_ID, jobParameters, JOB_CONFIGURATION_NAME);
List<StepExecution> stepsExecutions = new ArrayList<>();
stepsExecutions.add( new StepExecution("foo", jobExecution));
stepsExecutions.add( new StepExecution("bar", jobExecution));
stepsExecutions.add( new StepExecution("baz", jobExecution));
jobExecution.addStepExecutions(stepsExecutions);
JobExecutionEvent jobExecutionsEvent = new JobExecutionEvent(jobExecution);
assertEquals("stepExecutions count is incorrect", 3, jobExecutionsEvent.getStepExecutions().size());
Iterator<StepExecutionEvent> iter = jobExecutionsEvent.getStepExecutions().iterator();
assertEquals("foo stepExecution is not present", "foo", iter.next().getStepName());
assertEquals("bar stepExecution is not present", "bar", iter.next().getStepName());
assertEquals("baz stepExecution is not present", "baz", iter.next().getStepName());
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed 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.springframework.cloud.task.batch.listener;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
import static org.junit.Assert.assertEquals;
/**
* @author Glenn Renfro
*/
public class EventStepExecutionTests {
private static final String JOB_NAME = "FOO_JOB";
private static final String STEP_NAME = "STEP_NAME";
private static final Long JOB_INSTANCE_ID = 1l;
private static final Long JOB_EXECUTION_ID = 2l;
private static final String JOB_CONFIGURATION_NAME = "FOO_JOB_CONFIG";
@Test
public void testBasic(){
JobInstance jobInstance = new JobInstance(JOB_INSTANCE_ID, JOB_NAME);
JobParameters jobParameters = new JobParameters();
JobExecution jobExecution = new JobExecution(jobInstance, JOB_EXECUTION_ID, jobParameters, JOB_CONFIGURATION_NAME);
StepExecution stepExecution = new StepExecution(STEP_NAME, jobExecution);
stepExecution.setCommitCount(1);
stepExecution.setReadCount(2);
stepExecution.setWriteCount(3);
stepExecution.setReadSkipCount(4);
stepExecution.setWriteSkipCount(5);
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(stepExecution);
assertEquals("stepName result was not as expected", STEP_NAME, stepExecutionEvent.getStepName());
assertEquals("startTime result was not as expected", stepExecution.getStartTime(), stepExecutionEvent.getStartTime());
assertEquals("endTime result was not as expected", stepExecution.getEndTime(), stepExecutionEvent.getEndTime());
assertEquals("lastUpdated result was not as expected", stepExecution.getLastUpdated(), stepExecutionEvent.getLastUpdated());
assertEquals("commitCount result was not as expected", stepExecution.getCommitCount(), stepExecutionEvent.getCommitCount());
assertEquals("readCount result was not as expected", stepExecution.getReadCount(), stepExecutionEvent.getReadCount());
assertEquals("readSkipCount result was not as expected", stepExecution.getReadSkipCount(), stepExecutionEvent.getReadSkipCount());
assertEquals("writeCount result was not as expected", stepExecution.getWriteCount(), stepExecutionEvent.getWriteCount());
assertEquals("writeSkipCount result was not as expected", stepExecution.getWriteSkipCount(), stepExecutionEvent.getWriteSkipCount());
assertEquals("skipCount result was not as expected", stepExecution.getSkipCount(), stepExecutionEvent.getSkipCount());
}
}

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.task.launcher;
import java.util.HashMap;
import java.util.Map;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -27,6 +28,7 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.deployer.spi.task.LaunchState;
import org.springframework.cloud.stream.annotation.Bindings;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.test.junit.redis.RedisTestSupport;
import org.springframework.cloud.task.launcher.configuration.TaskConfiguration;
import org.springframework.cloud.task.launcher.util.TaskLauncherSinkApplication;
import org.springframework.context.ApplicationContext;
@@ -39,6 +41,9 @@ import static org.junit.Assert.assertEquals;
@SpringApplicationConfiguration(classes = {TaskLauncherSinkApplication.class, TaskConfiguration.class} )
public class TaskLauncherSinkTests {
@ClassRule
public static RedisTestSupport redisTestSupport = new RedisTestSupport();
private final static String DEFAULT_STATUS = "test_status";
@Autowired

View File

@@ -15,11 +15,12 @@
*/
package org.springframework.cloud.task.listener;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration;
import org.springframework.cloud.stream.test.junit.redis.RedisTestSupport;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
@@ -28,19 +29,24 @@ import static org.junit.Assert.assertNotNull;
/**
* @author Michael Minella
* @author Ilayaperumal Gopinathan
*/
public class TaskEventTests {
@Rule
public RedisTestSupport redisTestSupport = new RedisTestSupport();
private static final String TASK_NAME = "taskEventTest";
@Test
public void testDefaultConfiguration() {
ConfigurableApplicationContext applicationContext =
SpringApplication.run(new Object[] {TaskEventsConfiguration.class,
SpringApplication.run(new Object[]{ TaskEventsConfiguration.class,
TaskEventAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
TestSupportBinderAutoConfiguration.class},
new String[] {"--spring.cloud.task.closecontext.enable=false",
PropertyPlaceholderAutoConfiguration.class },
new String[]{ "--spring.cloud.task.closecontext.enable=false",
"--spring.main.web-environment=false",
"--spring.cloud.stream.defaultBinder=test"});
"--spring.cloud.stream.defaultBinder=test" });
assertNotNull(applicationContext.getBean("taskEventListener"));
assertNotNull(applicationContext.getBean(TaskEventAutoConfiguration.TaskEventChannels.class));
@@ -50,4 +56,5 @@ public class TaskEventTests {
@EnableTask
public static class TaskEventsConfiguration {
}
}