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