696e32acf6683db5a81b7c06f182f6751e4c515c
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / send / RuleAllocationTest.java
1 /**
2 * Copyright 2017 ZTE Corporation.
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
17 package org.onap.holmes.rulemgt.send;
18
19
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;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41 import static org.hamcrest.core.IsEqual.equalTo;
42 import static org.junit.Assert.assertThat;
43
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 {
48
49     @Before
50     public void prepare() {
51
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);
60
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);
68         try {
69             EasyMock.expect(engineIpList.getServiceCount()).andReturn(new ArrayList());
70         } catch (Exception e) {
71             // Do nothing
72         }
73
74
75         PowerMock.replayAll();
76
77     }
78
79     @After
80     public void destroy() {
81         PowerMock.resetAll();
82     }
83
84     @Test
85     public void extendCompareIpTest() throws Exception{
86         RuleAllocation ruleAllocation = new RuleAllocation();
87
88         List<String> newList = new ArrayList<>();
89         newList.add("10.96.33.34");
90         newList.add("10.74.65.24");
91
92         List<String> oldList = new ArrayList<>();
93         oldList.add("10.96.33.34");
94         List<String> extendIp = Whitebox.invokeMethod(ruleAllocation,"extendCompareIp",newList,oldList);
95
96         PowerMock.verifyAll();
97
98         assertThat(extendIp.get(0),equalTo("10.74.65.24"));
99     }
100
101     @Test
102     public void destroyCompareIpTest() throws Exception{
103         RuleAllocation ruleAllocation = new RuleAllocation();
104
105         List<String> newList = new ArrayList<>();
106         newList.add("10.96.33.34");
107
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);
112
113         PowerMock.verifyAll();
114
115         assertThat(destoryIp.get(0),equalTo("10.74.65.24"));
116     }
117
118 }