Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / TestGenericVnfManager.java
1 /*
2  * Copyright 2016-2017, Nokia 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.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
17
18 import com.nokia.cbam.lcm.v32.model.VnfInfo;
19 import io.reactivex.Observable;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.NoSuchElementException;
23 import java.util.Set;
24 import java.util.concurrent.atomic.AtomicLong;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32 import org.onap.aai.api.NetworkApi;
33 import org.onap.aai.model.GenericVnf;
34 import org.onap.aai.model.Relationship;
35 import org.onap.aai.model.RelationshipData;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
39
40 import static java.lang.Boolean.TRUE;
41
42 import static junit.framework.TestCase.assertEquals;
43 import static junit.framework.TestCase.fail;
44 import static org.mockito.Matchers.anyLong;
45 import static org.mockito.Matchers.eq;
46 import static org.mockito.Mockito.*;
47 import static org.springframework.test.util.ReflectionTestUtils.setField;
48
49 public class TestGenericVnfManager extends TestBase {
50     private ArgumentCaptor<GenericVnf> payload = ArgumentCaptor.forClass(GenericVnf.class);
51
52     @Mock
53     private AAIRestApiProvider aaiRestApiProvider;
54     @Mock
55     private NetworkApi networkApi;
56     private GenericVnfManager genericVnfManager;
57     private VnfInfo vnfInfo = new VnfInfo();
58
59     static void assertRelation(List<Relationship> relationShips, String relatedTo, RelationshipData... data) {
60         for (Relationship relationship : relationShips) {
61             if (relationship.getRelatedTo().equals(relatedTo)) {
62                 assertEquals(data.length, relationship.getRelationshipData().size());
63                 int i = 0;
64                 for (RelationshipData c : data) {
65                     assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
66                     assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
67                     i++;
68                 }
69                 return;
70             }
71         }
72         fail();
73     }
74
75     @Before
76     public void init() {
77         when(aaiRestApiProvider.getNetworkApi()).thenReturn(networkApi);
78         genericVnfManager = new GenericVnfManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
79         setField(GenericVnfManager.class, "logger", logger);
80         AtomicLong currentTime = new AtomicLong(0L);
81         when(systemFunctions.currentTimeMillis()).thenAnswer(new Answer<Long>() {
82             @Override
83             public Long answer(InvocationOnMock invocation) throws Throwable {
84                 return currentTime.get();
85             }
86         });
87         Mockito.doAnswer(new Answer() {
88             @Override
89             public Object answer(InvocationOnMock invocation) throws Throwable {
90                 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
91                 return null;
92             }
93         }).when(systemFunctions).sleep(anyLong());
94     }
95
96     /**
97      * if the VNF does not exist it is created
98      */
99     @Test
100     public void createNonExistingVnf() throws Exception {
101         GenericVnf vnfInAaai = new GenericVnf();
102         Set<GenericVnf> vnfs = new HashSet<>();
103         when(networkApi.getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)).thenAnswer((Answer<Observable<GenericVnf>>) invocation -> {
104             if (vnfs.size() == 0) {
105                 throw new NoSuchElementException();
106             }
107             return buildObservable(vnfs.iterator().next());
108         });
109         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
110         when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
111             vnfs.add(vnfInAaai);
112             return VOID_OBSERVABLE.value();
113         });
114         vnfInfo.setName("vnfName");
115         //when
116         genericVnfManager.createOrUpdate(VNF_ID, true);
117         //verify
118         GenericVnf vnfSentToAai = payload.getValue();
119         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
120         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
121         assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
122         assertEquals(TRUE, vnfSentToAai.isInMaint());
123         assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
124         assertEquals("vnfName", vnfSentToAai.getVnfName());
125         verify(systemFunctions, times(10)).sleep(3000);
126         verify(networkApi, times(10)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
127         VOID_OBSERVABLE.assertCalled();
128     }
129
130     /**
131      * if the VNF exist it is updated
132      */
133     @Test
134     public void testUpdateExistingVnf() throws Exception {
135         GenericVnf vnfInAaai = new GenericVnf();
136         vnfInAaai.setResourceVersion("v1");
137         when(networkApi.getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(vnfInAaai));
138         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
139         when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
140         vnfInfo.setName("vnfName");
141         //when
142         genericVnfManager.createOrUpdate(VNF_ID, true);
143         //verify
144         GenericVnf vnfSentToAai = payload.getValue();
145         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
146         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
147         assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
148         assertEquals(TRUE, vnfSentToAai.isInMaint());
149         assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
150         assertEquals("vnfName", vnfSentToAai.getVnfName());
151         verify(systemFunctions, never()).sleep(anyLong());
152         VOID_OBSERVABLE.assertCalled();
153         verify(networkApi, times(1)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
154     }
155
156     /**
157      * error is propagated if unable to query VNF from CBAM
158      */
159     @Test
160     public void testUnableToQueryVnfFromCBAM() throws Exception {
161         GenericVnf vnfInAaai = new GenericVnf();
162         vnfInAaai.setResourceVersion("v1");
163         when(networkApi.getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(vnfInAaai));
164         RuntimeException expectedException = new RuntimeException();
165         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
166         when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
167             vnfInAaai.setResourceVersion("v2");
168             return null;
169         });
170         vnfInfo.setName("vnfName");
171         //when
172         try {
173             genericVnfManager.createOrUpdate(VNF_ID, true);
174         } catch (Exception e) {
175             verify(logger).error("Unable to query VNF with myVnfId identifier from CBAM", expectedException);
176             assertEquals("Unable to query VNF with myVnfId identifier from CBAM", e.getMessage());
177         }
178     }
179
180     /**
181      * if the VNF is created after the last attempt to query VNF, but before the
182      * the driver creates the VNF it is not created but updated
183      */
184     @Test
185     public void testConcurency1() throws Exception {
186         GenericVnf vnfInAaai = new GenericVnf();
187         vnfInAaai.setResourceVersion("v3");
188         Set<Integer> queryCount = new HashSet<>();
189         when(networkApi.getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)).thenAnswer((Answer<Observable>) invocationOnMock -> {
190             queryCount.add(queryCount.size());
191             if (queryCount.size() >= 11) {
192                 return buildObservable(vnfInAaai);
193             }
194             throw new NoSuchElementException();
195         });
196         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
197         RuntimeException runtimeException = new RuntimeException();
198         when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
199             GenericVnf vnfSentToAAi = (GenericVnf) invocation.getArguments()[1];
200             if (vnfSentToAAi.getResourceVersion() == null) {
201                 throw runtimeException;
202             }
203             return VOID_OBSERVABLE.value();
204         });
205         vnfInfo.setName("vnfName");
206         //when
207         genericVnfManager.createOrUpdate(VNF_ID, true);
208         //verify
209         GenericVnf vnfSentToAai = payload.getValue();
210         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
211         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
212         assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
213         assertEquals(TRUE, vnfSentToAai.isInMaint());
214         assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
215         assertEquals("vnfName", vnfSentToAai.getVnfName());
216         assertEquals("v3", vnfSentToAai.getResourceVersion());
217         verify(systemFunctions, times(10)).sleep(3000);
218         verify(networkApi, times(11)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
219         verify(networkApi, times(2)).createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), any());
220         verify(logger).warn(eq("The VNF with myVnfId identifier did not appear in time"), any(NoSuchElementException.class));
221         verify(logger).warn("The VNF with myVnfId identifier has been created since after the maximal wait for VNF to appear timeout", runtimeException);
222         VOID_OBSERVABLE.assertCalled();
223     }
224
225     /**
226      * test how entities can refer to a VNF
227      */
228     @Test
229     public void testRelations() {
230         //when
231         Relationship relationship = GenericVnfManager.linkTo(VNF_ID);
232         //verify
233         assertEquals("generic-vnf", relationship.getRelatedTo());
234         assertEquals(1, relationship.getRelationshipData().size());
235         assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
236         assertEquals(VNF_ID, relationship.getRelationshipData().get(0).getRelationshipValue());
237     }
238
239     /**
240      * test inheritence
241      */
242     @Test
243     public void testInheritence() {
244         assertEquals(logger, genericVnfManager.getLogger());
245     }
246 }