Feature/complex test configuration (#16)

* custom imports support

* add missing options to generateTests
This commit is contained in:
Mariusz Smykuła
2016-04-26 16:34:15 +02:00
parent 45e8a053f1
commit 305452c54a
19 changed files with 429 additions and 37 deletions

View File

@@ -45,6 +45,30 @@ class GenerateTestsMojo extends AbstractMojo {
@Parameter
private String nameSuffixForTests
/**
* Imports that should be added to generated tests
*/
@Parameter
private String[] imports
/**
* Static imports that should be added to generated tests
*/
@Parameter
private String[] staticImports
/**
* Patterns that should not be taken into account for processing
*/
@Parameter
List<String> excludedFiles
/**
* Patterns for which Accurest should generate @Ignored tests
*/
@Parameter
List<String> ignoredFiles
@Parameter(defaultValue = '${project}', readonly = true)
private MavenProject project
@@ -70,6 +94,11 @@ class GenerateTestsMojo extends AbstractMojo {
config.ruleClassForTests = ruleClassForTests
config.nameSuffixForTests = nameSuffixForTests
config.setImports(imports)
config.setStaticImports(staticImports)
config.setIgnoredFiles(ignoredFiles)
config.setExcludedFiles(excludedFiles)
project.addTestCompileSourceRoot(generatedTestSourcesDir.absolutePath)
if (log.isInfoEnabled()) {

View File

@@ -0,0 +1,38 @@
== More Complex Plugin Configuration
Sample more complex configuration for Java Project with JUnit tests.
=== Project configuration for Accurest with JUnit tests and stub publishing
[source,xml]
----
include::../../../src/test/projects/complex-configuration/pom.xml[tags=plugin]
----
=== Base Test class
[source,java]
----
include::../../../src/test/projects/complex-configuration/src/test/java/com/blogspot/toomuchcoding/frauddetection/BaseAccurest.java[]
----
=== Sample additional matcher
[source,java]
----
include::../../../src/test/projects/complex-configuration/src/test/java/com/blogspot/toomuchcoding/frauddetection/matchers/CustomMatchers.java[]
----
=== Sample contract using matcher
[source,java]
----
include::../../../src/test/projects/complex-configuration/src/test/accurest/shouldMarkClientAsNotFraud.groovy[]
----
=== Project source code
https://github.com/Codearte/accurest-maven-plugin/tree/master/src/test/projects/complex-configuration

View File

@@ -30,6 +30,7 @@
<item name="Goals" href="plugin-info.html"/>
<item name="Accurest with JUnit" href="junit.html"/>
<item name="Accurest with Spock" href="spock.html"/>
<item name="Complex Configuration" href="complex.html"/>
</menu>
<menu name="Related Projects">
<item name="Accurate REST" href="https://github.com/Codearte/accurest"/>

View File

@@ -1,7 +1,9 @@
package io.codearte.accurest.maven;
import java.io.File;
import java.util.regex.Pattern;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -55,6 +57,18 @@ public class PluginIT {
.assertErrorFreeLog();
}
@Test
@Ignore("Ignored, because of bug accurest#245")
public void should_build_project_project_with_complex_configuration() throws Exception {
File basedir = resources.getBasedir("complex-configuration");
maven.forProject(basedir)
.execute("package")
.assertErrorFreeLog()
.assertLogText("Tests run: 2, Failures: 0, Errors: 0, Skipped: 1")
.assertLogText("Running com.blogspot.toomuchcoding.frauddetection.AccurestTest")
.assertErrorFreeLog();
}
@Test
public void should_convert_Accurest_Contracts_to_WireMock_Stubs_mappings() throws Exception {
File basedir = resources.getBasedir("pomless");

View File

@@ -5,6 +5,7 @@ import static io.takari.maven.testing.TestResources.assertFileContents;
import static io.takari.maven.testing.TestResources.assertFilesPresent;
import java.io.File;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
@@ -57,6 +58,14 @@ public class PluginUnitTest {
"target/generated-test-sources/accurest/io/codearte/accurest/tests/AccurestTest.java");
}
@Test
public void shouldGenerateContractTestsWithCustomImports() throws Exception {
File basedir = resources.getBasedir("basic");
maven.executeMojo(basedir, "generateTests", newParameter("imports", ""));
assertFilesPresent(basedir,
"target/generated-test-sources/accurest/io/codearte/accurest/tests/AccurestTest.java");
}
@Test
public void shouldGenerateStubs() throws Exception {
File basedir = resources.getBasedir("generatedStubs");

View File

@@ -0,0 +1 @@
target

View File

@@ -0,0 +1,112 @@
<?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>com.blogspot.toomuchcoding.frauddetection</groupId>
<artifactId>frauddetection</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<!-- accurest test dependencies -->
<!-- tag::dependencies[] -->
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.toomuchcoding.jsonassert</groupId>
<artifactId>jsonassert</artifactId>
<version>0.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<!-- end::dependencies[] -->
</dependencies>
<properties>
<java.version>1.8</java.version>
<accurest-plugin.version>${it-plugin.version}</accurest-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::plugin[] -->
<plugin>
<groupId>io.codearte.accurest</groupId>
<artifactId>accurest-maven-plugin</artifactId>
<version>${accurest-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>convert</goal>
<goal>generateStubs</goal>
<goal>generateTests</goal>
</goals>
<configuration>
<basePackageForTests>com.blogspot.toomuchcoding.frauddetection</basePackageForTests>
<testMode>MOCKMVC</testMode>
<testFramework>JUNIT</testFramework>
<classifier>stubs</classifier>
<nameSuffixForTests>Test</nameSuffixForTests>
<ruleClassForTests>org.junit.rules.ErrorCollector</ruleClassForTests>
<staticImports>
<staticImport>com.blogspot.toomuchcoding.frauddetection.matchers.CustomMatchers.*</staticImport>
</staticImports>
<imports>
<import>com.blogspot.toomuchcoding.frauddetection.matchers.CustomMatchers</import>
</imports>
<ignoredFiles>
<ignoredFile>broken**</ignoredFile>
</ignoredFiles>
<excludedFiles>
<param>shouldMarkClientAsFraud.groovy</param>
</excludedFiles>
</configuration>
</execution>
</executions>
<configuration>
<baseClassForTests>com.blogspot.toomuchcoding.frauddetection.BaseAccurest</baseClassForTests>
</configuration>
</plugin>
<!-- end::plugin[] -->
</plugins>
</build>
</project>

View File

@@ -0,0 +1,12 @@
package com.blogspot.toomuchcoding.frauddetection;
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);
}
}

View File

@@ -0,0 +1,39 @@
package com.blogspot.toomuchcoding.frauddetection;
import com.blogspot.toomuchcoding.frauddetection.model.FraudCheck;
import com.blogspot.toomuchcoding.frauddetection.model.FraudCheckResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import static com.blogspot.toomuchcoding.frauddetection.model.FraudCheckStatus.FRAUD;
import static com.blogspot.toomuchcoding.frauddetection.model.FraudCheckStatus.OK;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;
@RestController
public class FraudDetectionController {
private static final String FRAUD_SERVICE_JSON_VERSION_1 = "application/vnd.fraud.v1+json";
private static final String NO_REASON = null;
private static final String AMOUNT_TOO_HIGH = "Amount too high";
private static final BigDecimal MAX_AMOUNT = new BigDecimal("5000");
@RequestMapping(
value = "/fraudcheck",
method = PUT,
consumes = FRAUD_SERVICE_JSON_VERSION_1,
produces = FRAUD_SERVICE_JSON_VERSION_1)
public FraudCheckResult fraudCheck(@RequestBody FraudCheck fraudCheck) {
if (amountGreaterThanThreshold(fraudCheck)) {
return new FraudCheckResult(FRAUD, AMOUNT_TOO_HIGH);
}
return new FraudCheckResult(OK, NO_REASON);
}
private boolean amountGreaterThanThreshold(FraudCheck fraudCheck) {
return MAX_AMOUNT.compareTo(fraudCheck.getLoanAmount()) < 0;
}
}

View File

@@ -0,0 +1,29 @@
package com.blogspot.toomuchcoding.frauddetection.model;
import java.math.BigDecimal;
public class FraudCheck {
private String clientPesel;
private BigDecimal loanAmount;
public FraudCheck() {
}
public String getClientPesel() {
return clientPesel;
}
public void setClientPesel(String clientPesel) {
this.clientPesel = clientPesel;
}
public BigDecimal getLoanAmount() {
return loanAmount;
}
public void setLoanAmount(BigDecimal loanAmount) {
this.loanAmount = loanAmount;
}
}

View File

@@ -0,0 +1,32 @@
package com.blogspot.toomuchcoding.frauddetection.model;
public class FraudCheckResult {
private FraudCheckStatus fraudCheckStatus;
private String rejectionReason;
public FraudCheckResult() {
}
public FraudCheckResult(FraudCheckStatus fraudCheckStatus, String rejectionReason) {
this.fraudCheckStatus = fraudCheckStatus;
this.rejectionReason = rejectionReason;
}
public FraudCheckStatus getFraudCheckStatus() {
return fraudCheckStatus;
}
public void setFraudCheckStatus(FraudCheckStatus fraudCheckStatus) {
this.fraudCheckStatus = fraudCheckStatus;
}
public String getRejectionReason() {
return rejectionReason;
}
public void setRejectionReason(String rejectionReason) {
this.rejectionReason = rejectionReason;
}
}

View File

@@ -0,0 +1,5 @@
package com.blogspot.toomuchcoding.frauddetection.model;
public enum FraudCheckStatus {
OK, FRAUD
}

View File

@@ -0,0 +1,28 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method 'PUT'
url '/fraudcheck'
body("""
{
"clientPesel":"${value(client(regex('[0-9]{10}')), server('1234567890'))}",
"loanAmount":123.123
}
"""
)
headers {
header('Content-Type', 'application/vnd.fraud.v1+json')
}
}
response {
status 999
body(
fraudCheckStatus: "OK",
rejectionReason: $(client(null), server(execute('assertThatRejectionReasonIsNull($it)')))
)
headers {
header('Content-Type': 'application/vnd.fraud.v1+json')
}
}
}

View File

@@ -0,0 +1,27 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method """PUT"""
url """/fraudcheck"""
body("""
{
"clientPesel":"${value(client(regex('[0-9]{10}')), server('1234567890'))}",
"loanAmount":99999}
"""
)
headers {
header("""Content-Type""", """application/vnd.fraud.v1+json""")
}
}
response {
status 200
body( """{
"fraudCheckStatus": "${value(client('FRAUD'), server(regex('[A-Z]{5}')))}",
"rejectionReason": "Amount too high"
}""")
headers {
header('Content-Type': 'application/vnd.fraud.v1+json')
}
}
}

