Add license header for java files
[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");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at 
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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;
17
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mockito;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
28 import org.onap.msb.apiroute.wrapper.consulextend.Consul;
29 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
30 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchServiceHealthTask;
31 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask;
32 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask.Filter;
33 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
34 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
35 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
36 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
37 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PowerMockIgnore;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import com.fasterxml.jackson.core.type.TypeReference;
46 import com.orbitz.consul.model.ConsulResponse;
47 import com.orbitz.consul.model.health.ImmutableNode;
48 import com.orbitz.consul.option.CatalogOptions;
49 import com.orbitz.consul.option.QueryOptions;
50
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest({ Http.class })
53 @PowerMockIgnore({ "javax.net.ssl.*" })
54 public class WatchServiceHealthTaskTest {
55         private static final Logger LOGGER = LoggerFactory
56                         .getLogger(WatchServiceHealthTaskTest.class);
57
58         private Consul consul;
59
60         @SuppressWarnings({ "unchecked", "rawtypes" })
61         @Before
62         public void init() {
63                 
64                 List<ServiceHealth> list = new ArrayList<ServiceHealth>();
65                 
66                 Service service = ImmutableService.builder().id("").port(0).address("")
67                                 .service("huangleibo").addTags("").createIndex(1).modifyIndex(1).build();
68                 ServiceHealth serviceHealth = ImmutableServiceHealth.builder()
69                                 .service(service)
70                                 .node(ImmutableNode.builder().node("").address("").build())
71                                 .build();
72                 list.add(serviceHealth);
73                                 
74                 long lastContact = 1;
75                 boolean knownLeader = true;
76                 BigInteger index = BigInteger.valueOf(1);
77                 final ConsulResponse<List<ServiceHealth>> response = new ConsulResponse<List<ServiceHealth>>(
78                                 list, lastContact, knownLeader, index);
79
80                 //
81                 Http http = PowerMockito.mock(Http.class);
82                 
83                 PowerMockito
84                                 .doAnswer(new Answer() {
85                                         @Override
86                                         public Object answer(InvocationOnMock invocation)
87                                                         throws Throwable {
88                                                 Object[] args = invocation.getArguments();
89                                                 ((ConsulResponseCallback) args[2]).onComplete(response);                                                
90                                                 return null;
91                                         }
92                                 })
93                                 .when(http)
94                                 .asyncGet(Mockito.anyString(),
95                                                 Mockito.any(TypeReference.class),
96                                                 Mockito.any(ConsulResponseCallback.class));
97                 
98                 //
99                 PowerMockito.spy(Http.class);
100                 PowerMockito.when(Http.getInstance()).thenReturn(http);
101                 
102         }
103
104         @Test
105         public void testgetServiceName() {
106                 consul = Consul.newClient();
107
108                 WatchServiceHealthTask task0 = new WatchServiceHealthTask(
109                                 consul.healthClient(), "huangleibo_task0", true,
110                                 CatalogOptions.BLANK, 10, QueryOptions.BLANK);
111
112                 LOGGER.info("service name:" + task0.getServiceName());
113
114                 WatchServiceHealthTask task1 = new WatchServiceHealthTask(
115                                 consul.healthClient(), "huangleibo_task1", true, 10);
116
117                 LOGGER.debug("service name:" + task1.getServiceName());
118
119                 WatchServiceHealthTask task2 = new WatchServiceHealthTask(
120                                 consul.healthClient(), "huangleibo_task2", 10);
121
122                 LOGGER.debug("service name:" + task2.getServiceName());
123
124         }
125         
126         public class StopHandler implements WatchTask.Handler<List<ServiceHealth>>
127         {
128                 
129                 private WatchServiceHealthTask task;
130                 
131                 StopHandler(WatchServiceHealthTask task)
132                 {
133                         this.task = task;
134                 }
135
136                 @Override
137                 public void handle(ConsulResponse<List<ServiceHealth>> object) {
138                         // TODO Auto-generated method stub
139                         List<ServiceHealth> list = (List<ServiceHealth>)object.getResponse();
140                         LOGGER.debug("handler:"+list.get(0).getService().getService());
141                         task.stopWatch();
142                 }
143         }
144
145         @SuppressWarnings({ "unchecked", "rawtypes" })
146         @Test
147         public void teststartWatch() {
148                 Consul consul = Consul.newClient();
149                 String serviceName = "huangleibo";
150
151                 WatchServiceHealthTask task0 = new WatchServiceHealthTask(
152                                 consul.healthClient(), serviceName, true, CatalogOptions.BLANK,
153                                 10, QueryOptions.BLANK);
154                 
155                 task0.addFilter(new Filter() {
156
157                         @Override
158                         public boolean filter(ConsulResponse object) {
159                                 // TODO Auto-generated method stub
160                                 List<ServiceHealth> list = (List<ServiceHealth>)object.getResponse();
161                                 LOGGER.debug("filter:"+list.get(0).getService().getService());
162                                 return true;
163                         }
164                         
165                 });
166                 
167                 task0.addHandler(new StopHandler(task0));
168                 
169                 task0.startWatch();
170         }
171 }