17ede2f2ffebe1d1c70a375bb79435975c1382b2
[so.git] / bpmn / MSORESTClient / src / test / java / org / onap / so / rest / RESTClientTest.java
1 /*
2 * ============LICENSE_START=======================================================
3  * ONAP : SO
4  * ================================================================================
5  * Copyright (C) 2018 TechMahindra
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19 */
20
21 package org.onap.so.rest;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.patch;
27 import static com.github.tomakehurst.wiremock.client.WireMock.post;
28 import static com.github.tomakehurst.wiremock.client.WireMock.put;
29 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
30 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
31 import static org.junit.Assert.assertEquals;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.times;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.spy;
36
37 import org.json.JSONObject;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.springframework.http.HttpStatus;
42
43 import com.github.tomakehurst.wiremock.junit.WireMockRule;
44
45 public class RESTClientTest {
46
47         private RESTClient restClient;
48         private JSONObject jsonPayload;
49         private JSONObject jsonResponse;
50         private String jsonObjectAsString;
51         private String jsonResponseAsString;
52         
53         @Rule
54         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());    
55         
56         
57         @Before
58         public void before() throws Exception {
59                 jsonPayload = new JSONObject();
60                 jsonPayload.put("firstName", "firstName1");
61                 jsonPayload.put("lastName", "lastName1");
62                 jsonObjectAsString = jsonPayload.toString();
63                 jsonResponse = new JSONObject();
64                 jsonResponse.put("response", "responseValue");
65                 jsonResponseAsString = jsonResponse.toString(); 
66                 restClient = new RESTClient("http://localhost:" + wireMockRule.port() + "/example", "localhost", wireMockRule.port());
67         }       
68         
69         @Test
70         public void testHeadersParameters() throws Exception {
71                 restClient.setHeader("name", "value");
72                 restClient.setParameter("name", "value");
73                 assertEquals("[value]", restClient.getParameters().get("name").toString());             
74                 assertEquals("[value]", restClient.getHeaders().get("name").toString());
75                 restClient.setHeader("name", "value2");
76                 assertEquals("[value2]", restClient.getHeaders().get("name").toString());
77                 restClient.setParameter("name", "value2");
78                 assertEquals("[value2]", restClient.getParameters().get("name").toString());
79                 restClient.addParameter("name", "value");  
80                 assertEquals(1, restClient.getParameters().size());
81                 restClient.addAuthorizationHeader("token");  
82                 assertEquals("[token]", restClient.getHeaders().get("Authorization").toString());               
83                 assertEquals("http://localhost:" + wireMockRule.port() + "/example", restClient.getURL());
84                 restClient = new RESTClient("http://localhost:" + wireMockRule.port() + "/example1");
85                 assertEquals("http://localhost:" + wireMockRule.port() + "/example1", restClient.getURL());
86         }
87         
88         @Test
89         public void testHttpPost() throws Exception {
90                 RESTClient restClientMock = mock(RESTClient.class);
91                 restClientMock = spy(restClient);
92                 wireMockRule.stubFor(post(urlPathMatching("/example/*"))
93                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));              
94                 APIResponse apiResponse = restClientMock.httpPost(jsonObjectAsString);
95                 assertEquals(200, apiResponse.getStatusCode());
96                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
97                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
98                 verify(restClientMock, times(2)).getURL();
99         }       
100         
101         @Test
102         public void testPost() throws Exception {
103                 wireMockRule.stubFor(post(urlPathMatching("/example/*"))
104                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));              
105                 APIResponse apiResponse = restClient.post();
106                 assertEquals(200, apiResponse.getStatusCode());
107                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
108                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
109         }       
110         
111         @Test
112         public void testHttpPut() throws Exception {
113                 wireMockRule.stubFor(put(urlPathMatching("/example/*"))
114                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));              
115                 restClient.setParameter("name", "value");
116                 APIResponse apiResponse = restClient.httpPut(jsonObjectAsString);
117                 assertEquals(200, apiResponse.getStatusCode());
118                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
119                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
120                 
121         }               
122         
123         @Test
124         public void testHttpPatch() throws Exception {
125                 wireMockRule.stubFor(patch(urlPathMatching("/example/*"))
126                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
127                 APIResponse apiResponse = restClient.httpPatch(jsonObjectAsString);
128                 assertEquals(200, apiResponse.getStatusCode());
129                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
130                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
131         }       
132
133         @Test
134         public void testPatch_withParameter() throws Exception {
135                 wireMockRule.stubFor(patch(urlPathMatching("/example/*"))
136                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
137                 restClient.setParameter("name", "value");
138                 APIResponse apiResponse = restClient.patch(jsonObjectAsString);
139                 assertEquals(200, apiResponse.getStatusCode());
140                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
141                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
142         }               
143         
144         @Test
145         public void testHttpDelete_withPayload() throws Exception {
146                 wireMockRule.stubFor(delete(urlPathMatching("/example/*"))
147                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
148                 APIResponse apiResponse = restClient.httpDelete(jsonObjectAsString);
149                 assertEquals(200, apiResponse.getStatusCode());
150                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
151                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
152         }       
153         
154         @Test
155         public void testHttpDelete() throws Exception {
156                 wireMockRule.stubFor(delete(urlPathMatching("/example/*"))
157                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
158                 APIResponse apiResponse = restClient.httpDelete();
159                 assertEquals(200, apiResponse.getStatusCode());
160                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
161                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
162         }       
163         
164         @Test
165         public void testDelete() throws Exception {
166                 wireMockRule.stubFor(delete(urlPathMatching("/example/*"))
167                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
168                 APIResponse apiResponse = restClient.delete();
169                 assertEquals(200, apiResponse.getStatusCode());
170                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
171                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
172         }       
173         
174         @Test
175         public void testHttpGet() throws Exception {
176                 wireMockRule.stubFor(get(urlPathMatching("/example/*"))
177                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
178                 APIResponse apiResponse = restClient.httpGet();
179                 assertEquals(200, apiResponse.getStatusCode());
180                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
181                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
182         }       
183         
184         @Test
185         public void testGet_withParameter() throws Exception {
186                 wireMockRule.stubFor(get(urlPathMatching("/example/*"))
187                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
188                 restClient.setParameter("name", "value");
189                 restClient.setParameter("type", "valueType");
190                 APIResponse apiResponse = restClient.get();
191                 assertEquals(200, apiResponse.getStatusCode());
192                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
193                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
194         }       
195         
196 }