generating wiremock stubs from accurest contracts

This commit is contained in:
Mariusz Smykula
2016-03-25 22:42:47 +01:00
parent edb8a60a0f
commit dc7c350e5a
6 changed files with 247 additions and 0 deletions

View File

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

View File

@@ -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");
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.codearte.accurest.sample</groupId>
<artifactId>sample-project</artifactId>
<version>0.1</version>
<build>
<plugins>
<plugin>
<groupId>io.codearte.accurest</groupId>
<artifactId>accurest-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -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'
}
}
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.codearte.accurest.sample</groupId>
<artifactId>sample-project</artifactId>
<version>0.1</version>
<build>
<plugins>
<plugin>
<groupId>io.codearte.accurest</groupId>
<artifactId>accurest-maven-plugin</artifactId>
<version>${it-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>