2228de691263a18a12ecc824733a56c34470fb28
[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 import java.util.ArrayList;
23
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;
35
36 public class ServiceBuilderTest {
37
38   private List<HandlerContextBuilder> builders = new ArrayList<>();
39   private HttpClientConf globalHttpClientConf;
40   
41   /**
42    * @throws java.lang.Exception
43    */
44   @Before
45   public void setUp() throws Exception {
46     
47
48     globalHttpClientConf = new HttpClientConf();
49     RetrofitServiceHandlerContext.setGlobalHttpClientConf(globalHttpClientConf);
50     init();
51     
52   }
53
54   @Test
55   public void test_buildDefaultHandlerContext() {
56     
57    
58     RetrofitServiceHandlerContext ctx = buildCtx(AnimalServiceClient1.class);
59     
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());
64     
65     
66     
67   }
68   
69   
70   @Test
71   public void test_buildCustomizationHandlerContext() {
72     
73    
74     RetrofitServiceHandlerContext ctx = buildCtx(AnimalServiceClient2.class);
75     
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());
84   }
85   
86   
87   private RetrofitServiceHandlerContext buildCtx(Class<?> retrofitSrvInterfaceClazz){
88     RetrofitServiceHandlerContext ctx = new RetrofitServiceHandlerContext();
89     ctx.setRetrofitSrvInterfaceClazz(retrofitSrvInterfaceClazz);
90     for (HandlerContextBuilder builder : builders) {
91       builder.build(ctx);
92     }
93    
94     return ctx;
95   }
96   
97   
98   private void init() {
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());
104   }
105 }