View File

@@ -0,0 +1,28 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method 'PUT'
url '/fraudcheck'
body("""
{
"clientPesel":"${value(client(regex('[0-9]{10}')), server('1234567890'))}",
"loanAmount":123.123
}
"""
)
headers {
header('Content-Type', 'application/vnd.fraud.v1+json')
}
}
response {
status 200
body(
fraudCheckStatus: "OK",
rejectionReason: $(client(null), server(execute('assertThatRejectionReasonIsNull($it)')))
)
headers {
header('Content-Type': 'application/vnd.fraud.v1+json')
}
}
}

View File

@@ -0,0 +1,14 @@
package com.blogspot.toomuchcoding.frauddetection;
import org.junit.Before;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
public class BaseAccurest {
@Before
public void setup() {
RestAssuredMockMvc.standaloneSetup(new FraudDetectionController());
}
}

View File

@@ -0,0 +1,11 @@
package com.blogspot.toomuchcoding.frauddetection.matchers;
import org.junit.Assert;
public class CustomMatchers {
public static void assertThatRejectionReasonIsNull(String rejectionReason) {
Assert.assertNull(rejectionReason);
}
}

View File

@@ -1,21 +0,0 @@
{
"request" : {
"urlPath" : "/greeting",
"method" : "GET",
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
}
},
"queryParameters" : {
"name" : {
"equalTo" : "Something"
}
}
},
"response" : {
"status" : 200,
"body" : "{\"id\":1,\"content\":\"Hello, Something!\"}"
},
"priority" : 2
}

View File

@@ -1,16 +0,0 @@
{
"request" : {
"url" : "/greeting",
"method" : "GET",
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
}
}
},
"response" : {
"status" : 200,
"body" : "{\"id\":1,\"content\":\"Hello, World!\"}"
},
"priority" : 2
}