This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add raml and json schema validation mojo
- Loading branch information
1 parent
20bccc5
commit 9e4f33c
Showing
43 changed files
with
929 additions
and
65 deletions.
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
37 changes: 37 additions & 0 deletions
37
raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/common/BasicGoalConfig.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,37 @@ | ||
package uk.gov.justice.raml.maven.common; | ||
|
||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
/** | ||
* POJO to hold config for maven plugin goal. | ||
*/ | ||
|
||
public class BasicGoalConfig { | ||
|
||
private final Path sourceDirectory; | ||
|
||
private final List<String> includes; | ||
|
||
private final List<String> excludes; | ||
|
||
public BasicGoalConfig(final Path sourceDirectory, | ||
final List<String> includes, | ||
final List<String> excludes) { | ||
this.sourceDirectory = sourceDirectory; | ||
this.includes = includes; | ||
this.excludes = excludes; | ||
} | ||
|
||
public Path getSourceDirectory() { | ||
return sourceDirectory; | ||
} | ||
|
||
public List<String> getIncludes() { | ||
return includes; | ||
} | ||
|
||
public List<String> getExcludes() { | ||
return excludes; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/common/BasicMojo.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,34 @@ | ||
package uk.gov.justice.raml.maven.common; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.project.MavenProject; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
public abstract class BasicMojo extends AbstractMojo { | ||
private static final String DEFAULT_INCLUDE = "**/*.raml"; | ||
|
||
/** | ||
* Directory location of the RAML file(s). | ||
*/ | ||
@Parameter(property = "sourceDirectory", defaultValue = "CLASSPATH") | ||
protected File sourceDirectory; | ||
|
||
@Parameter(property = "includes.include") | ||
protected List<String> includes; | ||
|
||
@Parameter(property = "excludes.exclude") | ||
protected List<String> excludes; | ||
|
||
@Parameter(defaultValue = "${project}") | ||
protected MavenProject project; | ||
|
||
protected void configureDefaultFileIncludes() { | ||
if (includes.isEmpty()) { | ||
includes = ImmutableList.of(DEFAULT_INCLUDE); | ||
} | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...ml/jaxrs/maven/GenerateGoalProcessor.java → ...aven/generator/GenerateGoalProcessor.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
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
2 changes: 1 addition & 1 deletion
2
...ce/raml/jaxrs/maven/GeneratorFactory.java → ...aml/maven/generator/GeneratorFactory.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
84 changes: 84 additions & 0 deletions
84
raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/validator/RamlSyntaxCheckMojo.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,84 @@ | ||
package uk.gov.justice.raml.maven.validator; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.raml.parser.rule.ValidationResult; | ||
import uk.gov.justice.raml.io.FileTreeScannerFactory; | ||
import uk.gov.justice.raml.maven.common.BasicGoalConfig; | ||
import uk.gov.justice.raml.maven.common.BasicMojo; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
@Mojo(name = "check-syntax") | ||
public class RamlSyntaxCheckMojo extends BasicMojo { | ||
|
||
RamlSyntaxValidator ramlSyntaxValidator = new RamlSyntaxValidator(new FileTreeScannerFactory()); | ||
|
||
@Override | ||
public void execute() throws MojoExecutionException, MojoFailureException { | ||
|
||
configureDefaultFileIncludes(); | ||
|
||
report(ramlValidationResults()); | ||
} | ||
|
||
private void report(final Map<Path, ValidationResult> results) throws MojoFailureException { | ||
if (results.isEmpty()) { | ||
getLog().info("Raml and Json Schema Validation Complete"); | ||
} else { | ||
String validationResultsPrint = printResults(results, sourceDirectory); | ||
|
||
getLog().debug(validationResultsPrint); | ||
|
||
throw new MojoFailureException(results, "Raml & Json schema validation has failed", validationResultsPrint); | ||
} | ||
} | ||
|
||
private Map<Path, ValidationResult> ramlValidationResults() throws MojoExecutionException { | ||
try { | ||
return ramlSyntaxValidator.validateRamls(configuration()); | ||
} catch (IOException e) { | ||
throw new MojoExecutionException("Files not loaded", e); | ||
} | ||
} | ||
|
||
private String printResults(final Map<Path, ValidationResult> results, final File sourceDirectory) { | ||
|
||
final StringBuilder sb = new StringBuilder(); | ||
|
||
sb.append("\nThere are "); | ||
sb.append(results.size()); | ||
sb.append(" validation errors: \n"); | ||
|
||
results.entrySet().stream() | ||
.forEach(resultEntry -> { | ||
sb.append("\nSyntax check has failed for "); | ||
sb.append(sourceDirectory); | ||
sb.append(resultEntry.getKey()); | ||
sb.append("\nError Level : "); | ||
sb.append(resultEntry.getValue().getLevel()); | ||
sb.append(" Line : "); | ||
sb.append(resultEntry.getValue().getLine()); | ||
sb.append(" Column : "); | ||
sb.append(resultEntry.getValue().getStartColumn()); | ||
sb.append("\n"); | ||
sb.append(resultEntry.getValue().getMessage()); | ||
sb.append("\n"); | ||
}); | ||
return sb.toString(); | ||
|
||
} | ||
|
||
private BasicGoalConfig configuration() { | ||
|
||
return new BasicGoalConfig(sourceDirectory.toPath(), | ||
includes, | ||
excludes); | ||
|
||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/validator/RamlSyntaxValidator.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,51 @@ | ||
package uk.gov.justice.raml.maven.validator; | ||
|
||
import org.raml.parser.loader.FileResourceLoader; | ||
import org.raml.parser.rule.ValidationResult; | ||
import org.raml.parser.visitor.RamlValidationService; | ||
import uk.gov.justice.raml.io.FileTreeScannerFactory; | ||
import uk.gov.justice.raml.maven.common.BasicGoalConfig; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RamlSyntaxValidator { | ||
|
||
private final FileTreeScannerFactory scannerFactory; | ||
|
||
public RamlSyntaxValidator(final FileTreeScannerFactory scannerFactory) { | ||
|
||
this.scannerFactory = scannerFactory; | ||
|
||
} | ||
|
||
public Map<Path, ValidationResult> validateRamls(final BasicGoalConfig config) throws IOException { | ||
|
||
final String[] includes = config.getIncludes().toArray(new String[config.getIncludes().size()]); | ||
final String[] excludes = config.getExcludes().toArray(new String[config.getExcludes().size()]); | ||
|
||
final Collection<Path> paths = scannerFactory.create().find(config.getSourceDirectory(), includes, excludes); | ||
|
||
Map<Path, ValidationResult> results = new HashMap<>(); | ||
for (Path path : paths) { | ||
List<ValidationResult> tmpResults = validationResults(path, config); | ||
for (ValidationResult result : tmpResults) { | ||
results.put(path, result); | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
private List<ValidationResult> validationResults(final Path path, final BasicGoalConfig config) { | ||
|
||
return RamlValidationService.createDefault( | ||
new FileResourceLoader(config.getSourceDirectory().toFile())).validate(path.toFile().getName()); | ||
|
||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...xrs/maven/BetterAbstractMojoTestCase.java → ...generator/BetterAbstractMojoTestCase.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
2 changes: 1 addition & 1 deletion
2
...tice/raml/jaxrs/maven/DummyGenerator.java → .../raml/maven/generator/DummyGenerator.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
2 changes: 1 addition & 1 deletion
2
...aml/jaxrs/maven/DummyGeneratorCaptor.java → ...maven/generator/DummyGeneratorCaptor.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
2 changes: 1 addition & 1 deletion
2
...axrs/maven/GenerateGoalProcessorTest.java → .../generator/GenerateGoalProcessorTest.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
Oops, something went wrong.