2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.holmes.rulemgt.dcae;
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;
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;
47 @PrepareForTest({DaceConfigurationPolling.class, MicroServiceConfig.class, ObjectMapper.class})
48 @RunWith(PowerMockRunner.class)
49 public class DaceConfigurationPollingTest {
52 public ExpectedException thrown = ExpectedException.none();
54 private DaceConfigurationPolling daceConfigurationPolling;
58 daceConfigurationPolling = new DaceConfigurationPolling("holmes-rule-mgmt");
62 public void testDaceConfigurationPolling_getDcaeConfigurations_exception() throws Exception {
64 thrown.expect(CorrelationException.class);
65 thrown.expectMessage("host");
66 PowerMockito.mockStatic(MicroServiceConfig.class);
67 when(MicroServiceConfig.getServiceAddrInfoFromCBS("holmes-rule-mgmt"))
69 PowerMock.createMock(DcaeConfigurationParser.class);
70 PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
71 .andThrow(new CorrelationException("tests")).anyTimes();
73 PowerMock.replayAll();
74 Whitebox.invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
75 PowerMock.verifyAll();
79 public void testDaceConfigurationPolling_getDcaeConfigurations_null() throws Exception {
81 thrown.expect(CorrelationException.class);
82 PowerMockito.mockStatic(MicroServiceConfig.class);
83 when(MicroServiceConfig.getServiceAddrInfoFromCBS("holmes-rule-mgmt"))
85 PowerMock.createMock(DcaeConfigurationParser.class);
86 PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
87 .andReturn(null).anyTimes();
89 PowerMock.replayAll();
90 DcaeConfigurations dcaeConfigurations = Whitebox
91 .invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
92 PowerMock.verifyAll();
94 assertThat(dcaeConfigurations == null, equalTo(true));
98 public void testDaceConfigurationPolling_addAllCorrelationRules_connection_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);
106 PowerMock.replayAll();
107 Whitebox.invokeMethod(daceConfigurationPolling, "addAllCorrelationRules",
109 PowerMock.verifyAll();
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();
121 assertThat(actual.getRuleName(), equalTo("test"));
122 assertThat(actual.getContent(), equalTo("test1"));
123 assertThat(actual.getDescription(), equalTo(""));
124 assertThat(actual.getEnabled(), equalTo(1));
128 public void testDaceConfigurationPolling_run_null_exception() throws Exception {
129 PowerMock.replayAll();
130 PowerMockito.mockStatic(MicroServiceConfig.class);
131 when(MicroServiceConfig.getServiceAddrInfoFromCBS("holmes-rule-mgmt"))
133 PowerMock.replayAll();
134 Whitebox.invokeMethod(daceConfigurationPolling, "run");
135 PowerMock.verifyAll();