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.HashMap;
19 import java.util.List;
22 import org.apache.http.HttpEntity;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mockito;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29 import org.onap.msb.apiroute.wrapper.consulextend.Consul;
30 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
31 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask.Filter;
32 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PowerMockIgnore;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.modules.junit4.PowerMockRunner;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.fasterxml.jackson.core.type.TypeReference;
41 import com.orbitz.consul.model.ConsulResponse;
42 import com.orbitz.consul.option.CatalogOptions;
43 import com.orbitz.consul.option.QueryOptions;
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({Http.class})
47 @PowerMockIgnore({"javax.net.ssl.*", "jdk.internal.reflect.*"})
48 public class WatchCatalogServicesTaskTest {
49 private static final Logger LOGGER = LoggerFactory.getLogger(WatchCatalogServicesTaskTest.class);
51 private Consul consul;
53 @SuppressWarnings({"unchecked", "rawtypes"})
57 Map<String, List<String>> catalogservices = new HashMap<String, List<String>>();
58 String servicename = "huangleibo";
59 List<String> tags = new ArrayList<String>();
62 catalogservices.put(servicename, tags);
65 boolean knownLeader = true;
66 BigInteger index = BigInteger.valueOf(1);
67 final ConsulResponse<Map<String, List<String>>> response =
68 new ConsulResponse<Map<String, List<String>>>(catalogservices, lastContact, knownLeader, index);
71 Http http = PowerMockito.mock(Http.class);
73 PowerMockito.doAnswer(new Answer() {
75 public Object answer(InvocationOnMock invocation) throws Throwable {
76 Object[] args = invocation.getArguments();
77 ((ConsulResponseCallback) args[2]).onComplete(response);
80 }).when(http).asyncGet(Mockito.anyString(), Mockito.any(TypeReference.class),
81 Mockito.any(ConsulResponseCallback.class));
84 PowerMockito.spy(Http.class);
85 PowerMockito.when(Http.getInstance()).thenReturn(http);
87 consul = Consul.newClient();
89 WatchCatalogServicesTask task0 = new WatchCatalogServicesTask(consul.catalogClient(), CatalogOptions.BLANK,
90 QueryOptions.BLANK, 10);
92 WatchCatalogServicesTask task1 = new WatchCatalogServicesTask(consul.catalogClient(), 10);
94 WatchCatalogServicesTask task2 = new WatchCatalogServicesTask(consul.catalogClient());
98 public class StopHandler implements WatchTask.Handler<HttpEntity> {
100 private WatchCatalogServicesTask task;
102 StopHandler(WatchCatalogServicesTask task) {
107 public void handle(ConsulResponse<HttpEntity> object) {
108 // TODO Auto-generated method stub
109 // Map<String, List<String>> map = (Map<String, List<String>>)object.getResponse();
110 LOGGER.debug("handler is here");
115 @SuppressWarnings({"unchecked", "rawtypes"})
117 public void teststartWatch() {
118 WatchCatalogServicesTask task0 = new WatchCatalogServicesTask(consul.catalogClient(), CatalogOptions.BLANK,
119 QueryOptions.BLANK, 10);
121 task0.addFilter(new Filter() {
123 public boolean filter(ConsulResponse object) {
124 // TODO Auto-generated method stub
125 // Map<String, List<String>> map = (Map<String, List<String>>)object.getResponse();
126 LOGGER.debug("filter is here");
132 task0.addHandler(new StopHandler(task0));