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