1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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;
18 import java.util.List;
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;
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;
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;
53 private static final Logger LOGGER = LoggerFactory
54 .getLogger(ConsulTest.class);
56 @SuppressWarnings({ "unchecked", "rawtypes" })
58 public static void setUpBeforeClass() throws Exception {
60 Http http = PowerMockito.mock(Http.class);
63 .doAnswer(new Answer() {
65 public Object answer(InvocationOnMock invocation)
67 Object[] args = invocation.getArguments();
68 ((ConsulResponseCallback) args[2]).onComplete(null);
73 .asyncGet(Mockito.anyString(),
74 Mockito.any(TypeReference.class),
75 Mockito.any(ConsulResponseCallback.class));
78 PowerMockito.spy(Http.class);
79 PowerMockito.when(Http.getInstance()).thenReturn(http);
82 consul10081 = Consul.builder().withHostAndPort("10.74.148.111", 10081)
84 consul8500 = Consul.builder().withHostAndPort("10.74.148.111", 8500)
89 public void testcatalogClient() {
91 ConsulResponseCallback<HttpEntity> callback = new ConsulResponseCallback<HttpEntity>() {
94 public void onComplete(
95 ConsulResponse<HttpEntity> consulResponse) {
96 LOGGER.info("service list complete!");
100 public void onFailure(Throwable throwable) {
101 LOGGER.info("service list failure!");
105 public void onDelayComplete(
106 OriginalConsulResponse<HttpEntity> originalConsulResponse) {
107 // TODO Auto-generated method stub
108 LOGGER.info("service list complete!");
114 CatalogClient catalogclient10081 = consul10081.catalogClient();
115 catalogclient10081.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
118 CatalogClient catalogclient8500 = consul8500.catalogClient();
119 catalogclient8500.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
123 public void testhealthClient() {
125 ConsulResponseCallback<List<ServiceHealth>> callback = new ConsulResponseCallback<List<ServiceHealth>>() {
128 public void onComplete(
129 ConsulResponse<List<ServiceHealth>> consulResponse) {
130 LOGGER.info("health service complete!");
135 public void onFailure(Throwable throwable) {
136 LOGGER.info("health service failure!");
140 public void onDelayComplete(
141 OriginalConsulResponse<List<ServiceHealth>> originalConsulResponse) {
142 // TODO Auto-generated method stub
143 LOGGER.info("health service complete!");
149 HealthClient healthClient10081 = consul10081.healthClient();
150 healthClient10081.getAllServiceInstances("apigateway-default", CatalogOptions.BLANK,
151 QueryOptions.BLANK, callback);
153 healthClient10081.getHealthyServiceInstances("apigateway-default",
154 CatalogOptions.BLANK, QueryOptions.BLANK, callback);
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);