Containerization feature of SO
[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().port(28090));      
55         
56         @Before
57         public void before() throws Exception {
58                 jsonPayload = new JSONObject();
59                 jsonPayload.put("firstName", "firstName1");
60                 jsonPayload.put("lastName", "lastName1");
61                 jsonObjectAsString = jsonPayload.toString();
62                 jsonResponse = new JSONObject();
63                 jsonResponse.put("response", "responseValue");
64                 jsonResponseAsString = jsonResponse.toString(); 
65                 restClient = new RESTClient("http://localhost:28090/example", "localhost", 28090);
66         }       
67         
68         @Test
69         public void testHeadersParameters() throws Exception {
70                 restClient.setHeader("name", "value");
71                 restClient.setParameter("name", "value");
72                 assertEquals("[value]", restClient.getParameters().get("name").toString());             
73                 assertEquals("[value]", restClient.getHeaders().get("name").toString());
74                 restClient.setHeader("name", "value2");
75                 assertEquals("[value2]", restClient.getHeaders().get("name").toString());
76                 restClient.setParameter("name", "value2");
77                 assertEquals("[value2]", restClient.getParameters().get("name").toString());
78                 restClient.addParameter("name", "value");  
79                 assertEquals(1, restClient.getParameters().size());
80                 restClient.addAuthorizationHeader("token");  
81                 assertEquals("[token]", restClient.getHeaders().get("Authorization").toString());               
82                 assertEquals("http://localhost:28090/example", restClient.getURL());
83                 restClient = new RESTClient("http://localhost:28090/example1");
84                 assertEquals("http://localhost:28090/example1", restClient.getURL());
85         }
86         
87         @Test
88         public void testHttpPost() throws Exception {
89                 RESTClient restClientMock = mock(RESTClient.class);
90                 restClientMock = spy(restClient);
91                 wireMockRule.stubFor(post(urlPathMatching("/example/*"))
92                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));              
93                 APIResponse apiResponse = restClientMock.httpPost(jsonObjectAsString);
94                 assertEquals(200, apiResponse.getStatusCode());
95                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
96                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
97                 verify(restClientMock, times(2)).getURL();
98         }       
99         
100         @Test
101         public void testPost() throws Exception {
102                 wireMockRule.stubFor(post(urlPathMatching("/example/*"))
103                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));              
104                 APIResponse apiResponse = restClient.post();
105                 assertEquals(200, apiResponse.getStatusCode());
106                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
107                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
108         }       
109         
110         @Test
111         public void testHttpPut() throws Exception {
112                 wireMockRule.stubFor(put(urlPathMatching("/example/*"))
113                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));              
114                 restClient.setParameter("name", "value");
115                 APIResponse apiResponse = restClient.httpPut(jsonObjectAsString);
116                 assertEquals(200, apiResponse.getStatusCode());
117                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
118                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
119                 
120         }               
121         
122         @Test
123         public void testHttpPatch() throws Exception {
124                 wireMockRule.stubFor(patch(urlPathMatching("/example/*"))
125                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
126                 APIResponse apiResponse = restClient.httpPatch(jsonObjectAsString);
127                 assertEquals(200, apiResponse.getStatusCode());
128                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
129                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
130         }       
131
132         @Test
133         public void testPatch_withParameter() throws Exception {
134                 wireMockRule.stubFor(patch(urlPathMatching("/example/*"))
135                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
136                 restClient.setParameter("name", "value");
137                 APIResponse apiResponse = restClient.patch(jsonObjectAsString);
138                 assertEquals(200, apiResponse.getStatusCode());
139                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
140                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
141         }               
142         
143         @Test
144         public void testHttpDelete_withPayload() throws Exception {
145                 wireMockRule.stubFor(delete(urlPathMatching("/example/*"))
146                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
147                 APIResponse apiResponse = restClient.httpDelete(jsonObjectAsString);
148                 assertEquals(200, apiResponse.getStatusCode());
149                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
150                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
151         }       
152         
153         @Test
154         public void testHttpDelete() throws Exception {
155                 wireMockRule.stubFor(delete(urlPathMatching("/example/*"))
156                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
157                 APIResponse apiResponse = restClient.httpDelete();
158                 assertEquals(200, apiResponse.getStatusCode());
159                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
160                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
161         }       
162         
163         @Test
164         public void testDelete() throws Exception {
165                 wireMockRule.stubFor(delete(urlPathMatching("/example/*"))
166                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
167                 APIResponse apiResponse = restClient.delete();
168                 assertEquals(200, apiResponse.getStatusCode());
169                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
170                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
171         }       
172         
173         @Test
174         public void testHttpGet() throws Exception {
175                 wireMockRule.stubFor(get(urlPathMatching("/example/*"))
176                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
177                 APIResponse apiResponse = restClient.httpGet();
178                 assertEquals(200, apiResponse.getStatusCode());
179                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
180                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
181         }       
182         
183         @Test
184         public void testGet_withParameter() throws Exception {
185                 wireMockRule.stubFor(get(urlPathMatching("/example/*"))
186                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString)));                              
187                 restClient.setParameter("name", "value");
188                 restClient.setParameter("type", "valueType");
189                 APIResponse apiResponse = restClient.get();
190                 assertEquals(200, apiResponse.getStatusCode());
191                 assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString());
192                 assertEquals("application/json", apiResponse.getFirstHeader("Content-Type"));
193         }       
194         
195 }