Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Add raml and json schema validation mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
skiddykong authored and jaceko committed May 5, 2016
1 parent 20bccc5 commit 9e4f33c
Show file tree
Hide file tree
Showing 43 changed files with 929 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/
public class FileTreeScanner {

private static final Logger LOGGER = LoggerFactory.getLogger(FileTreeScanner.class);
private static final String CLASSPATH = "CLASSPATH";
private static final String RAML_PATTERN = "**/*.raml";

Expand Down
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;
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package uk.gov.justice.raml.jaxrs.maven;
package uk.gov.justice.raml.maven.generator;

import uk.gov.justice.raml.maven.common.BasicGoalConfig;

import java.nio.file.Path;
import java.util.List;
Expand All @@ -7,20 +9,14 @@
/**
* POJO to hold config for the generate goal.
*/
public class GenerateGoalConfig {
public class GenerateGoalConfig extends BasicGoalConfig {

private final String generatorName;

private final Path outputDirectory;

private final Path sourceDirectory;

private final String basePackageName;

private final List<String> includes;

private final List<String> excludes;

private final Map<String, String> properties;

public GenerateGoalConfig(final String generatorName,
Expand All @@ -30,12 +26,10 @@ public GenerateGoalConfig(final String generatorName,
final List<String> includes,
final List<String> excludes,
final Map<String, String> properties) {
super(sourceDirectory, includes, excludes);
this.generatorName = generatorName;
this.outputDirectory = outputDirectory;
this.sourceDirectory = sourceDirectory;
this.basePackageName = basePackageName;
this.includes = includes;
this.excludes = excludes;
this.properties = properties;
}

Expand All @@ -47,22 +41,10 @@ public Path getOutputDirectory() {
return outputDirectory;
}

public Path getSourceDirectory() {
return sourceDirectory;
}

public String getBasePackageName() {
return basePackageName;
}

public List<String> getIncludes() {
return includes;
}

public List<String> getExcludes() {
return excludes;
}

public Map<String, String> getProperties() {
return properties;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jaxrs.maven;
package uk.gov.justice.raml.maven.generator;

import uk.gov.justice.raml.core.GeneratorConfig;
import uk.gov.justice.raml.io.FileTreeScannerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,34 @@
package uk.gov.justice.raml.jaxrs.maven;
package uk.gov.justice.raml.maven.generator;

import com.google.common.collect.ImmutableList;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
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.project.MavenProject;
import uk.gov.justice.raml.io.FileTreeScannerFactory;
import uk.gov.justice.raml.maven.common.BasicMojo;

import java.io.File;
import java.util.List;
import java.util.Map;

import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE_PLUS_RUNTIME;

@Mojo(name = "generate", requiresProject = true, threadSafe = false, requiresDependencyResolution = COMPILE_PLUS_RUNTIME, defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class GenerateMojo extends AbstractMojo {
@Mojo(name = "generate", requiresDependencyResolution = COMPILE_PLUS_RUNTIME, defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class GenerateMojo extends BasicMojo {

private static final String DEFAULT_INCLUDE = "**/*.raml";

/**
* The fully qualified classname for the generator to use
*/
@Parameter(property = "generatorName", required = true)
private String generatorName;

@Parameter(defaultValue = "${project}")
private MavenProject project;

/**
* Target directory for generated Java source files.
*/
@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}/generated-sources")
private File outputDirectory;

/**
* Directory location of the RAML file(s).
*/
@Parameter(property = "sourceDirectory", defaultValue = "CLASSPATH")
private File sourceDirectory;

@Parameter(property = "includes.include")
private List<String> includes;

@Parameter(property = "excludes.exclude")
private List<String> excludes;

/**
* Base package name used for generated Java classes.
Expand All @@ -60,9 +42,7 @@ public class GenerateMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {

if (includes.isEmpty()) {
includes = ImmutableList.of(DEFAULT_INCLUDE);
}
configureDefaultFileIncludes();

project.addCompileSourceRoot(outputDirectory.getPath());
project.addTestCompileSourceRoot(outputDirectory.getPath());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jaxrs.maven;
package uk.gov.justice.raml.maven.generator;

import uk.gov.justice.raml.core.Generator;

Expand Down
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);

}

}
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());

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jaxrs.maven;
package uk.gov.justice.raml.maven.generator;

import org.apache.maven.DefaultMaven;
import org.apache.maven.Maven;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jaxrs.maven;
package uk.gov.justice.raml.maven.generator;

import org.raml.model.Raml;
import uk.gov.justice.raml.core.Generator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jaxrs.maven;
package uk.gov.justice.raml.maven.generator;

import org.apache.commons.lang3.tuple.Pair;
import org.raml.model.Raml;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jaxrs.maven;
package uk.gov.justice.raml.maven.generator;

import org.junit.Before;
import org.junit.Rule;
Expand Down
Loading

0 comments on commit 9e4f33c

Please sign in to comment.