931c09dfb5fa5b80baf66ca239a4f3dcdbffbc35
[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"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.queue;
15
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;
22 import java.util.Map;
23
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;
48
49 import redis.clients.jedis.JedisPool;
50 import redis.clients.jedis.JedisPoolConfig;
51
52 import com.fiftyonred.mock_jedis.MockJedisPool;
53 import com.orbitz.consul.model.health.ImmutableNode;
54
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({JedisUtil.class, RouteUtil.class, RedisAccessWrapper.class})
57 @PowerMockIgnore({"javax.management.*"})
58 public class QueueManagerTest {
59     private static QueueManager queueManager;
60
61
62     @BeforeClass
63     public static void setUpBeforeClass() throws Exception {
64         queueManager = QueueManager.getInstance();
65         putInServiceListQueue();
66         putInServiceQueue4Update();
67
68     }
69
70     @Before
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());
76
77         PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
78             @Override
79             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
80                 return mockJedisPool.getResource().keys((String) args[0]);
81             }
82         });
83     }
84
85
86
87     public void test_ServiceConsumer() {
88
89         // start ServiceListConsumer
90         // new Thread(new ServiceListConsumer(this),"ServiceListConsumerThread").start();
91
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();
96         }
97
98     }
99
100
101     public void test_ServiceListConsumer() {
102
103         // start ServiceListConsumer
104         new Thread(new ServiceListConsumer(), "ServiceListConsumerThread").start();
105         try {
106             Thread.sleep(2000);
107         } catch (InterruptedException e) {
108             // TODO Auto-generated catch block
109             e.printStackTrace();
110         }
111         putInServiceQueue4Delete();
112     }
113
114
115
116     private static void putInServiceListQueue() {
117         ServiceData<HttpEntity> data = new ServiceData<HttpEntity>();
118         data.setDataType(ServiceData.DataType.service_list);
119
120         BasicHttpEntity entity = new BasicHttpEntity();
121         InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
122         entity.setContent(content);
123         data.setData(entity);
124
125         try {
126             queueManager.putIn(data);
127         } catch (Exception e) {
128             Assert.assertTrue(e instanceof InterruptedException);
129         }
130     }
131
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);
136
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\"}");
141
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);
148
149         data.setData(serviceHealthList);
150
151         try {
152             queueManager.putIn(data);
153         } catch (Exception e) {
154             Assert.assertTrue(e instanceof InterruptedException);
155         }
156     }
157
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);
162
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\"}");
167
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);
174
175         data.setData(serviceHealthList);
176
177         try {
178             queueManager.putIn(data);
179         } catch (Exception e) {
180             Assert.assertTrue(e instanceof InterruptedException);
181         }
182     }
183
184
185 }