[rest-gradle-plugin] 15/83: Add task and plugin tests

Alastair McKinstry mckinstry at moszumanska.debian.org
Wed Oct 25 15:59:02 UTC 2017


This is an automated email from the git hooks/post-receive script.

mckinstry pushed a commit to branch debian/master
in repository rest-gradle-plugin.

commit 602f72e128cd5d6aaebf4d32aa63f893d80ef588
Author: noamt <noamt at jfrog.com>
Date:   Thu Apr 4 11:47:05 2013 +0300

    Add task and plugin tests
---
 build.gradle                                       |  3 +-
 .../groovy/org/_10ne/gradle/rest/RestPlugin.groovy |  2 +-
 .../groovy/org/_10ne/gradle/rest/RestTask.groovy   | 11 ++--
 .../org/_10ne/gradle/rest/RestPluginSpec.groovy    | 26 +++++++++
 .../org/_10ne/gradle/rest/RestTaskSpec.groovy      | 67 ++++++++++++++++++++++
 5 files changed, 103 insertions(+), 6 deletions(-)

diff --git a/build.gradle b/build.gradle
index 7998522..c3a9934 100644
--- a/build.gradle
+++ b/build.gradle
@@ -30,11 +30,12 @@ repositories {
 
 dependencies {
     compile gradleApi()
-    groovy 'org.codehaus.groovy:groovy-all:2.1.2'
     compile('org.codehaus.groovy.modules.http-builder:http-builder:0.6') {
         exclude group: 'org.codehaus.groovy', module: 'groovy-all'
         exclude group: 'org.codehaus.groovy', module: 'groovy'
     }
+    testCompile 'org.spockframework:spock-core:0.7-groovy-1.8'
+    testCompile 'cglib:cglib-nodep:2.2.2'
 }
 
 task sourcesJar(type: Jar, dependsOn: classes) {
diff --git a/src/main/groovy/org/_10ne/gradle/rest/RestPlugin.groovy b/src/main/groovy/org/_10ne/gradle/rest/RestPlugin.groovy
index 0d8b6a0..98b3a4c 100644
--- a/src/main/groovy/org/_10ne/gradle/rest/RestPlugin.groovy
+++ b/src/main/groovy/org/_10ne/gradle/rest/RestPlugin.groovy
@@ -26,6 +26,6 @@ class RestPlugin implements Plugin<Project> {
 
     @Override
     void apply(Project project) {
-        project.task("rest", type: RestTask)
+        project.task('rest', type: RestTask)
     }
 }
diff --git a/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy b/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy
index a790bc7..55c5dec 100644
--- a/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy
+++ b/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy
@@ -20,16 +20,18 @@ import groovyx.net.http.HttpResponseDecorator
 import groovyx.net.http.RESTClient
 import org.apache.commons.lang.StringUtils
 import org.gradle.api.DefaultTask
+import org.gradle.api.InvalidUserDataException
 import org.gradle.api.tasks.Input
 import org.gradle.api.tasks.Optional
 import org.gradle.api.tasks.TaskAction
-import org.gradle.api.tasks.TaskExecutionException
 
 /**
  * @author Noam Y. Tenne
  */
 class RestTask extends DefaultTask {
 
+    def client
+
     @Input String httpMethod
     @Input Object uri
     @Input
@@ -45,15 +47,16 @@ class RestTask extends DefaultTask {
 
     RestTask() {
         httpMethod = 'get'
+        client = new RESTClient()
     }
 
     @TaskAction
     void executeRequest() {
-        if (!uri || StringUtils.isBlank(uri.toString())) {
-            throw new TaskExecutionException(this, new IllegalArgumentException('No resource URI provided'))
+        if (!uri || StringUtils.isBlank(uri)) {
+            throw new InvalidUserDataException('No resource URI provided')
         }
 
-        def client = new RESTClient(uri)
+        client.uri = uri
         if (StringUtils.isNotBlank(username)) {
             client.auth.basic(username, password)
         }
diff --git a/src/test/groovy/org/_10ne/gradle/rest/RestPluginSpec.groovy b/src/test/groovy/org/_10ne/gradle/rest/RestPluginSpec.groovy
new file mode 100644
index 0000000..ab86879
--- /dev/null
+++ b/src/test/groovy/org/_10ne/gradle/rest/RestPluginSpec.groovy
@@ -0,0 +1,26 @@
+package org._10ne.gradle.rest
+
+import org.gradle.testfixtures.ProjectBuilder
+import spock.lang.Specification
+
+/**
+ * @author Noam Y. Tenne
+ */
+class RestPluginSpec extends Specification {
+
+    def 'Apply the plugin to a project and check the default values of the configuration'() {
+        setup:
+        def project = ProjectBuilder.builder().build()
+
+        expect:
+        project.tasks.findByName('rest') == null
+
+        when:
+        project.apply plugin: RestPlugin
+
+        then:
+        def restTask = project.tasks.findByName('rest')
+        restTask != null
+        restTask.httpMethod == 'get'
+    }
+}
diff --git a/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy b/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy
new file mode 100644
index 0000000..cf40d0d
--- /dev/null
+++ b/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy
@@ -0,0 +1,67 @@
+package org._10ne.gradle.rest
+
+import groovyx.net.http.AuthConfig
+import groovyx.net.http.HttpResponseDecorator
+import groovyx.net.http.RESTClient
+import org.gradle.api.InvalidUserDataException
+import org.gradle.api.Project
+import org.gradle.api.Task
+import org.gradle.testfixtures.ProjectBuilder
+import spock.lang.Specification
+
+/**
+ * @author Noam Y. Tenne
+ */
+class RestTaskSpec extends Specification {
+
+    Project project
+
+    def setup() {
+        project = ProjectBuilder.builder().build()
+    }
+
+    def 'Execute a request with no URI'() {
+        setup:
+        Task task = project.tasks.add(name: 'noUri', type: RestTask) {}
+
+        when:
+        task.executeRequest()
+
+        then:
+        thrown(InvalidUserDataException)
+    }
+
+    def 'Configure and execute a request'() {
+        setup:
+        Task task = project.tasks.add(name: 'request', type: RestTask) {
+            httpMethod = 'post'
+            uri = 'bob.com'
+            username = 'username'
+            password = 'password'
+            requestContentType = 'requestContentType'
+            requestBody = 'requestBody'
+            contentType = 'contentType'
+        }
+        def mockClient = Mock(RESTClient)
+        task.client = mockClient
+
+        def mockAuth = Mock(AuthConfig)
+
+        def mockResponse = Mock(HttpResponseDecorator)
+
+        when:
+        task.executeRequest()
+
+        then:
+        1 * mockClient.setUri('bob.com')
+        1 * mockClient.getAuth() >> { mockAuth }
+        1 * mockAuth.basic('username', 'password')
+        1 * mockClient.post(_ as Map) >> { Map params ->
+            assert params.body == 'requestBody'
+            assert params.contentType == 'contentType'
+            assert params.requestContentType == 'requestContentType'
+            mockResponse
+        }
+        1 * mockResponse.getData() >> { 'somedata' }
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/rest-gradle-plugin.git



More information about the pkg-java-commits mailing list