fix powermock issue
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / consulextend / expose / WatchServiceHealthTaskTest.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.List;
19
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;
42
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;
48
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({Http.class})
51 @PowerMockIgnore({"javax.net.ssl.*", "jdk.internal.reflect.*"})
52 public class WatchServiceHealthTaskTest {
53     private static final Logger LOGGER = LoggerFactory.getLogger(WatchServiceHealthTaskTest.class);
54
55     private Consul consul;
56
57     @SuppressWarnings({"unchecked", "rawtypes"})
58     @Before
59     public void init() {
60
61         List<ServiceHealth> list = new ArrayList<ServiceHealth>();
62
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);
68
69         long lastContact = 1;
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);
74
75         //
76         Http http = PowerMockito.mock(Http.class);
77
78         PowerMockito.doAnswer(new Answer() {
79             @Override
80             public Object answer(InvocationOnMock invocation) throws Throwable {
81                 Object[] args = invocation.getArguments();
82                 ((ConsulResponseCallback) args[2]).onComplete(response);
83                 return null;
84             }
85         }).when(http).asyncGet(Mockito.anyString(), Mockito.any(TypeReference.class),
86                         Mockito.any(ConsulResponseCallback.class));
87
88         //
89         PowerMockito.spy(Http.class);
90         PowerMockito.when(Http.getInstance()).thenReturn(http);
91
92     }
93
94     @Test
95     public void testgetServiceName() {
96         consul = Consul.newClient();
97
98         WatchServiceHealthTask task0 = new WatchServiceHealthTask(consul.healthClient(), "huangleibo_task0", true,
99                         CatalogOptions.BLANK, 10, QueryOptions.BLANK);
100
101         LOGGER.info("service name:" + task0.getServiceName());
102
103         WatchServiceHealthTask task1 = new WatchServiceHealthTask(consul.healthClient(), "huangleibo_task1", true, 10);
104
105         LOGGER.debug("service name:" + task1.getServiceName());
106
107         WatchServiceHealthTask task2 = new WatchServiceHealthTask(consul.healthClient(), "huangleibo_task2", 10);
108
109         LOGGER.debug("service name:" + task2.getServiceName());
110
111     }
112
113     public class StopHandler implements WatchTask.Handler<List<ServiceHealth>> {
114
115         private WatchServiceHealthTask task;
116
117         StopHandler(WatchServiceHealthTask task) {
118             this.task = task;
119         }
120
121         @Override
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());
126             task.stopWatch();
127         }
128     }
129
130     @SuppressWarnings({"unchecked", "rawtypes"})
131     @Test
132     public void teststartWatch() {
133         Consul consul = Consul.newClient();
134         String serviceName = "huangleibo";
135
136         WatchServiceHealthTask task0 = new WatchServiceHealthTask(consul.healthClient(), serviceName, true,
137                         CatalogOptions.BLANK, 10, QueryOptions.BLANK);
138
139         task0.addFilter(new Filter() {
140
141             @Override
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());
146                 return true;
147             }
148
149         });
150
151         task0.addHandler(new StopHandler(task0));
152
153         task0.startWatch();
154     }
155 }