1 package org.onap.msb.apiroute.wrapper.consulextend.expose;
3 import java.math.BigInteger;
4 import java.util.ArrayList;
7 import org.junit.Before;
9 import org.junit.runner.RunWith;
10 import org.mockito.Mockito;
11 import org.mockito.invocation.InvocationOnMock;
12 import org.mockito.stubbing.Answer;
13 import org.onap.msb.apiroute.wrapper.consulextend.Consul;
14 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
15 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchServiceHealthTask;
16 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask;
17 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask.Filter;
18 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
19 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
20 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
21 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
22 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
23 import org.powermock.api.mockito.PowerMockito;
24 import org.powermock.core.classloader.annotations.PowerMockIgnore;
25 import org.powermock.core.classloader.annotations.PrepareForTest;
26 import org.powermock.modules.junit4.PowerMockRunner;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.fasterxml.jackson.core.type.TypeReference;
31 import com.orbitz.consul.model.ConsulResponse;
32 import com.orbitz.consul.model.health.ImmutableNode;
33 import com.orbitz.consul.option.CatalogOptions;
34 import com.orbitz.consul.option.QueryOptions;
36 @RunWith(PowerMockRunner.class)
37 @PrepareForTest({ Http.class })
38 @PowerMockIgnore({ "javax.net.ssl.*" })
39 public class WatchServiceHealthTaskTest {
40 private static final Logger LOGGER = LoggerFactory
41 .getLogger(WatchServiceHealthTaskTest.class);
43 private Consul consul;
45 @SuppressWarnings({ "unchecked", "rawtypes" })
49 List<ServiceHealth> list = new ArrayList<ServiceHealth>();
51 Service service = ImmutableService.builder().id("").port(0).address("")
52 .service("huangleibo").addTags("").createIndex(1).modifyIndex(1).build();
53 ServiceHealth serviceHealth = ImmutableServiceHealth.builder()
55 .node(ImmutableNode.builder().node("").address("").build())
57 list.add(serviceHealth);
60 boolean knownLeader = true;
61 BigInteger index = BigInteger.valueOf(1);
62 final ConsulResponse<List<ServiceHealth>> response = new ConsulResponse<List<ServiceHealth>>(
63 list, lastContact, knownLeader, index);
66 Http http = PowerMockito.mock(Http.class);
69 .doAnswer(new Answer() {
71 public Object answer(InvocationOnMock invocation)
73 Object[] args = invocation.getArguments();
74 ((ConsulResponseCallback) args[2]).onComplete(response);
79 .asyncGet(Mockito.anyString(),
80 Mockito.any(TypeReference.class),
81 Mockito.any(ConsulResponseCallback.class));
84 PowerMockito.spy(Http.class);
85 PowerMockito.when(Http.getInstance()).thenReturn(http);
90 public void testgetServiceName() {
91 consul = Consul.newClient();
93 WatchServiceHealthTask task0 = new WatchServiceHealthTask(
94 consul.healthClient(), "huangleibo_task0", true,
95 CatalogOptions.BLANK, 10, QueryOptions.BLANK);
97 LOGGER.info("service name:" + task0.getServiceName());
99 WatchServiceHealthTask task1 = new WatchServiceHealthTask(
100 consul.healthClient(), "huangleibo_task1", true, 10);
102 LOGGER.debug("service name:" + task1.getServiceName());
104 WatchServiceHealthTask task2 = new WatchServiceHealthTask(
105 consul.healthClient(), "huangleibo_task2", 10);
107 LOGGER.debug("service name:" + task2.getServiceName());
111 public class StopHandler implements WatchTask.Handler<List<ServiceHealth>>
114 private WatchServiceHealthTask task;
116 StopHandler(WatchServiceHealthTask task)
122 public void handle(ConsulResponse<List<ServiceHealth>> object) {
123 // TODO Auto-generated method stub
124 List<ServiceHealth> list = (List<ServiceHealth>)object.getResponse();
125 LOGGER.debug("handler:"+list.get(0).getService().getService());
130 @SuppressWarnings({ "unchecked", "rawtypes" })
132 public void teststartWatch() {
133 Consul consul = Consul.newClient();
134 String serviceName = "huangleibo";
136 WatchServiceHealthTask task0 = new WatchServiceHealthTask(
137 consul.healthClient(), serviceName, true, CatalogOptions.BLANK,
138 10, QueryOptions.BLANK);
140 task0.addFilter(new Filter() {
143 public boolean filter(ConsulResponse object) {
144 // TODO Auto-generated method stub
145 List<ServiceHealth> list = (List<ServiceHealth>)object.getResponse();
146 LOGGER.debug("filter:"+list.get(0).getService().getService());
152 task0.addHandler(new StopHandler(task0));