From 522ec70552694e9eb0067ab6879c78e94a2065c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Smyku=C5=82a?= Date: Sun, 24 Apr 2016 20:34:52 +0200 Subject: [PATCH] Add contracts to stub.jar by default (#13) --- .gitignore | 2 +- .../accurest/maven/ConvertMojo.groovy | 11 ++-------- .../accurest/maven/GenerateStubsMojo.groovy | 13 ++++++++++- .../accurest/maven/PluginUnitTest.java | 16 ++++++++++++++ src/test/projects/generatedStubs/pom.xml | 19 ++++++++++++++++ .../target/accurest/contracts/Sample.groovy | 18 +++++++++++++++ .../target/accurest/mappings/Sample.json | 22 +++++++++++++++++++ 7 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 src/test/projects/generatedStubs/pom.xml create mode 100644 src/test/projects/generatedStubs/target/accurest/contracts/Sample.groovy create mode 100644 src/test/projects/generatedStubs/target/accurest/mappings/Sample.json diff --git a/.gitignore b/.gitignore index 833eab7406..6c515d9c17 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ .idea/ *.iml -target +/target pom.xml.tag pom.xml.releaseBackup pom.xml.versionsBackup diff --git a/src/main/groovy/io/codearte/accurest/maven/ConvertMojo.groovy b/src/main/groovy/io/codearte/accurest/maven/ConvertMojo.groovy index 6fab4377dd..9820d50781 100644 --- a/src/main/groovy/io/codearte/accurest/maven/ConvertMojo.groovy +++ b/src/main/groovy/io/codearte/accurest/maven/ConvertMojo.groovy @@ -37,9 +37,6 @@ class ConvertMojo extends AbstractMojo { @Parameter(property = 'accurest.skip', defaultValue = 'false') private boolean skip - @Parameter(defaultValue = 'false') - private boolean attachContracts - @Parameter(defaultValue = '${session}', readonly = true) private MavenSession mavenSession @@ -56,13 +53,9 @@ class ConvertMojo extends AbstractMojo { return } - if (attachContracts) { - log.info("Attaching accurest contracts") - copyContracts() - } + copyContracts() AccurestConfigProperties config = new AccurestConfigProperties() - config.contractsDslDir = insideProject ? contractsDirectory : source config.stubsOutputDir = insideProject ? new File(outputDirectory, 'mappings') : destination @@ -75,8 +68,8 @@ class ConvertMojo extends AbstractMojo { } private void copyContracts() { + log.info("Copying accurest contracts") Resource testResource = new Resource(directory: contractsDirectory.absolutePath) - MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution([testResource], new File(outputDirectory, 'contracts'), project, 'UTF-8', diff --git a/src/main/groovy/io/codearte/accurest/maven/GenerateStubsMojo.groovy b/src/main/groovy/io/codearte/accurest/maven/GenerateStubsMojo.groovy index ed88bd2b74..028ce108bc 100644 --- a/src/main/groovy/io/codearte/accurest/maven/GenerateStubsMojo.groovy +++ b/src/main/groovy/io/codearte/accurest/maven/GenerateStubsMojo.groovy @@ -17,6 +17,9 @@ import org.codehaus.plexus.archiver.jar.JarArchiver @CompileStatic class GenerateStubsMojo extends AbstractMojo { + private static final String STUB_MAPPING_FILE_PATTERN = '**/*.json' + private static final String ACCUREST_FILE_PATTERN = '**/*.groovy' + @Parameter(defaultValue = '${project.build.directory}', readonly = true, required = true) private File projectBuildDirectory @@ -35,6 +38,9 @@ class GenerateStubsMojo extends AbstractMojo { @Component(role = Archiver.class, hint = "jar") private JarArchiver jarArchiver; + @Parameter(defaultValue = 'true') + private boolean attachContracts + void execute() throws MojoExecutionException, MojoFailureException { if (skip) { @@ -54,7 +60,12 @@ class GenerateStubsMojo extends AbstractMojo { File stubJarFile = new File(projectBuildDirectory, stubArchiveName); try { - jarArchiver.addDirectory(stubsOutputDir); + if (attachContracts) { + jarArchiver.addDirectory(stubsOutputDir, [STUB_MAPPING_FILE_PATTERN, ACCUREST_FILE_PATTERN] as String[], [] as String[]); + } else { + log.info("Skipping attaching accurest contracts") + jarArchiver.addDirectory(stubsOutputDir, [STUB_MAPPING_FILE_PATTERN] as String[], [ACCUREST_FILE_PATTERN] as String[]); + } jarArchiver.setCompress(true); jarArchiver.setDestFile(stubJarFile); jarArchiver.createArchive(); diff --git a/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java b/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java index d008614a00..8f2f195b97 100644 --- a/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java +++ b/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java @@ -1,6 +1,7 @@ package io.codearte.accurest.maven; import static io.takari.maven.testing.TestMavenRuntime.newParameter; +import static io.takari.maven.testing.TestResources.assertFileContents; import static io.takari.maven.testing.TestResources.assertFilesPresent; import java.io.File; @@ -56,4 +57,19 @@ public class PluginUnitTest { "target/generated-test-sources/accurest/io/codearte/accurest/tests/AccurestTest.java"); } + @Test + public void shouldGenerateStubs() throws Exception { + File basedir = resources.getBasedir("generatedStubs"); + maven.executeMojo(basedir, "generateStubs"); + assertFilesPresent(basedir, "target/sample-project-0.1-stubs.jar"); + } + + @Test + public void shouldGenerateStubsWithMappingsOnly() throws Exception { + File basedir = resources.getBasedir("generatedStubs"); + maven.executeMojo(basedir, "generateStubs", newParameter("attachContracts", "false")); + assertFilesPresent(basedir, "target/sample-project-0.1-stubs.jar"); + // FIXME: add assertion for jar content + } + } diff --git a/src/test/projects/generatedStubs/pom.xml b/src/test/projects/generatedStubs/pom.xml new file mode 100644 index 0000000000..fed7b7d67e --- /dev/null +++ b/src/test/projects/generatedStubs/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/generatedStubs/target/accurest/contracts/Sample.groovy b/src/test/projects/generatedStubs/target/accurest/contracts/Sample.groovy new file mode 100644 index 0000000000..673ab7bd8d --- /dev/null +++ b/src/test/projects/generatedStubs/target/accurest/contracts/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/generatedStubs/target/accurest/mappings/Sample.json b/src/test/projects/generatedStubs/target/accurest/mappings/Sample.json new file mode 100644 index 0000000000..6641a7d26f --- /dev/null +++ b/src/test/projects/generatedStubs/target/accurest/mappings/Sample.json @@ -0,0 +1,22 @@ +{ + "request" : { + "url" : "/users", + "method" : "POST", + "bodyPatterns" : [ { + "matchesJsonPath" : "$[?(@.login == 'john')]" + }, { + "matchesJsonPath" : "$[?(@.name == 'John The Contract')]" + } ], + "headers" : { + "Content-Type" : { + "equalTo" : "application/json" + } + } + }, + "response" : { + "status" : 200, + "headers" : { + "Location" : "/users/john" + } + } +} \ No newline at end of file