Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / consulextend / expose / WatchServiceHealthTaskTest.java
1 package org.onap.msb.apiroute.wrapper.consulextend.expose;
2
3 import java.math.BigInteger;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.junit.Before;
8 import org.junit.Test;
9 import org.junit.runner.RunWith;
10 import org.mockito.Mockito;
11 import org.mockito.invocation.InvocationOnMock;
12 import org.mockito.stubbing.Answer;
13 import org.onap.msb.apiroute.wrapper.consulextend.Consul;
14 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
15 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchServiceHealthTask;
16 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask;
17 import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask.Filter;
18 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
19 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
20 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
21 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
22 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
23 import org.powermock.api.mockito.PowerMockito;
24 import org.powermock.core.classloader.annotations.PowerMockIgnore;
25 import org.powermock.core.classloader.annotations.PrepareForTest;
26 import org.powermock.modules.junit4.PowerMockRunner;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.fasterxml.jackson.core.type.TypeReference;
31 import com.orbitz.consul.model.ConsulResponse;
32 import com.orbitz.consul.model.health.ImmutableNode;
33 import com.orbitz.consul.option.CatalogOptions;
34 import com.orbitz.consul.option.QueryOptions;
35
36 @RunWith(PowerMockRunner.class)
37 @PrepareForTest({ Http.class })
38 @PowerMockIgnore({ "javax.net.ssl.*" })
39 public class WatchServiceHealthTaskTest {
40         private static final Logger LOGGER = LoggerFactory
41                         .getLogger(WatchServiceHealthTaskTest.class);
42
43         private Consul consul;
44
45         @SuppressWarnings({ "unchecked", "rawtypes" })
46         @Before
47         public void init() {
48                 
49                 List<ServiceHealth> list = new ArrayList<ServiceHealth>();
50                 
51                 Service service = ImmutableService.builder().id("").port(0).address("")
52                                 .service("huangleibo").addTags("").createIndex(1).modifyIndex(1).build();
53                 ServiceHealth serviceHealth = ImmutableServiceHealth.builder()
54                                 .service(service)
55                                 .node(ImmutableNode.builder().node("").address("").build())
56                                 .build();
57                 list.add(serviceHealth);
58                                 
59                 long lastContact = 1;
60                 boolean knownLeader = true;
61                 BigInteger index = BigInteger.valueOf(1);
62                 final ConsulResponse<List<ServiceHealth>> response = new ConsulResponse<List<ServiceHealth>>(
63                                 list, lastContact, knownLeader, index);
64
65                 //
66                 Http http = PowerMockito.mock(Http.class);
67                 
68                 PowerMockito
69                                 .doAnswer(new Answer() {
70                                         @Override
71                                         public Object answer(InvocationOnMock invocation)
72                                                         throws Throwable {
73                                                 Object[] args = invocation.getArguments();
74                                                 ((ConsulResponseCallback) args[2]).onComplete(response);                                                
75                                                 return null;
76                                         }
77                                 })
78                                 .when(http)
79                                 .asyncGet(Mockito.anyString(),
80                                                 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         }
88
89         @Test
90         public void testgetServiceName() {
91                 consul = Consul.newClient();
92
93                 WatchServiceHealthTask task0 = new WatchServiceHealthTask(
94                                 consul.healthClient(), "huangleibo_task0", true,
95                                 CatalogOptions.BLANK, 10, QueryOptions.BLANK);
96
97                 LOGGER.info("service name:" + task0.getServiceName());
98
99                 WatchServiceHealthTask task1 = new WatchServiceHealthTask(
100                                 consul.healthClient(), "huangleibo_task1", true, 10);
101
102                 LOGGER.debug("service name:" + task1.getServiceName());
103
104                 WatchServiceHealthTask task2 = new WatchServiceHealthTask(
105                                 consul.healthClient(), "huangleibo_task2", 10);
106
107                 LOGGER.debug("service name:" + task2.getServiceName());
108
109         }
110         
111         public class StopHandler implements WatchTask.Handler<List<ServiceHealth>>
112         {
113                 
114                 private WatchServiceHealthTask task;
115                 
116                 StopHandler(WatchServiceHealthTask task)
117                 {
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(
137                                 consul.healthClient(), serviceName, true, CatalogOptions.BLANK,
138                                 10, QueryOptions.BLANK);
139                 
140                 task0.addFilter(new Filter() {
141
142                         @Override
143                         public boolean filter(ConsulResponse object) {
144                                 // TODO Auto-generated method stub
145                                 List<ServiceHealth> list = (List<ServiceHealth>)object.getResponse();
146                                 LOGGER.debug("filter:"+list.get(0).getService().getService());
147                                 return true;
148                         }
149                         
150                 });
151                 
152                 task0.addHandler(new StopHandler(task0));
153                 
154                 task0.startWatch();
155         }
156 }