diff options
author | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-10-10 13:59:16 +0800 |
---|---|---|
committer | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-10-10 14:32:30 +0800 |
commit | 422fe052e2962bacd902bc0a10488ab017071a37 (patch) | |
tree | ddfc45d924780709d0f742f00560949cbd23e6bb | |
parent | 42e2102bb052235b563554d24d5b199b7d16ea07 (diff) |
Add unit test for MSB JAVA SDK
Issue-Id: MSB-86
Change-Id: Icfd790e04014c47aae87beab39fc812c2a64914c
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
7 files changed, 318 insertions, 13 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 b202ff4..fc8f483 100644 --- a/src/test/java/org/onap/msb/sdk/discovery/MSBServiceTest.java +++ b/src/test/java/org/onap/msb/sdk/discovery/MSBServiceTest.java @@ -97,14 +97,21 @@ public class MSBServiceTest { 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"); - // } + @Test + public void test_unregistration() throws Exception { + String msbAddress = "127.0.0.1:10081"; + mockHttpDel(); + MSBService msbService = new MSBService(); + msbService.cancelMicroServiceInfo(msbAddress, "aai", "v8"); + } + @Test + public void test_unregistration_a_instance() throws Exception { + String msbAddress = "127.0.0.1:10081"; + mockHttpDel(); + MSBService msbService = new MSBService(); + msbService.cancelMicroServiceInfo(msbAddress, "aai", "v8", "10.74.44.1", "8443"); + } private MicroServiceFullInfo mockMicroServiceFullInfo(MicroServiceInfo info) { MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo(); @@ -130,10 +137,8 @@ public class MSBServiceTest { 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)); - // } + private void mockHttpDel() throws Exception { + PowerMockito.mockStatic(HttpClientUtil.class); + + } } diff --git a/src/test/java/org/onap/msb/sdk/httpclient/ServiceHttpEndPointObjectTest.java b/src/test/java/org/onap/msb/sdk/httpclient/ServiceHttpEndPointObjectTest.java new file mode 100644 index 0000000..9ee875d --- /dev/null +++ b/src/test/java/org/onap/msb/sdk/httpclient/ServiceHttpEndPointObjectTest.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 org.junit.Assert; +import org.junit.Test; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.NodeInfo; + +public class ServiceHttpEndPointObjectTest { + + @Test + public void testBean() { + NodeInfo nodeInfo = new NodeInfo(); + nodeInfo.setIp("127.0.0.1"); + nodeInfo.setPort("80"); + MicroServiceFullInfo cloneFullInfo = new MicroServiceFullInfo(); + cloneFullInfo.setUrl("/aai/v8"); + cloneFullInfo.setServiceName("aai"); + cloneFullInfo.setVersion("v8"); + cloneFullInfo.setVisualRange("1"); + + ServiceHttpEndPointObject endPointOjb = new ServiceHttpEndPointObject("aai", "v8", nodeInfo, cloneFullInfo); + Assert.assertEquals(endPointOjb.getIp(), "127.0.0.1"); + Assert.assertEquals(endPointOjb.getPort(), "80"); + Assert.assertEquals(endPointOjb.getServiceName(), "aai"); + Assert.assertEquals(endPointOjb.getServiceVersion(), "v8"); + } + +} diff --git a/src/test/java/org/onap/msb/sdk/httpclient/conf/HttpClientConfTest.java b/src/test/java/org/onap/msb/sdk/httpclient/conf/HttpClientConfTest.java new file mode 100644 index 0000000..8cd3c5f --- /dev/null +++ b/src/test/java/org/onap/msb/sdk/httpclient/conf/HttpClientConfTest.java @@ -0,0 +1,69 @@ +/** + * 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.conf; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Test; + +public class HttpClientConfTest { + + @Test + public void testEquals() { + HttpClientConf conf1 = new HttpClientConf(); + conf1.setConnectTimeout(100000); + conf1.setReadTimeout(200000); + conf1.setWriteTimeout(30000); + + HttpClientConf conf2 = new HttpClientConf(); + conf2.setConnectTimeout(100000); + conf2.setReadTimeout(200000); + conf2.setWriteTimeout(30000); + + Assert.assertEquals(conf1, conf2); + + conf2.setWriteTimeout(10000); + Assert.assertNotEquals(conf1, conf2); + } + + @Test + public void testHashCode() { + HttpClientConf conf1 = new HttpClientConf(); + conf1.setConnectTimeout(100000); + conf1.setReadTimeout(200000); + conf1.setWriteTimeout(30000); + + HttpClientConf conf2 = new HttpClientConf(); + conf2.setConnectTimeout(100000); + conf2.setReadTimeout(200000); + conf2.setWriteTimeout(30000); + + Set set = new HashSet(); + set.add(conf1); + set.add(conf2); + + Assert.assertEquals(set.size(), 1); + + HttpClientConf conf3 = new HttpClientConf(); + conf3.setConnectTimeout(100000); + conf3.setReadTimeout(200000); + conf3.setWriteTimeout(10000); + set.add(conf3); + Assert.assertEquals(set.size(), 2); + } + + +} diff --git a/src/test/java/org/onap/msb/sdk/httpclient/handler/AnimalServiceClient1.java b/src/test/java/org/onap/msb/sdk/httpclient/handler/AnimalServiceClient1.java new file mode 100644 index 0000000..d3a55fc --- /dev/null +++ b/src/test/java/org/onap/msb/sdk/httpclient/handler/AnimalServiceClient1.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * 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.handler; + +import org.onap.msb.sdk.httpclient.annotaion.ServiceHttpEndPoint; + +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Path; + +@ServiceHttpEndPoint(serviceName = "animals", serviceVersion = "v1") +public interface AnimalServiceClient1 { + + @GET("animals/{name}") + Call<String> queryAnimal(@Path("name") String name); +} diff --git a/src/test/java/org/onap/msb/sdk/httpclient/handler/AnimalServiceClient2.java b/src/test/java/org/onap/msb/sdk/httpclient/handler/AnimalServiceClient2.java new file mode 100644 index 0000000..c1c6228 --- /dev/null +++ b/src/test/java/org/onap/msb/sdk/httpclient/handler/AnimalServiceClient2.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * 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.handler; + +import org.onap.msb.sdk.httpclient.annotaion.BodyConverter; +import org.onap.msb.sdk.httpclient.annotaion.ConnectionParams; +import org.onap.msb.sdk.httpclient.annotaion.LoadBalance; +import org.onap.msb.sdk.httpclient.annotaion.LoadBalance.LBSTYLE; +import org.onap.msb.sdk.httpclient.annotaion.ServiceHttpEndPoint; + +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Path; + +@ServiceHttpEndPoint(serviceName = "animals", serviceVersion = "v1") +@LoadBalance(lbStyle=LBSTYLE.MSB,lbClassName="org.onap.msb.sdk.httpclient.lb.RoundRobinLBStrategy") +@BodyConverter(builderClassName="org.onap.msb.sdk.httpclient.convert.jackson.JacksonConverterFactoryBuilder") +@ConnectionParams(readTimeout=1000,connectTimeout=2000,writeTimeout=3000) +public interface AnimalServiceClient2 { + + @GET("animals/{name}") + + Call<String> queryAnimal(@Path("name") String name); +} diff --git a/src/test/java/org/onap/msb/sdk/httpclient/handler/ServiceBuilderTest.java b/src/test/java/org/onap/msb/sdk/httpclient/handler/ServiceBuilderTest.java new file mode 100644 index 0000000..6c29440 --- /dev/null +++ b/src/test/java/org/onap/msb/sdk/httpclient/handler/ServiceBuilderTest.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * 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.handler; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.onap.msb.sdk.httpclient.annotaion.LoadBalance.LBSTYLE; +import org.onap.msb.sdk.httpclient.conf.HttpClientConf; +import org.onap.msb.sdk.httpclient.convert.jackson.JacksonConverterFactoryBuilder; +import org.onap.msb.sdk.httpclient.handler.impl.ConnectionParamsBuilder; +import org.onap.msb.sdk.httpclient.handler.impl.ConverterFactoryBuilder; +import org.onap.msb.sdk.httpclient.handler.impl.LBBuilder; +import org.onap.msb.sdk.httpclient.handler.impl.MetricmanagerBuilder; +import org.onap.msb.sdk.httpclient.handler.impl.RetrofitHandlerContextBuilder; +import org.onap.msb.sdk.httpclient.handler.impl.ServiceHttpEndPointBeanObjectBuilder; +import org.onap.msb.sdk.httpclient.lb.RoundRobinLBStrategy; + +import com.google.common.collect.Lists; + + +public class ServiceBuilderTest { + + private List<HandlerContextBuilder> builders = Lists.newArrayList(); + private HttpClientConf globalHttpClientConf; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + + + globalHttpClientConf = new HttpClientConf(); + RetrofitServiceHandlerContext.setGlobalHttpClientConf(globalHttpClientConf); + init(); + + } + + @Test + public void test_buildDefaultHandlerContext() { + + + RetrofitServiceHandlerContext ctx = buildCtx(AnimalServiceClient1.class); + + assertEquals(RoundRobinLBStrategy.class,ctx.getLbStrategy().getClass()); + assertEquals(LBSTYLE.CLIENT, ctx.getLbStyle()); + assertEquals("animals",ctx.getServiceHttpEndPointBeanObject().getServiceName()); + assertEquals("v1",ctx.getServiceHttpEndPointBeanObject().getServiceVersion()); + + + + } + + + @Test + public void test_buildCustomizationHandlerContext() { + + + RetrofitServiceHandlerContext ctx = buildCtx(AnimalServiceClient2.class); + + assertEquals(RoundRobinLBStrategy.class,ctx.getLbStrategy().getClass()); + assertEquals(LBSTYLE.MSB, ctx.getLbStyle()); + assertEquals("animals",ctx.getServiceHttpEndPointBeanObject().getServiceName()); + assertEquals("v1",ctx.getServiceHttpEndPointBeanObject().getServiceVersion()); + assertEquals(JacksonConverterFactoryBuilder.class,ctx.getConverterFactoryBuilder().getClass()); + assertEquals(1000,ctx.getHttpClientConf().getReadTimeout()); + assertEquals(2000,ctx.getHttpClientConf().getConnectTimeout()); + assertEquals(3000,ctx.getHttpClientConf().getWriteTimeout()); + } + + + private RetrofitServiceHandlerContext buildCtx(Class<?> retrofitSrvInterfaceClazz){ + RetrofitServiceHandlerContext ctx = new RetrofitServiceHandlerContext(); + ctx.setRetrofitSrvInterfaceClazz(retrofitSrvInterfaceClazz); + for (HandlerContextBuilder builder : builders) { + builder.build(ctx); + } + + return ctx; + } + + + private void init() { + builders.add(new ServiceHttpEndPointBeanObjectBuilder()); + builders.add(new MetricmanagerBuilder()); + builders.add(new LBBuilder()); + builders.add(new ConnectionParamsBuilder()); + builders.add(new ConverterFactoryBuilder()); + builders.add(new RetrofitHandlerContextBuilder()); + } +} 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 index 71acd1a..403e44b 100644 --- a/src/test/java/org/onap/msb/sdk/httpclient/msb/MSBServiceClientTest.java +++ b/src/test/java/org/onap/msb/sdk/httpclient/msb/MSBServiceClientTest.java @@ -16,6 +16,7 @@ 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.MSBService; import org.onap.msb.sdk.discovery.common.RouteException; import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; @@ -89,6 +90,20 @@ public class MSBServiceClientTest { Assert.assertTrue(microServiceFullInfo.getVisualRange().equals("1")); } + @Test + public void test_unregistration() throws Exception { + mockHttpDel(); + MSBServiceClient msbClient = new MSBServiceClient("127.0.0.1", 10081); + msbClient.cancelMicroServiceInfo("aai", "v8"); + } + + @Test + public void test_unregistration_a_instance() throws Exception { + mockHttpDel(); + MSBServiceClient msbClient = new MSBServiceClient("127.0.0.1", 10081); + msbClient.cancelMicroServiceInfo("aai", "v8", "10.74.44.1", "8443"); + } + private MicroServiceFullInfo mockMicroServiceFullInfo(MicroServiceInfo info) { MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo(); serviceInfo.setServiceName(info.getServiceName()); @@ -113,4 +128,8 @@ public class MSBServiceClientTest { PowerMockito.when(HttpClientUtil.httpGet(mockMSBUrl)).thenReturn(mockServiceInfoJson); } + private void mockHttpDel() throws Exception { + PowerMockito.mockStatic(HttpClientUtil.class); + + } } |