Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / queue / QueueManagerTest.java
1 package org.onap.msb.apiroute.wrapper.queue;
2
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;
8 import java.util.List;
9 import java.util.Map;
10
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;
35
36 import redis.clients.jedis.JedisPool;
37 import redis.clients.jedis.JedisPoolConfig;
38
39 import com.fiftyonred.mock_jedis.MockJedisPool;
40 import com.orbitz.consul.model.health.ImmutableNode;
41
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;
47   
48   
49   @BeforeClass
50   public static void setUpBeforeClass() throws Exception {
51     queueManager=QueueManager.getInstance();
52     putInServiceListQueue();
53     putInServiceQueue4Update();
54     
55   }
56   
57   @Before
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());
63
64       PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
65           @Override
66           public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
67               return mockJedisPool.getResource().keys((String) args[0]);
68           }
69       });
70   }
71   
72   
73
74   public void test_ServiceConsumer(){
75     
76     //start ServiceListConsumer
77 //    new Thread(new ServiceListConsumer(this),"ServiceListConsumerThread").start();
78     
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();
83     }
84     
85   }
86   
87
88   public void test_ServiceListConsumer(){
89     
90     //start ServiceListConsumer
91     new Thread(new ServiceListConsumer(),"ServiceListConsumerThread").start();
92     try {
93       Thread.sleep(2000);
94     } catch (InterruptedException e) {
95       // TODO Auto-generated catch block
96       e.printStackTrace();
97     }
98     putInServiceQueue4Delete();
99   }
100   
101   
102
103   private static  void putInServiceListQueue(){
104     ServiceData<HttpEntity> data=new ServiceData<HttpEntity>();
105     data.setDataType(ServiceData.DataType.service_list);
106         
107     BasicHttpEntity entity = new BasicHttpEntity();
108     InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
109     entity.setContent(content);
110     data.setData(entity);
111     
112     try {
113       queueManager.putIn(data);
114     } catch (Exception e) {      
115       Assert.assertTrue(e instanceof InterruptedException);
116     }
117   }
118   
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);
123     
124     List<String> tagList = new ArrayList<String>();
125     tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
126     tagList
127         .add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
128     tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
129
130     Service service =
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);
138     
139     data.setData(serviceHealthList);
140     
141     try {
142       queueManager.putIn(data);
143     } catch (Exception e) {      
144       Assert.assertTrue(e instanceof InterruptedException);
145     }
146   }
147   
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);
152     
153     List<String> tagList = new ArrayList<String>();
154     tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
155     tagList
156         .add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
157     tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
158
159     Service service =
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);
167     
168     data.setData(serviceHealthList);
169     
170     try {
171       queueManager.putIn(data);
172     } catch (Exception e) {      
173       Assert.assertTrue(e instanceof InterruptedException);
174     }
175   }
176   
177   
178 }