Removing jackson to mitigate cve-2017-4995
[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 null;
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     }
128
129     /**
130      * if the VNF exist it is updated
131      */
132     @Test
133     public void testUpdateExistingVnf() throws Exception {
134         GenericVnf vnfInAaai = new GenericVnf();
135         vnfInAaai.setResourceVersion("v1");
136         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));
137         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
138         when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenReturn(null);
139         vnfInfo.setName("vnfName");
140         //when
141         genericVnfManager.createOrUpdate(VNF_ID, true);
142         //verify
143         GenericVnf vnfSentToAai = payload.getValue();
144         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
145         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
146         assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
147         assertEquals(TRUE, vnfSentToAai.isInMaint());
148         assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
149         assertEquals("vnfName", vnfSentToAai.getVnfName());
150         verify(systemFunctions, never()).sleep(anyLong());
151         verify(networkApi, times(1)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
152     }
153
154     /**
155      * error is propagated if unable to query VNF from CBAM
156      */
157     @Test
158     public void testUnableToQueryVnfFromCBAM() throws Exception {
159         GenericVnf vnfInAaai = new GenericVnf();
160         vnfInAaai.setResourceVersion("v1");
161         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));
162         RuntimeException expectedException = new RuntimeException();
163         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
164         when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
165             vnfInAaai.setResourceVersion("v2");
166             return null;
167         });
168         vnfInfo.setName("vnfName");
169         //when
170         try {
171             genericVnfManager.createOrUpdate(VNF_ID, true);
172         } catch (Exception e) {
173             verify(logger).error("Unable to query VNF with myVnfId identifier from CBAM", expectedException);
174             assertEquals("Unable to query VNF with myVnfId identifier from CBAM", e.getMessage());
175         }
176     }
177
178     /**
179      * if the VNF is created after the last attempt to query VNF, but before the
180      * the driver creates the VNF it is not created but updated
181      */
182     @Test
183     public void testConcurency1() throws Exception {
184         GenericVnf vnfInAaai = new GenericVnf();
185         vnfInAaai.setResourceVersion("v3");
186         Set<Integer> queryCount = new HashSet<>();
187         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 -> {
188             queryCount.add(queryCount.size());
189             if (queryCount.size() >= 11) {
190                 return buildObservable(vnfInAaai);
191             }
192             throw new NoSuchElementException();
193         });
194         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
195         RuntimeException runtimeException = new RuntimeException();
196         when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
197             GenericVnf vnfSentToAAi = (GenericVnf) invocation.getArguments()[1];
198             if (vnfSentToAAi.getResourceVersion() == null) {
199                 throw runtimeException;
200             }
201             return null;
202         });
203         vnfInfo.setName("vnfName");
204         //when
205         genericVnfManager.createOrUpdate(VNF_ID, true);
206         //verify
207         GenericVnf vnfSentToAai = payload.getValue();
208         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
209         assertEquals(VNF_ID, vnfSentToAai.getVnfId());
210         assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
211         assertEquals(TRUE, vnfSentToAai.isInMaint());
212         assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
213         assertEquals("vnfName", vnfSentToAai.getVnfName());
214         assertEquals("v3", vnfSentToAai.getResourceVersion());
215         verify(systemFunctions, times(10)).sleep(3000);
216         verify(networkApi, times(11)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
217         verify(networkApi, times(2)).createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), any());
218         verify(logger).warn(eq("The VNF with myVnfId identifier did not appear in time"), any(NoSuchElementException.class));
219         verify(logger).warn("The VNF with myVnfId identifier has been created since after the maximal wait for VNF to appear timeout", runtimeException);
220     }
221
222     /**
223      * test how entities can refer to a VNF
224      */
225     @Test
226     public void testRelations() {
227         //when
228         Relationship relationship = GenericVnfManager.linkTo(VNF_ID);
229         //verify
230         assertEquals("generic-vnf", relationship.getRelatedTo());
231         assertEquals(1, relationship.getRelationshipData().size());
232         assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
233         assertEquals(VNF_ID, relationship.getRelationshipData().get(0).getRelationshipValue());
234     }
235
236     /**
237      * test inheritence
238      */
239     @Test
240     public void testInheritence() {
241         assertEquals(logger, genericVnfManager.getLogger());
242     }
243 }