Refactor Junit test code to remove duplication
[aai/babel.git] / src / test / java / org / onap / aai / babel / csar / vnfcatalog / VnfVendorImageExtractorTest.java
index de5ea3f..b1229b4 100644 (file)
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.onap.aai.babel.csar.vnfcatalog;
 
 import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertThat;
@@ -29,10 +31,11 @@ import java.io.IOException;
 import org.junit.Test;
 import org.onap.aai.babel.service.data.BabelArtifact;
 import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
+import org.onap.aai.babel.testdata.CsarTest;
 import org.onap.aai.babel.util.ArtifactTestUtils;
 
 /**
- * Tests {@link VnfVendorImageExtractor}
+ * Tests {@link VnfVendorImageExtractor}.
  */
 public class VnfVendorImageExtractorTest {
 
@@ -48,29 +51,37 @@ public class VnfVendorImageExtractorTest {
 
     @Test(expected = ToscaToCatalogException.class)
     public void createVendorImageMappingsInvalidCsarFile() throws IOException, ToscaToCatalogException {
-        extractArtifact("noYmlFilesArchive.zip");
+        CsarTest.NO_YAML_FILES.extractVnfVendorImages();
     }
 
     @Test(expected = ToscaToCatalogException.class)
     public void createVendorImageMappingsInvalidFile() throws IOException, ToscaToCatalogException {
-        extractArtifact("Duff.txt");
+        new VnfVendorImageExtractor().extract("not a real file".getBytes());
+    }
+
+    @Test
+    public void createVendorImageMappingsMoreThanOneVnfConfigurationExists() throws IOException {
+        try {
+            CsarTest.MULTIPLE_VNF_CSAR.extractArtifacts();
+        } catch (Exception e) {
+            assertThat(e, is(instanceOf(ToscaToCatalogException.class)));
+            assertThat(e.getLocalizedMessage(),
+                    is(equalTo("An error occurred trying to get the vnf catalog from a csar file. "
+                            + "2 vnfConfigurations were found in the csar file and only one is allowed.")));
+        }
     }
 
     @Test
     public void createVendorImageMappingsNoVnfConfigurationExists() throws IOException, ToscaToCatalogException {
-        assertThat(extractArtifact("noVnfConfiguration.csar"), is(nullValue()));
+        assertThat(CsarTest.NO_VNF_CONFIG_CSAR.extractVnfVendorImages(), is(nullValue()));
     }
 
     @Test
     public void createVendorImageMappingsValidFile() throws IOException, ToscaToCatalogException {
-        BabelArtifact artifact = extractArtifact("catalog_csar.csar");
+        BabelArtifact artifact = CsarTest.VNF_VENDOR_CSAR.extractVnfVendorImages();
         assertThat(artifact.getName(), is(equalTo("vnfVendorImageConfigurations")));
         assertThat(artifact.getType(), is(equalTo(ArtifactType.VNFCATALOG)));
         assertThat(artifact.getPayload(),
                 is(equalTo(new ArtifactTestUtils().getRequestJson("vnfVendorImageConfigurations.json"))));
     }
-
-    private BabelArtifact extractArtifact(String artifactName) throws ToscaToCatalogException, IOException {
-        return new VnfVendorImageExtractor().extract(new ArtifactTestUtils().getCompressedArtifact(artifactName));
-    }
 }