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.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;
37 @PrepareForTest({DcaeConfigurationPolling.class, MicroServiceConfig.class})
38 @RunWith(PowerMockRunner.class)
39 public class DcaeConfigurationPollingTest {
42 public ExpectedException thrown = ExpectedException.none();
44 private DcaeConfigurationPolling daceConfigurationPolling;
48 daceConfigurationPolling = new DcaeConfigurationPolling("holmes-rule-mgmt");
52 public void testDaceConfigurationPolling_getDcaeConfigurations_exception() throws Exception {
54 thrown.expect(CorrelationException.class);
55 thrown.expectMessage("syntax error, pos 1");
56 PowerMockito.mockStatic(MicroServiceConfig.class);
57 when(MicroServiceConfig.getServiceConfigInfoFromCBS("holmes-rule-mgmt"))
59 PowerMock.createMock(DcaeConfigurationParser.class);
60 PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
61 .andThrow(new CorrelationException("tests")).anyTimes();
63 PowerMock.replayAll();
64 Whitebox.invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
65 PowerMock.verifyAll();
69 public void testDaceConfigurationPolling_getDcaeConfigurations_null() throws Exception {
71 thrown.expect(CorrelationException.class);
72 PowerMockito.mockStatic(MicroServiceConfig.class);
73 when(MicroServiceConfig.getServiceConfigInfoFromCBS("holmes-rule-mgmt"))
75 PowerMock.createMock(DcaeConfigurationParser.class);
76 PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
77 .andReturn(null).anyTimes();
79 PowerMock.replayAll();
80 DcaeConfigurations dcaeConfigurations = Whitebox
81 .invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
82 PowerMock.verifyAll();
84 assertThat(dcaeConfigurations == null, equalTo(true));
88 public void testDaceConfigurationPolling_addAllCorrelationRules_connection_exception()
91 thrown.expect(CorrelationException.class);
92 DcaeConfigurations dcaeConfigurations = new DcaeConfigurations();
93 Rule rule = new Rule("test", "test", "tset",1);
94 dcaeConfigurations.getDefaultRules().add(rule);
96 PowerMock.replayAll();
97 Whitebox.invokeMethod(daceConfigurationPolling, "addAllCorrelationRules",
99 PowerMock.verifyAll();
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();
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));