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