diff options
author | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-10-10 09:28:55 +0800 |
---|---|---|
committer | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-10-10 09:29:05 +0800 |
commit | ad83506ad7fd18611d71770ad5647360013ffad1 (patch) | |
tree | 823d61b2c7fff9be57f2c261114f4e310c43e1fe /src/test | |
parent | 3f1f8de8efb9019e613927697acce40f41dc8d16 (diff) |
Add unit test for MSB JAVA SDK
Issue-Id: MSB-86
Change-Id: Id383cd1d81a36d9bbb2a121938f3957ad16e7cf0
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
Diffstat (limited to 'src/test')
3 files changed, 211 insertions, 10 deletions
diff --git a/src/test/java/org/onap/msb/sdk/discovery/MSBServiceTest.java b/src/test/java/org/onap/msb/sdk/discovery/MSBServiceTest.java index a09e64b..b202ff4 100644 --- a/src/test/java/org/onap/msb/sdk/discovery/MSBServiceTest.java +++ b/src/test/java/org/onap/msb/sdk/discovery/MSBServiceTest.java @@ -19,6 +19,7 @@ import java.util.Set; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; +import org.onap.msb.sdk.discovery.common.RouteConst; import org.onap.msb.sdk.discovery.common.RouteException; import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; @@ -32,20 +33,25 @@ import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest({HttpClientUtil.class}) public class MSBServiceTest { - private static final String mockMSBUrl1 = + private static final String MOCK_MSB_URL_REG_UPDATE_TRUE = "http://127.0.0.1:10081/api/microservices/v1/services?createOrUpdate=true"; - private static final String mockMSBUrl2 = + private static final String MOCK_MSB_URL_REG_UPDATE_FALSE = "http://127.0.0.1:10081/api/microservices/v1/services?createOrUpdate=false"; - private static final String mockRegistrationJson = + private static final String MOCK_MSB_URL_DIS = + "http://127.0.0.1:10081/api/microservices/v1/services/aai/version/v8?ifPassStatus=true"; + private static final String MOCK_MSB_URL_UNREG = + "http://127.0.0.1:10081/api/microservices/v1/services/aai/version/v8"; + + private static final String MOCK_REG_SERVICE_JSON = "{\"serviceName\":\"aai\",\"version\":\"v8\",\"url\":\"/aai/v8\",\"protocol\":\"REST\",\"visualRange\":\"1\",\"lb_policy\":\"\",\"path\":\"/aai/v8\",\"nodes\":[{\"ip\":\"10.74.44.1\",\"port\":\"8443\",\"ttl\":\"\"}],\"metadata\":null}"; @Test - public void test_registration() throws RouteException { + public void test_registration_update_true() throws RouteException { String msbAddress = "127.0.0.1:10081"; MicroServiceInfo microServiceInfo = - (MicroServiceInfo) JacksonJsonUtil.jsonToBean(mockRegistrationJson, MicroServiceInfo.class); + (MicroServiceInfo) JacksonJsonUtil.jsonToBean(MOCK_REG_SERVICE_JSON, MicroServiceInfo.class); MicroServiceFullInfo microServiceFullInfo = mockMicroServiceFullInfo(microServiceInfo); - mockGetRest(mockMSBUrl1, JacksonJsonUtil.beanToJson(microServiceFullInfo)); + mockHttpPost(MOCK_MSB_URL_REG_UPDATE_TRUE, JacksonJsonUtil.beanToJson(microServiceFullInfo)); MSBService msbService = new MSBService(); microServiceFullInfo = msbService.registerMicroServiceInfo(msbAddress, microServiceInfo); Assert.assertTrue(microServiceFullInfo.getPath().equals("/aai/v8")); @@ -60,9 +66,9 @@ public class MSBServiceTest { public void test_registration_update_false() throws RouteException { String msbAddress = "127.0.0.1:10081"; MicroServiceInfo microServiceInfo = - (MicroServiceInfo) JacksonJsonUtil.jsonToBean(mockRegistrationJson, MicroServiceInfo.class); + (MicroServiceInfo) JacksonJsonUtil.jsonToBean(MOCK_REG_SERVICE_JSON, MicroServiceInfo.class); MicroServiceFullInfo microServiceFullInfo = mockMicroServiceFullInfo(microServiceInfo); - mockGetRest(mockMSBUrl2, JacksonJsonUtil.beanToJson(microServiceFullInfo)); + mockHttpPost(MOCK_MSB_URL_REG_UPDATE_FALSE, JacksonJsonUtil.beanToJson(microServiceFullInfo)); MSBService msbService = new MSBService(); microServiceFullInfo = msbService.registerMicroServiceInfo(msbAddress, microServiceInfo, false); Assert.assertTrue(microServiceFullInfo.getPath().equals("/aai/v8")); @@ -73,6 +79,32 @@ public class MSBServiceTest { Assert.assertTrue(microServiceFullInfo.getVisualRange().equals("1")); } + @Test + public void test_discovery() throws RouteException { + String msbAddress = "127.0.0.1:10081"; + MicroServiceInfo microServiceInfo = + (MicroServiceInfo) JacksonJsonUtil.jsonToBean(MOCK_REG_SERVICE_JSON, MicroServiceInfo.class); + MicroServiceFullInfo microServiceFullInfo = mockMicroServiceFullInfo(microServiceInfo); + mockHttpGet(MOCK_MSB_URL_DIS, JacksonJsonUtil.beanToJson(microServiceFullInfo)); + + MSBService msbService = new MSBService(); + microServiceFullInfo = msbService.queryMicroServiceInfo(msbAddress, "aai", "v8"); + Assert.assertTrue(microServiceFullInfo.getPath().equals("/aai/v8")); + Assert.assertTrue(microServiceFullInfo.getProtocol().equals("REST")); + Assert.assertTrue(microServiceFullInfo.getServiceName().equals("aai")); + Assert.assertTrue(microServiceFullInfo.getUrl().equals("/aai/v8")); + Assert.assertTrue(microServiceFullInfo.getVersion().equals("v8")); + Assert.assertTrue(microServiceFullInfo.getVisualRange().equals("1")); + } + + // @Test + // public void test_unregistration() throws Exception { + // String msbAddress = "127.0.0.1:10081"; + // mockHttpDel(); + // MSBService msbService = new MSBService(); + // msbService.cancelMicroServiceInfo(msbAddress, "aai", "v8"); + // } + private MicroServiceFullInfo mockMicroServiceFullInfo(MicroServiceInfo info) { MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo(); @@ -87,9 +119,21 @@ public class MSBServiceTest { } - private void mockGetRest(String mockMSBUrl, String mockServiceInfoJson) throws RouteException { + private void mockHttpPost(String mockMSBUrl, String mockServiceInfoJson) throws RouteException { PowerMockito.mockStatic(HttpClientUtil.class); - PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockMSBUrl, mockRegistrationJson)) + PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockMSBUrl, MOCK_REG_SERVICE_JSON)) .thenReturn(mockServiceInfoJson); } + + private void mockHttpGet(String mockMSBUrl, String mockServiceInfoJson) throws RouteException { + PowerMockito.mockStatic(HttpClientUtil.class); + PowerMockito.when(HttpClientUtil.httpGet(mockMSBUrl)).thenReturn(mockServiceInfoJson); + } + + // private void mockHttpDel() throws Exception { + // PowerMockito.mockStatic(HttpClientUtil.class); + // HttpClientUtil myClass = PowerMockito.spy(new HttpClientUtil()); + // PowerMockito.doNothing().when(myClass, HttpClientUtil.class.getMethod("delete", String.class, + // String.class)); + // } } diff --git a/src/test/java/org/onap/msb/sdk/httpclient/ServiceHttpEndPointBeanObjectTest.java b/src/test/java/org/onap/msb/sdk/httpclient/ServiceHttpEndPointBeanObjectTest.java new file mode 100644 index 0000000..22f752e --- /dev/null +++ b/src/test/java/org/onap/msb/sdk/httpclient/ServiceHttpEndPointBeanObjectTest.java @@ -0,0 +1,41 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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. + */ +package org.onap.msb.sdk.httpclient; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.onap.msb.sdk.discovery.common.RouteException; + +import org.junit.Assert; + +public class ServiceHttpEndPointBeanObjectTest { + + @Test + public void testBean() { + ServiceHttpEndPointBeanObject endPointbean = new ServiceHttpEndPointBeanObject(); + endPointbean.setClientProtocl("https"); + endPointbean.setMsbProtocl("rest"); + endPointbean.setServiceName("aai"); + endPointbean.setServiceVersion("v8"); + endPointbean.setVisualRange("v1"); + + Assert.assertEquals(endPointbean.getClientProtocl(), "https"); + Assert.assertEquals(endPointbean.getMsbProtocl(), "rest"); + Assert.assertEquals(endPointbean.getServiceName(), "aai"); + Assert.assertEquals(endPointbean.getServiceVersion(), "v8"); + Assert.assertEquals(endPointbean.getVisualRange(), "v1"); + } + +} diff --git a/src/test/java/org/onap/msb/sdk/httpclient/msb/MSBServiceClientTest.java b/src/test/java/org/onap/msb/sdk/httpclient/msb/MSBServiceClientTest.java new file mode 100644 index 0000000..71acd1a --- /dev/null +++ b/src/test/java/org/onap/msb/sdk/httpclient/msb/MSBServiceClientTest.java @@ -0,0 +1,116 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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. + */ +package org.onap.msb.sdk.httpclient.msb; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.msb.sdk.discovery.common.RouteException; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.onap.msb.sdk.discovery.util.HttpClientUtil; +import org.onap.msb.sdk.discovery.util.JacksonJsonUtil; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({HttpClientUtil.class}) +public class MSBServiceClientTest { + private static final String MOCK_MSB_URL_REG_UPDATE_TRUE = + "http://127.0.0.1:10081/api/microservices/v1/services?createOrUpdate=true"; + private static final String MOCK_MSB_URL_REG_UPDATE_FALSE = + "http://127.0.0.1:10081/api/microservices/v1/services?createOrUpdate=false"; + private static final String MOCK_MSB_URL_DIS = + "http://127.0.0.1:10081/api/microservices/v1/services/aai/version/v8?ifPassStatus=true"; + private static final String MOCK_MSB_URL_UNREG = + "http://127.0.0.1:10081/api/microservices/v1/services/aai/version/v8"; + + private static final String MOCK_REG_SERVICE_JSON = + "{\"serviceName\":\"aai\",\"version\":\"v8\",\"url\":\"/aai/v8\",\"protocol\":\"REST\",\"visualRange\":\"1\",\"lb_policy\":\"\",\"path\":\"/aai/v8\",\"nodes\":[{\"ip\":\"10.74.44.1\",\"port\":\"8443\",\"ttl\":\"\"}],\"metadata\":null}"; + + @Test + public void test_registration_update_true() throws RouteException { + MicroServiceInfo microServiceInfo = + (MicroServiceInfo) JacksonJsonUtil.jsonToBean(MOCK_REG_SERVICE_JSON, MicroServiceInfo.class); + MicroServiceFullInfo microServiceFullInfo = mockMicroServiceFullInfo(microServiceInfo); + mockHttpPost(MOCK_MSB_URL_REG_UPDATE_TRUE, JacksonJsonUtil.beanToJson(microServiceFullInfo)); + MSBServiceClient msbClient = new MSBServiceClient("127.0.0.1", 10081); + microServiceFullInfo = msbClient.registerMicroServiceInfo(microServiceInfo); + Assert.assertTrue(microServiceFullInfo.getPath().equals("/aai/v8")); + Assert.assertTrue(microServiceFullInfo.getProtocol().equals("REST")); + Assert.assertTrue(microServiceFullInfo.getServiceName().equals("aai")); + Assert.assertTrue(microServiceFullInfo.getUrl().equals("/aai/v8")); + Assert.assertTrue(microServiceFullInfo.getVersion().equals("v8")); + Assert.assertTrue(microServiceFullInfo.getVisualRange().equals("1")); + } + + @Test + public void test_registration_update_false() throws RouteException { + MicroServiceInfo microServiceInfo = + (MicroServiceInfo) JacksonJsonUtil.jsonToBean(MOCK_REG_SERVICE_JSON, MicroServiceInfo.class); + MicroServiceFullInfo microServiceFullInfo = mockMicroServiceFullInfo(microServiceInfo); + mockHttpPost(MOCK_MSB_URL_REG_UPDATE_FALSE, JacksonJsonUtil.beanToJson(microServiceFullInfo)); + MSBServiceClient msbClient = new MSBServiceClient("127.0.0.1", 10081); + microServiceFullInfo = msbClient.registerMicroServiceInfo(microServiceInfo, false); + Assert.assertTrue(microServiceFullInfo.getPath().equals("/aai/v8")); + Assert.assertTrue(microServiceFullInfo.getProtocol().equals("REST")); + Assert.assertTrue(microServiceFullInfo.getServiceName().equals("aai")); + Assert.assertTrue(microServiceFullInfo.getUrl().equals("/aai/v8")); + Assert.assertTrue(microServiceFullInfo.getVersion().equals("v8")); + Assert.assertTrue(microServiceFullInfo.getVisualRange().equals("1")); + } + + @Test + public void test_discovery() throws RouteException { + MicroServiceInfo microServiceInfo = + (MicroServiceInfo) JacksonJsonUtil.jsonToBean(MOCK_REG_SERVICE_JSON, MicroServiceInfo.class); + MicroServiceFullInfo microServiceFullInfo = mockMicroServiceFullInfo(microServiceInfo); + mockHttpGet(MOCK_MSB_URL_DIS, JacksonJsonUtil.beanToJson(microServiceFullInfo)); + + MSBServiceClient msbClient = new MSBServiceClient("127.0.0.1", 10081); + microServiceFullInfo = msbClient.queryMicroServiceInfo("aai", "v8"); + Assert.assertTrue(microServiceFullInfo.getPath().equals("/aai/v8")); + Assert.assertTrue(microServiceFullInfo.getProtocol().equals("REST")); + Assert.assertTrue(microServiceFullInfo.getServiceName().equals("aai")); + Assert.assertTrue(microServiceFullInfo.getUrl().equals("/aai/v8")); + Assert.assertTrue(microServiceFullInfo.getVersion().equals("v8")); + Assert.assertTrue(microServiceFullInfo.getVisualRange().equals("1")); + } + + private MicroServiceFullInfo mockMicroServiceFullInfo(MicroServiceInfo info) { + MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo(); + serviceInfo.setServiceName(info.getServiceName()); + serviceInfo.setVersion(info.getVersion()); + serviceInfo.setUrl(info.getUrl()); + serviceInfo.setProtocol(info.getProtocol()); + serviceInfo.setVisualRange(info.getVisualRange()); + serviceInfo.setLb_policy(info.getLb_policy()); + serviceInfo.setPath(info.getPath()); + return serviceInfo; + } + + + private void mockHttpPost(String mockMSBUrl, String mockServiceInfoJson) throws RouteException { + PowerMockito.mockStatic(HttpClientUtil.class); + PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockMSBUrl, MOCK_REG_SERVICE_JSON)) + .thenReturn(mockServiceInfoJson); + } + + private void mockHttpGet(String mockMSBUrl, String mockServiceInfoJson) throws RouteException { + PowerMockito.mockStatic(HttpClientUtil.class); + PowerMockito.when(HttpClientUtil.httpGet(mockMSBUrl)).thenReturn(mockServiceInfoJson); + } + +} |