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 / restapi / TestConverterApi.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
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi;
18
19 import com.google.common.collect.Lists;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.PrintStream;
24 import java.util.Arrays;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.Part;
27 import org.apache.http.entity.ContentType;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.CbamVnfPackageBuilder;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.CbamVnfdBuilder;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfdBuilder;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.mock.web.DelegatingServletOutputStream;
41
42 import static junit.framework.TestCase.*;
43 import static org.mockito.Matchers.any;
44 import static org.mockito.Matchers.eq;
45 import static org.mockito.Mockito.verify;
46 import static org.mockito.Mockito.when;
47 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
48 import static org.springframework.test.util.ReflectionTestUtils.setField;
49
50
51 public class TestConverterApi extends TestBase {
52
53     @InjectMocks
54     private ConverterApi converterApi;
55     @Mock
56     private HttpServletRequest httpRequest;
57
58     @Before
59     public void initMocks() throws Exception {
60         setField(ConverterApi.class, "logger", logger);
61     }
62
63     /**
64      * test VNF package conversion success scenario
65      */
66     @Test
67     public void testConversion() throws Exception {
68         ByteArrayOutputStream bos = new ByteArrayOutputStream();
69         PrintStream actualOut = new PrintStream(bos, true);
70         when(systemFunctions.out()).thenReturn(actualOut);
71         when(systemFunctions.loadFile("cbam.pre.collectConnectionPoints.js")).thenCallRealMethod();
72         when(systemFunctions.loadFile("cbam.collectConnectionPoints.js")).thenCallRealMethod();
73         when(systemFunctions.loadFile("cbam.post.collectConnectionPoints.js")).thenCallRealMethod();
74         when(systemFunctions.loadFile("TOSCA.meta")).thenCallRealMethod();
75         when(systemFunctions.loadFile("MainServiceTemplate.meta")).thenCallRealMethod();
76         when(httpResponse.getOutputStream()).thenReturn(new DelegatingServletOutputStream(actualOut));
77         Part part = Mockito.mock(Part.class);
78         when(part.getInputStream()).thenReturn(new ByteArrayInputStream(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip")));
79         when(httpRequest.getParts()).thenReturn(Lists.newArrayList(part));
80         //when
81         converterApi.convert(httpResponse, httpRequest);
82         //verify
83         verifyVnfPackageWritterToOutputStream(bos);
84         verify(httpResponse).addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_OCTET_STREAM.getMimeType());
85         verify(httpResponse).setStatus(HttpStatus.OK.value());
86         verify(httpResponse).addHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(bos.toByteArray().length));
87         verify(httpResponse).addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "core.csar" + "\"");
88     }
89
90     private void verifyVnfPackageWritterToOutputStream(ByteArrayOutputStream bos) throws Exception {
91         String cbamVnfd = new String(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip.vnfd"));
92         String expectedOnapVnfd = new OnapVnfdBuilder().toOnapVnfd(cbamVnfd);
93         assertFileInZip(bos.toByteArray(), "TOSCA-Metadata/TOSCA.meta", TestUtil.loadFile("TOSCA.meta"));
94         assertFileInZip(bos.toByteArray(), "Definitions/MainServiceTemplate.yaml", expectedOnapVnfd.getBytes());
95         assertFileInZip(bos.toByteArray(), "MainServiceTemplate.yaml", expectedOnapVnfd.getBytes());
96         assertFileInZip(bos.toByteArray(), "MainServiceTemplate.meta", TestUtil.loadFile("MainServiceTemplate.meta"));
97         ByteArrayOutputStream actualModifiedCbamVnfPackage = getFileInZip(new ByteArrayInputStream(bos.toByteArray()), "Artifacts/Deployment/OTHER/cbam.package.zip");
98         byte[] expectedModifiedCbamPackage = new CbamVnfPackageBuilder().toModifiedCbamVnfPackage(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip"), "vnfdloc/a.yaml", new CbamVnfdBuilder().build(cbamVnfd));
99         assertItenticalZips(expectedModifiedCbamPackage, actualModifiedCbamVnfPackage.toByteArray());
100     }
101
102     /**
103      * the HTML based converted page works
104      */
105     @Test
106     public void testConverterPage() throws Exception {
107         ByteArrayOutputStream bos = new ByteArrayOutputStream();
108         PrintStream actualOut = new PrintStream(bos, true);
109         when(httpResponse.getOutputStream()).thenReturn(new DelegatingServletOutputStream(actualOut));
110         when(systemFunctions.loadFile("upload.html")).thenCallRealMethod();
111         //when
112         converterApi.getUploadPageForConvertingVnfd(httpResponse);
113         //verify
114         assertTrue(Arrays.equals(TestUtil.loadFile("upload.html"), bos.toByteArray()));
115         verify(httpResponse).addHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(bos.toByteArray().length));
116     }
117
118     /**
119      * error is propagated if unable to extract package from HTTP request
120      */
121     @Test
122     public void testUnableToExtractPackageToBeConverted() throws Exception {
123         IOException expectedException = new IOException();
124         when(httpRequest.getParts()).thenThrow(expectedException);
125         try {
126             converterApi.convert(httpResponse, httpRequest);
127             fail();
128         } catch (Exception e) {
129             verify(logger).error("Unable to extract package from REST parameters", expectedException);
130             assertEquals("Unable to extract package from REST parameters", e.getMessage());
131             assertEquals(expectedException, e.getCause());
132         }
133     }
134
135     /**
136      * error is propagated if unable to extract package from HTTP request
137      */
138     @Test
139     public void testUnableToConvertPackage() throws Exception {
140         Part part = Mockito.mock(Part.class);
141         when(part.getInputStream()).thenReturn(new ByteArrayInputStream(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip")));
142         when(httpRequest.getParts()).thenReturn(Lists.newArrayList(part));
143         try {
144             converterApi.convert(httpResponse, httpRequest);
145             fail();
146         } catch (Exception e) {
147             verify(logger).error(eq("Unable to convert VNF package"), any(RuntimeException.class));
148             assertEquals("Unable to convert VNF package", e.getMessage());
149         }
150     }
151 }