SCT-4 Added SimpleTaskConfiguration

* Added unit tests
* Introduced @EnableTask
* Readme updated to include @EnableTask
* Removed warnings from build
* Cleanup
This commit is contained in:
Glenn Renfro
2015-11-23 15:52:03 -05:00
committed by Michael Minella
parent 93e4698175
commit ce8854b89e
19 changed files with 697 additions and 38 deletions

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2015 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;
import java.util.UUID;
import ch.qos.logback.core.Appender;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.util.LoggerTestUtils;
import org.springframework.cloud.task.util.TestDefaultConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Verifies that the LoggerRepository has correct prefixes written to logs.
* @author Glenn Renfro
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestDefaultConfiguration.class)
public class LoggerTaskRepositoryTests {
@Autowired
private TaskRepository taskRepository;
private TaskExecution taskExecution;
private String uuId;
@Before
public void setup(){
taskExecution = new TaskExecution();
uuId = UUID.randomUUID().toString();
taskExecution.setExecutionId(uuId);
}
@Test
public void testCreateTaskExecution() {
final Appender mockAppender = LoggerTestUtils.getMockAppender();
taskRepository.createTaskExecution(taskExecution);
LoggerTestUtils.verifyLogEntryExists(mockAppender,
"Creating: TaskExecution{executionId='" + uuId);
}
@Test
public void testTaskUpdate() {
final Appender mockAppender = LoggerTestUtils.getMockAppender();
taskRepository.update(taskExecution);
LoggerTestUtils.verifyLogEntryExists(mockAppender,
"Updating: TaskExecution{executionId='" + uuId);
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2015 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;
import static junit.framework.TestCase.assertNotNull;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.task.configuration.SimpleTaskConfiguration;
import org.springframework.cloud.task.repository.support.LoggerTaskRepository;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Verifies that the beans created by the SimpleTaskConfiguration.
*
* @author Glenn Renfro
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SimpleTaskConfiguration.class)
public class SimpleTaskConfigurationTests {
@Autowired
private TaskRepository taskRepository;
@Test
public void testRepository() {
assertNotNull("testRepository should not be null", taskRepository);
assertThat(taskRepository, instanceOf(LoggerTaskRepository.class));
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2015 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;
import static org.junit.Assert.assertEquals;
import ch.qos.logback.core.Appender;
import org.aspectj.lang.JoinPoint;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.task.configuration.TaskHandler;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.util.LoggerTestUtils;
import org.springframework.cloud.task.util.TestDefaultConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Verifies that the TaskHandler Methods record the appropriate log header entries and
* result codes.
*
* @author Glenn Renfro
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestDefaultConfiguration.class)
public class TaskHandlerDefaultTests {
@Autowired
private TaskHandler taskHandler;
@Autowired
private JoinPoint joinPoint;
@Test
public void testTaskException() {
taskHandler.beforeCommandLineRunner(joinPoint);
final Appender mockAppender = LoggerTestUtils.getMockAppender();
taskHandler.logExceptionCommandLineRunner(joinPoint);
LoggerTestUtils.verifyLogEntryExists(mockAppender,
"Updating: TaskExecution{executionId='" +
taskHandler.getTaskExecution().getExecutionId());
TaskExecution taskExecution = taskHandler.getTaskExecution();
assertEquals("exit code for exception should be 1", taskExecution.getExitCode(),
1);
}
@Test
public void testTaskCreate() {
final Appender mockAppender = LoggerTestUtils.getMockAppender();
taskHandler.beforeCommandLineRunner(joinPoint);
LoggerTestUtils.verifyLogEntryExists(mockAppender,
"Creating: TaskExecution{executionId='" +
taskHandler.getTaskExecution().getExecutionId());
assertEquals("Create should report that exit code is zero",
0, taskHandler.getTaskExecution().getExitCode());
}
@Test
public void testTaskUpdate() {
taskHandler.beforeCommandLineRunner(joinPoint);
final Appender mockAppender = LoggerTestUtils.getMockAppender();
taskHandler.afterReturnCommandLineRunner(joinPoint);
LoggerTestUtils.verifyLogEntryExists(mockAppender,
"Updating: TaskExecution{executionId='" +
taskHandler.getTaskExecution().getExecutionId());
assertEquals("Update should report that exit code is zero",
0, taskHandler.getTaskExecution().getExitCode());
}
}

View File

@@ -23,7 +23,7 @@ import org.junit.Test;
import org.springframework.cloud.task.repository.TaskExplorer;
public class NoOpTaskExplorerTest {
public class NoOpTaskExplorerTests {
private TaskExplorer taskExplorer;

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2015 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.util;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;
import org.mockito.ArgumentMatcher;
import org.slf4j.LoggerFactory;
/**
* Offers utils to test the log results produced by the code being tested.
*
* @author Glenn Renfro
*/
public class LoggerTestUtils {
/**
* Creates a mock {@link Appender} to be added to the root logger.
* @return reference to the mock appender.
*/
public static Appender getMockAppender(){
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
final Appender mockAppender = mock(Appender.class);
when(mockAppender.getName()).thenReturn("MOCK");
root.addAppender(mockAppender);
return mockAppender;
}
/**
* Verifies that the log sample is contained within the content that was written
* to the mock appender.
* @param mockAppender The appender that is associated with the test.
* @param logSample The string to search for in the log entry.
*/
public static void verifyLogEntryExists(Appender mockAppender, final String logSample){
verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {
@Override
public boolean matches(final Object argument) {
return ((LoggingEvent)argument).getFormattedMessage().contains(logSample);
}
}));
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2015 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.util;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.task.annotation.Task;
import org.springframework.stereotype.Component;
/**
* Basic {@link CommandLineRunner} implementation, with task annotation.
* @author Glenn Renfro
*/
@Task
@Component
public class TaskBasic implements CommandLineRunner{
@Override
public void run(String... args) {
//noop
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2015 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.util;
import org.aspectj.lang.JoinPoint;
import org.springframework.cloud.task.configuration.TaskHandler;
import org.springframework.cloud.task.repository.support.LoggerTaskRepository;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Initializes the beans needed to test default task behavior.
*
* @author Glenn Renfro
*/
@Configuration
public class TestDefaultConfiguration {
@Bean
public TaskRepository taskRepository(){
return new LoggerTaskRepository();
}
@Bean
public TaskHandler taskHandler(){
return new TaskHandler();
}
@Bean
public JoinPoint joinPoint(){
return new TestJoinPoint();
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2015 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.util;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.SourceLocation;
/**
* Stubbed out join point for testing purposes.
*
* @author Glenn Renfro
*/
public class TestJoinPoint implements JoinPoint {
public String toShortString() {
return null;
}
public String toLongString() {
return null;
}
public Object getThis() {
return null;
}
public Object getTarget() {
return new TaskBasic();
}
public Object[] getArgs() {
return new Object[0];
}
public Signature getSignature() {
return null;
}
public SourceLocation getSourceLocation() {
return null;
}
public String getKind() {
return null;
}
public StaticPart getStaticPart() {
return null;
}
}