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.expose;
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mockito;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
28 import org.onap.msb.apiroute.wrapper.consulextend.Consul;
29 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
30 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchServiceHealthTask;
31 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask;
32 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask.Filter;
33 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
34 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
35 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
36 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
37 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PowerMockIgnore;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.fasterxml.jackson.core.type.TypeReference;
46 import com.orbitz.consul.model.ConsulResponse;
47 import com.orbitz.consul.model.health.ImmutableNode;
48 import com.orbitz.consul.option.CatalogOptions;
49 import com.orbitz.consul.option.QueryOptions;
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest({ Http.class })
53 @PowerMockIgnore({ "javax.net.ssl.*" })
54 public class WatchServiceHealthTaskTest {
55 private static final Logger LOGGER = LoggerFactory
56 .getLogger(WatchServiceHealthTaskTest.class);
58 private Consul consul;
60 @SuppressWarnings({ "unchecked", "rawtypes" })
64 List<ServiceHealth> list = new ArrayList<ServiceHealth>();
66 Service service = ImmutableService.builder().id("").port(0).address("")
67 .service("huangleibo").addTags("").createIndex(1).modifyIndex(1).build();
68 ServiceHealth serviceHealth = ImmutableServiceHealth.builder()
70 .node(ImmutableNode.builder().node("").address("").build())
72 list.add(serviceHealth);
75 boolean knownLeader = true;
76 BigInteger index = BigInteger.valueOf(1);
77 final ConsulResponse<List<ServiceHealth>> response = new ConsulResponse<List<ServiceHealth>>(
78 list, lastContact, knownLeader, index);
81 Http http = PowerMockito.mock(Http.class);
84 .doAnswer(new Answer() {
86 public Object answer(InvocationOnMock invocation)
88 Object[] args = invocation.getArguments();
89 ((ConsulResponseCallback) args[2]).onComplete(response);
94 .asyncGet(Mockito.anyString(),
95 Mockito.any(TypeReference.class),
96 Mockito.any(ConsulResponseCallback.class));
99 PowerMockito.spy(Http.class);
100 PowerMockito.when(Http.getInstance()).thenReturn(http);
105 public void testgetServiceName() {
106 consul = Consul.newClient();
108 WatchServiceHealthTask task0 = new WatchServiceHealthTask(
109 consul.healthClient(), "huangleibo_task0", true,
110 CatalogOptions.BLANK, 10, QueryOptions.BLANK);
112 LOGGER.info("service name:" + task0.getServiceName());
114 WatchServiceHealthTask task1 = new WatchServiceHealthTask(
115 consul.healthClient(), "huangleibo_task1", true, 10);
117 LOGGER.debug("service name:" + task1.getServiceName());
119 WatchServiceHealthTask task2 = new WatchServiceHealthTask(
120 consul.healthClient(), "huangleibo_task2", 10);
122 LOGGER.debug("service name:" + task2.getServiceName());
126 public class StopHandler implements WatchTask.Handler<List<ServiceHealth>>
129 private WatchServiceHealthTask task;
131 StopHandler(WatchServiceHealthTask task)
137 public void handle(ConsulResponse<List<ServiceHealth>> object) {
138 // TODO Auto-generated method stub
139 List<ServiceHealth> list = (List<ServiceHealth>)object.getResponse();
140 LOGGER.debug("handler:"+list.get(0).getService().getService());
145 @SuppressWarnings({ "unchecked", "rawtypes" })
147 public void teststartWatch() {
148 Consul consul = Consul.newClient();
149 String serviceName = "huangleibo";
151 WatchServiceHealthTask task0 = new WatchServiceHealthTask(
152 consul.healthClient(), serviceName, true, CatalogOptions.BLANK,
153 10, QueryOptions.BLANK);
155 task0.addFilter(new Filter() {
158 public boolean filter(ConsulResponse object) {
159 // TODO Auto-generated method stub
160 List<ServiceHealth> list = (List<ServiceHealth>)object.getResponse();
161 LOGGER.debug("filter:"+list.get(0).getService().getService());
167 task0.addHandler(new StopHandler(task0));