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
index 9ac3891..8f8cc8c 100644 (file)
 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi;
 
 import com.google.common.collect.Lists;
-import junit.framework.TestCase;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Arrays;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.Part;
 import org.apache.http.entity.ContentType;
 import org.junit.Before;
 import org.junit.Test;
@@ -33,13 +39,9 @@ import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.mock.web.DelegatingServletOutputStream;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.Part;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
-import java.util.Arrays;
-
+import static junit.framework.TestCase.*;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
@@ -48,7 +50,6 @@ import static org.springframework.test.util.ReflectionTestUtils.setField;
 
 public class TestConverterApi extends TestBase {
 
-
     @InjectMocks
     private ConverterApi converterApi;
     @Mock
@@ -60,9 +61,10 @@ public class TestConverterApi extends TestBase {
     }
 
     /**
+     * test VNF package conversion success scenario
      */
     @Test
-    public void test() throws Exception {
+    public void testConversion() throws Exception {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         PrintStream actualOut = new PrintStream(bos, true);
         when(systemFunctions.out()).thenReturn(actualOut);
@@ -97,8 +99,11 @@ public class TestConverterApi extends TestBase {
         assertItenticalZips(expectedModifiedCbamPackage, actualModifiedCbamVnfPackage.toByteArray());
     }
 
+    /**
+     * the HTML based converted page works
+     */
     @Test
-    public void testDownloaderPage() throws Exception {
+    public void testConverterPage() throws Exception {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         PrintStream actualOut = new PrintStream(bos, true);
         when(httpResponse.getOutputStream()).thenReturn(new DelegatingServletOutputStream(actualOut));
@@ -106,9 +111,41 @@ public class TestConverterApi extends TestBase {
         //when
         converterApi.getUploadPageForConvertingVnfd(httpResponse);
         //verify
-        TestCase.assertTrue(Arrays.equals(TestUtil.loadFile("upload.html"), bos.toByteArray()));
+        assertTrue(Arrays.equals(TestUtil.loadFile("upload.html"), bos.toByteArray()));
         verify(httpResponse).addHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(bos.toByteArray().length));
+    }
 
+    /**
+     * error is propagated if unable to extract package from HTTP request
+     */
+    @Test
+    public void testUnableToExtractPackageToBeConverted() throws Exception {
+        IOException expectedException = new IOException();
+        when(httpRequest.getParts()).thenThrow(expectedException);
+        try {
+            converterApi.convert(httpResponse, httpRequest);
+            fail();
+        } catch (Exception e) {
+            verify(logger).error("Unable to extract package from REST parameters", expectedException);
+            assertEquals("Unable to extract package from REST parameters", e.getMessage());
+            assertEquals(expectedException, e.getCause());
+        }
     }
 
+    /**
+     * error is propagated if unable to extract package from HTTP request
+     */
+    @Test
+    public void testUnableToConvertPackage() throws Exception {
+        Part part = Mockito.mock(Part.class);
+        when(part.getInputStream()).thenReturn(new ByteArrayInputStream(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip")));
+        when(httpRequest.getParts()).thenReturn(Lists.newArrayList(part));
+        try {
+            converterApi.convert(httpResponse, httpRequest);
+            fail();
+        } catch (Exception e) {
+            verify(logger).error(eq("Unable to convert VNF package"), any(RuntimeException.class));
+            assertEquals("Unable to convert VNF package", e.getMessage());
+        }
+    }
 }