diff options
Diffstat (limited to 'adapters/mso-vfc-adapter')
3 files changed, 169 insertions, 10 deletions
diff --git a/adapters/mso-vfc-adapter/src/main/java/org/onap/so/adapters/vfc/util/JsonUtil.java b/adapters/mso-vfc-adapter/src/main/java/org/onap/so/adapters/vfc/util/JsonUtil.java index cefaadbb63..efc0a303c0 100644 --- a/adapters/mso-vfc-adapter/src/main/java/org/onap/so/adapters/vfc/util/JsonUtil.java +++ b/adapters/mso-vfc-adapter/src/main/java/org/onap/so/adapters/vfc/util/JsonUtil.java @@ -24,6 +24,7 @@ package org.onap.so.adapters.vfc.util; import java.io.IOException; +import com.google.common.base.Strings; import org.onap.so.adapters.vfc.constant.HttpCode; import org.onap.so.adapters.vfc.exceptions.ApplicationException; import org.onap.so.logger.ErrorCode; @@ -83,8 +84,8 @@ public class JsonUtil { try { return MAPPER.readValue(jsonstr, type); } catch (IOException e) { - logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(), - UNMARSHAL_FAIL_MSG, e); + logger.error(Strings.repeat("{} ", 3), MessageEnum.RA_NS_EXC.toString(), + ErrorCode.BusinessProcesssError.getValue(), UNMARSHAL_FAIL_MSG, e); throw new ApplicationException(HttpCode.BAD_REQUEST, UNMARSHAL_FAIL_MSG); } } @@ -101,8 +102,8 @@ public class JsonUtil { try { return MAPPER.readValue(jsonstr, type); } catch (IOException e) { - logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(), - UNMARSHAL_FAIL_MSG, e); + logger.error(Strings.repeat("{} ", 3), MessageEnum.RA_NS_EXC.toString(), + ErrorCode.BusinessProcesssError.getValue(), UNMARSHAL_FAIL_MSG, e); throw new ApplicationException(HttpCode.BAD_REQUEST, UNMARSHAL_FAIL_MSG); } } @@ -118,8 +119,8 @@ public class JsonUtil { try { return MAPPER.writeValueAsString(srcObj); } catch (IOException e) { - logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(), - "fail to marshal json", e); + logger.error(Strings.repeat("{} ", 3), MessageEnum.RA_NS_EXC.toString(), + ErrorCode.BusinessProcesssError.getValue(), "fail to marshal json", e); throw new ApplicationException(HttpCode.BAD_REQUEST, "srcObj marshal failed!"); } } 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..a84785bcad 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 @@ -27,6 +27,7 @@ package org.onap.so.adapters.vfc.util; import java.net.HttpURLConnection; import java.net.SocketTimeoutException; import javax.ws.rs.core.UriBuilder; +import com.google.common.base.Strings; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; @@ -79,6 +80,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 +115,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); @@ -193,12 +195,12 @@ public class RestfulUtil { } private static void logError(String errMsg, Throwable t) { - logger.error("{} {} {} {}", MessageEnum.RA_NS_EXC.toString(), VFC_ADAPTER, + logger.error(Strings.repeat("{} ", 4), MessageEnum.RA_NS_EXC.toString(), VFC_ADAPTER, ErrorCode.AvailabilityError.getValue(), errMsg, t); } private static void logError(String errMsg) { - logger.error("{} {} {} {}", MessageEnum.RA_NS_EXC.toString(), VFC_ADAPTER, + logger.error(Strings.repeat("{} ", 4), MessageEnum.RA_NS_EXC.toString(), VFC_ADAPTER, ErrorCode.AvailabilityError.toString(), errMsg); } 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()); + + } + +} |