From ead2de59b3b3b4879307199b9c42df741800d4c7 Mon Sep 17 00:00:00 2001 From: Justin Cooke Date: Fri, 24 Feb 2017 14:09:20 +0000 Subject: [PATCH] Add maven project to lintchecker config --- .../gov/justice/raml/io/FileTreeScanner.java | 28 +++++----- lint-checker-core/pom.xml | 19 +++++++ .../lintchecker/LintCheckConfiguration.java | 21 +++++++ .../lintchecker/LintCheckPluginException.java | 12 ++++ .../LintCheckRuleFailedException.java | 12 ++++ .../lintchecker/LintCheckerException.java | 9 ++- .../{ => rules}/LintCheckRule.java | 5 +- .../LintCheckConfigurationTest.java | 33 +++++++++++ .../LintCheckPluginExceptionTest.java | 13 +++++ .../LintCheckRuleFailedExceptionTest.java | 14 +++++ raml-maven-plugin-it/pom.xml | 42 ++++++++++++-- raml-maven-plugin/pom.xml | 2 +- .../LintCheckGoalProcessor.java | 20 +++---- .../processor/LintCheckerGoalConfig.java | 56 +++++++++++++++++++ .../{ => rules}/LintCheckMojo.java | 27 ++++----- .../LintCheckGoalProcessorTest.java | 32 +++++++++-- .../rules/FailingLintCheckRule.java | 4 +- .../{ => rules}/LintCheckMojoTest.java | 18 +++--- .../rules/SucceedingLintCheckRule.java | 6 +- .../resources/lint-check/exception/pom.xml | 29 ++++++++++ .../src/raml/an-action-command-api.raml | 0 .../src/raml/json/an-action.json | 0 .../raml/json/schema/example.an-action.json | 0 .../lint-check/{ => execute}/pom.xml | 1 - .../src/raml/an-action-command-api.raml | 22 ++++++++ .../execute/src/raml/json/an-action.json | 18 ++++++ .../raml/json/schema/example.an-action.json | 45 +++++++++++++++ 27 files changed, 424 insertions(+), 64 deletions(-) create mode 100644 lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckPluginException.java create mode 100644 lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRuleFailedException.java rename lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/{ => rules}/LintCheckRule.java (73%) create mode 100644 lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckConfigurationTest.java create mode 100644 lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckPluginExceptionTest.java create mode 100644 lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRuleFailedExceptionTest.java rename raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/{ => processor}/LintCheckGoalProcessor.java (66%) create mode 100644 raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckerGoalConfig.java rename raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/{ => rules}/LintCheckMojo.java (51%) rename raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/{ => processor}/LintCheckGoalProcessorTest.java (74%) rename raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/{ => rules}/LintCheckMojoTest.java (76%) create mode 100644 raml-maven-plugin/src/test/resources/lint-check/exception/pom.xml rename raml-maven-plugin/src/test/resources/lint-check/{ => exception}/src/raml/an-action-command-api.raml (100%) rename raml-maven-plugin/src/test/resources/lint-check/{ => exception}/src/raml/json/an-action.json (100%) rename raml-maven-plugin/src/test/resources/lint-check/{ => exception}/src/raml/json/schema/example.an-action.json (100%) rename raml-maven-plugin/src/test/resources/lint-check/{ => execute}/pom.xml (90%) create mode 100644 raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/an-action-command-api.raml create mode 100644 raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/json/an-action.json create mode 100644 raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/json/schema/example.an-action.json diff --git a/io-utils/src/main/java/uk/gov/justice/raml/io/FileTreeScanner.java b/io-utils/src/main/java/uk/gov/justice/raml/io/FileTreeScanner.java index b9b526b..489e3f6 100644 --- a/io-utils/src/main/java/uk/gov/justice/raml/io/FileTreeScanner.java +++ b/io-utils/src/main/java/uk/gov/justice/raml/io/FileTreeScanner.java @@ -1,10 +1,12 @@ package uk.gov.justice.raml.io; -import com.google.common.base.Predicate; -import org.reflections.Reflections; -import org.reflections.scanners.ResourcesScanner; -import org.reflections.util.ClasspathHelper; -import org.reflections.util.ConfigurationBuilder; +import static com.google.common.base.Predicates.and; +import static com.google.common.base.Predicates.not; +import static com.google.common.base.Predicates.or; +import static java.util.Arrays.asList; +import static java.util.Arrays.stream; +import static java.util.Collections.emptyList; +import static java.util.stream.Collectors.toList; import java.io.IOException; import java.net.MalformedURLException; @@ -16,13 +18,11 @@ import java.util.Set; import java.util.regex.Pattern; -import static com.google.common.base.Predicates.and; -import static com.google.common.base.Predicates.not; -import static com.google.common.base.Predicates.or; -import static java.util.Arrays.asList; -import static java.util.Arrays.stream; -import static java.util.Collections.emptyList; -import static java.util.stream.Collectors.toList; +import com.google.common.base.Predicate; +import org.reflections.Reflections; +import org.reflections.scanners.ResourcesScanner; +import org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; /** * Utility class for searching directories. @@ -45,12 +45,14 @@ public Collection find(final Path baseDir, final String[] includes, final if (!shouldSearchOnClasspath(baseDir) && !baseDir.toFile().exists()) { return emptyList(); } - Reflections reflections = new Reflections( + + final Reflections reflections = new Reflections( new ConfigurationBuilder() .filterInputsBy(filterOf(includes, excludes)) .setUrls(urlsToScan(baseDir)) .setScanners(new ResourcesScanner())); Set resources = reflections.getResources(Pattern.compile(".*")); + return resources.stream().map(Paths::get).collect(toList()); } diff --git a/lint-checker-core/pom.xml b/lint-checker-core/pom.xml index 36b78a2..2834eb8 100644 --- a/lint-checker-core/pom.xml +++ b/lint-checker-core/pom.xml @@ -17,6 +17,25 @@ raml-parser ${project.version} + + org.apache.maven + maven-core + + + junit + junit + test + + + org.mockito + mockito-core + test + + + org.hamcrest + hamcrest-all + test + diff --git a/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckConfiguration.java b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckConfiguration.java index 309dbd8..807503d 100644 --- a/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckConfiguration.java +++ b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckConfiguration.java @@ -1,9 +1,30 @@ package uk.gov.justice.raml.maven.lintchecker; +import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckRule; + +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; + /** * Contains any configuration needed by any of the {@link LintCheckRule}s. Currently this class * contains nothing, but to future proof the {@link LintCheckRule} interface we pass this class * in the execute() method. Add any configuration your rule may need here. */ public class LintCheckConfiguration { + + private final MavenProject mavenProject; + private final Log log; + + public LintCheckConfiguration(final MavenProject mavenProject, final Log log) { + this.mavenProject = mavenProject; + this.log = log; + } + + public MavenProject getMavenProject() { + return mavenProject; + } + + public Log getLog() { + return log; + } } diff --git a/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckPluginException.java b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckPluginException.java new file mode 100644 index 0000000..fb122fe --- /dev/null +++ b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckPluginException.java @@ -0,0 +1,12 @@ +package uk.gov.justice.raml.maven.lintchecker; + +public class LintCheckPluginException extends LintCheckerException { + + public LintCheckPluginException(final String message) { + super(message); + } + + public LintCheckPluginException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRuleFailedException.java b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRuleFailedException.java new file mode 100644 index 0000000..979aac7 --- /dev/null +++ b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRuleFailedException.java @@ -0,0 +1,12 @@ +package uk.gov.justice.raml.maven.lintchecker; + +public class LintCheckRuleFailedException extends LintCheckerException { + + public LintCheckRuleFailedException(final String message) { + super(message); + } + + public LintCheckRuleFailedException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckerException.java b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckerException.java index e53a1ac..e0b797d 100644 --- a/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckerException.java +++ b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckerException.java @@ -1,11 +1,18 @@ package uk.gov.justice.raml.maven.lintchecker; +import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckRule; + /** * Exception that is thrown if any of the {@link LintCheckRule}s fail. */ -public class LintCheckerException extends Exception { +public abstract class LintCheckerException extends Exception { public LintCheckerException(final String message) { super(message); } + + public LintCheckerException(final String message, final Throwable cause) { + super(message, cause); + } + } diff --git a/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRule.java b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/rules/LintCheckRule.java similarity index 73% rename from lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRule.java rename to lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/rules/LintCheckRule.java index 2c8d2e5..5214e55 100644 --- a/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRule.java +++ b/lint-checker-core/src/main/java/uk/gov/justice/raml/maven/lintchecker/rules/LintCheckRule.java @@ -1,4 +1,7 @@ -package uk.gov.justice.raml.maven.lintchecker; +package uk.gov.justice.raml.maven.lintchecker.rules; + +import uk.gov.justice.raml.maven.lintchecker.LintCheckConfiguration; +import uk.gov.justice.raml.maven.lintchecker.LintCheckerException; import org.raml.model.Raml; diff --git a/lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckConfigurationTest.java b/lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckConfigurationTest.java new file mode 100644 index 0000000..8151047 --- /dev/null +++ b/lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckConfigurationTest.java @@ -0,0 +1,33 @@ +package uk.gov.justice.raml.maven.lintchecker; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class LintCheckConfigurationTest { + + @Mock + private MavenProject mavenProject; + + @Mock + private Log log; + + @Test + public void shouldCreateConfiguration() { + + final LintCheckConfiguration lintCheckConfiguration = new LintCheckConfiguration(mavenProject, log); + + assertThat(lintCheckConfiguration.getLog(), is(log)); + assertThat(lintCheckConfiguration.getMavenProject(), is(mavenProject)); + + + } + +} \ No newline at end of file diff --git a/lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckPluginExceptionTest.java b/lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckPluginExceptionTest.java new file mode 100644 index 0000000..0404152 --- /dev/null +++ b/lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckPluginExceptionTest.java @@ -0,0 +1,13 @@ +package uk.gov.justice.raml.maven.lintchecker; + +import org.junit.Test; + +public class LintCheckPluginExceptionTest { + + @Test + public void shouldCreateException() { + final LintCheckPluginException exception = new LintCheckPluginException("test"); + new LintCheckPluginException("test", exception); + } + +} \ No newline at end of file diff --git a/lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRuleFailedExceptionTest.java b/lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRuleFailedExceptionTest.java new file mode 100644 index 0000000..7b3ae18 --- /dev/null +++ b/lint-checker-core/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckRuleFailedExceptionTest.java @@ -0,0 +1,14 @@ +package uk.gov.justice.raml.maven.lintchecker; + +import org.junit.Test; + +public class LintCheckRuleFailedExceptionTest { + + + @Test + public void shouldCreateException() { + final LintCheckerException exception = new LintCheckRuleFailedException("test"); + new LintCheckRuleFailedException("test", exception); + } + +} \ No newline at end of file diff --git a/raml-maven-plugin-it/pom.xml b/raml-maven-plugin-it/pom.xml index 4ec4fa8..ac16111 100644 --- a/raml-maven-plugin-it/pom.xml +++ b/raml-maven-plugin-it/pom.xml @@ -3,16 +3,48 @@ xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + uk.gov.justice.maven + raml-maven + 1.5.0-SNAPSHOT + + 4.0.0 - uk.gov.justice.maven raml-maven-plugin-it - 1.4.0-SNAPSHOT - - - + + + maven-enforcer-plugin + + + enforce-rules + none + + + enforce-rules-local + + enforce + + + + + + ${enforcer.java.version.range} + + + ${enforcer.maven.version.range} + + + + + + + org.apache.maven.plugins maven-compiler-plugin diff --git a/raml-maven-plugin/pom.xml b/raml-maven-plugin/pom.xml index ba1a66c..6d2b45d 100644 --- a/raml-maven-plugin/pom.xml +++ b/raml-maven-plugin/pom.xml @@ -21,7 +21,7 @@ org.apache.maven.plugins maven-plugin-plugin - 3.4 + 3.5 false raml diff --git a/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckGoalProcessor.java b/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckGoalProcessor.java similarity index 66% rename from raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckGoalProcessor.java rename to raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckGoalProcessor.java index c6ebec2..5f8a974 100644 --- a/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckGoalProcessor.java +++ b/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckGoalProcessor.java @@ -1,7 +1,10 @@ -package uk.gov.justice.raml.maven.lintchecker; +package uk.gov.justice.raml.maven.lintchecker.processor; import uk.gov.justice.raml.io.FileTreeScanner; import uk.gov.justice.raml.io.files.parser.RamlFileParser; +import uk.gov.justice.raml.maven.lintchecker.LintCheckConfiguration; +import uk.gov.justice.raml.maven.lintchecker.LintCheckerException; +import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckRule; import java.io.File; import java.io.IOException; @@ -24,19 +27,16 @@ public LintCheckGoalProcessor( this.fileTreeScanner = fileTreeScanner; } - public void execute( - final File sourceDirectory, - final List rules, - final List includes, - final List excludes) throws MojoExecutionException { + public void execute(final LintCheckerGoalConfig lintCheckerGoalConfig) throws MojoExecutionException { - final Collection paths = getPaths(sourceDirectory, includes, excludes); - final Collection ramls = ramlFileParser.ramlOf(sourceDirectory.toPath(), paths); + final Collection paths = getPaths(lintCheckerGoalConfig.getSourceDirectory(), lintCheckerGoalConfig.getIncludes(), lintCheckerGoalConfig.getExcludes()); + final Collection ramls = ramlFileParser.ramlOf(lintCheckerGoalConfig.getSourceDirectory().toPath(), paths); for (final Raml raml : ramls) { - for (final LintCheckRule rule : rules) { + for (final LintCheckRule rule : lintCheckerGoalConfig.getRules()) { try { - rule.execute(raml, new LintCheckConfiguration()); + rule.execute(raml, new LintCheckConfiguration(lintCheckerGoalConfig.getCurrentProject(), + lintCheckerGoalConfig.getLog())); } catch (final LintCheckerException e) { throw new MojoExecutionException("Lint checker rule failed for rule " + rule.getClass().getSimpleName(), e); } diff --git a/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckerGoalConfig.java b/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckerGoalConfig.java new file mode 100644 index 0000000..3d49141 --- /dev/null +++ b/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckerGoalConfig.java @@ -0,0 +1,56 @@ +package uk.gov.justice.raml.maven.lintchecker.processor; + +import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckRule; + +import java.io.File; +import java.util.List; + +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; + +public class LintCheckerGoalConfig { + private final File sourceDirectory; + private final List rules; + private final List includes; + private final List excludes; + private final MavenProject currentProject; + private final Log log; + + public LintCheckerGoalConfig(final File sourceDirectory, + final List rules, + final List includes, + final List excludes, + final MavenProject currentProject, + final Log log) { + this.sourceDirectory = sourceDirectory; + this.rules = rules; + this.includes = includes; + this.excludes = excludes; + this.currentProject = currentProject; + this.log = log; + } + + public File getSourceDirectory() { + return sourceDirectory; + } + + public List getRules() { + return rules; + } + + public List getIncludes() { + return includes; + } + + public List getExcludes() { + return excludes; + } + + public MavenProject getCurrentProject() { + return currentProject; + } + + public Log getLog() { + return log; + } +} diff --git a/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckMojo.java b/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/rules/LintCheckMojo.java similarity index 51% rename from raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckMojo.java rename to raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/rules/LintCheckMojo.java index 4f834cf..fa2dc5a 100644 --- a/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/LintCheckMojo.java +++ b/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/lintchecker/rules/LintCheckMojo.java @@ -1,42 +1,39 @@ -package uk.gov.justice.raml.maven.lintchecker; +package uk.gov.justice.raml.maven.lintchecker.rules; import uk.gov.justice.raml.io.FileTreeScanner; import uk.gov.justice.raml.io.files.parser.RamlFileParser; import uk.gov.justice.raml.maven.common.BasicMojo; +import uk.gov.justice.raml.maven.lintchecker.processor.LintCheckGoalProcessor; +import uk.gov.justice.raml.maven.lintchecker.processor.LintCheckerGoalConfig; import java.util.List; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; - -@Mojo(name = "lint-check") +@Mojo(name = "raml-lint-check", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.GENERATE_SOURCES) public class LintCheckMojo extends BasicMojo { - private final RamlFileParser ramlFileParser = new RamlFileParser(); - private final FileTreeScanner fileTreeScanner = new FileTreeScanner(); - /** * Array of objects that implement the LintCheckerRule interface to execute. */ @SuppressWarnings("unused") @Parameter(required = true) - protected List rules; - + private List rules; @Override public void execute() throws MojoExecutionException { + configureDefaultFileIncludes(); + final LintCheckGoalProcessor lintCheckGoalProcessor = new LintCheckGoalProcessor( - ramlFileParser, - fileTreeScanner); + new RamlFileParser(), + new FileTreeScanner()); lintCheckGoalProcessor.execute( - sourceDirectory, - rules, - includes, - excludes - ); + new LintCheckerGoalConfig(sourceDirectory, rules, includes, excludes, project, getLog())); } } diff --git a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckGoalProcessorTest.java b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckGoalProcessorTest.java similarity index 74% rename from raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckGoalProcessorTest.java rename to raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckGoalProcessorTest.java index 92a03f2..05c50b6 100644 --- a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckGoalProcessorTest.java +++ b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/processor/LintCheckGoalProcessorTest.java @@ -1,4 +1,4 @@ -package uk.gov.justice.raml.maven.lintchecker; +package uk.gov.justice.raml.maven.lintchecker.processor; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; @@ -14,6 +14,10 @@ import uk.gov.justice.raml.io.FileTreeScanner; import uk.gov.justice.raml.io.files.parser.RamlFileParser; +import uk.gov.justice.raml.maven.lintchecker.LintCheckConfiguration; +import uk.gov.justice.raml.maven.lintchecker.LintCheckRuleFailedException; +import uk.gov.justice.raml.maven.lintchecker.LintCheckerException; +import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckRule; import java.io.File; import java.nio.file.Path; @@ -21,10 +25,13 @@ import java.util.List; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; import org.raml.model.Raml; @@ -59,7 +66,14 @@ public void shouldExecuteAllRulesOnAllRamls() throws Exception { when(fileTreeScanner.find(sourceDirectory.toPath(), includes, excludes)).thenReturn(paths); when(ramlFileParser.ramlOf(sourceDirectory.toPath(), paths)).thenReturn(ramls); - lintCheckGoalProcessor.execute(sourceDirectory, rules, asList(includes), asList(excludes)); + lintCheckGoalProcessor.execute( + new LintCheckerGoalConfig( + sourceDirectory, + rules, + asList(includes), + asList(excludes), + mock(MavenProject.class), + mock(Log.class))); verify(lintCheckRule_1).execute(eq(raml_1), any(LintCheckConfiguration.class)); verify(lintCheckRule_1).execute(eq(raml_2), any(LintCheckConfiguration.class)); @@ -70,7 +84,7 @@ public void shouldExecuteAllRulesOnAllRamls() throws Exception { @Test public void shouldThrowMojoExecutionExceptionAIfExecutingARuleFails() throws Exception { - final LintCheckerException lintCheckerException = new LintCheckerException("Ooops"); + final LintCheckerException lintCheckerException = new LintCheckRuleFailedException("Ooops"); final LintCheckRule lintCheckRule_1 = mock(LintCheckRule.class); final LintCheckRule lintCheckRule_2 = mock(LintCheckRule.class); @@ -91,7 +105,17 @@ public void shouldThrowMojoExecutionExceptionAIfExecutingARuleFails() throws Exc doThrow(lintCheckerException).when(lintCheckRule_1).execute(eq(raml_1), any(LintCheckConfiguration.class)); try { - lintCheckGoalProcessor.execute(sourceDirectory, rules, asList(includes), asList(excludes)); + lintCheckGoalProcessor + .execute + ( + new LintCheckerGoalConfig(sourceDirectory, + rules, + asList(includes), + asList(excludes), + mock(MavenProject.class), + mock(Log.class) + ) + ); fail(); } catch (final MojoExecutionException expected) { assertThat(expected.getCause(), is(lintCheckerException)); diff --git a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/FailingLintCheckRule.java b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/FailingLintCheckRule.java index 0ba341c..3e37924 100644 --- a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/FailingLintCheckRule.java +++ b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/FailingLintCheckRule.java @@ -1,7 +1,7 @@ package uk.gov.justice.raml.maven.lintchecker.rules; import uk.gov.justice.raml.maven.lintchecker.LintCheckConfiguration; -import uk.gov.justice.raml.maven.lintchecker.LintCheckRule; +import uk.gov.justice.raml.maven.lintchecker.LintCheckRuleFailedException; import uk.gov.justice.raml.maven.lintchecker.LintCheckerException; import org.raml.model.Raml; @@ -10,6 +10,6 @@ public class FailingLintCheckRule implements LintCheckRule { @Override public void execute(final Raml raml, final LintCheckConfiguration lintCheckConfiguration) throws LintCheckerException { - throw new LintCheckerException("Goodness gracious me"); + throw new LintCheckRuleFailedException("Goodness gracious me"); } } diff --git a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckMojoTest.java b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/LintCheckMojoTest.java similarity index 76% rename from raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckMojoTest.java rename to raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/LintCheckMojoTest.java index aa1352d..1e3ecb7 100644 --- a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/LintCheckMojoTest.java +++ b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/LintCheckMojoTest.java @@ -1,10 +1,12 @@ -package uk.gov.justice.raml.maven.lintchecker; +package uk.gov.justice.raml.maven.lintchecker.rules; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import uk.gov.justice.raml.maven.lintchecker.LintCheckerException; import uk.gov.justice.raml.maven.lintchecker.rules.FailingLintCheckRule; +import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckMojo; import uk.gov.justice.raml.maven.lintchecker.rules.SucceedingLintCheckRule; import uk.gov.justice.raml.maven.test.utils.BetterAbstractMojoTestCase; @@ -17,18 +19,16 @@ public class LintCheckMojoTest extends BetterAbstractMojoTestCase { public void testFindAllMojosUnderTheSourceDirectory() throws Exception { - final File pom = getTestFile("src/test/resources/lint-check/pom.xml"); - final LintCheckMojo mojo = (LintCheckMojo) lookupConfiguredMojo(pom, "lint-check"); - - assertThat(mojo.rules.size(), is(2)); - assertThat(mojo.rules.get(0), is(instanceOf(SucceedingLintCheckRule.class))); - assertThat(mojo.rules.get(1), is(instanceOf(FailingLintCheckRule.class))); + final File pom = getTestFile("src/test/resources/lint-check/execute/pom.xml"); + final LintCheckMojo mojo = (LintCheckMojo) lookupConfiguredMojo(pom, "raml-lint-check"); + mojo.execute(); + assertThat(true, is(new SucceedingLintCheckRule().tripped)); } public void testRunAllRulesAndFailIfTheRuleFails() throws Exception { - final File pom = getTestFile("src/test/resources/lint-check/pom.xml"); - final LintCheckMojo mojo = (LintCheckMojo) lookupConfiguredMojo(pom, "lint-check"); + final File pom = getTestFile("src/test/resources/lint-check/exception/pom.xml"); + final LintCheckMojo mojo = (LintCheckMojo) lookupConfiguredMojo(pom, "raml-lint-check"); try { mojo.execute(); diff --git a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/SucceedingLintCheckRule.java b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/SucceedingLintCheckRule.java index 4248df0..823f80c 100644 --- a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/SucceedingLintCheckRule.java +++ b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/lintchecker/rules/SucceedingLintCheckRule.java @@ -1,15 +1,17 @@ package uk.gov.justice.raml.maven.lintchecker.rules; import uk.gov.justice.raml.maven.lintchecker.LintCheckConfiguration; -import uk.gov.justice.raml.maven.lintchecker.LintCheckRule; import uk.gov.justice.raml.maven.lintchecker.LintCheckerException; import org.raml.model.Raml; public class SucceedingLintCheckRule implements LintCheckRule { + + public static boolean tripped; + @Override public void execute(final Raml raml, final LintCheckConfiguration lintCheckConfiguration) throws LintCheckerException { - // cool + tripped = true; } } diff --git a/raml-maven-plugin/src/test/resources/lint-check/exception/pom.xml b/raml-maven-plugin/src/test/resources/lint-check/exception/pom.xml new file mode 100644 index 0000000..0ba9b9a --- /dev/null +++ b/raml-maven-plugin/src/test/resources/lint-check/exception/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + + uk.gov.justice.raml.test + project-to-test + 1.0.0-SNAPSHOT + + + + raml-maven-plugin + uk.gov.justice.maven + @project.version@ + + ${basedir}/src/raml + + **/*.raml + + + + + + + + + + + diff --git a/raml-maven-plugin/src/test/resources/lint-check/src/raml/an-action-command-api.raml b/raml-maven-plugin/src/test/resources/lint-check/exception/src/raml/an-action-command-api.raml similarity index 100% rename from raml-maven-plugin/src/test/resources/lint-check/src/raml/an-action-command-api.raml rename to raml-maven-plugin/src/test/resources/lint-check/exception/src/raml/an-action-command-api.raml diff --git a/raml-maven-plugin/src/test/resources/lint-check/src/raml/json/an-action.json b/raml-maven-plugin/src/test/resources/lint-check/exception/src/raml/json/an-action.json similarity index 100% rename from raml-maven-plugin/src/test/resources/lint-check/src/raml/json/an-action.json rename to raml-maven-plugin/src/test/resources/lint-check/exception/src/raml/json/an-action.json diff --git a/raml-maven-plugin/src/test/resources/lint-check/src/raml/json/schema/example.an-action.json b/raml-maven-plugin/src/test/resources/lint-check/exception/src/raml/json/schema/example.an-action.json similarity index 100% rename from raml-maven-plugin/src/test/resources/lint-check/src/raml/json/schema/example.an-action.json rename to raml-maven-plugin/src/test/resources/lint-check/exception/src/raml/json/schema/example.an-action.json diff --git a/raml-maven-plugin/src/test/resources/lint-check/pom.xml b/raml-maven-plugin/src/test/resources/lint-check/execute/pom.xml similarity index 90% rename from raml-maven-plugin/src/test/resources/lint-check/pom.xml rename to raml-maven-plugin/src/test/resources/lint-check/execute/pom.xml index 31372f1..5d2190c 100644 --- a/raml-maven-plugin/src/test/resources/lint-check/pom.xml +++ b/raml-maven-plugin/src/test/resources/lint-check/execute/pom.xml @@ -21,7 +21,6 @@ - diff --git a/raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/an-action-command-api.raml b/raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/an-action-command-api.raml new file mode 100644 index 0000000..afcf9ff --- /dev/null +++ b/raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/an-action-command-api.raml @@ -0,0 +1,22 @@ +#%RAML 0.8 +title: An Action Command API +baseUri: http://localhost:8080/example-command-api/command/api/rest/an-action +version: v0.1 +mediaType: application/json +protocols: [ HTTP ] + +/doStuff: + description: | + stub file for testing that raml files are found correctly + post: + description: | + ... + (mapping): + requestType: application/vnd.example.an-action+json + name: example.an-action + ... + + body: + application/vnd.example.an-action+json: + schema: !include json/schema/example.an-action.json + example: !include json/an-action.json diff --git a/raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/json/an-action.json b/raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/json/an-action.json new file mode 100644 index 0000000..cda61a9 --- /dev/null +++ b/raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/json/an-action.json @@ -0,0 +1,18 @@ +{ + "name": "Eton Mess", + "glutenFree": false, + "ingredients": [ + { + "name": "custard", + "quantity": 2 + }, + { + "name": "egg", + "quantity": 6 + }, + { + "name": "sugar", + "quantity": 500 + } + ] +} diff --git a/raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/json/schema/example.an-action.json b/raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/json/schema/example.an-action.json new file mode 100644 index 0000000..6cf6e1d --- /dev/null +++ b/raml-maven-plugin/src/test/resources/lint-check/execute/src/raml/json/schema/example.an-action.json @@ -0,0 +1,45 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "", + "type": "object", + "properties": { + "name": { + "id": "/name", + "type": "string", + "description": "Uniquely identifies the cake to be produced from the recipe", + "name": "Name of Cake", + "title": "Name of Cake" + }, + "glutenFree": { + "id": "/glutenFree", + "type": "boolean" + }, + "ingredients": { + "id": "/ingredients", + "type": "array", + "items": [ + { + "id": "/ingredients/0", + "type": "object", + "properties": { + "name": { + "id": "/ingredients/0/name", + "type": "string" + }, + "quantity": { + "id": "/ingredients/0/quantity", + "type": "integer" + } + } + } + ], + "minItems": 1, + "description": "List ingredients and quantities for recipe" + } + }, + "required": [ + "name", + "ingredients", + "glutenFree" + ] +}