6c294404bd035d208c4f97c981ba9cd47bf22c99
[msb/java-sdk.git] / src / test / java / org / onap / msb / sdk / httpclient / handler / ServiceBuilderTest.java
1 /*******************************************************************************
2  * Copyright 2017 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 /**
15  * 
16  */
17 package org.onap.msb.sdk.httpclient.handler;
18
19 import static org.junit.Assert.assertEquals;
20
21 import java.util.List;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.onap.msb.sdk.httpclient.annotaion.LoadBalance.LBSTYLE;
26 import org.onap.msb.sdk.httpclient.conf.HttpClientConf;
27 import org.onap.msb.sdk.httpclient.convert.jackson.JacksonConverterFactoryBuilder;
28 import org.onap.msb.sdk.httpclient.handler.impl.ConnectionParamsBuilder;
29 import org.onap.msb.sdk.httpclient.handler.impl.ConverterFactoryBuilder;
30 import org.onap.msb.sdk.httpclient.handler.impl.LBBuilder;
31 import org.onap.msb.sdk.httpclient.handler.impl.MetricmanagerBuilder;
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;
35
36 import com.google.common.collect.Lists;
37
38
39 public class ServiceBuilderTest {
40
41   private List<HandlerContextBuilder> builders = Lists.newArrayList();
42   private HttpClientConf globalHttpClientConf;
43   
44   /**
45    * @throws java.lang.Exception
46    */
47   @Before
48   public void setUp() throws Exception {
49     
50
51     globalHttpClientConf = new HttpClientConf();
52     RetrofitServiceHandlerContext.setGlobalHttpClientConf(globalHttpClientConf);
53     init();
54     
55   }
56
57   @Test
58   public void test_buildDefaultHandlerContext() {
59     
60    
61     RetrofitServiceHandlerContext ctx = buildCtx(AnimalServiceClient1.class);
62     
63    assertEquals(RoundRobinLBStrategy.class,ctx.getLbStrategy().getClass());
64     assertEquals(LBSTYLE.CLIENT, ctx.getLbStyle());
65     assertEquals("animals",ctx.getServiceHttpEndPointBeanObject().getServiceName());
66     assertEquals("v1",ctx.getServiceHttpEndPointBeanObject().getServiceVersion());
67     
68     
69     
70   }
71   
72   
73   @Test
74   public void test_buildCustomizationHandlerContext() {
75     
76    
77     RetrofitServiceHandlerContext ctx = buildCtx(AnimalServiceClient2.class);
78     
79     assertEquals(RoundRobinLBStrategy.class,ctx.getLbStrategy().getClass());
80     assertEquals(LBSTYLE.MSB, ctx.getLbStyle());
81     assertEquals("animals",ctx.getServiceHttpEndPointBeanObject().getServiceName());
82     assertEquals("v1",ctx.getServiceHttpEndPointBeanObject().getServiceVersion());
83     assertEquals(JacksonConverterFactoryBuilder.class,ctx.getConverterFactoryBuilder().getClass());
84     assertEquals(1000,ctx.getHttpClientConf().getReadTimeout());
85     assertEquals(2000,ctx.getHttpClientConf().getConnectTimeout());
86     assertEquals(3000,ctx.getHttpClientConf().getWriteTimeout());
87   }
88   
89   
90   private RetrofitServiceHandlerContext buildCtx(Class<?> retrofitSrvInterfaceClazz){
91     RetrofitServiceHandlerContext ctx = new RetrofitServiceHandlerContext();
92     ctx.setRetrofitSrvInterfaceClazz(retrofitSrvInterfaceClazz);
93     for (HandlerContextBuilder builder : builders) {
94       builder.build(ctx);
95     }
96    
97     return ctx;
98   }
99   
100   
101   private void init() {
102     builders.add(new ServiceHttpEndPointBeanObjectBuilder());
103     builders.add(new MetricmanagerBuilder());
104     builders.add(new LBBuilder());
105     builders.add(new ConnectionParamsBuilder());
106     builders.add(new ConverterFactoryBuilder());
107     builders.add(new RetrofitHandlerContextBuilder());
108   }
109 }