Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / consulextend / ConsulTest.java
1 package org.onap.msb.apiroute.wrapper.consulextend;
2
3 import java.util.List;
4
5 import org.apache.http.HttpEntity;
6 import org.junit.BeforeClass;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 import org.mockito.Mockito;
10 import org.mockito.invocation.InvocationOnMock;
11 import org.mockito.stubbing.Answer;
12 import org.onap.msb.apiroute.wrapper.consulextend.CatalogClient;
13 import org.onap.msb.apiroute.wrapper.consulextend.Consul;
14 import org.onap.msb.apiroute.wrapper.consulextend.HealthClient;
15 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
16 import org.onap.msb.apiroute.wrapper.consulextend.async.OriginalConsulResponse;
17 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
18 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
19 import org.powermock.api.mockito.PowerMockito;
20 import org.powermock.core.classloader.annotations.PowerMockIgnore;
21 import org.powermock.core.classloader.annotations.PrepareForTest;
22 import org.powermock.modules.junit4.PowerMockRunner;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.fasterxml.jackson.core.type.TypeReference;
27 import com.orbitz.consul.model.ConsulResponse;
28 import com.orbitz.consul.option.CatalogOptions;
29 import com.orbitz.consul.option.QueryOptions;
30
31 @RunWith(PowerMockRunner.class)
32 @PrepareForTest({ Http.class })
33 @PowerMockIgnore({ "javax.net.ssl.*" })
34 public class ConsulTest {
35         private static Consul consul10081;
36         private static Consul consul8500;
37
38         private static final Logger LOGGER = LoggerFactory
39                         .getLogger(ConsulTest.class);
40
41         @SuppressWarnings({ "unchecked", "rawtypes" })
42         @BeforeClass
43         public static void setUpBeforeClass() throws Exception {
44                 
45                 Http http = PowerMockito.mock(Http.class);
46                 
47                 PowerMockito
48                                 .doAnswer(new Answer() {
49                                         @Override
50                                         public Object answer(InvocationOnMock invocation)
51                                                         throws Throwable {
52                                                 Object[] args = invocation.getArguments();
53                                                 ((ConsulResponseCallback) args[2]).onComplete(null);                                            
54                                                 return null;
55                                         }
56                                 })
57                                 .when(http)
58                                 .asyncGet(Mockito.anyString(),
59                                                 Mockito.any(TypeReference.class),
60                                                 Mockito.any(ConsulResponseCallback.class));
61                 
62                 //
63                 PowerMockito.spy(Http.class);
64                 PowerMockito.when(Http.getInstance()).thenReturn(http);
65                                 
66                 //
67                 consul10081 = Consul.builder().withHostAndPort("10.74.148.111", 10081)
68                                 .build();
69                 consul8500 = Consul.builder().withHostAndPort("10.74.148.111", 8500)
70                                 .build();
71         }
72
73         @Test
74         public void testcatalogClient() {
75                 
76                 ConsulResponseCallback<HttpEntity> callback = new ConsulResponseCallback<HttpEntity>() {
77
78                         @Override
79                         public void onComplete(
80                                         ConsulResponse<HttpEntity> consulResponse) {
81                                 LOGGER.info("service list complete!");
82                         }
83
84                         @Override
85                         public void onFailure(Throwable throwable) {
86                                 LOGGER.info("service list failure!");
87                         }
88
89                         @Override
90                         public void onDelayComplete(
91                                         OriginalConsulResponse<HttpEntity> originalConsulResponse) {
92                                 // TODO Auto-generated method stub
93                                 LOGGER.info("service list complete!");
94                         }
95
96                 };
97
98                 // 10081
99                 CatalogClient catalogclient10081 = consul10081.catalogClient();
100                 catalogclient10081.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
101
102                 // 8500
103                 CatalogClient catalogclient8500 = consul8500.catalogClient();
104                 catalogclient8500.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
105         }
106
107         @Test
108         public void testhealthClient() {
109
110                 ConsulResponseCallback<List<ServiceHealth>> callback = new ConsulResponseCallback<List<ServiceHealth>>() {
111
112                         @Override
113                         public void onComplete(
114                                         ConsulResponse<List<ServiceHealth>> consulResponse) {
115                                 LOGGER.info("health service complete!");
116
117                         }
118
119                         @Override
120                         public void onFailure(Throwable throwable) {
121                                 LOGGER.info("health service failure!");
122                         }
123
124                         @Override
125                         public void onDelayComplete(
126                                         OriginalConsulResponse<List<ServiceHealth>> originalConsulResponse) {
127                                 // TODO Auto-generated method stub
128                                 LOGGER.info("health service complete!");
129                         }
130
131                 };
132
133                 // 10081
134                 HealthClient healthClient10081 = consul10081.healthClient();
135                 healthClient10081.getAllServiceInstances("apigateway-default", CatalogOptions.BLANK,
136                                 QueryOptions.BLANK, callback);
137
138                 healthClient10081.getHealthyServiceInstances("apigateway-default",
139                                 CatalogOptions.BLANK, QueryOptions.BLANK, callback);
140
141                 // 8500
142                 HealthClient healthClient8500 = consul8500.healthClient();
143                 healthClient8500.getAllServiceInstances("apigateway-default", CatalogOptions.BLANK,
144                                 QueryOptions.BLANK, callback);
145                 healthClient8500.getHealthyServiceInstances("apigateway-default", CatalogOptions.BLANK,
146                                 QueryOptions.BLANK, callback);
147
148         }
149 }