diff options
Diffstat (limited to 'bpmn/MSORESTClient/src/test/java/org/onap')
5 files changed, 374 insertions, 0 deletions
diff --git a/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/APIResponseTest.java b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/APIResponseTest.java new file mode 100644 index 0000000000..2c81fda32b --- /dev/null +++ b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/APIResponseTest.java @@ -0,0 +1,57 @@ +/* +* ============LICENSE_START======================================================= + * ONAP : SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= +*/ + +package org.onap.so.rest; + +import static org.junit.Assert.assertEquals; + +import org.json.JSONObject; +import org.junit.Test; + +import org.apache.http.HttpResponse; +import org.apache.http.ProtocolVersion; +import org.apache.http.HttpEntity; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicHttpResponse; + +public class APIResponseTest { + + @Test + public void test() throws Exception { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("firstName", "firstName1"); + jsonObject.put("lastName", "lastName1"); + String jsonObjectAsString= jsonObject.toString(); + HttpEntity entity = new StringEntity(jsonObjectAsString, ContentType.APPLICATION_JSON); + ProtocolVersion ver = new ProtocolVersion("HTTP", 1, 1); + HttpResponse response = new BasicHttpResponse(ver, 1, "Ok"); + response.setStatusLine(ver, 200); + response.setEntity(entity); + response.setHeader("name", "value"); + response.setStatusCode(200); + APIResponse apiResponse = new APIResponse(response); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonObject.toString(), apiResponse.getResponseBodyAsString()); + assertEquals("value", apiResponse.getFirstHeader("name")); + assertEquals(1, apiResponse.getAllHeaders().length); + assertEquals(49, apiResponse.getResponseBodyAsByteArray().length); + } +} diff --git a/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/HttpHeaderTest.java b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/HttpHeaderTest.java new file mode 100644 index 0000000000..bc096687b7 --- /dev/null +++ b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/HttpHeaderTest.java @@ -0,0 +1,40 @@ +/* +* ============LICENSE_START======================================================= + * ONAP : SO + * ================================================================================ + * Copyright (C) 2018 TechMahindra + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= +*/ + +package org.onap.so.rest; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class HttpHeaderTest { + + @Test + public void test() { + HttpHeader hth= new HttpHeader("name", "value"); + assertEquals("name", hth.getName()); + assertEquals("value", hth.getValue()); + } + + @Test (expected = IllegalArgumentException.class) + public void testException() { + HttpHeader httpHeader = new HttpHeader(null, "value"); //null + } +} diff --git a/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/RESTClientTest.java b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/RESTClientTest.java new file mode 100644 index 0000000000..bbdaa94581 --- /dev/null +++ b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/RESTClientTest.java @@ -0,0 +1,195 @@ +/* +* ============LICENSE_START======================================================= + * ONAP : SO + * ================================================================================ + * Copyright (C) 2018 TechMahindra + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= +*/ + +package org.onap.so.rest; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.delete; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.patch; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.put; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.spy; + +import org.json.JSONObject; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.springframework.http.HttpStatus; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; + +public class RESTClientTest { + + private RESTClient restClient; + private JSONObject jsonPayload; + private JSONObject jsonResponse; + private String jsonObjectAsString; + private String jsonResponseAsString; + + @Rule + public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(28090)); + + @Before + public void before() throws Exception { + jsonPayload = new JSONObject(); + jsonPayload.put("firstName", "firstName1"); + jsonPayload.put("lastName", "lastName1"); + jsonObjectAsString = jsonPayload.toString(); + jsonResponse = new JSONObject(); + jsonResponse.put("response", "responseValue"); + jsonResponseAsString = jsonResponse.toString(); + restClient = new RESTClient("http://localhost:28090/example", "localhost", 28090); + } + + @Test + public void testHeadersParameters() throws Exception { + restClient.setHeader("name", "value"); + restClient.setParameter("name", "value"); + assertEquals("[value]", restClient.getParameters().get("name").toString()); + assertEquals("[value]", restClient.getHeaders().get("name").toString()); + restClient.setHeader("name", "value2"); + assertEquals("[value2]", restClient.getHeaders().get("name").toString()); + restClient.setParameter("name", "value2"); + assertEquals("[value2]", restClient.getParameters().get("name").toString()); + restClient.addParameter("name", "value"); + assertEquals(1, restClient.getParameters().size()); + restClient.addAuthorizationHeader("token"); + assertEquals("[token]", restClient.getHeaders().get("Authorization").toString()); + assertEquals("http://localhost:28090/example", restClient.getURL()); + restClient = new RESTClient("http://localhost:28090/example1"); + assertEquals("http://localhost:28090/example1", restClient.getURL()); + } + + @Test + public void testHttpPost() throws Exception { + RESTClient restClientMock = mock(RESTClient.class); + restClientMock = spy(restClient); + wireMockRule.stubFor(post(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + APIResponse apiResponse = restClientMock.httpPost(jsonObjectAsString); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + verify(restClientMock, times(2)).getURL(); + } + + @Test + public void testPost() throws Exception { + wireMockRule.stubFor(post(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + APIResponse apiResponse = restClient.post(); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + } + + @Test + public void testHttpPut() throws Exception { + wireMockRule.stubFor(put(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + restClient.setParameter("name", "value"); + APIResponse apiResponse = restClient.httpPut(jsonObjectAsString); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + + } + + @Test + public void testHttpPatch() throws Exception { + wireMockRule.stubFor(patch(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + APIResponse apiResponse = restClient.httpPatch(jsonObjectAsString); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + } + + @Test + public void testPatch_withParameter() throws Exception { + wireMockRule.stubFor(patch(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + restClient.setParameter("name", "value"); + APIResponse apiResponse = restClient.patch(jsonObjectAsString); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + } + + @Test + public void testHttpDelete_withPayload() throws Exception { + wireMockRule.stubFor(delete(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + APIResponse apiResponse = restClient.httpDelete(jsonObjectAsString); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + } + + @Test + public void testHttpDelete() throws Exception { + wireMockRule.stubFor(delete(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + APIResponse apiResponse = restClient.httpDelete(); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + } + + @Test + public void testDelete() throws Exception { + wireMockRule.stubFor(delete(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + APIResponse apiResponse = restClient.delete(); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + } + + @Test + public void testHttpGet() throws Exception { + wireMockRule.stubFor(get(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + APIResponse apiResponse = restClient.httpGet(); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + } + + @Test + public void testGet_withParameter() throws Exception { + wireMockRule.stubFor(get(urlPathMatching("/example/*")) + .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.OK.value()).withBody(jsonResponseAsString))); + restClient.setParameter("name", "value"); + restClient.setParameter("type", "valueType"); + APIResponse apiResponse = restClient.get(); + assertEquals(200, apiResponse.getStatusCode()); + assertEquals(jsonResponseAsString, apiResponse.getResponseBodyAsString()); + assertEquals("application/json", apiResponse.getFirstHeader("Content-Type")); + } + +} diff --git a/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/RESTConfigTest.java b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/RESTConfigTest.java new file mode 100644 index 0000000000..82c3c37be0 --- /dev/null +++ b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/RESTConfigTest.java @@ -0,0 +1,42 @@ +/* +* ============LICENSE_START======================================================= + * ONAP : SO + * ================================================================================ + * Copyright (C) 2018 TechMahindra + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= +*/ + +package org.onap.so.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import org.junit.Test; + +public class RESTConfigTest { + + @Test + public void test() { + RESTConfig rcfg=mock(RESTConfig.class); + RESTConfig rcf=new RESTConfig("URL"); + RESTConfig rcfi=new RESTConfig("URL", true); + RESTConfig rcfo=new RESTConfig("URL", "10.2.3.101", 5020); + RESTConfig rcfn=new RESTConfig("URL", "10.2.3.101", 5020, true); + assert(rcfo.getProxyHost().equals("10.2.3.101")); + assertEquals(5020,rcfn.getProxyPort()); + assert(rcf.getURL().equals("URL")); + assertTrue(rcfn.trustAllCerts()); + } +} diff --git a/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/RESTExceptionTest.java b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/RESTExceptionTest.java new file mode 100644 index 0000000000..19339a187d --- /dev/null +++ b/bpmn/MSORESTClient/src/test/java/org/onap/so/rest/RESTExceptionTest.java @@ -0,0 +1,40 @@ +/* +* ============LICENSE_START======================================================= + * ONAP : SO + * ================================================================================ + * Copyright (C) 2018 TechMahindra + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= +*/ + +package org.onap.so.rest; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import org.junit.Test; + +public class RESTExceptionTest { + + @Test + public void test() { + RESTException rexm=mock(RESTException.class); + RESTException rex=new RESTException("Error"); + RESTException recpt=new RESTException(200, "Error"); + assert(rex.getErrorMessage().equals("Error")); + assertEquals(200,recpt.getStatusCode()); + Throwable throwable = mock(Throwable.class); + RESTException restException = new RESTException(throwable); + assertEquals(-1, restException.getStatusCode()); + } +} |