1 /*******************************************************************************
2 * Copyright 2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
17 package org.onap.msb.sdk.httpclient.handler;
19 import static org.junit.Assert.assertEquals;
21 import java.util.List;
22 import java.util.ArrayList;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.onap.msb.sdk.httpclient.annotaion.LoadBalance.LBSTYLE;
27 import org.onap.msb.sdk.httpclient.conf.HttpClientConf;
28 import org.onap.msb.sdk.httpclient.convert.jackson.JacksonConverterFactoryBuilder;
29 import org.onap.msb.sdk.httpclient.handler.impl.ConnectionParamsBuilder;
30 import org.onap.msb.sdk.httpclient.handler.impl.ConverterFactoryBuilder;
31 import org.onap.msb.sdk.httpclient.handler.impl.LBBuilder;
32 import org.onap.msb.sdk.httpclient.handler.impl.RetrofitHandlerContextBuilder;
33 import org.onap.msb.sdk.httpclient.handler.impl.ServiceHttpEndPointBeanObjectBuilder;
34 import org.onap.msb.sdk.httpclient.lb.RoundRobinLBStrategy;
36 public class ServiceBuilderTest {
38 private List<HandlerContextBuilder> builders = new ArrayList<>();
39 private HttpClientConf globalHttpClientConf;
42 * @throws java.lang.Exception
45 public void setUp() throws Exception {
48 globalHttpClientConf = new HttpClientConf();
49 RetrofitServiceHandlerContext.setGlobalHttpClientConf(globalHttpClientConf);
55 public void test_buildDefaultHandlerContext() {
58 RetrofitServiceHandlerContext ctx = buildCtx(AnimalServiceClient1.class);
60 assertEquals(RoundRobinLBStrategy.class,ctx.getLbStrategy().getClass());
61 assertEquals(LBSTYLE.CLIENT, ctx.getLbStyle());
62 assertEquals("animals",ctx.getServiceHttpEndPointBeanObject().getServiceName());
63 assertEquals("v1",ctx.getServiceHttpEndPointBeanObject().getServiceVersion());
71 public void test_buildCustomizationHandlerContext() {
74 RetrofitServiceHandlerContext ctx = buildCtx(AnimalServiceClient2.class);
76 assertEquals(RoundRobinLBStrategy.class,ctx.getLbStrategy().getClass());
77 assertEquals(LBSTYLE.MSB, ctx.getLbStyle());
78 assertEquals("animals",ctx.getServiceHttpEndPointBeanObject().getServiceName());
79 assertEquals("v1",ctx.getServiceHttpEndPointBeanObject().getServiceVersion());
80 assertEquals(JacksonConverterFactoryBuilder.class,ctx.getConverterFactoryBuilder().getClass());
81 assertEquals(1000,ctx.getHttpClientConf().getReadTimeout());
82 assertEquals(2000,ctx.getHttpClientConf().getConnectTimeout());
83 assertEquals(3000,ctx.getHttpClientConf().getWriteTimeout());
87 private RetrofitServiceHandlerContext buildCtx(Class<?> retrofitSrvInterfaceClazz){
88 RetrofitServiceHandlerContext ctx = new RetrofitServiceHandlerContext();
89 ctx.setRetrofitSrvInterfaceClazz(retrofitSrvInterfaceClazz);
90 for (HandlerContextBuilder builder : builders) {
99 builders.add(new ServiceHttpEndPointBeanObjectBuilder());
100 builders.add(new LBBuilder());
101 builders.add(new ConnectionParamsBuilder());
102 builders.add(new ConverterFactoryBuilder());
103 builders.add(new RetrofitHandlerContextBuilder());