1 package org.onap.msb.apiroute.wrapper.queue;
3 import java.io.InputStream;
4 import java.lang.reflect.InvocationHandler;
5 import java.lang.reflect.Method;
6 import java.util.ArrayList;
7 import java.util.HashMap;
11 import org.apache.http.HttpEntity;
12 import org.apache.http.entity.BasicHttpEntity;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.onap.msb.apiroute.SyncDataManager;
19 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
20 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
21 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
22 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
23 import org.onap.msb.apiroute.wrapper.consulextend.util.HttpTest;
24 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
25 import org.onap.msb.apiroute.wrapper.queue.QueueManager;
26 import org.onap.msb.apiroute.wrapper.queue.ServiceConsumer;
27 import org.onap.msb.apiroute.wrapper.queue.ServiceData;
28 import org.onap.msb.apiroute.wrapper.queue.ServiceListConsumer;
29 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
30 import org.onap.msb.apiroute.wrapper.util.RouteUtil;
31 import org.powermock.api.mockito.PowerMockito;
32 import org.powermock.core.classloader.annotations.PowerMockIgnore;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
36 import redis.clients.jedis.JedisPool;
37 import redis.clients.jedis.JedisPoolConfig;
39 import com.fiftyonred.mock_jedis.MockJedisPool;
40 import com.orbitz.consul.model.health.ImmutableNode;
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest({JedisUtil.class,RouteUtil.class,RedisAccessWrapper.class})
44 @PowerMockIgnore( {"javax.management.*"})
45 public class QueueManagerTest {
46 private static QueueManager queueManager;
50 public static void setUpBeforeClass() throws Exception {
51 queueManager=QueueManager.getInstance();
52 putInServiceListQueue();
53 putInServiceQueue4Update();
58 public void setUpBeforeTest() throws Exception {
59 final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
60 PowerMockito.mockStatic(JedisUtil.class);
61 JedisUtil jedisUtil=PowerMockito.mock(JedisUtil.class);
62 PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
64 PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
66 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
67 return mockJedisPool.getResource().keys((String) args[0]);
74 public void test_ServiceConsumer(){
76 //start ServiceListConsumer
77 // new Thread(new ServiceListConsumer(this),"ServiceListConsumerThread").start();
79 //start Service Consumer
80 int serviceQueneNum=RouteUtil.SERVICE_DATA_QUEUE_NUM;
81 for(int i=0;i<serviceQueneNum;i++){
82 new Thread(new ServiceConsumer(i),"ServiceConsumerThread"+i).start();
88 public void test_ServiceListConsumer(){
90 //start ServiceListConsumer
91 new Thread(new ServiceListConsumer(),"ServiceListConsumerThread").start();
94 } catch (InterruptedException e) {
95 // TODO Auto-generated catch block
98 putInServiceQueue4Delete();
103 private static void putInServiceListQueue(){
104 ServiceData<HttpEntity> data=new ServiceData<HttpEntity>();
105 data.setDataType(ServiceData.DataType.service_list);
107 BasicHttpEntity entity = new BasicHttpEntity();
108 InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
109 entity.setContent(content);
110 data.setData(entity);
113 queueManager.putIn(data);
114 } catch (Exception e) {
115 Assert.assertTrue(e instanceof InterruptedException);
119 private static void putInServiceQueue4Update(){
120 ServiceData<List<ServiceHealth>> data=new ServiceData<List<ServiceHealth>>();
121 data.setDataType(ServiceData.DataType.service);
122 data.setOperate(ServiceData.Operate.delete);
124 List<String> tagList = new ArrayList<String>();
125 tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
127 .add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
128 tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
131 ImmutableService.builder().id("id").port(8686).address("10.74.165.246").service("msbtest")
132 .addAllTags(tagList).createIndex(0).modifyIndex(0).build();
133 ServiceHealth serviceHealth =
134 ImmutableServiceHealth.builder().service(service)
135 .node(ImmutableNode.builder().node("server").address("192.168.1.98").build()).build();
136 List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
137 serviceHealthList.add(serviceHealth);
139 data.setData(serviceHealthList);
142 queueManager.putIn(data);
143 } catch (Exception e) {
144 Assert.assertTrue(e instanceof InterruptedException);
148 private static void putInServiceQueue4Delete(){
149 ServiceData<List<ServiceHealth>> data=new ServiceData<List<ServiceHealth>>();
150 data.setDataType(ServiceData.DataType.service);
151 data.setOperate(ServiceData.Operate.update);
153 List<String> tagList = new ArrayList<String>();
154 tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
156 .add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
157 tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
160 ImmutableService.builder().id("id").port(8686).address("10.74.165.246").service("msbtest")
161 .addAllTags(tagList).createIndex(0).modifyIndex(0).build();
162 ServiceHealth serviceHealth =
163 ImmutableServiceHealth.builder().service(service)
164 .node(ImmutableNode.builder().node("server").address("192.168.1.98").build()).build();
165 List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
166 serviceHealthList.add(serviceHealth);
168 data.setData(serviceHealthList);
171 queueManager.putIn(data);
172 } catch (Exception e) {
173 Assert.assertTrue(e instanceof InterruptedException);