From 723aa2d37b7f654b24433b06764e982aa4926bca Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Tue, 23 Feb 2016 14:09:19 +0100 Subject: [PATCH] Fixed selecting the right method builder for test framework. Added functional tests for JUnit. --- .../accurest/SingleTestGenerator.groovy | 5 ++ .../accurest/builder/MethodBuilder.groovy | 8 ++- .../plugin/AccurestGradlePlugin.groovy | 1 + .../plugin/SampleJerseyProjectSpec.groovy | 30 +++++++- .../accurest/plugin/SampleProjectSpec.groovy | 30 +++++++- .../com/blogspot/toomuchcoding/MvcTest.java | 70 +++++++++++++++++++ .../com/blogspot/toomuchcoding/MvcTest.java | 18 +++++ build.gradle | 1 + 8 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 accurest-gradle-plugin/src/test/resources/functionalTest/sampleJerseyProject/fraudDetectionService/src/test/java/com/blogspot/toomuchcoding/MvcTest.java create mode 100644 accurest-gradle-plugin/src/test/resources/functionalTest/sampleProject/fraudDetectionService/src/test/java/com/blogspot/toomuchcoding/MvcTest.java diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy index 12fefbe155..6175152361 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy @@ -62,7 +62,12 @@ class SingleTestGenerator { } if (configProperties.targetFramework == TestFramework.JUNIT) { + if (configProperties.testMode == TestMode.MOCKMVC) { + clazz.addImport('com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification') + clazz.addImport('com.jayway.restassured.response.ResponseOptions') + } clazz.addImport('org.junit.Test') + clazz.addStaticImport('org.assertj.core.api.Assertions.assertThat') } if (configProperties.ruleClassForTests) { diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/MethodBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/MethodBuilder.groovy index 9f0ba231d7..b3d9c5c286 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/MethodBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/MethodBuilder.groovy @@ -48,8 +48,14 @@ class MethodBuilder { blockBuilder.addLine('}') } - private SpockMethodBodyBuilder getMethodBodyBuilder() { + private MethodBodyBuilder getMethodBodyBuilder() { + if (configProperties.testMode == TestMode.MOCKMVC && configProperties.targetFramework == TestFramework.JUNIT){ + return new MockMvcJUnitMethodBodyBuilder(stubContent) + } if (configProperties.testMode == TestMode.JAXRSCLIENT) { + if (configProperties.targetFramework == TestFramework.JUNIT){ + return new JaxRsClientJUnitMethodBodyBuilder(stubContent) + } return new JaxRsClientSpockMethodBodyBuilder(stubContent) } return new MockMvcSpockMethodBodyBuilder(stubContent) diff --git a/accurest-gradle-plugin/src/main/groovy/io/codearte/accurest/plugin/AccurestGradlePlugin.groovy b/accurest-gradle-plugin/src/main/groovy/io/codearte/accurest/plugin/AccurestGradlePlugin.groovy index fc8d4bfded..7edaf642d6 100644 --- a/accurest-gradle-plugin/src/main/groovy/io/codearte/accurest/plugin/AccurestGradlePlugin.groovy +++ b/accurest-gradle-plugin/src/main/groovy/io/codearte/accurest/plugin/AccurestGradlePlugin.groovy @@ -30,6 +30,7 @@ class AccurestGradlePlugin implements Plugin { deprecatedCreateAndConfigureGenerateWiremockClientStubsFromDslTask() project.dependencies.add("testCompile", "com.github.tomakehurst:wiremock:2.0.5-beta") project.dependencies.add("testCompile", "com.blogspot.toomuchcoding:jsonassert:${extension.getJsonAssertVersion()}") + project.dependencies.add("testCompile", "org.assertj:assertj-core:3.3.0") project.afterEvaluate { def hasIdea = project.plugins.findPlugin(IDEA_PLUGIN_CLASS) diff --git a/accurest-gradle-plugin/src/test/groovy/io/codearte/accurest/plugin/SampleJerseyProjectSpec.groovy b/accurest-gradle-plugin/src/test/groovy/io/codearte/accurest/plugin/SampleJerseyProjectSpec.groovy index 789d897199..9784abcb05 100755 --- a/accurest-gradle-plugin/src/test/groovy/io/codearte/accurest/plugin/SampleJerseyProjectSpec.groovy +++ b/accurest-gradle-plugin/src/test/groovy/io/codearte/accurest/plugin/SampleJerseyProjectSpec.groovy @@ -3,19 +3,47 @@ package io.codearte.accurest.plugin import nebula.test.IntegrationSpec import spock.lang.Stepwise +import java.nio.file.Files +import java.nio.file.Path + +import static java.nio.charset.StandardCharsets.UTF_8 +import static java.nio.charset.StandardCharsets.UTF_8 + @Stepwise class SampleJerseyProjectSpec extends IntegrationSpec { + public static final String SPOCK = "targetFramework = 'Spock'" + public static final String JUNIT = "targetFramework = 'JUnit'" + public static final String MVC_SPEC = "baseClassForTests = 'com.blogspot.toomuchcoding.MvcSpec'" + public static final String MVC_TEST = "baseClassForTests = 'com.blogspot.toomuchcoding.MvcTest'" + void setup() { copyResources("functionalTest/sampleJerseyProject", "") runTasksSuccessfully('clean') //delete accidental output when previously importing SimpleBoot into Idea to tweak it } - def "should pass basic flow"() { + def "should pass basic flow for Spock"() { given: assert fileExists('build.gradle') expect: runTasksSuccessfully('check') } + def "should pass basic flow for JUnit"() { + given: + switchToJunitTestFramework() + assert fileExists('build.gradle') + expect: + runTasksSuccessfully('check') + } + + + private void switchToJunitTestFramework() { + Path path = buildFile.toPath() + String content = new StringBuilder(new String(Files.readAllBytes(path), UTF_8)).replaceAll(SPOCK, JUNIT) + .replaceAll(MVC_SPEC, MVC_TEST) + Files.write(path, content.getBytes(UTF_8)) + } + + } diff --git a/accurest-gradle-plugin/src/test/groovy/io/codearte/accurest/plugin/SampleProjectSpec.groovy b/accurest-gradle-plugin/src/test/groovy/io/codearte/accurest/plugin/SampleProjectSpec.groovy index e32b8810f4..4ff481157f 100755 --- a/accurest-gradle-plugin/src/test/groovy/io/codearte/accurest/plugin/SampleProjectSpec.groovy +++ b/accurest-gradle-plugin/src/test/groovy/io/codearte/accurest/plugin/SampleProjectSpec.groovy @@ -2,20 +2,48 @@ package io.codearte.accurest.plugin import nebula.test.IntegrationSpec import spock.lang.Stepwise +import spock.lang.Unroll + +import java.nio.file.Files +import java.nio.file.Path + +import static java.nio.charset.StandardCharsets.UTF_8 +import static java.nio.charset.StandardCharsets.UTF_8 @Stepwise class SampleProjectSpec extends IntegrationSpec { + public static final String SPOCK = "targetFramework = 'Spock'" + public static final String JUNIT = "targetFramework = 'JUnit'" + public static final String MVC_SPEC = "baseClassForTests = 'com.blogspot.toomuchcoding.MvcSpec'" + public static final String MVC_TEST = "baseClassForTests = 'com.blogspot.toomuchcoding.MvcTest'" + void setup() { copyResources("functionalTest/sampleProject", "") runTasksSuccessfully('clean') //delete accidental output when previously importing SimpleBoot into Idea to tweak it } - def "should pass basic flow"() { + def "should pass basic flow for Spock"() { given: assert fileExists('build.gradle') expect: runTasksSuccessfully('check') } + def "should pass basic flow for JUnit"() { + given: + switchToJunitTestFramework() + assert fileExists('build.gradle') + expect: + runTasksSuccessfully('check') + } + + + private void switchToJunitTestFramework() { + Path path = buildFile.toPath() + String content = new StringBuilder(new String(Files.readAllBytes(path), UTF_8)).replaceAll(SPOCK, JUNIT) + .replaceAll(MVC_SPEC, MVC_TEST) + Files.write(path, content.getBytes(UTF_8)) + } + } diff --git a/accurest-gradle-plugin/src/test/resources/functionalTest/sampleJerseyProject/fraudDetectionService/src/test/java/com/blogspot/toomuchcoding/MvcTest.java b/accurest-gradle-plugin/src/test/resources/functionalTest/sampleJerseyProject/fraudDetectionService/src/test/java/com/blogspot/toomuchcoding/MvcTest.java new file mode 100644 index 0000000000..d13134ecc8 --- /dev/null +++ b/accurest-gradle-plugin/src/test/resources/functionalTest/sampleJerseyProject/fraudDetectionService/src/test/java/com/blogspot/toomuchcoding/MvcTest.java @@ -0,0 +1,70 @@ +package com.blogspot.toomuchcoding; +import com.blogspot.toomuchcoding.frauddetection.Application; +import com.blogspot.toomuchcoding.frauddetection.FraudRestApplication; +import org.eclipse.jetty.server.Server; +import org.glassfish.jersey.apache.connector.ApacheConnectorProvider; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.jetty.JettyHttpContainerFactory; +import org.glassfish.jersey.server.ResourceConfig; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.UriBuilder; + +import java.net.URI; + +import static org.springframework.util.SocketUtils.findAvailableTcpPort; + +public abstract class MvcTest { + + public static WebTarget webTarget; + + private static Server server; + + private static Client client; + + @BeforeClass + public static void setupTest() { + + URI baseUri = UriBuilder.fromUri("http://localhost").port(findAvailableTcpPort(8000)).build(); + + + ResourceConfig resourceConfig = ResourceConfig.forApplication(new FraudRestApplication()); + resourceConfig.property("contextConfig", new AnnotationConfigApplicationContext(Application.class)); + server = JettyHttpContainerFactory.createServer(baseUri, resourceConfig, true); + + ClientConfig clientConfig = new ClientConfig(); + clientConfig.connectorProvider(new ApacheConnectorProvider()); + client = ClientBuilder.newClient(clientConfig); + + webTarget = client.target(baseUri); + + try { + server.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void cleanupTest() { + if(client != null) { + client.close(); + } + if (server != null) { + try { + server.stop(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public void assertThatRejectionReasonIsNull(Object rejectionReason) { + assert rejectionReason == null; + } +} \ No newline at end of file diff --git a/accurest-gradle-plugin/src/test/resources/functionalTest/sampleProject/fraudDetectionService/src/test/java/com/blogspot/toomuchcoding/MvcTest.java b/accurest-gradle-plugin/src/test/resources/functionalTest/sampleProject/fraudDetectionService/src/test/java/com/blogspot/toomuchcoding/MvcTest.java new file mode 100644 index 0000000000..e9d083bde4 --- /dev/null +++ b/accurest-gradle-plugin/src/test/resources/functionalTest/sampleProject/fraudDetectionService/src/test/java/com/blogspot/toomuchcoding/MvcTest.java @@ -0,0 +1,18 @@ +package com.blogspot.toomuchcoding; + +import com.blogspot.toomuchcoding.frauddetection.FraudDetectionController; +import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc; +import spock.lang.Specification; +import org.junit.Before; + +public class MvcTest { + + @Before + public void setup() { + RestAssuredMockMvc.standaloneSetup(new com.blogspot.toomuchcoding.frauddetection.FraudDetectionController()); + } + + public void assertThatRejectionReasonIsNull(Object rejectionReason) { + assert rejectionReason== null; + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 16011ddca1..1895c32f22 100644 --- a/build.gradle +++ b/build.gradle @@ -73,6 +73,7 @@ project(':accurest-core') { compile 'asm:asm:3.3.1' compile "com.github.tomakehurst:wiremock:$wiremockVersion" compile "com.blogspot.toomuchcoding:jsonassert:$jsonassertVersion" + compile 'org.assertj:assertj-core:[3.3.0,)' testCompile 'cglib:cglib-nodep:2.2' testCompile 'org.objenesis:objenesis:2.1' testCompile project(':accurest-testing-utils')