diff --git a/maven-plugin/pom.xml b/maven-plugin/pom.xml index 5222f6de08..9059d2bf0f 100644 --- a/maven-plugin/pom.xml +++ b/maven-plugin/pom.xml @@ -31,7 +31,12 @@ logback-core 1.1.3 - + + org.slf4j + slf4j-simple + 1.7.12 + test + org.apache.maven maven-core diff --git a/maven-plugin/src/main/java/io/codearte/accurest/maven/AbstractGenerateVerificationCodeMojo.java b/maven-plugin/src/main/java/io/codearte/accurest/maven/AbstractGenerateVerificationCodeMojo.java new file mode 100644 index 0000000000..c7691c3662 --- /dev/null +++ b/maven-plugin/src/main/java/io/codearte/accurest/maven/AbstractGenerateVerificationCodeMojo.java @@ -0,0 +1,70 @@ +package io.codearte.accurest.maven; + +import static java.lang.String.format; + +import java.io.File; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Parameter; +import org.assertj.core.util.Strings; + +import io.codearte.accurest.AccurestException; +import io.codearte.accurest.TestGenerator; +import io.codearte.accurest.config.AccurestConfigProperties; +import io.codearte.accurest.config.TestFramework; +import io.codearte.accurest.config.TestMode; + +public abstract class AbstractGenerateVerificationCodeMojo extends AbstractMojo { + + @Parameter(defaultValue = "${basedir}", readonly = true, required = true) + protected File baseDir; + + @Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true) + protected File projectBuildDirectory; + + @Parameter(defaultValue = "/src/test/accurest") + protected String contractsDir; + + @Parameter(defaultValue = "/generated-test-sources/accurest") + protected String generatedTestSourcesDir; + + @Parameter(defaultValue = "io.codearte.accurest.tests") + protected String basePackageForTests; + + @Parameter + protected String baseClassForTests; + + @Parameter(defaultValue = "MOCKMVC") + protected TestMode testMode; + + protected void generateVerificationCode(TestFramework testFramework) throws MojoExecutionException { + + AccurestConfigProperties config = new AccurestConfigProperties(); + config.setContractsDslDir(new File(baseDir, contractsDir)); + config.setBasePackageForTests(basePackageForTests); + config.setGeneratedTestSourcesDir(new File(projectBuildDirectory, generatedTestSourcesDir)); + if (Strings.isNullOrEmpty(baseClassForTests)) { + if (!Strings.isNullOrEmpty(basePackageForTests)) { + baseClassForTests = basePackageForTests + '.' + "BaseAccurest"; + } else { + baseClassForTests = "io.codearte.accurest.tests.BaseAccurest"; + } + } + config.setBaseClassForTests(baseClassForTests); + config.setTargetFramework(testFramework); + config.setTestMode(testMode); + + getLog().info(format("Using %s as test source directory", config.getGeneratedTestSourcesDir())); + getLog().info(format("Using %s as base class for verification classes", config.getBaseClassForTests())); + + try { + TestGenerator generator = new TestGenerator(config); + int generatedClasses = generator.generate(); + getLog().info(format("Generated %s test classes.", generatedClasses)); + } catch (AccurestException e) { + throw new MojoExecutionException(format("Accurest Plugin exception: %s", e.getMessage()), e); + } + } + +} diff --git a/maven-plugin/src/main/java/io/codearte/accurest/maven/GenerateSpecsMojo.java b/maven-plugin/src/main/java/io/codearte/accurest/maven/GenerateSpecsMojo.java index 2a6e35f081..4f1844b49d 100644 --- a/maven-plugin/src/main/java/io/codearte/accurest/maven/GenerateSpecsMojo.java +++ b/maven-plugin/src/main/java/io/codearte/accurest/maven/GenerateSpecsMojo.java @@ -1,74 +1,18 @@ package io.codearte.accurest.maven; -import static java.lang.String.format; - -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.AccurestException; -import io.codearte.accurest.TestGenerator; -import io.codearte.accurest.config.AccurestConfigProperties; import io.codearte.accurest.config.TestFramework; -import io.codearte.accurest.config.TestMode; @Mojo(name = "generateSpecs", defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES) -public class GenerateSpecsMojo 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 = "/src/test/accurest") - private String contractsDir; - - @Parameter(defaultValue = "/generated-test-sources/accurest") - private String generatedTestSourcesDir; - - @Parameter(defaultValue = "io.codearte.accurest.tests") - private String basePackageForTests; - - @Parameter - private String baseClassForTests; - - @Parameter(defaultValue = "SPOCK") - private TestFramework targetFramework; - - @Parameter(defaultValue = "MOCKMVC") - private TestMode testMode; - - public GenerateSpecsMojo() { - } +public class GenerateSpecsMojo extends AbstractGenerateVerificationCodeMojo { public void execute() throws MojoExecutionException, MojoFailureException { - AccurestConfigProperties config = new AccurestConfigProperties(); - - config.setContractsDslDir(new File(baseDir, contractsDir)); - config.setBasePackageForTests(basePackageForTests); - config.setGeneratedTestSourcesDir(new File(projectBuildDirectory, generatedTestSourcesDir)); - config.setBaseClassForTests(baseClassForTests); - config.setTargetFramework(targetFramework); - config.setTestMode(testMode); - - getLog().info("Accurest Plugin: Invoking test sources generation"); - getLog().info(format("Using %s as test source directory", config.getGeneratedTestSourcesDir())); - getLog().info(format("Using %s as base class for tests", config.getBaseClassForTests())); - - try { - TestGenerator generator = new TestGenerator(config); - int generatedClasses = generator.generate(); - getLog().info(format("Generated %s test classes.", generatedClasses)); - } catch (AccurestException e) { - throw new MojoExecutionException(format("Accurest Plugin exception: %s", e.getMessage()), e); - } - + getLog().info("Generating Spock Specifications source code for Accurest contract verification"); + generateVerificationCode(TestFramework.SPOCK); } } diff --git a/maven-plugin/src/main/java/io/codearte/accurest/maven/GenerateTestsMojo.java b/maven-plugin/src/main/java/io/codearte/accurest/maven/GenerateTestsMojo.java new file mode 100644 index 0000000000..2b82877740 --- /dev/null +++ b/maven-plugin/src/main/java/io/codearte/accurest/maven/GenerateTestsMojo.java @@ -0,0 +1,18 @@ +package io.codearte.accurest.maven; + +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 io.codearte.accurest.config.TestFramework; + +@Mojo(name = "generateTests", defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES) +public class GenerateTestsMojo extends AbstractGenerateVerificationCodeMojo { + + public void execute() throws MojoExecutionException, MojoFailureException { + getLog().info("Generating JUnit Tests source code for Accurest contract verification"); + generateVerificationCode(TestFramework.JUNIT); + } + +} diff --git a/maven-plugin/src/test/java/io/codearte/accurest/maven/PluginIntegrationTest.java b/maven-plugin/src/test/java/io/codearte/accurest/maven/PluginIntegrationTest.java index 40bd3f697d..6d7ed574e2 100644 --- a/maven-plugin/src/test/java/io/codearte/accurest/maven/PluginIntegrationTest.java +++ b/maven-plugin/src/test/java/io/codearte/accurest/maven/PluginIntegrationTest.java @@ -28,12 +28,24 @@ public class PluginIntegrationTest { } @Test - public void shouldBuildSimpleBootWIthAccurtesProject() throws Exception { - File basedir = resources.getBasedir("bootSimple"); + public void should_build_project_Spring_Boot_Groovy_with_Accurest() throws Exception { + File basedir = resources.getBasedir("spring-boot-groovy"); maven.forProject(basedir) .execute("package") .assertErrorFreeLog() - .assertLogText("Accurest Plugin: Invoking test sources generation") + .assertLogText("Generating Spock Specifications source code for Accurest contract verification") + .assertLogText("Generated 1 test classes.") + .assertLogText("Accurest Plugin: Invoking GroovyDSL to WireMock client stubs conversion") + .assertLogText("Creating new json") + .assertErrorFreeLog(); + } + @Test + public void should_build_project_Spring_Boot_Java_with_Accurest() throws Exception { + File basedir = resources.getBasedir("spring-boot-java"); + maven.forProject(basedir) + .execute("package") + .assertErrorFreeLog() + .assertLogText("Generating JUnit Tests source code for Accurest contract verification") .assertLogText("Generated 1 test classes.") .assertLogText("Accurest Plugin: Invoking GroovyDSL to WireMock client stubs conversion") .assertLogText("Creating new json") @@ -41,7 +53,7 @@ public class PluginIntegrationTest { } @Test - public void shouldConvertAccurestToWireMockStubs() throws Exception { + public void should_convert_Accurest_Contracts_to_WireMock_Stubs_mappings() throws Exception { File basedir = resources.getBasedir("pomless"); properties.getPluginVersion(); maven.forProject(basedir) diff --git a/maven-plugin/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java b/maven-plugin/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java index 864b844cd8..1d04aa339a 100644 --- a/maven-plugin/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java +++ b/maven-plugin/src/test/java/io/codearte/accurest/maven/PluginUnitTest.java @@ -1,5 +1,6 @@ package io.codearte.accurest.maven; +import static io.takari.maven.testing.TestMavenRuntime.newParameter; import static io.takari.maven.testing.TestResources.assertFilesPresent; import java.io.File; @@ -19,23 +20,23 @@ public class PluginUnitTest { public final TestMavenRuntime maven = new TestMavenRuntime(); @Test - public void shouldGenerateWiremockStubsInDefaultcLocation() throws Exception { + public void shouldGenerateWireMockStubsInDefaultLocation() throws Exception { File basedir = resources.getBasedir("basic"); maven.executeMojo(basedir, "generateStubs"); assertFilesPresent(basedir, "target/mappings/Sample.json"); } @Test - public void shouldGenerateWiremockFromStubsDirectory() throws Exception { + public void shouldGenerateWireMockFromStubsDirectory() throws Exception { File basedir = resources.getBasedir("withStubs"); - maven.executeMojo(basedir, "generateStubs", TestMavenRuntime.newParameter("contractsDir", "/src/test/resources/stubs")); + maven.executeMojo(basedir, "generateStubs", newParameter("contractsDir", "/src/test/resources/stubs")); assertFilesPresent(basedir, "target/mappings/Sample.json"); } @Test - public void shouldGenerateWiremockStubsInSelectedLocation() throws Exception { + public void shouldGenerateWireMockStubsInSelectedLocation() throws Exception { File basedir = resources.getBasedir("basic"); - maven.executeMojo(basedir, "generateStubs", TestMavenRuntime.newParameter("mappingsDir", "foo")); + maven.executeMojo(basedir, "generateStubs", newParameter("mappingsDir", "foo")); assertFilesPresent(basedir, "target/foo/Sample.json"); } @@ -47,4 +48,12 @@ public class PluginUnitTest { "target/generated-test-sources/accurest/io/codearte/accurest/tests/AccurestSpec.groovy"); } + @Test + public void shouldGenerateContractTestsInDefaultLocation() throws Exception { + File basedir = resources.getBasedir("basic"); + maven.executeMojo(basedir, "generateTests"); + assertFilesPresent(basedir, + "target/generated-test-sources/accurest/io/codearte/accurest/tests/AccurestTest.java"); + } + } diff --git a/maven-plugin/src/test/projects/bootSimple/pom.xml b/maven-plugin/src/test/projects/spring-boot-groovy/pom.xml similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/pom.xml rename to maven-plugin/src/test/projects/spring-boot-groovy/pom.xml diff --git a/maven-plugin/src/test/projects/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy b/maven-plugin/src/test/projects/spring-boot-groovy/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy rename to maven-plugin/src/test/projects/spring-boot-groovy/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy diff --git a/maven-plugin/src/test/projects/bootSimple/src/main/groovy/com/ofg/twitter/place/Tweet.java b/maven-plugin/src/test/projects/spring-boot-groovy/src/main/groovy/com/ofg/twitter/place/Tweet.java similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/main/groovy/com/ofg/twitter/place/Tweet.java rename to maven-plugin/src/test/projects/spring-boot-groovy/src/main/groovy/com/ofg/twitter/place/Tweet.java diff --git a/maven-plugin/src/test/projects/bootSimple/src/main/java/io/codearte/sample/SampleSimpleBootApplication.java b/maven-plugin/src/test/projects/spring-boot-groovy/src/main/java/io/codearte/sample/SampleSimpleBootApplication.java similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/main/java/io/codearte/sample/SampleSimpleBootApplication.java rename to maven-plugin/src/test/projects/spring-boot-groovy/src/main/java/io/codearte/sample/SampleSimpleBootApplication.java diff --git a/maven-plugin/src/test/projects/bootSimple/src/main/resources/application.properties b/maven-plugin/src/test/projects/spring-boot-groovy/src/main/resources/application.properties similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/main/resources/application.properties rename to maven-plugin/src/test/projects/spring-boot-groovy/src/main/resources/application.properties diff --git a/maven-plugin/src/test/projects/bootSimple/src/test/groovy/com/ofg/twitter/place/AcceptanceSpec.groovy b/maven-plugin/src/test/projects/spring-boot-groovy/src/test/groovy/com/ofg/twitter/place/AcceptanceSpec.groovy similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/test/groovy/com/ofg/twitter/place/AcceptanceSpec.groovy rename to maven-plugin/src/test/projects/spring-boot-groovy/src/test/groovy/com/ofg/twitter/place/AcceptanceSpec.groovy diff --git a/maven-plugin/src/test/projects/bootSimple/src/test/groovy/com/ofg/twitter/place/BaseMockMvcSpec.groovy b/maven-plugin/src/test/projects/spring-boot-groovy/src/test/groovy/com/ofg/twitter/place/BaseMockMvcSpec.groovy similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/test/groovy/com/ofg/twitter/place/BaseMockMvcSpec.groovy rename to maven-plugin/src/test/projects/spring-boot-groovy/src/test/groovy/com/ofg/twitter/place/BaseMockMvcSpec.groovy diff --git a/maven-plugin/src/test/projects/bootSimple/src/test/java/io/codearte/sample/SampleSimpleBootApplicationTests.java b/maven-plugin/src/test/projects/spring-boot-groovy/src/test/java/io/codearte/sample/SampleSimpleBootApplicationTests.java similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/test/java/io/codearte/sample/SampleSimpleBootApplicationTests.java rename to maven-plugin/src/test/projects/spring-boot-groovy/src/test/java/io/codearte/sample/SampleSimpleBootApplicationTests.java diff --git a/maven-plugin/src/test/projects/bootSimple/src/test/resources/logback-test.groovy b/maven-plugin/src/test/projects/spring-boot-groovy/src/test/resources/logback-test.groovy similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/test/resources/logback-test.groovy rename to maven-plugin/src/test/projects/spring-boot-groovy/src/test/resources/logback-test.groovy diff --git a/maven-plugin/src/test/projects/bootSimple/src/test/resources/stubs/collerate_PlacesFrom_Tweet.groovy b/maven-plugin/src/test/projects/spring-boot-groovy/src/test/resources/stubs/collerate_PlacesFrom_Tweet.groovy similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/test/resources/stubs/collerate_PlacesFrom_Tweet.groovy rename to maven-plugin/src/test/projects/spring-boot-groovy/src/test/resources/stubs/collerate_PlacesFrom_Tweet.groovy diff --git a/maven-plugin/src/test/projects/bootSimple/src/test/resources/stubs/moreComplexVersion.groovy b/maven-plugin/src/test/projects/spring-boot-groovy/src/test/resources/stubs/moreComplexVersion.groovy similarity index 100% rename from maven-plugin/src/test/projects/bootSimple/src/test/resources/stubs/moreComplexVersion.groovy rename to maven-plugin/src/test/projects/spring-boot-groovy/src/test/resources/stubs/moreComplexVersion.groovy diff --git a/maven-plugin/src/test/projects/spring-boot-java/pom.xml b/maven-plugin/src/test/projects/spring-boot-java/pom.xml new file mode 100644 index 0000000000..e7be85facd --- /dev/null +++ b/maven-plugin/src/test/projects/spring-boot-java/pom.xml @@ -0,0 +1,139 @@ + + + 4.0.0 + + org.springframework + gs-rest-service + 0.1.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.3.3.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + + ch.qos.logback + logback-classic + 1.1.3 + + + + io.codearte.accurest + accurest-test-deps + ${it-plugin.version} + pom + test + + + com.jayway.restassured + spring-mock-mvc + test + + + + + + + io.codearte.accurest + accurest-test-deps + ${it-plugin.version} + pom + import + + + + + + 1.8 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + io.codearte.accurest + accurest-maven-plugin + ${it-plugin.version} + + + + generateStubs + generateTests + + + + + hello + + + + maven-assembly-plugin + 2.6 + + + io.codearte.accurest + stubs-assembly-descriptor + 1.1 + + + + + package + + single + + + + + + stubs + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.10 + + + add-accurest-test-sources + generate-test-sources + + add-test-source + + + + ${project.build.directory}/generated-test-sources/accurest + + + + + + + + + + + spring-releases + https://repo.spring.io/libs-release + + + + + spring-releases + https://repo.spring.io/libs-release + + + diff --git a/maven-plugin/src/test/projects/spring-boot-java/src/main/java/hello/Application.java b/maven-plugin/src/test/projects/spring-boot-java/src/main/java/hello/Application.java new file mode 100644 index 0000000000..5abd411428 --- /dev/null +++ b/maven-plugin/src/test/projects/spring-boot-java/src/main/java/hello/Application.java @@ -0,0 +1,12 @@ +package hello; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/maven-plugin/src/test/projects/spring-boot-java/src/main/java/hello/Greeting.java b/maven-plugin/src/test/projects/spring-boot-java/src/main/java/hello/Greeting.java new file mode 100644 index 0000000000..5d10b59b77 --- /dev/null +++ b/maven-plugin/src/test/projects/spring-boot-java/src/main/java/hello/Greeting.java @@ -0,0 +1,20 @@ +package hello; + +public class Greeting { + + private final long id; + private final String content; + + public Greeting(long id, String content) { + this.id = id; + this.content = content; + } + + public long getId() { + return id; + } + + public String getContent() { + return content; + } +} diff --git a/maven-plugin/src/test/projects/spring-boot-java/src/main/java/hello/GreetingController.java b/maven-plugin/src/test/projects/spring-boot-java/src/main/java/hello/GreetingController.java new file mode 100644 index 0000000000..e356ab2b10 --- /dev/null +++ b/maven-plugin/src/test/projects/spring-boot-java/src/main/java/hello/GreetingController.java @@ -0,0 +1,19 @@ +package hello; + +import java.util.concurrent.atomic.AtomicLong; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class GreetingController { + + private static final String template = "Hello, %s!"; + private final AtomicLong counter = new AtomicLong(); + + @RequestMapping("/greeting") + public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { + return new Greeting(counter.incrementAndGet(), + String.format(template, name)); + } +} diff --git a/maven-plugin/src/test/projects/spring-boot-java/src/test/accurest/greetings_ok.groovy b/maven-plugin/src/test/projects/spring-boot-java/src/test/accurest/greetings_ok.groovy new file mode 100644 index 0000000000..0ffb665f6e --- /dev/null +++ b/maven-plugin/src/test/projects/spring-boot-java/src/test/accurest/greetings_ok.groovy @@ -0,0 +1,21 @@ +io.codearte.accurest.dsl.GroovyDsl.make { + priority 2 + request { + method 'GET' + urlPath('/greeting') { + queryParameters { + parameter 'name': 'Something' + } + } + headers { + header 'Content-Type': 'application/json' + } + } + response { + status 200 + body( + id: 1, + content: "Hello, Something!" + ) + } +} \ No newline at end of file diff --git a/maven-plugin/src/test/projects/spring-boot-java/src/test/accurest/greetings_with_default_value.groovy b/maven-plugin/src/test/projects/spring-boot-java/src/test/accurest/greetings_with_default_value.groovy new file mode 100644 index 0000000000..2c5c32f159 --- /dev/null +++ b/maven-plugin/src/test/projects/spring-boot-java/src/test/accurest/greetings_with_default_value.groovy @@ -0,0 +1,17 @@ +io.codearte.accurest.dsl.GroovyDsl.make { + priority 2 + request { + method 'GET' + url('/greeting') + headers { + header 'Content-Type': 'application/json' + } + } + response { + status 200 + body( + id: 1, + content: "Hello, World!" + ) + } +} \ No newline at end of file diff --git a/maven-plugin/src/test/projects/spring-boot-java/src/test/java/hello/BaseAccurest.java b/maven-plugin/src/test/projects/spring-boot-java/src/test/java/hello/BaseAccurest.java new file mode 100644 index 0000000000..d3bf7124e2 --- /dev/null +++ b/maven-plugin/src/test/projects/spring-boot-java/src/test/java/hello/BaseAccurest.java @@ -0,0 +1,13 @@ +package hello; + +import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc; +import org.junit.Before; + +public class BaseAccurest { + + @Before + public void setup() { + RestAssuredMockMvc.standaloneSetup(new GreetingController()); + } + +}