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