1037495e72a9f3920667800265e5b64069d003fd
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / dcae / DcaeConfigurationPollingTest.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 package org.onap.holmes.rulemgt.dcae;
17
18 import static org.hamcrest.CoreMatchers.equalTo;
19 import static org.junit.Assert.assertThat;
20 import static org.powermock.api.mockito.PowerMockito.when;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.rules.ExpectedException;
24 import org.junit.runner.RunWith;
25 import org.onap.holmes.common.config.MicroServiceConfig;
26 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
27 import org.onap.holmes.common.dcae.entity.Rule;
28 import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser;
29 import org.onap.holmes.common.exception.CorrelationException;
30 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
31 import org.powermock.api.easymock.PowerMock;
32 import org.powermock.api.mockito.PowerMockito;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35 import org.powermock.reflect.Whitebox;
36
37 @PrepareForTest({DcaeConfigurationPolling.class, MicroServiceConfig.class})
38 @RunWith(PowerMockRunner.class)
39 public class DcaeConfigurationPollingTest {
40
41     @org.junit.Rule
42     public ExpectedException thrown = ExpectedException.none();
43
44     private DcaeConfigurationPolling daceConfigurationPolling;
45
46     @Before
47     public void setUp() {
48         daceConfigurationPolling = new DcaeConfigurationPolling("holmes-rule-mgmt");
49     }
50
51     @Test
52     public void testDaceConfigurationPolling_getDcaeConfigurations_exception() throws Exception {
53         PowerMock.resetAll();
54         thrown.expect(CorrelationException.class);
55         thrown.expectMessage("syntax error, pos 1");
56         PowerMockito.mockStatic(MicroServiceConfig.class);
57         when(MicroServiceConfig.getServiceConfigInfoFromCBS("holmes-rule-mgmt"))
58                 .thenReturn("host");
59         PowerMock.createMock(DcaeConfigurationParser.class);
60         PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
61                 .andThrow(new CorrelationException("tests")).anyTimes();
62
63         PowerMock.replayAll();
64         Whitebox.invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
65         PowerMock.verifyAll();
66     }
67
68     @Test
69     public void testDaceConfigurationPolling_getDcaeConfigurations_null() throws Exception {
70         PowerMock.resetAll();
71         thrown.expect(CorrelationException.class);
72         PowerMockito.mockStatic(MicroServiceConfig.class);
73         when(MicroServiceConfig.getServiceConfigInfoFromCBS("holmes-rule-mgmt"))
74                 .thenReturn("host");
75         PowerMock.createMock(DcaeConfigurationParser.class);
76         PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
77                 .andReturn(null).anyTimes();
78
79         PowerMock.replayAll();
80         DcaeConfigurations dcaeConfigurations = Whitebox
81                 .invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
82         PowerMock.verifyAll();
83
84         assertThat(dcaeConfigurations == null, equalTo(true));
85     }
86
87     @Test
88     public void testDaceConfigurationPolling_addAllCorrelationRules_connection_exception()
89             throws Exception {
90         PowerMock.resetAll();
91         thrown.expect(CorrelationException.class);
92         DcaeConfigurations dcaeConfigurations = new DcaeConfigurations();
93         Rule rule = new Rule("test", "test", "tset",1);
94         dcaeConfigurations.getDefaultRules().add(rule);
95
96         PowerMock.replayAll();
97         Whitebox.invokeMethod(daceConfigurationPolling, "addAllCorrelationRules",
98                 dcaeConfigurations);
99         PowerMock.verifyAll();
100     }
101
102     @Test
103     public void testDaceConfigurationPolling_getRuleCreateRequest() throws Exception {
104         PowerMock.resetAll();
105         Rule rule = new Rule("test", "test1", "stest",1);
106         PowerMock.replayAll();
107         RuleCreateRequest actual = Whitebox
108                 .invokeMethod(daceConfigurationPolling, "getRuleCreateRequest", rule);
109         PowerMock.verifyAll();
110
111         assertThat(actual.getRuleName(), equalTo("test"));
112         assertThat(actual.getLoopControlName(), equalTo("test1"));
113         assertThat(actual.getContent(), equalTo("stest"));
114         assertThat(actual.getDescription(), equalTo(""));
115         assertThat(actual.getEnabled(), equalTo(1));
116     }
117 }