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