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.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;
42 @PrepareForTest({DcaeConfigurationPolling.class, MicroServiceConfig.class, ObjectMapper.class})
43 @RunWith(PowerMockRunner.class)
44 public class DcaeConfigurationPollingTest {
47 public ExpectedException thrown = ExpectedException.none();
49 private DcaeConfigurationPolling daceConfigurationPolling;
53 daceConfigurationPolling = new DcaeConfigurationPolling("holmes-rule-mgmt");
57 public void testDaceConfigurationPolling_getDcaeConfigurations_exception() throws Exception {
59 thrown.expect(CorrelationException.class);
60 thrown.expectMessage("host");
61 PowerMockito.mockStatic(MicroServiceConfig.class);
62 when(MicroServiceConfig.getServiceAddrInfoFromCBS("holmes-rule-mgmt"))
64 PowerMock.createMock(DcaeConfigurationParser.class);
65 PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
66 .andThrow(new CorrelationException("tests")).anyTimes();
68 PowerMock.replayAll();
69 Whitebox.invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
70 PowerMock.verifyAll();
74 public void testDaceConfigurationPolling_getDcaeConfigurations_null() throws Exception {
76 thrown.expect(CorrelationException.class);
77 PowerMockito.mockStatic(MicroServiceConfig.class);
78 when(MicroServiceConfig.getServiceAddrInfoFromCBS("holmes-rule-mgmt"))
80 PowerMock.createMock(DcaeConfigurationParser.class);
81 PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
82 .andReturn(null).anyTimes();
84 PowerMock.replayAll();
85 DcaeConfigurations dcaeConfigurations = Whitebox
86 .invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
87 PowerMock.verifyAll();
89 assertThat(dcaeConfigurations == null, equalTo(true));
93 public void testDaceConfigurationPolling_addAllCorrelationRules_connection_exception()
96 thrown.expect(ProcessingException.class);
97 DcaeConfigurations dcaeConfigurations = new DcaeConfigurations();
98 Rule rule = new Rule("test", "test", "tset",1);
99 dcaeConfigurations.getDefaultRules().add(rule);
101 PowerMock.replayAll();
102 Whitebox.invokeMethod(daceConfigurationPolling, "addAllCorrelationRules",
104 PowerMock.verifyAll();
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();
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));