diff --git a/pom.xml b/pom.xml index 1ca7979efb..5417aad6a9 100644 --- a/pom.xml +++ b/pom.xml @@ -8,5 +8,105 @@ accurest-maven-plugin 1.0-SNAPSHOT + takari-maven-plugin + + 3.3.3 + + + + 3.2.5 + 3.4 + + + + + io.codearte.accurest + accurest-core + 1.0.1 + + + io.codearte.accurest + accurest-converters + 1.0.1 + + + ch.qos.logback + logback-classic + 1.1.6 + test + + + + + org.codehaus.groovy + groovy-all + 2.4.6 + + + + org.apache.maven + maven-core + ${mavenVersion} + test + + + org.apache.maven + maven-compat + ${mavenVersion} + test + + + javax.inject + javax.inject + 1 + provided + + + org.apache.maven + maven-plugin-api + ${mavenVersion} + provided + + + org.apache.maven.plugin-tools + maven-plugin-annotations + ${mavenPluginPluginVersion} + provided + + + + io.takari.maven.plugins + takari-plugin-testing + 2.8.0 + test + + + + junit + junit + 4.12 + test + + + + + + + + + + io.takari.maven.plugins + takari-lifecycle-plugin + 1.11.12 + true + + none + + + + + + + \ No newline at end of file diff --git a/src/main/java/io/codearte/accurest/maven/GenerateStubsMojo.java b/src/main/java/io/codearte/accurest/maven/GenerateStubsMojo.java new file mode 100644 index 0000000000..499311cf65 --- /dev/null +++ b/src/main/java/io/codearte/accurest/maven/GenerateStubsMojo.java @@ -0,0 +1,55 @@ +package io.codearte.accurest.maven; + +import java.io.File; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +import io.codearte.accurest.config.AccurestConfigProperties; +import io.codearte.accurest.wiremock.DslToWireMockClientConverter; +import io.codearte.accurest.wiremock.RecursiveFilesConverter; + +@Mojo(name = "generateStubs", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class GenerateStubsMojo extends AbstractMojo { + + @Parameter(defaultValue = "${basedir}") + protected File baseDir; + + @Parameter(defaultValue = "${project.build.directory}") + protected File projectBuildDirectory; + + @Parameter(property = "mappingsDir", defaultValue = "mappings", required = false) + private String mappingsDir = "mappings"; + + public void execute() throws MojoExecutionException, MojoFailureException { + getLog().info(""); + getLog().info("*** Accurest Maven Plugin ***"); + getLog().info(""); + + File stubsOutputDir = new File(projectBuildDirectory, mappingsDir); + + getLog().info("baseDir = " + baseDir); + getLog().info("projectBuildDirectory = " + projectBuildDirectory); + getLog().info("mappings = " + mappingsDir); + + AccurestConfigProperties configProperties = new AccurestConfigProperties(); + + File contractsDslDir = new File(baseDir, "/src/test/resources/stubs"); + + configProperties.setContractsDslDir(contractsDslDir); + configProperties.setBasePackageForTests("io.codearte.accurest.tests"); + configProperties.setStubsOutputDir(stubsOutputDir); + getLog().info("Accurest Plugin: Invoking GroovyDSL to WireMock client stubs conversion"); + getLog().info(String.format("From '%s' to '%s'", contractsDslDir, stubsOutputDir)); + + RecursiveFilesConverter converter = new RecursiveFilesConverter(new DslToWireMockClientConverter(), + configProperties); + converter.processFiles(); + + } + +} diff --git a/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java b/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java new file mode 100644 index 0000000000..1d402cf58e --- /dev/null +++ b/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java @@ -0,0 +1,35 @@ +package io.codearte.accurest.maven; + +import static io.takari.maven.testing.TestResources.assertFilesPresent; + +import java.io.File; + +import org.junit.Rule; +import org.junit.Test; + +import io.takari.maven.testing.TestMavenRuntime; +import io.takari.maven.testing.TestResources; + +public class PluginUnitTest { + + @Rule + public final TestResources resources = new TestResources(); + + @Rule + public final TestMavenRuntime maven = new TestMavenRuntime(); + + @Test + public void shouldGenerateWarlockStubsInDefaultLocation() throws Exception { + File basedir = resources.getBasedir("basic"); + maven.executeMojo(basedir, "generateStubs"); + assertFilesPresent(basedir, "target/mappings/Sample.json"); + } + + @Test + public void shouldGenerateWarlockStubsInSelectedLocation() throws Exception { + File basedir = resources.getBasedir("basic"); + maven.executeMojo(basedir, "generateStubs", TestMavenRuntime.newParameter("mappingsDir", "foo")); + assertFilesPresent(basedir, "target/foo/Sample.json"); + } + +} diff --git a/src/test/projects/basic/pom.xml b/src/test/projects/basic/pom.xml new file mode 100644 index 0000000000..fed7b7d67e --- /dev/null +++ b/src/test/projects/basic/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + + io.codearte.accurest.sample + sample-project + 0.1 + + + + + io.codearte.accurest + accurest-maven-plugin + + + + + \ No newline at end of file diff --git a/src/test/projects/basic/src/test/resources/stubs/Sample.groovy b/src/test/projects/basic/src/test/resources/stubs/Sample.groovy new file mode 100644 index 0000000000..673ab7bd8d --- /dev/null +++ b/src/test/projects/basic/src/test/resources/stubs/Sample.groovy @@ -0,0 +1,18 @@ +io.codearte.accurest.dsl.GroovyDsl.make { + request { + method 'POST' + url('/users') { + + } + headers { + header 'Content-Type': 'application/json' + } + body '''{ "login" : "john", "name": "John The Contract" }''' + } + response { + status 200 + headers { + header 'Location': '/users/john' + } + } +} \ No newline at end of file diff --git a/src/test/projects/example/pom.xml b/src/test/projects/example/pom.xml new file mode 100644 index 0000000000..faa1b92be3 --- /dev/null +++ b/src/test/projects/example/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + io.codearte.accurest.sample + sample-project + 0.1 + + + + + io.codearte.accurest + accurest-maven-plugin + ${it-plugin.version} + + + + + \ No newline at end of file