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.expose;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mockito;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.onap.msb.apiroute.wrapper.consulextend.Consul;
27 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
28 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchServiceHealthTask;
29 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask;
30 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask.Filter;
31 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
32 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
33 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
34 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
35 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PowerMockIgnore;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.fasterxml.jackson.core.type.TypeReference;
44 import com.orbitz.consul.model.ConsulResponse;
45 import com.orbitz.consul.model.health.ImmutableNode;
46 import com.orbitz.consul.option.CatalogOptions;
47 import com.orbitz.consul.option.QueryOptions;
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({Http.class})
51 @PowerMockIgnore({"javax.net.ssl.*"})
52 public class WatchServiceHealthTaskTest {
53 private static final Logger LOGGER = LoggerFactory.getLogger(WatchServiceHealthTaskTest.class);
55 private Consul consul;
57 @SuppressWarnings({"unchecked", "rawtypes"})
61 List<ServiceHealth> list = new ArrayList<ServiceHealth>();
63 Service service = ImmutableService.builder().id("").port(0).address("").service("huangleibo").addTags("")
64 .createIndex(1).modifyIndex(1).build();
65 ServiceHealth serviceHealth = ImmutableServiceHealth.builder().service(service)
66 .node(ImmutableNode.builder().node("").address("").build()).build();
67 list.add(serviceHealth);
70 boolean knownLeader = true;
71 BigInteger index = BigInteger.valueOf(1);
72 final ConsulResponse<List<ServiceHealth>> response =
73 new ConsulResponse<List<ServiceHealth>>(list, lastContact, knownLeader, index);
76 Http http = PowerMockito.mock(Http.class);
78 PowerMockito.doAnswer(new Answer() {
80 public Object answer(InvocationOnMock invocation) throws Throwable {
81 Object[] args = invocation.getArguments();
82 ((ConsulResponseCallback) args[2]).onComplete(response);
85 }).when(http).asyncGet(Mockito.anyString(), Mockito.any(TypeReference.class),
86 Mockito.any(ConsulResponseCallback.class));
89 PowerMockito.spy(Http.class);
90 PowerMockito.when(Http.getInstance()).thenReturn(http);
95 public void testgetServiceName() {
96 consul = Consul.newClient();
98 WatchServiceHealthTask task0 = new WatchServiceHealthTask(consul.healthClient(), "huangleibo_task0", true,
99 CatalogOptions.BLANK, 10, QueryOptions.BLANK);
101 LOGGER.info("service name:" + task0.getServiceName());
103 WatchServiceHealthTask task1 = new WatchServiceHealthTask(consul.healthClient(), "huangleibo_task1", true, 10);
105 LOGGER.debug("service name:" + task1.getServiceName());
107 WatchServiceHealthTask task2 = new WatchServiceHealthTask(consul.healthClient(), "huangleibo_task2", 10);
109 LOGGER.debug("service name:" + task2.getServiceName());
113 public class StopHandler implements WatchTask.Handler<List<ServiceHealth>> {
115 private WatchServiceHealthTask task;
117 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(consul.healthClient(), serviceName, true,
137 CatalogOptions.BLANK, 10, QueryOptions.BLANK);
139 task0.addFilter(new Filter() {
142 public boolean filter(ConsulResponse object) {
143 // TODO Auto-generated method stub
144 List<ServiceHealth> list = (List<ServiceHealth>) object.getResponse();
145 LOGGER.debug("filter:" + list.get(0).getService().getService());
151 task0.addHandler(new StopHandler(task0));