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