add support for stubs with contracts for messaging (#12)
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -230,6 +230,12 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.shared</groupId>
|
||||
<artifactId>maven-filtering</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
|
||||
@@ -5,44 +5,49 @@ import io.codearte.accurest.config.AccurestConfigProperties
|
||||
import io.codearte.accurest.wiremock.DslToWireMockClientConverter
|
||||
import io.codearte.accurest.wiremock.RecursiveFilesConverter
|
||||
import org.apache.maven.execution.MavenSession
|
||||
import org.apache.maven.model.path.PathTranslator
|
||||
import org.apache.maven.model.Resource
|
||||
import org.apache.maven.plugin.AbstractMojo
|
||||
import org.apache.maven.plugin.MojoExecutionException
|
||||
import org.apache.maven.plugin.MojoFailureException
|
||||
import org.apache.maven.plugins.annotations.Component
|
||||
import org.apache.maven.plugins.annotations.LifecyclePhase
|
||||
import org.apache.maven.plugins.annotations.Mojo
|
||||
import org.apache.maven.plugins.annotations.Parameter
|
||||
|
||||
import javax.inject.Inject
|
||||
import org.apache.maven.project.MavenProject
|
||||
import org.apache.maven.shared.filtering.MavenFilteringException
|
||||
import org.apache.maven.shared.filtering.MavenResourcesExecution
|
||||
import org.apache.maven.shared.filtering.MavenResourcesFiltering
|
||||
|
||||
@Mojo(name = 'convert', requiresProject = false, defaultPhase = LifecyclePhase.PROCESS_TEST_RESOURCES)
|
||||
@CompileStatic
|
||||
class ConvertMojo extends AbstractMojo {
|
||||
|
||||
@Parameter(defaultValue = '${basedir}', readonly = true, required = true)
|
||||
private File basedir
|
||||
@Parameter(defaultValue = '${basedir}/src/test/accurest')
|
||||
private File contractsDirectory
|
||||
|
||||
@Parameter(defaultValue = '${project.build.directory}', readonly = true, required = true)
|
||||
private File projectBuildDirectory
|
||||
@Parameter(defaultValue = '${project.build.directory}/accurest')
|
||||
private File outputDirectory
|
||||
|
||||
@Parameter(property = 'contractsDirectory')
|
||||
private String contractsDirectory
|
||||
@Parameter(property = 'contractsDirectory', defaultValue = '${basedir}')
|
||||
private File source
|
||||
|
||||
@Parameter(property = 'stubsDirectory')
|
||||
private String outputDirectory
|
||||
@Parameter(property = 'stubsDirectory', defaultValue = '${basedir}')
|
||||
private File destination
|
||||
|
||||
@Parameter(property = 'accurest.skip', defaultValue = 'false')
|
||||
private boolean skip
|
||||
|
||||
@Parameter(defaultValue = 'false')
|
||||
private boolean attachContracts
|
||||
|
||||
@Parameter(defaultValue = '${session}', readonly = true)
|
||||
private MavenSession mavenSession
|
||||
|
||||
private final PathTranslator translator
|
||||
@Parameter(defaultValue = '${project}', readonly = true)
|
||||
private MavenProject project
|
||||
|
||||
@Inject
|
||||
ConvertMojo(PathTranslator translator) {
|
||||
this.translator = translator
|
||||
}
|
||||
@Component(role = MavenResourcesFiltering.class, hint = "default")
|
||||
private MavenResourcesFiltering mavenResourcesFiltering;
|
||||
|
||||
void execute() throws MojoExecutionException, MojoFailureException {
|
||||
|
||||
@@ -51,15 +56,15 @@ class ConvertMojo extends AbstractMojo {
|
||||
return
|
||||
}
|
||||
|
||||
if (attachContracts) {
|
||||
log.info("Attaching accurest contracts")
|
||||
copyContracts()
|
||||
}
|
||||
|
||||
AccurestConfigProperties config = new AccurestConfigProperties()
|
||||
|
||||
if (insideProject) {
|
||||
config.contractsDslDir = resolveFile(basedir, contractsDirectory, 'src/test/accurest')
|
||||
config.stubsOutputDir = resolveFile(projectBuildDirectory, outputDirectory, 'mappings')
|
||||
} else {
|
||||
config.contractsDslDir = resolveFile(basedir, contractsDirectory, '')
|
||||
config.stubsOutputDir = resolveFile(basedir, outputDirectory, '')
|
||||
}
|
||||
config.contractsDslDir = insideProject ? contractsDirectory : source
|
||||
config.stubsOutputDir = insideProject ? new File(outputDirectory, 'mappings') : destination
|
||||
|
||||
log.info('Converting from accurest contracts written in GroovyDSL to WireMock stubs mappings')
|
||||
log.info(" Accurest contracts directory: ${config.contractsDslDir}")
|
||||
@@ -69,16 +74,27 @@ class ConvertMojo extends AbstractMojo {
|
||||
converter.processFiles()
|
||||
}
|
||||
|
||||
private void copyContracts() {
|
||||
Resource testResource = new Resource(directory: contractsDirectory.absolutePath)
|
||||
|
||||
MavenResourcesExecution mavenResourcesExecution =
|
||||
new MavenResourcesExecution([testResource],
|
||||
new File(outputDirectory, 'contracts'), project, 'UTF-8',
|
||||
[], [], mavenSession);
|
||||
mavenResourcesExecution.injectProjectBuildFilters = false
|
||||
mavenResourcesExecution.overwrite = true
|
||||
mavenResourcesExecution.includeEmptyDirs = false
|
||||
mavenResourcesExecution.filterFilenames = false
|
||||
try {
|
||||
mavenResourcesFiltering.filterResources(mavenResourcesExecution);
|
||||
} catch (MavenFilteringException e) {
|
||||
throw new MojoExecutionException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInsideProject() {
|
||||
return mavenSession.request.projectPresent
|
||||
}
|
||||
|
||||
private File resolveFile(File baseDir, String requestedPath, String defaultPath) {
|
||||
return requestedPath ? alignToBaseDirectory(baseDir, requestedPath) : alignToBaseDirectory(baseDir, defaultPath)
|
||||
}
|
||||
|
||||
private File alignToBaseDirectory(File baseDir, String path) {
|
||||
return new File(translator.alignToBaseDirectory(path, baseDir))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ class GenerateStubsMojo extends AbstractMojo {
|
||||
@Parameter(defaultValue = '${project.build.directory}', readonly = true, required = true)
|
||||
private File projectBuildDirectory
|
||||
|
||||
@Parameter(property = 'stubsDirectory', defaultValue = '${project.build.directory}/mappings')
|
||||
@Parameter(property = 'stubsDirectory', defaultValue = '${project.build.directory}/accurest')
|
||||
private File outputDirectory
|
||||
|
||||
@Parameter(property = 'accurest.skip', defaultValue = 'false')
|
||||
|
||||
@@ -5,7 +5,6 @@ import io.codearte.accurest.maven.stubrunner.LocalStubRunner
|
||||
import io.codearte.accurest.maven.stubrunner.RemoteStubRunner
|
||||
import io.codearte.accurest.stubrunner.StubRunnerOptions
|
||||
import org.apache.maven.execution.MavenSession
|
||||
import org.apache.maven.model.path.PathTranslator
|
||||
import org.apache.maven.plugin.AbstractMojo
|
||||
import org.apache.maven.plugin.MojoExecutionException
|
||||
import org.apache.maven.plugin.MojoFailureException
|
||||
@@ -19,17 +18,14 @@ import javax.inject.Inject
|
||||
@CompileStatic
|
||||
class RunMojo extends AbstractMojo {
|
||||
|
||||
@Parameter(defaultValue = '${basedir}', readonly = true, required = true)
|
||||
private File basedir
|
||||
|
||||
@Parameter(defaultValue = '${project.build.directory}', readonly = true, required = true)
|
||||
private File projectBuildDirectory
|
||||
|
||||
@Parameter(defaultValue = '${repositorySystemSession}', readonly = true)
|
||||
private RepositorySystemSession repoSession
|
||||
|
||||
@Parameter(defaultValue = '${project.build.directory}/accurest/mappings')
|
||||
private File stubsDirectory
|
||||
|
||||
@Parameter(property = 'stubsDirectory', defaultValue = '${basedir}')
|
||||
private String stubsDirectory
|
||||
private File destination
|
||||
|
||||
@Parameter(property = 'accurest.http.port', defaultValue = '8080')
|
||||
private int httpPort;
|
||||
@@ -53,13 +49,11 @@ class RunMojo extends AbstractMojo {
|
||||
|
||||
private final LocalStubRunner localStubRunner
|
||||
private final RemoteStubRunner remoteStubRunner
|
||||
private final PathTranslator translator
|
||||
|
||||
@Inject
|
||||
RunMojo(LocalStubRunner localStubRunner, RemoteStubRunner remoteStubRunner, PathTranslator translator) {
|
||||
RunMojo(LocalStubRunner localStubRunner, RemoteStubRunner remoteStubRunner) {
|
||||
this.localStubRunner = localStubRunner
|
||||
this.remoteStubRunner = remoteStubRunner
|
||||
this.translator = translator
|
||||
}
|
||||
|
||||
void execute() throws MojoExecutionException, MojoFailureException {
|
||||
@@ -84,9 +78,9 @@ class RunMojo extends AbstractMojo {
|
||||
|
||||
private File resolveStubsDirectory() {
|
||||
if (insideProject) {
|
||||
return resolveFile(projectBuildDirectory, stubsDirectory, 'mappings')
|
||||
stubsDirectory
|
||||
} else {
|
||||
return resolveFile(basedir, stubsDirectory, '')
|
||||
destination
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,13 +96,4 @@ class RunMojo extends AbstractMojo {
|
||||
return mavenSession.request.projectPresent
|
||||
}
|
||||
|
||||
private File resolveFile(File baseDir, String requestedPath, String defaultPath) {
|
||||
return requestedPath ? alignToBaseDirectory(baseDir, requestedPath) : alignToBaseDirectory(baseDir, defaultPath)
|
||||
}
|
||||
|
||||
private File alignToBaseDirectory(File baseDir, String path) {
|
||||
return new File(translator.alignToBaseDirectory(path, baseDir))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -23,21 +23,21 @@ public class PluginUnitTest {
|
||||
public void shouldGenerateWireMockStubsInDefaultLocation() throws Exception {
|
||||
File basedir = resources.getBasedir("basic");
|
||||
maven.executeMojo(basedir, "convert");
|
||||
assertFilesPresent(basedir, "target/mappings/Sample.json");
|
||||
assertFilesPresent(basedir, "target/accurest/mappings/Sample.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGenerateWireMockFromStubsDirectory() throws Exception {
|
||||
File basedir = resources.getBasedir("withStubs");
|
||||
maven.executeMojo(basedir, "convert", newParameter("contractsDirectory", "src/test/resources/stubs"));
|
||||
assertFilesPresent(basedir, "target/mappings/Sample.json");
|
||||
assertFilesPresent(basedir, "target/accurest/mappings/Sample.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGenerateWireMockStubsInSelectedLocation() throws Exception {
|
||||
File basedir = resources.getBasedir("basic");
|
||||
maven.executeMojo(basedir, "convert", newParameter("outputDirectory", "foo"));
|
||||
assertFilesPresent(basedir, "target/foo/Sample.json");
|
||||
maven.executeMojo(basedir, "convert", newParameter("outputDirectory", "target/foo"));
|
||||
assertFilesPresent(basedir, "target/foo/mappings/Sample.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user