Merge "Add vfc-ztevnfmdriver logDig check"
[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 java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import okhttp3.Headers;
25 import okhttp3.RequestBody;
26 import okhttp3.ResponseBody;
27 import okhttp3.internal.http.RealResponseBody;
28 import okio.Buffer;
29 import okio.BufferedSource;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IPackageProvider;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
39 import retrofit2.Call;
40
41 import static junit.framework.TestCase.*;
42 import static org.junit.Assert.assertArrayEquals;
43 import static org.mockito.Mockito.*;
44 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
45 import static org.springframework.test.util.ReflectionTestUtils.setField;
46
47 public class TestCbamCatalogManager extends TestBase {
48
49     private static final String CSAR_ID = "csarId";
50     private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID";
51     private CatalogManager cbamCatalogManager;
52     @Mock
53     private IPackageProvider packageProvider;
54
55     private List<CatalogAdapterVnfpackage> existingVnfPackages = new ArrayList<>();
56     private ArgumentCaptor<RequestBody> uploadedFile = ArgumentCaptor.forClass(RequestBody.class);
57
58     @Before
59     public void initMocks() throws Exception {
60         setField(CatalogManager.class, "logger", logger);
61         Call<List<CatalogAdapterVnfpackage>> value = buildCall(existingVnfPackages);
62         when(cbamCatalogApi.list()).thenReturn(value);
63         cbamCatalogManager = new CatalogManager(cbamRestApiProvider, packageProvider);
64     }
65
66     /**
67      * the package is transferred from source to CBAM catalog
68      */
69     @Test
70     public void testPackageTransfer() throws Exception {
71         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
72         existingPackage.setVnfdId("unknownId");
73         existingVnfPackages.add(existingPackage);
74         CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
75         createdPackage.setVnfdId(CBAM_VNFD_ID);
76         Call<CatalogAdapterVnfpackage> catalogAdapterVnfpackageCall = buildCall(createdPackage);
77         when(cbamCatalogApi.create(uploadedFile.capture())).thenReturn(catalogAdapterVnfpackageCall);
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 = getContent(uploadedFile.getValue());
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                 Call<CatalogAdapterVnfpackage> catalogAdapterVnfpackageCall = buildCall(createdPackage);
110                 when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(catalogAdapterVnfpackageCall);
111                 throw can_not_upload_package;
112             }
113         });
114         //when
115         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
116         //verify
117         //the correct portion of the package is extracted and uploaded to CBAM
118         byte[] expectedContentToUpload = getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray();
119         assertTrue(Arrays.equals(expectedContentToUpload, getContent(uploadedFile.getValue())));
120         assertEquals(createdPackage, cbamPackage);
121         verify(logger).debug("Probably concurrent package uploads", can_not_upload_package);
122     }
123
124     /**
125      * If the package already exists in CBAM catalog it is not re-uploaded
126      */
127     @Test
128     public void testIdempotentPackageUpload() throws Exception {
129         CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
130         createdPackage.setVnfdId(CBAM_VNFD_ID);
131         when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() {
132             @Override
133             public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
134                 return createdPackage;
135             }
136         });
137         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
138         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
139         existingPackage.setVnfdId(CBAM_VNFD_ID);
140         existingVnfPackages.add(existingPackage);
141         Call<CatalogAdapterVnfpackage> catalogAdapterVnfpackageCall = buildCall(existingPackage);
142         when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(catalogAdapterVnfpackageCall);
143         //when
144         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
145         //verify
146         verify(cbamCatalogApi, never()).create(Mockito.any());
147         assertEquals(existingPackage, cbamPackage);
148         verify(packageProvider, never()).getPackage(CSAR_ID);
149     }
150
151     /**
152      * failure to list package in CBAM results in error
153      */
154     @Test
155     public void testFailureToListVnfPackagesInCbam() throws Exception {
156         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
157         RuntimeException expectedException = new RuntimeException();
158         when(cbamCatalogApi.list()).thenThrow(expectedException);
159         //when
160         try {
161             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
162             fail();
163         } catch (Exception e) {
164             verify(logger).error("Unable to determine if the VNF package has been replicated in CBAM", expectedException);
165             assertEquals(expectedException, e.getCause());
166         }
167     }
168
169     /**
170      * failure to query package from CBAM results in error
171      */
172     @Test
173     public void testFailureToQueryVnfPackagesFromCbam() throws Exception {
174         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
175         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
176         existingPackage.setVnfdId(CBAM_VNFD_ID);
177         existingVnfPackages.add(existingPackage);
178         RuntimeException expectedException = new RuntimeException();
179         when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenThrow(expectedException);
180         //when
181         try {
182             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
183             fail();
184         } catch (Exception e) {
185             verify(logger).error("Unable to query VNF package with CBAM_VNFD_ID from CBAM", expectedException);
186             assertEquals(expectedException, e.getCause());
187         }
188     }
189
190     /**
191      * failure to create package in CBAM results in error
192      */
193     @Test
194     public void testFailureToCreatePackageInCbam() throws Exception {
195         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
196         existingPackage.setVnfdId("unknownId");
197         existingVnfPackages.add(existingPackage);
198         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
199         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
200         when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
201         RuntimeException expectedException = new RuntimeException();
202         when(cbamCatalogApi.create(Mockito.any())).thenThrow(expectedException);
203         try {
204             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
205             fail();
206         } catch (Exception e) {
207             verify(logger).error("Unable to create VNF with csarId CSAR identifier in package in CBAM", expectedException);
208             assertEquals(expectedException, e.getCause());
209         }
210     }
211
212     /**
213      * the VNFD is extracted from zip
214      */
215     @Test
216     public void testExtractVnfdFromPackage() throws Exception {
217         Call<ResponseBody> responseBodyCall = buildCall(buildResponse(TestUtil.loadFile("unittests/cbam.package.zip")));
218         when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(responseBodyCall);
219         //when
220         String content = cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
221         //verify
222         assertEquals("dummy vnfd\n", content);
223     }
224
225     /**
226      * if VNFD the Tosca meta can not be extracted sensible error is returned
227      */
228     @Test
229     public void testEmptyCbamPackage() throws Exception {
230         Call<ResponseBody> responseBodyCall = buildCall(buildResponse(TestUtil.loadFile("unittests/empty.zip")));
231         when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(responseBodyCall);
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         byte[] bytes = TestUtil.loadFile("unittests/missing.vnfd.zip");
248         Call<ResponseBody> response = buildCall(buildResponse(bytes));
249         when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(response);
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
262     private ResponseBody buildResponse(byte[] content) throws IOException {
263         Headers headers = new Headers.Builder().build();
264         Buffer buffer = new Buffer();
265         buffer.write(content);
266         BufferedSource response = buffer;
267         return new RealResponseBody(headers, response);
268     }
269 }