Merge "Enable vfc-ztevnfmdriver sonar scan"
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / TestCbamCatalogManager.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.vnfm;
17
18 import com.nokia.cbam.catalog.v1.model.CatalogAdapterVnfpackage;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IPackageProvider;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.File;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.UUID;
37
38 import static junit.framework.TestCase.*;
39 import static org.junit.Assert.assertArrayEquals;
40 import static org.mockito.Mockito.*;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
42 import static org.springframework.test.util.ReflectionTestUtils.setField;
43
44 public class TestCbamCatalogManager extends TestBase {
45
46     private static final String CSAR_ID = "csarId";
47     private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID";
48     private CatalogManager cbamCatalogManager;
49     @Mock
50     private IPackageProvider packageProvider;
51
52     private List<CatalogAdapterVnfpackage> existingVnfPackages = new ArrayList<>();
53     private ArgumentCaptor<File> uploadedFile = ArgumentCaptor.forClass(File.class);
54
55     @Before
56     public void initMocks() throws Exception {
57         setField(CatalogManager.class, "logger", logger);
58         when(cbamCatalogApi.list()).thenReturn(existingVnfPackages);
59         cbamCatalogManager = new CatalogManager(cbamRestApiProvider, packageProvider);
60     }
61
62     /**
63      * the package is transferred from source to CBAM catalog
64      */
65     @Test
66     public void testPackageTransfer() throws Exception {
67         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
68         existingPackage.setVnfdId("unknownId");
69         existingVnfPackages.add(existingPackage);
70         CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
71         createdPackage.setVnfdId(CBAM_VNFD_ID);
72         when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() {
73             @Override
74             public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
75                 return createdPackage;
76             }
77         });
78         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
79         when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
80         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
81         //when
82         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
83         //verify
84         byte[] a2 = Files.readAllBytes(uploadedFile.getValue().toPath());
85         assertArrayEquals(getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray(), a2);
86         assertEquals(createdPackage, cbamPackage);
87     }
88
89     /**
90      * the package is transfer fails, but the package has been uploaded (possibly by other thread / work flow)
91      * the transfer succeeds
92      */
93     @Test
94     public void testPackageTransferConcurrency() throws Exception {
95         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
96         existingPackage.setVnfdId("unknownId");
97         existingVnfPackages.add(existingPackage);
98         CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
99         createdPackage.setVnfdId(CBAM_VNFD_ID);
100         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
101         when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
102         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
103         RuntimeException can_not_upload_package = new RuntimeException("Can not upload package");
104         when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() {
105             @Override
106             public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
107                 //this is done by an other thread
108                 existingVnfPackages.add(createdPackage);
109                 when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(createdPackage);
110                 throw can_not_upload_package;
111             }
112         });
113         //when
114         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
115         //verify
116         //the correct portion of the package is extracted and uploaded to CBAM
117         byte[] expectedContentToUpload = getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray();
118         assertTrue(Arrays.equals(expectedContentToUpload, Files.readAllBytes(uploadedFile.getValue().toPath())));
119         assertEquals(createdPackage, cbamPackage);
120         verify(logger).debug("Probably concurrent package uploads", can_not_upload_package);
121     }
122
123     /**
124      * If the package already exists in CBAM catalog it is not re-uploaded
125      */
126     @Test
127     public void testIdempotentPackageUpload() throws Exception {
128         CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
129         createdPackage.setVnfdId(CBAM_VNFD_ID);
130         when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() {
131             @Override
132             public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
133                 return createdPackage;
134             }
135         });
136         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
137         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
138         existingPackage.setVnfdId(CBAM_VNFD_ID);
139         existingVnfPackages.add(existingPackage);
140         when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(existingPackage);
141         //when
142         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
143         //verify
144         verify(cbamCatalogApi, never()).create(Mockito.any());
145         assertEquals(existingPackage, cbamPackage);
146         verify(packageProvider, never()).getPackage(CSAR_ID);
147     }
148
149     /**
150      * failure to list package in CBAM results in error
151      */
152     @Test
153     public void testFailureToListVnfPackagesInCbam() throws Exception {
154         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
155         com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException();
156         when(cbamCatalogApi.list()).thenThrow(expectedException);
157         //when
158         try {
159             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
160             fail();
161         } catch (Exception e) {
162             verify(logger).error("Unable to determine if the VNF package has been replicated in CBAM", expectedException);
163             assertEquals(expectedException, e.getCause());
164         }
165     }
166
167     /**
168      * failure to query package from CBAM results in error
169      */
170     @Test
171     public void testFailureToQueryVnfPackagesFromCbam() throws Exception {
172         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
173         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
174         existingPackage.setVnfdId(CBAM_VNFD_ID);
175         existingVnfPackages.add(existingPackage);
176         com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException();
177         when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenThrow(expectedException);
178         //when
179         try {
180             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
181             fail();
182         } catch (Exception e) {
183             verify(logger).error("Unable to query VNF package with CBAM_VNFD_ID from CBAM", expectedException);
184             assertEquals(expectedException, e.getCause());
185         }
186     }
187
188     /**
189      * failure to create package in CBAM results in error
190      */
191     @Test
192     public void testFailureToCreatePackageInCbam() throws Exception {
193         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
194         existingPackage.setVnfdId("unknownId");
195         existingVnfPackages.add(existingPackage);
196         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
197         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
198         when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
199         com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException();
200         when(cbamCatalogApi.create(Mockito.any())).thenThrow(expectedException);
201         try {
202             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
203             fail();
204         } catch (Exception e) {
205             verify(logger).error("Unable to create VNF with csarId CSAR identifier in package in CBAM", expectedException);
206             assertEquals(expectedException, e.getCause());
207         }
208     }
209
210     /**
211      * the VNFD is extracted from zip
212      */
213     @Test
214     public void testExtractVnfdFromPackage() throws Exception {
215         Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar");
216         Files.write(csar, TestUtil.loadFile("unittests/cbam.package.zip"));
217         when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile());
218         //when
219         String content = cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
220         //verify
221         assertEquals("dummy vnfd\n", content);
222     }
223
224     /**
225      * if VNFD the Tosca meta can not be extracted sensible error is returned
226      */
227     @Test
228     public void testEmptyCbamPackage() throws Exception {
229         Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar");
230         Files.write(csar, TestUtil.loadFile("unittests/empty.zip"));
231         when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile());
232         //when
233         try {
234             cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
235             fail();
236         } catch (RuntimeException e) {
237             verify(logger).error("Unable to get package with (CBAM_VNFD_ID)", e.getCause());
238             assertEquals("Unable to find the TOSCA-Metadata/TOSCA.meta in archive found: []", e.getCause().getMessage());
239         }
240     }
241
242     /**
243      * if VNFD can not be extracted sensible error is returned
244      */
245     @Test
246     public void testMissingVnfdCbamPackage() throws Exception {
247         Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar");
248         Files.write(csar, TestUtil.loadFile("unittests/missing.vnfd.zip"));
249         when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile());
250         //when
251         try {
252             cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
253             fail();
254         } catch (RuntimeException e) {
255             verify(logger).error("Unable to get package with (" + CBAM_VNFD_ID + ")", e.getCause());
256             assertTrue("Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/, TOSCA-Metadata/TOSCA.meta]".equals(e.getCause().getMessage())
257                     || "Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/TOSCA.meta, TOSCA-Metadata/]".equals(e.getCause().getMessage())
258             );
259         }
260     }
261 }