aboutsummaryrefslogtreecommitdiffstats
path: root/apiroute/apiroute-service/src/test/java/org/onap/msb/apiroute/wrapper/consulextend/expose/WatchServiceHealthTaskTest.java
blob: ae8fa3b51edfd75138c5b43f2e7a9ac0f0605552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*******************************************************************************
 * Copyright 2016-2017 ZTE, Inc. and others.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.onap.msb.apiroute.wrapper.consulextend.expose;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.onap.msb.apiroute.wrapper.consulextend.Consul;
import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchServiceHealthTask;
import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask;
import org.onap.msb.apiroute.wrapper.consulextend.expose.WatchTask.Filter;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.orbitz.consul.model.ConsulResponse;
import com.orbitz.consul.model.health.ImmutableNode;
import com.orbitz.consul.option.CatalogOptions;
import com.orbitz.consul.option.QueryOptions;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Http.class })
@PowerMockIgnore({ "javax.net.ssl.*" })
public class WatchServiceHealthTaskTest {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(WatchServiceHealthTaskTest.class);

	private Consul consul;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Before
	public void init() {
		
		List<ServiceHealth> list = new ArrayList<ServiceHealth>();
		
		Service service = ImmutableService.builder().id("").port(0).address("")
				.service("huangleibo").addTags("").createIndex(1).modifyIndex(1).build();
		ServiceHealth serviceHealth = ImmutableServiceHealth.builder()
				.service(service)
				.node(ImmutableNode.builder().node("").address("").build())
				.build();
		list.add(serviceHealth);
				
		long lastContact = 1;
		boolean knownLeader = true;
		BigInteger index = BigInteger.valueOf(1);
		final ConsulResponse<List<ServiceHealth>> response = new ConsulResponse<List<ServiceHealth>>(
				list, lastContact, knownLeader, index);

		//
		Http http = PowerMockito.mock(Http.class);
		
		PowerMockito
				.doAnswer(new Answer() {
					@Override
					public Object answer(InvocationOnMock invocation)
							throws Throwable {
						Object[] args = invocation.getArguments();
						((ConsulResponseCallback) args[2]).onComplete(response);						
						return null;
					}
				})
				.when(http)
				.asyncGet(Mockito.anyString(),
						Mockito.any(TypeReference.class),
						Mockito.any(ConsulResponseCallback.class));
		
		//
		PowerMockito.spy(Http.class);
		PowerMockito.when(Http.getInstance()).thenReturn(http);
		
	}

	@Test
	public void testgetServiceName() {
		consul = Consul.newClient();

		WatchServiceHealthTask task0 = new WatchServiceHealthTask(
				consul.healthClient(), "huangleibo_task0", true,
				CatalogOptions.BLANK, 10, QueryOptions.BLANK);

		LOGGER.info("service name:" + task0.getServiceName());

		WatchServiceHealthTask task1 = new WatchServiceHealthTask(
				consul.healthClient(), "huangleibo_task1", true, 10);

		LOGGER.debug("service name:" + task1.getServiceName());

		WatchServiceHealthTask task2 = new WatchServiceHealthTask(
				consul.healthClient(), "huangleibo_task2", 10);

		LOGGER.debug("service name:" + task2.getServiceName());

	}
	
	public class StopHandler implements WatchTask.Handler<List<ServiceHealth>>
	{
		
		private WatchServiceHealthTask task;
		
		StopHandler(WatchServiceHealthTask task)
		{
			this.task = task;
		}

		@Override
		public void handle(ConsulResponse<List<ServiceHealth>> object) {
			// TODO Auto-generated method stub
			List<ServiceHealth> list = (List<ServiceHealth>)object.getResponse();
			LOGGER.debug("handler:"+list.get(0).getService().getService());
			task.stopWatch();
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Test
	public void teststartWatch() {
		Consul consul = Consul.newClient();
		String serviceName = "huangleibo";

		WatchServiceHealthTask task0 = new WatchServiceHealthTask(
				consul.healthClient(), serviceName, true, CatalogOptions.BLANK,
				10, QueryOptions.BLANK);
		
		task0.addFilter(new Filter() {

			@Override
			public boolean filter(ConsulResponse object) {
				// TODO Auto-generated method stub
				List<ServiceHealth> list = (List<ServiceHealth>)object.getResponse();
				LOGGER.debug("filter:"+list.get(0).getService().getService());
				return true;
			}
			
		});
		
		task0.addHandler(new StopHandler(task0));
		
		task0.startWatch();
	}
}