aboutsummaryrefslogtreecommitdiffstats
path: root/apiroute/apiroute-service/src/test/java/org/onap/msb/apiroute/wrapper/queue/QueueManagerTest.java
blob: 931c09dfb5fa5b80baf66ca239a4f3dcdbffbc35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * Copyright 2016-2017 ZTE, Inc. and others.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package org.onap.msb.apiroute.wrapper.queue;

import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.entity.BasicHttpEntity;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.msb.apiroute.SyncDataManager;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
import org.onap.msb.apiroute.wrapper.consulextend.util.HttpTest;
import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
import org.onap.msb.apiroute.wrapper.queue.QueueManager;
import org.onap.msb.apiroute.wrapper.queue.ServiceConsumer;
import org.onap.msb.apiroute.wrapper.queue.ServiceData;
import org.onap.msb.apiroute.wrapper.queue.ServiceListConsumer;
import org.onap.msb.apiroute.wrapper.util.JedisUtil;
import org.onap.msb.apiroute.wrapper.util.RouteUtil;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.fiftyonred.mock_jedis.MockJedisPool;
import com.orbitz.consul.model.health.ImmutableNode;

@RunWith(PowerMockRunner.class)
@PrepareForTest({JedisUtil.class, RouteUtil.class, RedisAccessWrapper.class})
@PowerMockIgnore({"javax.management.*"})
public class QueueManagerTest {
    private static QueueManager queueManager;


    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        queueManager = QueueManager.getInstance();
        putInServiceListQueue();
        putInServiceQueue4Update();

    }

    @Before
    public void setUpBeforeTest() throws Exception {
        final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
        PowerMockito.mockStatic(JedisUtil.class);
        JedisUtil jedisUtil = PowerMockito.mock(JedisUtil.class);
        PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());

        PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return mockJedisPool.getResource().keys((String) args[0]);
            }
        });
    }



    public void test_ServiceConsumer() {

        // start ServiceListConsumer
        // new Thread(new ServiceListConsumer(this),"ServiceListConsumerThread").start();

        // start Service Consumer
        int serviceQueneNum = RouteUtil.SERVICE_DATA_QUEUE_NUM;
        for (int i = 0; i < serviceQueneNum; i++) {
            new Thread(new ServiceConsumer(i), "ServiceConsumerThread" + i).start();
        }

    }


    public void test_ServiceListConsumer() {

        // start ServiceListConsumer
        new Thread(new ServiceListConsumer(), "ServiceListConsumerThread").start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        putInServiceQueue4Delete();
    }



    private static void putInServiceListQueue() {
        ServiceData<HttpEntity> data = new ServiceData<HttpEntity>();
        data.setDataType(ServiceData.DataType.service_list);

        BasicHttpEntity entity = new BasicHttpEntity();
        InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
        entity.setContent(content);
        data.setData(entity);

        try {
            queueManager.putIn(data);
        } catch (Exception e) {
            Assert.assertTrue(e instanceof InterruptedException);
        }
    }

    private static void putInServiceQueue4Update() {
        ServiceData<List<ServiceHealth>> data = new ServiceData<List<ServiceHealth>>();
        data.setDataType(ServiceData.DataType.service);
        data.setOperate(ServiceData.Operate.delete);

        List<String> tagList = new ArrayList<String>();
        tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
        tagList.add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
        tagList.add("\"ns\":{\"namespace\":\"ns1\"}");

        Service service = ImmutableService.builder().id("id").port(8686).address("10.74.165.246").service("msbtest")
                        .addAllTags(tagList).createIndex(0).modifyIndex(0).build();
        ServiceHealth serviceHealth = ImmutableServiceHealth.builder().service(service)
                        .node(ImmutableNode.builder().node("server").address("192.168.1.98").build()).build();
        List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
        serviceHealthList.add(serviceHealth);

        data.setData(serviceHealthList);

        try {
            queueManager.putIn(data);
        } catch (Exception e) {
            Assert.assertTrue(e instanceof InterruptedException);
        }
    }

    private static void putInServiceQueue4Delete() {
        ServiceData<List<ServiceHealth>> data = new ServiceData<List<ServiceHealth>>();
        data.setDataType(ServiceData.DataType.service);
        data.setOperate(ServiceData.Operate.update);

        List<String> tagList = new ArrayList<String>();
        tagList.add("\"base\":{\"protocol\":\"REST\",\"version\":\"v1\",\"url\":\"/api/msbtest/v1\"}");
        tagList.add("\"labels\":{\"visualRange\":\"0\",\"network_plane_type\":\"net\",\"customLabel\":\"custom\"}");
        tagList.add("\"ns\":{\"namespace\":\"ns1\"}");

        Service service = ImmutableService.builder().id("id").port(8686).address("10.74.165.246").service("msbtest")
                        .addAllTags(tagList).createIndex(0).modifyIndex(0).build();
        ServiceHealth serviceHealth = ImmutableServiceHealth.builder().service(service)
                        .node(ImmutableNode.builder().node("server").address("192.168.1.98").build()).build();
        List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
        serviceHealthList.add(serviceHealth);

        data.setData(serviceHealthList);

        try {
            queueManager.putIn(data);
        } catch (Exception e) {
            Assert.assertTrue(e instanceof InterruptedException);
        }
    }


}