548639ea466372793faf69bbba43057bca28efc1
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / consulextend / expose / WatchCatalogServicesTaskTest.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.consulextend.expose;
15
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
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;
39
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;
44
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({Http.class})
47 @PowerMockIgnore({"javax.net.ssl.*"})
48 public class WatchCatalogServicesTaskTest {
49     private static final Logger LOGGER = LoggerFactory.getLogger(WatchCatalogServicesTaskTest.class);
50
51     private Consul consul;
52
53     @SuppressWarnings({"unchecked", "rawtypes"})
54     @Before
55     public void init() {
56
57         Map<String, List<String>> catalogservices = new HashMap<String, List<String>>();
58         String servicename = "huangleibo";
59         List<String> tags = new ArrayList<String>();
60         tags.add("1111");
61         tags.add("2222");
62         catalogservices.put(servicename, tags);
63
64         long lastContact = 1;
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);
69
70         //
71         Http http = PowerMockito.mock(Http.class);
72
73         PowerMockito.doAnswer(new Answer() {
74             @Override
75             public Object answer(InvocationOnMock invocation) throws Throwable {
76                 Object[] args = invocation.getArguments();
77                 ((ConsulResponseCallback) args[2]).onComplete(response);
78                 return null;
79             }
80         }).when(http).asyncGet(Mockito.anyString(), Mockito.any(TypeReference.class),
81                         Mockito.any(ConsulResponseCallback.class));
82
83         //
84         PowerMockito.spy(Http.class);
85         PowerMockito.when(Http.getInstance()).thenReturn(http);
86
87         consul = Consul.newClient();
88
89         WatchCatalogServicesTask task0 = new WatchCatalogServicesTask(consul.catalogClient(), CatalogOptions.BLANK,
90                         QueryOptions.BLANK, 10);
91
92         WatchCatalogServicesTask task1 = new WatchCatalogServicesTask(consul.catalogClient(), 10);
93
94         WatchCatalogServicesTask task2 = new WatchCatalogServicesTask(consul.catalogClient());
95
96     }
97
98     public class StopHandler implements WatchTask.Handler<HttpEntity> {
99
100         private WatchCatalogServicesTask task;
101
102         StopHandler(WatchCatalogServicesTask task) {
103             this.task = task;
104         }
105
106         @Override
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");
111             task.stopWatch();
112         }
113     }
114
115     @SuppressWarnings({"unchecked", "rawtypes"})
116     @Test
117     public void teststartWatch() {
118         WatchCatalogServicesTask task0 = new WatchCatalogServicesTask(consul.catalogClient(), CatalogOptions.BLANK,
119                         QueryOptions.BLANK, 10);
120
121         task0.addFilter(new Filter() {
122             @Override
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");
127                 return true;
128             }
129
130         });
131
132         task0.addHandler(new StopHandler(task0));
133
134         task0.startWatch();
135     }
136
137 }