modify not publish messages to DMaaP
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / dmaap / DmaapServiceTest.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.common.dmaap;
17
18 import static org.easymock.EasyMock.anyObject;
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.junit.Assert.assertThat;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.junit.runner.RunWith;
25 import org.omg.CORBA.Any;
26 import org.onap.holmes.common.aai.AaiQuery;
27 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
28 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
29 import org.onap.holmes.common.aai.entity.VmEntity;
30 import org.onap.holmes.common.aai.entity.VnfEntity;
31 import org.onap.holmes.common.api.stat.VesAlarm;
32 import org.onap.holmes.common.exception.CorrelationException;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
41 import org.powermock.api.easymock.PowerMock;
42 import org.powermock.modules.junit4.PowerMockRunner;
43 import org.powermock.reflect.Whitebox;
44
45 @PrepareForTest({DmaapService.class, Publisher.class, AaiQuery.class})
46 @RunWith(PowerMockRunner.class)
47 public class DmaapServiceTest {
48
49     @Rule
50     public ExpectedException thrown = ExpectedException.none();
51
52     private AaiQuery aaiQuery;
53
54     private DmaapService dmaapService;
55
56     @Before
57     public void setUp() {
58         dmaapService = new DmaapService();
59         aaiQuery = PowerMock.createMock(AaiQuery.class);
60         Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery);
61     }
62
63     @Test
64     public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception {
65         PowerMock.resetAll();
66
67         PowerMock.replayAll();
68         PolicyMsg policyMsg = Whitebox
69                 .invokeMethod(dmaapService, "getDefaultPolicyMsg", "tetss");
70         PowerMock.verifyAll();
71
72         assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
73         assertThat(policyMsg.getTargetType(), equalTo("VM"));
74         assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("tetss"));
75     }
76
77     @Test
78     public void testDmaapService_getVnfEntity_ok() throws Exception {
79         PowerMock.resetAll();
80         VnfEntity expect = new VnfEntity();
81         expect.setVnfName("test");
82         PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
83                 anyObject(String.class)).andReturn(expect).anyTimes();
84         PowerMock.replayAll();
85         VnfEntity actual = Whitebox
86                 .invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
87         PowerMock.verifyAll();
88
89         assertThat(actual.getVnfName(), equalTo("test"));
90     }
91
92     @Test
93     public void testDmaapService_getVnfEntity_exception() throws Exception {
94         PowerMock.resetAll();
95         PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
96                 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
97         PowerMock.replayAll();
98         VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
99         PowerMock.verifyAll();
100
101         assertThat(actual == null, equalTo(true));
102     }
103
104     @Test
105     public void testDmaapService_getVmEntity_ok() throws Exception {
106         PowerMock.resetAll();
107         VmEntity expect = new VmEntity();
108         expect.setVserverId("11111");
109         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
110                 anyObject(String.class)).andReturn(expect).anyTimes();
111         PowerMock.replayAll();
112         VmEntity actual = Whitebox
113                 .invokeMethod(dmaapService, "getVmEntity", "tset", "test");
114         PowerMock.verifyAll();
115
116         assertThat(actual.getVserverId(), equalTo("11111"));
117     }
118
119     @Test
120     public void testDmaapService_getVmEntity_exception() throws Exception {
121         PowerMock.resetAll();
122         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
123                 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
124         PowerMock.replayAll();
125         VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVmEntity", "tset", "test");
126         PowerMock.verifyAll();
127
128         assertThat(actual == null, equalTo(true));
129     }
130
131     @Test
132     public void testDmaapService_getVserverInstanceId_ok() throws Exception {
133         PowerMock.resetAll();
134         VnfEntity vnfEntity = new VnfEntity();
135         Relationship relationship = new Relationship();
136         relationship.setRelatedTo("service-instance");
137
138         List<RelationshipData> relationshipDataList = new ArrayList<>();
139
140         RelationshipData relationshipData = new RelationshipData();
141         relationshipData.setRelationshipKey("service-instance.service-instance-id");
142         relationshipData.setRelationshipValue("USUCP0PCOIL0110UJZZ01");
143         relationshipDataList.add(relationshipData);
144         relationship.setRelationshipDataList(relationshipDataList);
145
146         List<Relationship> relationships = new ArrayList<>();
147         relationships.add(relationship);
148         vnfEntity.getRelationshipList().setRelationships(relationships);
149
150         PowerMock.replayAll();
151         String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
152         PowerMock.verifyAll();
153
154         assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
155     }
156
157     @Test
158     public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
159         PowerMock.resetAll();
160         VnfEntity vnfEntity = null;
161
162         PowerMock.replayAll();
163         String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
164         PowerMock.verifyAll();
165
166         assertThat(actual, equalTo(""));
167     }
168
169     @Test
170     public void testDmaapService_getEnrichedPolicyMsg_ok() throws Exception {
171         PowerMock.resetAll();
172         VmEntity vmEntity = new VmEntity();
173         vmEntity.setInMaint(false);
174         vmEntity.setClosedLoopDisable(true);
175         vmEntity.setProvStatus("prov");
176         vmEntity.setResourceVersion("kkkk");
177         VesAlarm vesAlarm = new VesAlarm();
178         vesAlarm.setEventId("11111");
179         vesAlarm.setEventName("3333");
180
181         PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
182                 anyObject(String.class)).andReturn(null).anyTimes();
183
184         PowerMock.replayAll();
185         PolicyMsg actual = Whitebox
186                 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, "loopName");
187         PowerMock.verifyAll();
188
189         assertThat(actual.getClosedLoopControlName(), equalTo(null));
190         assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov"));
191         assertThat(actual.getAai().get("vserver.vserver-name2") == null, equalTo(true));
192         assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo(""));
193     }
194 }