From 69bdf2b5fcfa1423585354c1be2486d86f2747b5 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 1 Sep 2019 15:46:43 +0300 Subject: [PATCH 1/8] BAEL-3190 - Working with web services in Groovy --- core-groovy-2/pom.xml | 5 + .../webservice/WebserviceUnitTest.groovy | 148 ++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index ba3fd0802b..85f6106e95 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -48,6 +48,11 @@ ${spock-core.version} test + + com.github.groovy-wslite + groovy-wslite + 1.1.3 + diff --git a/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy new file mode 100644 index 0000000000..edf2dca33f --- /dev/null +++ b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy @@ -0,0 +1,148 @@ +package com.baeldung.webservice + +import groovy.json.JsonSlurper +import wslite.rest.ContentType +import wslite.rest.RESTClient +import wslite.rest.RESTClientException +import wslite.soap.SOAPClient +import wslite.soap.SOAPMessageBuilder + +class WebserviceUnitTest extends GroovyTestCase { + + JsonSlurper jsonSlurper = new JsonSlurper() + + static RESTClient client = new RESTClient("https://postman-echo.com") + + static { + client.defaultAcceptHeader = ContentType.JSON + client.httpClient.sslTrustAllCerts = true + } + + void testHttpGetRequest() { + def postmanGet = new URL('https://postman-echo.com/get') + def getConnection = postmanGet.openConnection() + getConnection.requestMethod = 'GET' + assert getConnection.responseCode == 200 + if (getConnection.responseCode == 200) { + assert jsonSlurper.parseText(getConnection.content.text)?.headers?.host == "postman-echo.com" + } + } + + void testHttpPostRequest() { + def postmanPost = new URL('https://postman-echo.com/post') + def query = "q=This is post request form parameter." + def postConnection = postmanPost.openConnection() + postConnection.requestMethod = 'POST' + assert postConnection.responseCode == 200 + } + + void testHttpPostRequestWithParams() { + def postmanPost = new URL('https://postman-echo.com/post') + def form = "param1=This is request parameter." + def postConnection = postmanPost.openConnection() + postConnection.requestMethod = 'POST' + postConnection.doOutput = true + def text + postConnection.with { + outputStream.withWriter { outputStreamWriter -> + outputStreamWriter << form + } + text = content.text + } + assert postConnection.responseCode == 200 + assert jsonSlurper.parseText(text)?.json.param1 == "This is request parameter." + } + + void testRssFeed() { + def rssFeed = new XmlParser().parse("https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en") + def stories = [] + (0..4).each { + def item = rssFeed.channel.item.get(it) + stories << item.title.text() + } + assert stories.size() == 5 + } + + void testAtomFeed() { + def atomFeed = new XmlParser().parse("https://news.google.com/atom?hl=en-US&gl=US&ceid=US:en") + def stories = [] + (0..4).each { + def entry = atomFeed.entry.get(it) + stories << entry.title.text() + } + assert stories.size() == 5 + } + + void testSoapClient() { + def url = "http://www.dataaccess.com/webservicesserver/numberconversion.wso" + def soapClient = new SOAPClient(url) + def message = new SOAPMessageBuilder().build({ + body { + NumberToWords(xmlns: "http://www.dataaccess.com/webservicesserver/") { + ubiNum(1234) + } + } + }) + def response = soapClient.send(message.toString()); + def words = response.NumberToWordsResponse + assert words == "one thousand two hundred and thirty four " + } + + void testRestClientGet() { + def path = "/get" + def response + try { + response = client.get(path: path) + assert response.statusCode == 200 + assert response.json?.headers?.host == "postman-echo.com" + } catch (RESTClientException e) { + assert e?.response?.statusCode != 200 + } + } + + void testRestClientPost() { + def path = "/post" + def params = ["foo":1,"bar":2] + def response + try { + response = client.post(path: path) { + type ContentType.JSON + json params + } + assert response.json?.data == params + } catch (RESTClientException e) { + e.printStackTrace() + assert e?.response?.statusCode != 200 + } + } + + void testBasicAuthentication() { + def path = "/basic-auth" + def response + try { + response = client.get(path: path, headers: ["Authorization": "Basic cG9zdG1hbjpwYXNzd29yZA=="]) + assert response.statusCode == 200 + assert response.json?.authenticated == true + } catch (RESTClientException e) { + assert e?.response?.statusCode != 200 + } + } + + void testOAuth() { + RESTClient oAuthClient = new RESTClient("https://postman-echo.com") + oAuthClient.defaultAcceptHeader = ContentType.JSON + oAuthClient.httpClient.sslTrustAllCerts = true + + def path = "/oauth1" + def params = [oauth_consumer_key: "RKCGzna7bv9YD57c", oauth_signature_method: "HMAC-SHA1", oauth_timestamp:1567089944, oauth_nonce: "URT7v4", oauth_version: 1.0, oauth_signature: 'RGgR/ktDmclkM0ISWaFzebtlO0A='] + def response + try { + response = oAuthClient.get(path: path, query: params) + assert response.statusCode == 200 + assert response.statusMessage == "OK" + assert response.json.status == "pass" + } catch (RESTClientException e) { + assert e?.response?.statusCode != 200 + } + } +} \ No newline at end of file From f29bb940134706877f4df7e5dc5d404188468383 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 1 Sep 2019 15:48:07 +0300 Subject: [PATCH 2/8] BAEL-3190 - Working with web services in Groovy - pom.xml code indentation --- core-groovy-2/pom.xml | 357 +++++++++++++++++++++--------------------- 1 file changed, 179 insertions(+), 178 deletions(-) diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index 85f6106e95..6f2f9bd533 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -1,189 +1,190 @@ - - 4.0.0 - core-groovy-2 - 1.0-SNAPSHOT - core-groovy-2 - jar + + 4.0.0 + core-groovy-2 + 1.0-SNAPSHOT + core-groovy-2 + jar - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - org.codehaus.groovy - groovy-all - ${groovy.version} - pom - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - org.hsqldb - hsqldb - ${hsqldb.version} - test - - - org.spockframework - spock-core - ${spock-core.version} - test - - - com.github.groovy-wslite - groovy-wslite - 1.1.3 + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} - + + ch.qos.logback + logback-classic + ${logback.version} + + + org.codehaus.groovy + groovy-all + ${groovy.version} + pom + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.hsqldb + hsqldb + ${hsqldb.version} + test + + + org.spockframework + spock-core + ${spock-core.version} + test + + + com.github.groovy-wslite + groovy-wslite + 1.1.3 + + - - src/main/groovy - src/main/java - - - org.codehaus.groovy - groovy-eclipse-compiler - 3.3.0-01 - true - - - maven-compiler-plugin - 3.8.0 - - groovy-eclipse-compiler - ${java.version} - ${java.version} - - - - org.codehaus.groovy - groovy-eclipse-compiler - 3.3.0-01 - - - org.codehaus.groovy - groovy-eclipse-batch - ${groovy.version}-01 - - - - - maven-failsafe-plugin - ${maven-failsafe-plugin.version} - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - - - - - junit5 - - integration-test - verify - - - - **/*Test5.java - - - - - - - maven-surefire-plugin - 2.20.1 - - false - - **/*Test.java - **/*Spec.java - - - - - - org.apache.maven.plugins - maven-assembly-plugin - 3.1.0 - - - - jar-with-dependencies - - - - - com.baeldung.MyJointCompilationApp - - + + src/main/groovy + src/main/java + + + org.codehaus.groovy + groovy-eclipse-compiler + 3.3.0-01 + true + + + maven-compiler-plugin + 3.8.0 + + groovy-eclipse-compiler + ${java.version} + ${java.version} + + + + org.codehaus.groovy + groovy-eclipse-compiler + 3.3.0-01 + + + org.codehaus.groovy + groovy-eclipse-batch + ${groovy.version}-01 + + + + + maven-failsafe-plugin + ${maven-failsafe-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + junit5 + + integration-test + verify + + + + **/*Test5.java + + + + + + + maven-surefire-plugin + 2.20.1 + + false + + **/*Test.java + **/*Spec.java + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.1.0 + + + + jar-with-dependencies + + + + + com.baeldung.MyJointCompilationApp + + - - - - make-assembly - - package - - single - - - - - - + + + + make-assembly + + package + + single + + + + + + - - - central - http://jcenter.bintray.com - - + + + central + http://jcenter.bintray.com + + - - - bintray - Groovy Bintray - https://dl.bintray.com/groovy/maven - - - never - - - false - - - + + + bintray + Groovy Bintray + https://dl.bintray.com/groovy/maven + + + never + + + false + + + - - 1.0.0 - 2.4.0 - 1.1-groovy-2.4 - 3.8.1 - 1.2.3 - 2.5.7 - UTF-8 - + + 1.0.0 + 2.4.0 + 1.1-groovy-2.4 + 3.8.1 + 1.2.3 + 2.5.7 + UTF-8 + From 78e2331c4fc6be1a1e577b8cda7543eb4aba0dc5 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 1 Sep 2019 15:51:19 +0300 Subject: [PATCH 3/8] BAEL-3190 - Working with web services in Groovy - pom.xml code indentation --- core-groovy-2/pom.xml | 351 +++++++++++++++++++++--------------------- 1 file changed, 175 insertions(+), 176 deletions(-) diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index 6f2f9bd533..2ffef7ae94 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -1,190 +1,189 @@ - - 4.0.0 - core-groovy-2 - 1.0-SNAPSHOT - core-groovy-2 - jar + + 4.0.0 + core-groovy-2 + 1.0-SNAPSHOT + core-groovy-2 + jar - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - org.codehaus.groovy - groovy-all - ${groovy.version} - pom - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - org.hsqldb - hsqldb - ${hsqldb.version} - test - - - org.spockframework - spock-core - ${spock-core.version} - test - - + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + org.codehaus.groovy + groovy-all + ${groovy.version} + pom + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.hsqldb + hsqldb + ${hsqldb.version} + test + + + org.spockframework + spock-core + ${spock-core.version} + test + + com.github.groovy-wslite groovy-wslite 1.1.3 - + - - src/main/groovy - src/main/java - - - org.codehaus.groovy - groovy-eclipse-compiler - 3.3.0-01 - true - - - maven-compiler-plugin - 3.8.0 - - groovy-eclipse-compiler - ${java.version} - ${java.version} - - - - org.codehaus.groovy - groovy-eclipse-compiler - 3.3.0-01 - - - org.codehaus.groovy - groovy-eclipse-batch - ${groovy.version}-01 - - - - - maven-failsafe-plugin - ${maven-failsafe-plugin.version} - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - - - - - junit5 - - integration-test - verify - - - - **/*Test5.java - - - - - - - maven-surefire-plugin - 2.20.1 - - false - - **/*Test.java - **/*Spec.java - - - - - - org.apache.maven.plugins - maven-assembly-plugin - 3.1.0 - - - - jar-with-dependencies - - - - - com.baeldung.MyJointCompilationApp - - + + src/main/groovy + src/main/java + + + org.codehaus.groovy + groovy-eclipse-compiler + 3.3.0-01 + true + + + maven-compiler-plugin + 3.8.0 + + groovy-eclipse-compiler + ${java.version} + ${java.version} + + + + org.codehaus.groovy + groovy-eclipse-compiler + 3.3.0-01 + + + org.codehaus.groovy + groovy-eclipse-batch + ${groovy.version}-01 + + + + + maven-failsafe-plugin + ${maven-failsafe-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + junit5 + + integration-test + verify + + + + **/*Test5.java + + + + + + + maven-surefire-plugin + 2.20.1 + + false + + **/*Test.java + **/*Spec.java + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.1.0 + + + + jar-with-dependencies + + + + + com.baeldung.MyJointCompilationApp + + - - - - make-assembly - - package - - single - - - - - - + + + + make-assembly + + package + + single + + + + + + - - - central - http://jcenter.bintray.com - - + + + central + http://jcenter.bintray.com + + - - - bintray - Groovy Bintray - https://dl.bintray.com/groovy/maven - - - never - - - false - - - + + + bintray + Groovy Bintray + https://dl.bintray.com/groovy/maven + + + never + + + false + + + - - 1.0.0 - 2.4.0 - 1.1-groovy-2.4 - 3.8.1 - 1.2.3 - 2.5.7 - UTF-8 - + + 1.0.0 + 2.4.0 + 1.1-groovy-2.4 + 3.8.1 + 1.2.3 + 2.5.7 + UTF-8 + From e5ff15e4aa07667df47af930f6c30973e9c18ee3 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 1 Sep 2019 15:56:26 +0300 Subject: [PATCH 4/8] BAEL-3190 - Working with web services in Groovy - pom.xml code indentation --- core-groovy-2/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index 2ffef7ae94..1f5baaf904 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -51,7 +51,7 @@ com.github.groovy-wslite groovy-wslite - 1.1.3 + ${groovy-wslite.version} @@ -180,6 +180,7 @@ 1.0.0 2.4.0 1.1-groovy-2.4 + 1.1.3 3.8.1 1.2.3 2.5.7 From a92e0ec2bc48618066281fe383cf12b74fcea5bc Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 1 Sep 2019 15:57:37 +0300 Subject: [PATCH 5/8] BAEL-3190 - Working with web services in Groovy - pom.xml code indentation --- core-groovy-2/pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index 1f5baaf904..40e38bef16 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -49,10 +49,10 @@ test - com.github.groovy-wslite - groovy-wslite - ${groovy-wslite.version} - + com.github.groovy-wslite + groovy-wslite + ${groovy-wslite.version} + @@ -180,7 +180,7 @@ 1.0.0 2.4.0 1.1-groovy-2.4 - 1.1.3 + 1.1.3 3.8.1 1.2.3 2.5.7 From d3df5e0d9758abb8b157d3c7f677d1516cf04d25 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 1 Sep 2019 23:08:53 +0300 Subject: [PATCH 6/8] BAEL-3190 - Working with web services in Groovy - pom.xml code indentation --- core-groovy-2/pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index 40e38bef16..91e31d4cba 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -49,9 +49,9 @@ test - com.github.groovy-wslite - groovy-wslite - ${groovy-wslite.version} + com.github.groovy-wslite + groovy-wslite + ${groovy-wslite.version} From 5b2dec57486fc754e3ed5c02b4d80ddd0660eba6 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Tue, 3 Sep 2019 13:12:17 +0300 Subject: [PATCH 7/8] BAEL-3190 - Working with web services in Groovy - basic authorization --- .../groovy/com/baeldung/webservice/WebserviceUnitTest.groovy | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy index edf2dca33f..0d4b5cd7ee 100644 --- a/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy +++ b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy @@ -6,6 +6,7 @@ import wslite.rest.RESTClient import wslite.rest.RESTClientException import wslite.soap.SOAPClient import wslite.soap.SOAPMessageBuilder +import wslite.http.auth.HTTPBasicAuthorization class WebserviceUnitTest extends GroovyTestCase { @@ -120,10 +121,12 @@ class WebserviceUnitTest extends GroovyTestCase { def path = "/basic-auth" def response try { - response = client.get(path: path, headers: ["Authorization": "Basic cG9zdG1hbjpwYXNzd29yZA=="]) + client.authorization = new HTTPBasicAuthorization("postman", "password") + response = client.get(path: path) assert response.statusCode == 200 assert response.json?.authenticated == true } catch (RESTClientException e) { + e.printStackTrace() assert e?.response?.statusCode != 200 } } From cb1de34d0f6e04f13cf61c959adf314af26feeab Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Fri, 6 Sep 2019 07:06:26 +0300 Subject: [PATCH 8/8] BAEL-3190 - working with webservices in groovy - followed test names convention --- .../webservice/WebserviceUnitTest.groovy | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy index 0d4b5cd7ee..302959d0d9 100644 --- a/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy +++ b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy @@ -7,6 +7,7 @@ import wslite.rest.RESTClientException import wslite.soap.SOAPClient import wslite.soap.SOAPMessageBuilder import wslite.http.auth.HTTPBasicAuthorization +import org.junit.Test class WebserviceUnitTest extends GroovyTestCase { @@ -19,7 +20,7 @@ class WebserviceUnitTest extends GroovyTestCase { client.httpClient.sslTrustAllCerts = true } - void testHttpGetRequest() { + void test_whenSendingGet_thenRespose200() { def postmanGet = new URL('https://postman-echo.com/get') def getConnection = postmanGet.openConnection() getConnection.requestMethod = 'GET' @@ -29,7 +30,7 @@ class WebserviceUnitTest extends GroovyTestCase { } } - void testHttpPostRequest() { + void test_whenSendingPost_thenRespose200() { def postmanPost = new URL('https://postman-echo.com/post') def query = "q=This is post request form parameter." def postConnection = postmanPost.openConnection() @@ -37,7 +38,7 @@ class WebserviceUnitTest extends GroovyTestCase { assert postConnection.responseCode == 200 } - void testHttpPostRequestWithParams() { + void test_whenSendingPostWithParams_thenRespose200() { def postmanPost = new URL('https://postman-echo.com/post') def form = "param1=This is request parameter." def postConnection = postmanPost.openConnection() @@ -54,7 +55,7 @@ class WebserviceUnitTest extends GroovyTestCase { assert jsonSlurper.parseText(text)?.json.param1 == "This is request parameter." } - void testRssFeed() { + void test_whenReadingRSS_thenReceiveFeed() { def rssFeed = new XmlParser().parse("https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en") def stories = [] (0..4).each { @@ -64,7 +65,7 @@ class WebserviceUnitTest extends GroovyTestCase { assert stories.size() == 5 } - void testAtomFeed() { + void test_whenReadingAtom_thenReceiveFeed() { def atomFeed = new XmlParser().parse("https://news.google.com/atom?hl=en-US&gl=US&ceid=US:en") def stories = [] (0..4).each { @@ -74,7 +75,7 @@ class WebserviceUnitTest extends GroovyTestCase { assert stories.size() == 5 } - void testSoapClient() { + void test_whenConsumingSoap_thenReceiveResponse() { def url = "http://www.dataaccess.com/webservicesserver/numberconversion.wso" def soapClient = new SOAPClient(url) def message = new SOAPMessageBuilder().build({ @@ -89,7 +90,7 @@ class WebserviceUnitTest extends GroovyTestCase { assert words == "one thousand two hundred and thirty four " } - void testRestClientGet() { + void test_whenConsumingRestGet_thenReceiveResponse() { def path = "/get" def response try { @@ -101,7 +102,7 @@ class WebserviceUnitTest extends GroovyTestCase { } } - void testRestClientPost() { + void test_whenConsumingRestPost_thenReceiveResponse() { def path = "/post" def params = ["foo":1,"bar":2] def response @@ -117,7 +118,7 @@ class WebserviceUnitTest extends GroovyTestCase { } } - void testBasicAuthentication() { + void test_whenBasicAuthentication_thenReceive200() { def path = "/basic-auth" def response try { @@ -131,7 +132,7 @@ class WebserviceUnitTest extends GroovyTestCase { } } - void testOAuth() { + void test_whenOAuth_thenReceive200() { RESTClient oAuthClient = new RESTClient("https://postman-echo.com") oAuthClient.defaultAcceptHeader = ContentType.JSON oAuthClient.httpClient.sslTrustAllCerts = true