2 * Copyright 2017 ZTE Corporation.
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.
17 package org.onap.holmes.rulemgt.send;
20 import org.easymock.EasyMock;
21 import org.glassfish.hk2.api.ServiceLocator;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
27 import org.onap.holmes.common.utils.DbDaoUtil;
28 import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;
29 import org.onap.holmes.rulemgt.db.CorrelationRuleDao;
30 import org.onap.holmes.rulemgt.msb.EngineIpList;
31 import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;
32 import org.onap.holmes.rulemgt.wrapper.RuleQueryWrapper;
33 import org.powermock.api.easymock.PowerMock;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
36 import org.powermock.reflect.Whitebox;
38 import java.util.ArrayList;
39 import java.util.List;
41 import static org.hamcrest.core.IsEqual.equalTo;
42 import static org.junit.Assert.assertThat;
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest({ServiceLocator.class, RuleMgtWrapper.class, RuleQueryWrapper.class, EngineWrapper.class,
46 EngineIpList.class, DbDaoUtil.class, RuleAllocation.class, ServiceLocatorHolder.class})
47 public class RuleAllocationTest {
50 public void prepare() {
52 ServiceLocator locator = PowerMock.createMock(ServiceLocator.class);
53 RuleMgtWrapper ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class);
54 RuleQueryWrapper ruleQueryWrapper = PowerMock.createMock(RuleQueryWrapper.class);
55 EngineWrapper engineWrapper = PowerMock.createMock(EngineWrapper.class);
56 EngineIpList engineIpList = PowerMock.createMock(EngineIpList.class);
57 CorrelationRuleDao correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class);
58 DbDaoUtil daoUtil = PowerMock.createMock(DbDaoUtil.class);
59 PowerMock.mockStatic(ServiceLocatorHolder.class);
61 EasyMock.expect(ServiceLocatorHolder.getLocator()).andReturn(locator);
62 EasyMock.expect(locator.getService(RuleMgtWrapper.class)).andReturn(ruleMgtWrapper);
63 EasyMock.expect(locator.getService(RuleQueryWrapper.class)).andReturn(ruleQueryWrapper);
64 EasyMock.expect(locator.getService(EngineWrapper.class)).andReturn(engineWrapper);
65 EasyMock.expect(locator.getService(EngineIpList.class)).andReturn(engineIpList);
66 EasyMock.expect(locator.getService(DbDaoUtil.class)).andReturn(daoUtil);
67 EasyMock.expect(daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(correlationRuleDao);
69 EasyMock.expect(engineIpList.getServiceCount()).andReturn(new ArrayList());
70 } catch (Exception e) {
75 PowerMock.replayAll();
80 public void destroy() {
85 public void extendCompareIpTest() throws Exception{
86 RuleAllocation ruleAllocation = new RuleAllocation();
88 List<String> newList = new ArrayList<>();
89 newList.add("10.96.33.34");
90 newList.add("10.74.65.24");
92 List<String> oldList = new ArrayList<>();
93 oldList.add("10.96.33.34");
94 List<String> extendIp = Whitebox.invokeMethod(ruleAllocation,"extendCompareIp",newList,oldList);
96 PowerMock.verifyAll();
98 assertThat(extendIp.get(0),equalTo("10.74.65.24"));
102 public void destroyCompareIpTest() throws Exception{
103 RuleAllocation ruleAllocation = new RuleAllocation();
105 List<String> newList = new ArrayList<>();
106 newList.add("10.96.33.34");
108 List<String> oldList = new ArrayList<>();
109 oldList.add("10.96.33.34");
110 oldList.add("10.74.65.24");
111 List<String> destoryIp = Whitebox.invokeMethod(ruleAllocation,"destroyCompareIp",newList,oldList);
113 PowerMock.verifyAll();
115 assertThat(destoryIp.get(0),equalTo("10.74.65.24"));