Add license header for java files
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / consulextend / ConsulTest.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at 
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.consulextend;
17
18 import java.util.List;
19
20 import org.apache.http.HttpEntity;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mockito;
25 import org.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
27 import org.onap.msb.apiroute.wrapper.consulextend.CatalogClient;
28 import org.onap.msb.apiroute.wrapper.consulextend.Consul;
29 import org.onap.msb.apiroute.wrapper.consulextend.HealthClient;
30 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
31 import org.onap.msb.apiroute.wrapper.consulextend.async.OriginalConsulResponse;
32 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
33 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
34 import org.powermock.api.mockito.PowerMockito;
35 import org.powermock.core.classloader.annotations.PowerMockIgnore;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.fasterxml.jackson.core.type.TypeReference;
42 import com.orbitz.consul.model.ConsulResponse;
43 import com.orbitz.consul.option.CatalogOptions;
44 import com.orbitz.consul.option.QueryOptions;
45
46 @RunWith(PowerMockRunner.class)
47 @PrepareForTest({ Http.class })
48 @PowerMockIgnore({ "javax.net.ssl.*" })
49 public class ConsulTest {
50         private static Consul consul10081;
51         private static Consul consul8500;
52
53         private static final Logger LOGGER = LoggerFactory
54                         .getLogger(ConsulTest.class);
55
56         @SuppressWarnings({ "unchecked", "rawtypes" })
57         @BeforeClass
58         public static void setUpBeforeClass() throws Exception {
59                 
60                 Http http = PowerMockito.mock(Http.class);
61                 
62                 PowerMockito
63                                 .doAnswer(new Answer() {
64                                         @Override
65                                         public Object answer(InvocationOnMock invocation)
66                                                         throws Throwable {
67                                                 Object[] args = invocation.getArguments();
68                                                 ((ConsulResponseCallback) args[2]).onComplete(null);                                            
69                                                 return null;
70                                         }
71                                 })
72                                 .when(http)
73                                 .asyncGet(Mockito.anyString(),
74                                                 Mockito.any(TypeReference.class),
75                                                 Mockito.any(ConsulResponseCallback.class));
76                 
77                 //
78                 PowerMockito.spy(Http.class);
79                 PowerMockito.when(Http.getInstance()).thenReturn(http);
80                                 
81                 //
82                 consul10081 = Consul.builder().withHostAndPort("10.74.148.111", 10081)
83                                 .build();
84                 consul8500 = Consul.builder().withHostAndPort("10.74.148.111", 8500)
85                                 .build();
86         }
87
88         @Test
89         public void testcatalogClient() {
90                 
91                 ConsulResponseCallback<HttpEntity> callback = new ConsulResponseCallback<HttpEntity>() {
92
93                         @Override
94                         public void onComplete(
95                                         ConsulResponse<HttpEntity> consulResponse) {
96                                 LOGGER.info("service list complete!");
97                         }
98
99                         @Override
100                         public void onFailure(Throwable throwable) {
101                                 LOGGER.info("service list failure!");
102                         }
103
104                         @Override
105                         public void onDelayComplete(
106                                         OriginalConsulResponse<HttpEntity> originalConsulResponse) {
107                                 // TODO Auto-generated method stub
108                                 LOGGER.info("service list complete!");
109                         }
110
111                 };
112
113                 // 10081
114                 CatalogClient catalogclient10081 = consul10081.catalogClient();
115                 catalogclient10081.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
116
117                 // 8500
118                 CatalogClient catalogclient8500 = consul8500.catalogClient();
119                 catalogclient8500.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
120         }
121
122         @Test
123         public void testhealthClient() {
124
125                 ConsulResponseCallback<List<ServiceHealth>> callback = new ConsulResponseCallback<List<ServiceHealth>>() {
126
127                         @Override
128                         public void onComplete(
129                                         ConsulResponse<List<ServiceHealth>> consulResponse) {
130                                 LOGGER.info("health service complete!");
131
132                         }
133
134                         @Override
135                         public void onFailure(Throwable throwable) {
136                                 LOGGER.info("health service failure!");
137                         }
138
139                         @Override
140                         public void onDelayComplete(
141                                         OriginalConsulResponse<List<ServiceHealth>> originalConsulResponse) {
142                                 // TODO Auto-generated method stub
143                                 LOGGER.info("health service complete!");
144                         }
145
146                 };
147
148                 // 10081
149                 HealthClient healthClient10081 = consul10081.healthClient();
150                 healthClient10081.getAllServiceInstances("apigateway-default", CatalogOptions.BLANK,
151                                 QueryOptions.BLANK, callback);
152
153                 healthClient10081.getHealthyServiceInstances("apigateway-default",
154                                 CatalogOptions.BLANK, QueryOptions.BLANK, callback);
155
156                 // 8500
157                 HealthClient healthClient8500 = consul8500.healthClient();
158                 healthClient8500.getAllServiceInstances("apigateway-default", CatalogOptions.BLANK,
159                                 QueryOptions.BLANK, callback);
160                 healthClient8500.getHealthyServiceInstances("apigateway-default", CatalogOptions.BLANK,
161                                 QueryOptions.BLANK, callback);
162
163         }
164 }