1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.queue;
16 import java.io.InputStream;
17 import java.lang.reflect.InvocationHandler;
18 import java.lang.reflect.Method;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
24 import org.apache.http.HttpEntity;
25 import org.apache.http.entity.BasicHttpEntity;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.msb.apiroute.SyncDataManager;
32 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
33 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
34 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
35 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
36 import org.onap.msb.apiroute.wrapper.consulextend.util.HttpTest;
37 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
38 import org.onap.msb.apiroute.wrapper.queue.QueueManager;
39 import org.onap.msb.apiroute.wrapper.queue.ServiceConsumer;
40 import org.onap.msb.apiroute.wrapper.queue.ServiceData;
41 import org.onap.msb.apiroute.wrapper.queue.ServiceListConsumer;
42 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
43 import org.onap.msb.apiroute.wrapper.util.RouteUtil;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PowerMockIgnore;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
49 import redis.clients.jedis.JedisPool;
50 import redis.clients.jedis.JedisPoolConfig;
52 import com.fiftyonred.mock_jedis.MockJedisPool;
53 import com.orbitz.consul.model.health.ImmutableNode;
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({JedisUtil.class, RouteUtil.class, RedisAccessWrapper.class})
57 @PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*"})
58 public class QueueManagerTest {
59 private static QueueManager queueManager;
63 public static void setUpBeforeClass() throws Exception {
64 queueManager = QueueManager.getInstance();
65 putInServiceListQueue();
66 putInServiceQueue4Update();
71 public void setUpBeforeTest() throws Exception {
72 final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
73 PowerMockito.mockStatic(JedisUtil.class);
74 JedisUtil jedisUtil = PowerMockito.mock(JedisUtil.class);
75 PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
77 PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
79 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
80 return mockJedisPool.getResource().keys((String) args[0]);
87 public void test_ServiceConsumer() {
89 // start ServiceListConsumer
90 // new Thread(new ServiceListConsumer(this),"ServiceListConsumerThread").start();
92 // start Service Consumer
93 int serviceQueneNum = RouteUtil.SERVICE_DATA_QUEUE_NUM;
94 for (int i = 0; i < serviceQueneNum; i++) {
95 new Thread(new ServiceConsumer(i), "ServiceConsumerThread" + i).start();
101 public void test_ServiceListConsumer() {
103 // start ServiceListConsumer
104 new Thread(new ServiceListConsumer(), "ServiceListConsumerThread").start();
107 } catch (InterruptedException e) {
108 // TODO Auto-generated catch block
111 putInServiceQueue4Delete();
116 private static void putInServiceListQueue() {
117 ServiceData<HttpEntity> data = new ServiceData<HttpEntity>();
118 data.setDataType(ServiceData.DataType.service_list);
120 BasicHttpEntity entity = new BasicHttpEntity();
121 InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
122 entity.setContent(content);
123 data.setData(entity);
126 queueManager.putIn(data);
127 } catch (Exception e) {
128 Assert.assertTrue(e instanceof InterruptedException);
132 private static void putInServiceQueue4Update() {
133 ServiceData<List<ServiceHealth>> data = new ServiceData<List<ServiceHealth>>();
134 data.setDataType(ServiceData.DataType.service);
135 data.setOperate(ServiceData.Operate.delete);
137 List<String> tagList = new ArrayList<String>();
138 tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
139 tagList.add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
140 tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
142 Service service = ImmutableService.builder().id("id").port(8686).address("10.74.165.246").service("msbtest")
143 .addAllTags(tagList).createIndex(0).modifyIndex(0).build();
144 ServiceHealth serviceHealth = ImmutableServiceHealth.builder().service(service)
145 .node(ImmutableNode.builder().node("server").address("192.168.1.98").build()).build();
146 List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
147 serviceHealthList.add(serviceHealth);
149 data.setData(serviceHealthList);
152 queueManager.putIn(data);
153 } catch (Exception e) {
154 Assert.assertTrue(e instanceof InterruptedException);
158 private static void putInServiceQueue4Delete() {
159 ServiceData<List<ServiceHealth>> data = new ServiceData<List<ServiceHealth>>();
160 data.setDataType(ServiceData.DataType.service);
161 data.setOperate(ServiceData.Operate.update);
163 List<String> tagList = new ArrayList<String>();
164 tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
165 tagList.add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
166 tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
168 Service service = ImmutableService.builder().id("id").port(8686).address("10.74.165.246").service("msbtest")
169 .addAllTags(tagList).createIndex(0).modifyIndex(0).build();
170 ServiceHealth serviceHealth = ImmutableServiceHealth.builder().service(service)
171 .node(ImmutableNode.builder().node("server").address("192.168.1.98").build()).build();
172 List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
173 serviceHealthList.add(serviceHealth);
175 data.setData(serviceHealthList);
178 queueManager.putIn(data);
179 } catch (Exception e) {
180 Assert.assertTrue(e instanceof InterruptedException);