1+ package com.baeldung.webservice
2+
3+ import groovy.json.JsonSlurper
4+ import wslite.rest.ContentType
5+ import wslite.rest.RESTClient
6+ import wslite.rest.RESTClientException
7+ import wslite.soap.SOAPClient
8+ import wslite.soap.SOAPMessageBuilder
9+ import wslite.http.auth.HTTPBasicAuthorization
10+ import org.junit.Test
11+
12+ class WebserviceUnitTest extends GroovyTestCase {
13+
14+ JsonSlurper jsonSlurper = new JsonSlurper ()
15+
16+ static RESTClient client = new RESTClient (" https://postman-echo.com" )
17+
18+ static {
19+ client. defaultAcceptHeader = ContentType . JSON
20+ client. httpClient. sslTrustAllCerts = true
21+ }
22+
23+ void test_whenSendingGet_thenRespose200 () {
24+ def postmanGet = new URL (' https://postman-echo.com/get' )
25+ def getConnection = postmanGet. openConnection()
26+ getConnection. requestMethod = ' GET'
27+ assert getConnection. responseCode == 200
28+ if (getConnection. responseCode == 200 ) {
29+ assert jsonSlurper. parseText(getConnection. content. text)?. headers?. host == " postman-echo.com"
30+ }
31+ }
32+
33+ void test_whenSendingPost_thenRespose200 () {
34+ def postmanPost = new URL (' https://postman-echo.com/post' )
35+ def query = " q=This is post request form parameter."
36+ def postConnection = postmanPost. openConnection()
37+ postConnection. requestMethod = ' POST'
38+ assert postConnection. responseCode == 200
39+ }
40+
41+ void test_whenSendingPostWithParams_thenRespose200 () {
42+ def postmanPost = new URL (' https://postman-echo.com/post' )
43+ def form = " param1=This is request parameter."
44+ def postConnection = postmanPost. openConnection()
45+ postConnection. requestMethod = ' POST'
46+ postConnection. doOutput = true
47+ def text
48+ postConnection. with {
49+ outputStream. withWriter { outputStreamWriter ->
50+ outputStreamWriter << form
51+ }
52+ text = content. text
53+ }
54+ assert postConnection. responseCode == 200
55+ assert jsonSlurper. parseText(text)?. json. param1 == " This is request parameter."
56+ }
57+
58+ void test_whenReadingRSS_thenReceiveFeed () {
59+ def rssFeed = new XmlParser (). parse(" https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en" )
60+ def stories = []
61+ (0 .. 4 ). each {
62+ def item = rssFeed. channel. item. get(it)
63+ stories << item. title. text()
64+ }
65+ assert stories. size() == 5
66+ }
67+
68+ void test_whenReadingAtom_thenReceiveFeed () {
69+ def atomFeed = new XmlParser (). parse(" https://news.google.com/atom?hl=en-US&gl=US&ceid=US:en" )
70+ def stories = []
71+ (0 .. 4 ). each {
72+ def entry = atomFeed. entry. get(it)
73+ stories << entry. title. text()
74+ }
75+ assert stories. size() == 5
76+ }
77+
78+ void test_whenConsumingSoap_thenReceiveResponse () {
79+ def url = " http://www.dataaccess.com/webservicesserver/numberconversion.wso"
80+ def soapClient = new SOAPClient (url)
81+ def message = new SOAPMessageBuilder (). build({
82+ body {
83+ NumberToWords (xmlns : " http://www.dataaccess.com/webservicesserver/" ) {
84+ ubiNum(1234 )
85+ }
86+ }
87+ })
88+ def response = soapClient. send(message. toString());
89+ def words = response.NumberToWordsResponse
90+ assert words == " one thousand two hundred and thirty four "
91+ }
92+
93+ void test_whenConsumingRestGet_thenReceiveResponse () {
94+ def path = " /get"
95+ def response
96+ try {
97+ response = client. get(path : path)
98+ assert response. statusCode == 200
99+ assert response. json?. headers?. host == " postman-echo.com"
100+ } catch (RESTClientException e) {
101+ assert e?. response?. statusCode != 200
102+ }
103+ }
104+
105+ void test_whenConsumingRestPost_thenReceiveResponse () {
106+ def path = " /post"
107+ def params = [" foo" :1 ," bar" :2 ]
108+ def response
109+ try {
110+ response = client. post(path : path) {
111+ type ContentType . JSON
112+ json params
113+ }
114+ assert response. json?. data == params
115+ } catch (RESTClientException e) {
116+ e. printStackTrace()
117+ assert e?. response?. statusCode != 200
118+ }
119+ }
120+
121+ void test_whenBasicAuthentication_thenReceive200 () {
122+ def path = " /basic-auth"
123+ def response
124+ try {
125+ client. authorization = new HTTPBasicAuthorization (" postman" , " password" )
126+ response = client. get(path : path)
127+ assert response. statusCode == 200
128+ assert response. json?. authenticated == true
129+ } catch (RESTClientException e) {
130+ e. printStackTrace()
131+ assert e?. response?. statusCode != 200
132+ }
133+ }
134+
135+ void test_whenOAuth_thenReceive200 () {
136+ RESTClient oAuthClient = new RESTClient (" https://postman-echo.com" )
137+ oAuthClient. defaultAcceptHeader = ContentType . JSON
138+ oAuthClient. httpClient. sslTrustAllCerts = true
139+
140+ def path = " /oauth1"
141+ 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=' ]
142+ def response
143+ try {
144+ response = oAuthClient. get(path : path, query : params)
145+ assert response. statusCode == 200
146+ assert response. statusMessage == " OK"
147+ assert response. json. status == " pass"
148+ } catch (RESTClientException e) {
149+ assert e?. response?. statusCode != 200
150+ }
151+ }
152+ }
0 commit comments