1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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;
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;
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;
51 import redis.clients.jedis.JedisPool;
52 import redis.clients.jedis.JedisPoolConfig;
54 import com.fiftyonred.mock_jedis.MockJedisPool;
55 import com.orbitz.consul.model.health.ImmutableNode;
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;
65 public static void setUpBeforeClass() throws Exception {
66 queueManager=QueueManager.getInstance();
67 putInServiceListQueue();
68 putInServiceQueue4Update();
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());
79 PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
81 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
82 return mockJedisPool.getResource().keys((String) args[0]);
89 public void test_ServiceConsumer(){
91 //start ServiceListConsumer
92 // new Thread(new ServiceListConsumer(this),"ServiceListConsumerThread").start();
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();
103 public void test_ServiceListConsumer(){
105 //start ServiceListConsumer
106 new Thread(new ServiceListConsumer(),"ServiceListConsumerThread").start();
109 } catch (InterruptedException e) {
110 // TODO Auto-generated catch block
113 putInServiceQueue4Delete();
118 private static void putInServiceListQueue(){
119 ServiceData<HttpEntity> data=new ServiceData<HttpEntity>();
120 data.setDataType(ServiceData.DataType.service_list);
122 BasicHttpEntity entity = new BasicHttpEntity();
123 InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
124 entity.setContent(content);
125 data.setData(entity);
128 queueManager.putIn(data);
129 } catch (Exception e) {
130 Assert.assertTrue(e instanceof InterruptedException);
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);
139 List<String> tagList = new ArrayList<String>();
140 tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
142 .add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
143 tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
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);
154 data.setData(serviceHealthList);
157 queueManager.putIn(data);
158 } catch (Exception e) {
159 Assert.assertTrue(e instanceof InterruptedException);
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);
168 List<String> tagList = new ArrayList<String>();
169 tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
171 .add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
172 tagList.add("\"ns\":{\"namespace\":\"ns1\"}");
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);
183 data.setData(serviceHealthList);
186 queueManager.putIn(data);
187 } catch (Exception e) {
188 Assert.assertTrue(e instanceof InterruptedException);