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.HashMap;
21 import java.util.List;
24 import org.apache.http.HttpEntity;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mockito;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31 import org.onap.msb.apiroute.wrapper.consulextend.Consul;
32 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
33 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchCatalogServicesTask;
34 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask;
35 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask.Filter;
36 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PowerMockIgnore;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import com.fasterxml.jackson.core.type.TypeReference;
45 import com.orbitz.consul.model.ConsulResponse;
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 WatchCatalogServicesTaskTest {
53 private static final Logger LOGGER = LoggerFactory
54 .getLogger(WatchCatalogServicesTaskTest.class);
56 private Consul consul;
58 @SuppressWarnings({ "unchecked", "rawtypes" })
62 Map<String, List<String>> catalogservices = new HashMap<String, List<String>>();
63 String servicename = "huangleibo";
64 List<String> tags = new ArrayList<String>();
67 catalogservices.put(servicename, tags);
70 boolean knownLeader = true;
71 BigInteger index = BigInteger.valueOf(1);
72 final ConsulResponse<Map<String, List<String>>> response = new ConsulResponse<Map<String, List<String>>>(
73 catalogservices, lastContact, knownLeader, index);
76 Http http = PowerMockito.mock(Http.class);
79 .doAnswer(new Answer() {
81 public Object answer(InvocationOnMock invocation)
83 Object[] args = invocation.getArguments();
84 ((ConsulResponseCallback) args[2]).onComplete(response);
89 .asyncGet(Mockito.anyString(),
90 Mockito.any(TypeReference.class),
91 Mockito.any(ConsulResponseCallback.class));
94 PowerMockito.spy(Http.class);
95 PowerMockito.when(Http.getInstance()).thenReturn(http);
97 consul = Consul.newClient();
99 WatchCatalogServicesTask task0 = new WatchCatalogServicesTask(
100 consul.catalogClient(), CatalogOptions.BLANK,
101 QueryOptions.BLANK, 10);
103 WatchCatalogServicesTask task1 = new WatchCatalogServicesTask(
104 consul.catalogClient(), 10);
106 WatchCatalogServicesTask task2 = new WatchCatalogServicesTask(
107 consul.catalogClient());
111 public class StopHandler implements WatchTask.Handler<HttpEntity>
114 private WatchCatalogServicesTask task;
116 StopHandler(WatchCatalogServicesTask task)
122 public void handle(ConsulResponse<HttpEntity> object) {
123 // TODO Auto-generated method stub
124 // Map<String, List<String>> map = (Map<String, List<String>>)object.getResponse();
125 LOGGER.debug("handler is here");
130 @SuppressWarnings({ "unchecked", "rawtypes" })
132 public void teststartWatch() {
133 WatchCatalogServicesTask task0 = new WatchCatalogServicesTask(
134 consul.catalogClient(), CatalogOptions.BLANK,
135 QueryOptions.BLANK, 10);
137 task0.addFilter(new Filter() {
139 public boolean filter(ConsulResponse object) {
140 // TODO Auto-generated method stub
141 // Map<String, List<String>> map = (Map<String, List<String>>)object.getResponse();
142 LOGGER.debug("filter is here");
148 task0.addHandler(new StopHandler(task0));