add polling rule task
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / dcae / DaceConfigurationPollingTest.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.easymock.EasyMock.anyObject;
19 import static org.hamcrest.CoreMatchers.anyOf;
20 import static org.hamcrest.CoreMatchers.equalTo;
21 import static org.junit.Assert.assertThat;
22 import static org.powermock.api.mockito.PowerMockito.when;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.util.ArrayList;
27 import java.util.List;
28 import javax.ws.rs.ProcessingException;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.junit.runner.RunWith;
33 import org.onap.holmes.common.config.MicroServiceConfig;
34 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
35 import org.onap.holmes.common.dcae.entity.Rule;
36 import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser;
37 import org.onap.holmes.common.exception.CorrelationException;
38 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
39 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
40 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
41 import org.powermock.api.easymock.PowerMock;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
45 import org.powermock.reflect.Whitebox;
46
47 @PrepareForTest({DaceConfigurationPolling.class, MicroServiceConfig.class, ObjectMapper.class})
48 @RunWith(PowerMockRunner.class)
49 public class DaceConfigurationPollingTest {
50
51     @org.junit.Rule
52     public ExpectedException thrown = ExpectedException.none();
53
54     private DaceConfigurationPolling daceConfigurationPolling;
55
56     @Before
57     public void setUp() {
58         daceConfigurationPolling = new DaceConfigurationPolling("holmes-rule-mgmt");
59     }
60
61     @Test
62     public void testDaceConfigurationPolling_getDcaeConfigurations_exception() throws Exception {
63         PowerMock.resetAll();
64         thrown.expect(CorrelationException.class);
65         thrown.expectMessage("host");
66         PowerMockito.mockStatic(MicroServiceConfig.class);
67         when(MicroServiceConfig.getServiceAddrInfoFromCBS("holmes-rule-mgmt"))
68                 .thenReturn("host");
69         PowerMock.createMock(DcaeConfigurationParser.class);
70         PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
71                 .andThrow(new CorrelationException("tests")).anyTimes();
72
73         PowerMock.replayAll();
74         Whitebox.invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
75         PowerMock.verifyAll();
76     }
77
78     @Test
79     public void testDaceConfigurationPolling_getDcaeConfigurations_null() throws Exception {
80         PowerMock.resetAll();
81         thrown.expect(CorrelationException.class);
82         PowerMockito.mockStatic(MicroServiceConfig.class);
83         when(MicroServiceConfig.getServiceAddrInfoFromCBS("holmes-rule-mgmt"))
84                 .thenReturn("host");
85         PowerMock.createMock(DcaeConfigurationParser.class);
86         PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
87                 .andReturn(null).anyTimes();
88
89         PowerMock.replayAll();
90         DcaeConfigurations dcaeConfigurations = Whitebox
91                 .invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
92         PowerMock.verifyAll();
93
94         assertThat(dcaeConfigurations == null, equalTo(true));
95     }
96
97     @Test
98     public void testDaceConfigurationPolling_addAllCorrelationRules_connection_exception()
99             throws Exception {
100         PowerMock.resetAll();
101         thrown.expect(ProcessingException.class);
102         DcaeConfigurations dcaeConfigurations = new DcaeConfigurations();
103         Rule rule = new Rule("test", "test", 1);
104         dcaeConfigurations.getDefaultRules().add(rule);
105
106         PowerMock.replayAll();
107         Whitebox.invokeMethod(daceConfigurationPolling, "addAllCorrelationRules",
108                 dcaeConfigurations);
109         PowerMock.verifyAll();
110     }
111
112     @Test
113     public void testDaceConfigurationPolling_getRuleCreateRequest() throws Exception {
114         PowerMock.resetAll();
115         Rule rule = new Rule("test", "test1", 1);
116         PowerMock.replayAll();
117         RuleCreateRequest actual = Whitebox
118                 .invokeMethod(daceConfigurationPolling, "getRuleCreateRequest", rule);
119         PowerMock.verifyAll();
120
121         assertThat(actual.getRuleName(), equalTo("test"));
122         assertThat(actual.getContent(), equalTo("test1"));
123         assertThat(actual.getDescription(), equalTo(""));
124         assertThat(actual.getEnabled(), equalTo(1));
125     }
126
127     @Test
128     public void testDaceConfigurationPolling_run_null_exception() throws Exception {
129         PowerMock.replayAll();
130         PowerMockito.mockStatic(MicroServiceConfig.class);
131         when(MicroServiceConfig.getServiceAddrInfoFromCBS("holmes-rule-mgmt"))
132                 .thenReturn("host");
133         PowerMock.replayAll();
134         Whitebox.invokeMethod(daceConfigurationPolling, "run");
135         PowerMock.verifyAll();
136     }
137     
138 }