aboutsummaryrefslogtreecommitdiffstats
path: root/adapters
diff options
context:
space:
mode:
authorr.bogacki <r.bogacki@samsung.com>2019-05-24 13:51:36 +0200
committerRobert Bogacki <r.bogacki@samsung.com>2019-05-29 12:29:06 +0000
commita38e3fb6be80f815a6a06748f53d67c76e56f5c3 (patch)
tree3b66815ce4a23d4040adc49943c2d478c608f244 /adapters
parent4b7ebd265366542ec872869e9ec0016a88e879b1 (diff)
JUnit tests for RestfulUtil
Increased junit tests coverage according to Sonar analyses. Added missing tests for RestfulUtil. Issue-ID: SO-1692 Signed-off-by: Robert Bogacki <r.bogacki@samsung.com> Change-Id: I8744960c0ad3a36ca4c36e23867168bfda5cbd1d
Diffstat (limited to 'adapters')
-rw-r--r--adapters/mso-vfc-adapter/src/main/java/org/onap/so/adapters/vfc/util/RestfulUtil.java5
-rw-r--r--adapters/mso-vfc-adapter/src/test/java/org/onap/so/adapters/vfc/util/RestfulUtilTest.java156
2 files changed, 159 insertions, 2 deletions
diff --git a/adapters/mso-vfc-adapter/src/main/java/org/onap/so/adapters/vfc/util/RestfulUtil.java b/adapters/mso-vfc-adapter/src/main/java/org/onap/so/adapters/vfc/util/RestfulUtil.java
index 3419e6d20c..1e61d4deb2 100644
--- a/adapters/mso-vfc-adapter/src/main/java/org/onap/so/adapters/vfc/util/RestfulUtil.java
+++ b/adapters/mso-vfc-adapter/src/main/java/org/onap/so/adapters/vfc/util/RestfulUtil.java
@@ -79,6 +79,9 @@ public class RestfulUtil {
@Autowired
private Environment env;
+ @Autowired
+ private HttpClient client;
+
public String getMsbHost() {
// MSB_IP will be set as ONAP_IP environment parameter in install flow.
String msbIp = System.getenv().get(ONAP_IP);
@@ -111,8 +114,6 @@ public class RestfulUtil {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout).build();
- HttpClient client = HttpClientBuilder.create().build();
-
if ("POST".equalsIgnoreCase(methodType)) {
HttpPost httpPost = new HttpPost(msbUrl);
httpPost.setConfig(requestConfig);
diff --git a/adapters/mso-vfc-adapter/src/test/java/org/onap/so/adapters/vfc/util/RestfulUtilTest.java b/adapters/mso-vfc-adapter/src/test/java/org/onap/so/adapters/vfc/util/RestfulUtilTest.java
new file mode 100644
index 0000000000..c388016ab4
--- /dev/null
+++ b/adapters/mso-vfc-adapter/src/test/java/org/onap/so/adapters/vfc/util/RestfulUtilTest.java
@@ -0,0 +1,156 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (c) 2019 Samsung. 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.adapters.vfc.util;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.so.adapters.vfc.model.RestfulResponse;
+import org.springframework.http.HttpStatus;
+import javax.ws.rs.HttpMethod;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RestfulUtilTest {
+
+ @InjectMocks
+ @Spy
+ private RestfulUtil restfulUtil;
+
+ @Mock
+ private HttpClient client;
+
+ private HttpEntity httpEntity;
+ private HttpResponse httpResponse;
+ private StatusLine statusLine;
+
+ @Before
+ public void setUp() {
+ httpEntity = mock(HttpEntity.class);
+ httpResponse = mock(HttpResponse.class);
+ statusLine = mock(StatusLine.class);
+ }
+
+ private void sendInit() throws IOException {
+
+ doReturn("https://testHost/").when(restfulUtil).getMsbHost();
+
+ when(statusLine.getStatusCode()).thenReturn(HttpStatus.OK.value());
+ when(httpResponse.getStatusLine()).thenReturn(statusLine);
+ when(httpResponse.getEntity()).thenReturn(httpEntity);
+ }
+
+ @Test
+ public void sendGet() throws Exception {
+
+ sendInit();
+
+ ByteArrayInputStream responseStream = new ByteArrayInputStream(new String("GET").getBytes());
+ when(client.execute(any(HttpGet.class))).thenReturn(httpResponse);
+ when(httpEntity.getContent()).thenReturn(responseStream);
+
+ RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.GET, "some request content");
+
+ assertEquals(HttpStatus.OK.value(), restfulResponse.getStatus());
+ assertEquals("GET", restfulResponse.getResponseContent());
+
+ }
+
+ @Test
+ public void sendPost() throws Exception {
+
+ sendInit();
+
+
+ ByteArrayInputStream responseStream = new ByteArrayInputStream(new String("POST").getBytes());
+ when(client.execute(any(HttpPost.class))).thenReturn(httpResponse);
+ when(httpEntity.getContent()).thenReturn(responseStream);
+
+ RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.POST, "some request content");
+
+ assertEquals(HttpStatus.OK.value(), restfulResponse.getStatus());
+ assertEquals("POST", restfulResponse.getResponseContent());
+
+ }
+
+ @Test
+ public void sendPut() throws Exception {
+
+ sendInit();
+
+ ByteArrayInputStream responseStream = new ByteArrayInputStream(new String("PUT").getBytes());
+ when(client.execute(any(HttpPut.class))).thenReturn(httpResponse);
+ when(httpEntity.getContent()).thenReturn(responseStream);
+
+ RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.PUT, "some request content");
+
+ assertEquals(HttpStatus.OK.value(), restfulResponse.getStatus());
+ assertEquals("PUT", restfulResponse.getResponseContent());
+
+ }
+
+ @Test
+ public void sendDelete() throws Exception {
+
+ sendInit();
+
+ ByteArrayInputStream responseStream = new ByteArrayInputStream(new String("DELETE").getBytes());
+ when(client.execute(any(HttpDelete.class))).thenReturn(httpResponse);
+ when(httpEntity.getContent()).thenReturn(responseStream);
+
+ RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.DELETE, "some request content");
+
+ assertEquals(HttpStatus.OK.value(), restfulResponse.getStatus());
+ assertEquals("DELETE", restfulResponse.getResponseContent());
+
+ }
+
+ @Test
+ public void sendOptions() throws Exception {
+
+ doReturn("https://testHost/").when(restfulUtil).getMsbHost();
+
+ RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.OPTIONS, "some request content");
+
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), restfulResponse.getStatus());
+ assertEquals("Error processing request to VFC", restfulResponse.getResponseContent());
+
+ }
+
+}