Add license header for java files
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / queue / QueueManagerTest.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at 
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.queue;
17
18 import java.io.InputStream;
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.Method;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.http.HttpEntity;
27 import org.apache.http.entity.BasicHttpEntity;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.onap.msb.apiroute.SyncDataManager;
34 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
35 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
36 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
37 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
38 import org.onap.msb.apiroute.wrapper.consulextend.util.HttpTest;
39 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
40 import org.onap.msb.apiroute.wrapper.queue.QueueManager;
41 import org.onap.msb.apiroute.wrapper.queue.ServiceConsumer;
42 import org.onap.msb.apiroute.wrapper.queue.ServiceData;
43 import org.onap.msb.apiroute.wrapper.queue.ServiceListConsumer;
44 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
45 import org.onap.msb.apiroute.wrapper.util.RouteUtil;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.core.classloader.annotations.PowerMockIgnore;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50
51 import redis.clients.jedis.JedisPool;
52 import redis.clients.jedis.JedisPoolConfig;
53
54 import com.fiftyonred.mock_jedis.MockJedisPool;
55 import com.orbitz.consul.model.health.ImmutableNode;
56
57 @RunWith(PowerMockRunner.class)
58 @PrepareForTest({JedisUtil.class,RouteUtil.class,RedisAccessWrapper.class})
59 @PowerMockIgnore( {"javax.management.*"})
60 public class QueueManagerTest {
61   private static QueueManager queueManager;
62   
63   
64   @BeforeClass
65   public static void setUpBeforeClass() throws Exception {
66     queueManager=QueueManager.getInstance();
67     putInServiceListQueue();
68     putInServiceQueue4Update();
69     
70   }
71   
72   @Before
73   public void setUpBeforeTest() throws Exception {
74       final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
75       PowerMockito.mockStatic(JedisUtil.class);
76       JedisUtil jedisUtil=PowerMockito.mock(JedisUtil.class);
77       PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
78
79       PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
80           @Override
81           public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
82               return mockJedisPool.getResource().keys((String) args[0]);
83           }
84       });
85   }
86   
87   
88
89   public void test_ServiceConsumer(){
90     
91     //start ServiceListConsumer
92 //    new Thread(new ServiceListConsumer(this),"ServiceListConsumerThread").start();
93     
94     //start Service Consumer
95     int serviceQueneNum=RouteUtil.SERVICE_DATA_QUEUE_NUM;
96     for(int i=0;i<serviceQueneNum;i++){
97       new Thread(new ServiceConsumer(i),"ServiceConsumerThread"+i).start();
98     }
99     
100   }
101   
102
103   public void test_ServiceListConsumer(){
104     
105     //start ServiceListConsumer
106     new Thread(new ServiceListConsumer(),"ServiceListConsumerThread").start();
107     try {
108       Thread.sleep(2000);
109     } catch (InterruptedException e) {
110       // TODO Auto-generated catch block
111       e.printStackTrace();
112     }
113     putInServiceQueue4Delete();
114   }
115   
116   
117
118   private static  void putInServiceListQueue(){
119     ServiceData<HttpEntity> data=new ServiceData<HttpEntity>();
120     data.setDataType(ServiceData.DataType.service_list);
121         
122     BasicHttpEntity entity = new BasicHttpEntity();
123     InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
124     entity.setContent(content);
125     data.setData(entity);
126     
127     try {
128       queueManager.putIn(data);
129     } catch (Exception e) {      
130       Assert.assertTrue(e instanceof InterruptedException);
131     }
132   }
133   
134   private static void putInServiceQueue4Update(){
135     ServiceData<List<ServiceHealth>> data=new ServiceData<List<ServiceHealth>>();
136     data.setDataType(ServiceData.DataType.service);
137     data.setOperate(ServiceData.Operate.delete);
138     
139     List<String> tagList = new ArrayList<String>();
140     tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
141     tagList
142         .add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
143     tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
144
145     Service service =
146         ImmutableService.builder().id("id").port(8686).address("10.74.165.246").service("msbtest")
147             .addAllTags(tagList).createIndex(0).modifyIndex(0).build();
148     ServiceHealth serviceHealth =
149         ImmutableServiceHealth.builder().service(service)
150             .node(ImmutableNode.builder().node("server").address("192.168.1.98").build()).build();
151     List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
152     serviceHealthList.add(serviceHealth);
153     
154     data.setData(serviceHealthList);
155     
156     try {
157       queueManager.putIn(data);
158     } catch (Exception e) {      
159       Assert.assertTrue(e instanceof InterruptedException);
160     }
161   }
162   
163   private static void putInServiceQueue4Delete(){
164     ServiceData<List<ServiceHealth>> data=new ServiceData<List<ServiceHealth>>();
165     data.setDataType(ServiceData.DataType.service);
166     data.setOperate(ServiceData.Operate.update);
167     
168     List<String> tagList = new ArrayList<String>();
169     tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
170     tagList
171         .add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
172     tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
173
174     Service service =
175         ImmutableService.builder().id("id").port(8686).address("10.74.165.246").service("msbtest")
176             .addAllTags(tagList).createIndex(0).modifyIndex(0).build();
177     ServiceHealth serviceHealth =
178         ImmutableServiceHealth.builder().service(service)
179             .node(ImmutableNode.builder().node("server").address("192.168.1.98").build()).build();
180     List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
181     serviceHealthList.add(serviceHealth);
182     
183     data.setData(serviceHealthList);
184     
185     try {
186       queueManager.putIn(data);
187     } catch (Exception e) {      
188       Assert.assertTrue(e instanceof InterruptedException);
189     }
190   }
191   
192   
193 }