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