1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.consulextend;
16 import java.util.List;
18 import org.apache.http.HttpEntity;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mockito;
23 import org.mockito.invocation.InvocationOnMock;
24 import org.mockito.stubbing.Answer;
25 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
26 import org.onap.msb.apiroute.wrapper.consulextend.async.OriginalConsulResponse;
27 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
28 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
29 import org.powermock.api.mockito.PowerMockito;
30 import org.powermock.core.classloader.annotations.PowerMockIgnore;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.fasterxml.jackson.core.type.TypeReference;
37 import com.orbitz.consul.model.ConsulResponse;
38 import com.orbitz.consul.option.CatalogOptions;
39 import com.orbitz.consul.option.QueryOptions;
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest({Http.class})
43 @PowerMockIgnore({"javax.net.ssl.*", "jdk.internal.reflect.*"})
44 public class ConsulTest {
45 private static Consul consul10081;
46 private static Consul consul8500;
48 private static final Logger LOGGER = LoggerFactory.getLogger(ConsulTest.class);
50 @SuppressWarnings({"unchecked", "rawtypes"})
52 public static void setUpBeforeClass() throws Exception {
54 Http http = PowerMockito.mock(Http.class);
56 PowerMockito.doAnswer(new Answer() {
58 public Object answer(InvocationOnMock invocation) throws Throwable {
59 Object[] args = invocation.getArguments();
60 ((ConsulResponseCallback) args[2]).onComplete(null);
63 }).when(http).asyncGet(Mockito.anyString(), Mockito.any(TypeReference.class),
64 Mockito.any(ConsulResponseCallback.class));
67 PowerMockito.spy(Http.class);
68 PowerMockito.when(Http.getInstance()).thenReturn(http);
71 consul10081 = Consul.builder().withHostAndPort("10.74.148.111", 10081).build();
72 consul8500 = Consul.builder().withHostAndPort("10.74.148.111", 8500).build();
76 public void testcatalogClient() {
78 ConsulResponseCallback<HttpEntity> callback = new ConsulResponseCallback<HttpEntity>() {
81 public void onComplete(ConsulResponse<HttpEntity> consulResponse) {
82 LOGGER.info("service list complete!");
86 public void onFailure(Throwable throwable) {
87 LOGGER.info("service list failure!");
91 public void onDelayComplete(OriginalConsulResponse<HttpEntity> originalConsulResponse) {
92 // TODO Auto-generated method stub
93 LOGGER.info("service list complete!");
99 CatalogClient catalogclient10081 = consul10081.catalogClient();
100 catalogclient10081.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
103 CatalogClient catalogclient8500 = consul8500.catalogClient();
104 catalogclient8500.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
108 public void testhealthClient() {
110 ConsulResponseCallback<List<ServiceHealth>> callback = new ConsulResponseCallback<List<ServiceHealth>>() {
113 public void onComplete(ConsulResponse<List<ServiceHealth>> consulResponse) {
114 LOGGER.info("health service complete!");
119 public void onFailure(Throwable throwable) {
120 LOGGER.info("health service failure!");
124 public void onDelayComplete(OriginalConsulResponse<List<ServiceHealth>> originalConsulResponse) {
125 // TODO Auto-generated method stub
126 LOGGER.info("health service complete!");
132 HealthClient healthClient10081 = consul10081.healthClient();
133 healthClient10081.getAllServiceInstances("apigateway-default", CatalogOptions.BLANK, QueryOptions.BLANK,
136 healthClient10081.getHealthyServiceInstances("apigateway-default", CatalogOptions.BLANK, QueryOptions.BLANK,
140 HealthClient healthClient8500 = consul8500.healthClient();
141 healthClient8500.getAllServiceInstances("apigateway-default", CatalogOptions.BLANK, QueryOptions.BLANK,
143 healthClient8500.getHealthyServiceInstances("apigateway-default", CatalogOptions.BLANK, QueryOptions.BLANK,