diff options
Diffstat (limited to 'src/test/java')
6 files changed, 184 insertions, 121 deletions
diff --git a/src/test/java/org/onap/aai/babel/csar/extractor/YamlExtractorTest.java b/src/test/java/org/onap/aai/babel/csar/extractor/YamlExtractorTest.java index e01a8f0..abe6f4d 100644 --- a/src/test/java/org/onap/aai/babel/csar/extractor/YamlExtractorTest.java +++ b/src/test/java/org/onap/aai/babel/csar/extractor/YamlExtractorTest.java @@ -18,6 +18,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.aai.babel.csar.extractor; import static org.junit.Assert.assertEquals; @@ -28,6 +29,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.junit.Test; +import org.onap.aai.babel.testdata.CsarTest; import org.onap.aai.babel.util.ArtifactTestUtils; import org.onap.aai.babel.xml.generator.data.Artifact; @@ -95,7 +97,7 @@ public class YamlExtractorTest { @Test public void testArchiveContainsNoYmlFiles() throws IOException { try { - extractArchive("noYmlFilesArchive.zip"); + CsarTest.NO_YAML_FILES.extractArtifacts(); fail("An instance of InvalidArchiveException should have been thrown."); } catch (Exception e) { assertTrue("An instance of InvalidArchiveException should have been thrown.", @@ -108,7 +110,7 @@ public class YamlExtractorTest { @Test public void testArchiveContainsOnlyTheExpectedYmlFilesFromSdWanService() throws IOException, InvalidArchiveException { - final List<Artifact> ymlFiles = extractArchive("service-SdWanServiceTest-csar.csar"); + final List<Artifact> ymlFiles = CsarTest.SD_WAN_CSAR_FILE.extractArtifacts(); List<String> payloads = new ArrayList<>(); payloads.add("ymlFiles/resource-SdWanTestVsp-template.yml"); payloads.add("ymlFiles/resource-SdWanTestVsp-template-interface.yml"); @@ -130,10 +132,10 @@ public class YamlExtractorTest { /** * Call the extractor with the specified arguments and assert that an exception is thrown. * - * @param archive - * @param name - * @param version - * @param expectedErrorMessage + * @param archive the compressed archive in the form of a byte array, expected to contain one or more YAML files + * @param name the name of the archive + * @param version the version of the archive + * @param expectedErrorMessage the text of the InvalidArchiveException thrown by the extractor */ private void invalidArgumentsTest(byte[] archive, String name, String version, String expectedErrorMessage) { try { @@ -144,19 +146,4 @@ public class YamlExtractorTest { assertEquals(expectedErrorMessage, ex.getLocalizedMessage()); } } - - /** - * Extract Artifacts from the specified CSAR resource. - * - * @param resourceName - * the CSAR file - * @return the extracted artifacts - * @throws InvalidArchiveException - * @throws IOException - * for I/O errors - */ - private List<Artifact> extractArchive(String resourceName) throws InvalidArchiveException, IOException { - byte[] csar = new ArtifactTestUtils().getCompressedArtifact(resourceName); - return new YamlExtractor().extract(csar, resourceName, "v1"); - } } diff --git a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java index 49784c1..b1229b4 100644 --- a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java +++ b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java @@ -31,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 { @@ -50,18 +51,18 @@ 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 { - extractArtifact("catalog_csar_too_many_vnfConfigurations.csar"); + CsarTest.MULTIPLE_VNF_CSAR.extractArtifacts(); } catch (Exception e) { assertThat(e, is(instanceOf(ToscaToCatalogException.class))); assertThat(e.getLocalizedMessage(), @@ -72,19 +73,15 @@ public class VnfVendorImageExtractorTest { @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)); - } } diff --git a/src/test/java/org/onap/aai/babel/parser/TestToscaParser.java b/src/test/java/org/onap/aai/babel/parser/TestToscaParser.java index 93b3576..f340132 100644 --- a/src/test/java/org/onap/aai/babel/parser/TestToscaParser.java +++ b/src/test/java/org/onap/aai/babel/parser/TestToscaParser.java @@ -34,7 +34,7 @@ import java.util.Properties; import org.junit.Before; import org.junit.Test; import org.onap.aai.babel.csar.extractor.InvalidArchiveException; -import org.onap.aai.babel.csar.extractor.YamlExtractor; +import org.onap.aai.babel.testdata.CsarTest; import org.onap.aai.babel.util.ArtifactTestUtils; import org.onap.aai.babel.xml.generator.api.AaiArtifactGenerator; import org.onap.aai.babel.xml.generator.data.AdditionalParams; @@ -59,6 +59,7 @@ public class TestToscaParser { public void setup() throws FileNotFoundException, IOException { System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE, new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG)); + InputStream in = TestToscaParser.class.getClassLoader().getResourceAsStream("artifact-generator.properties"); Properties properties = new Properties(); properties.load(in); @@ -68,15 +69,13 @@ public class TestToscaParser { @Test public void testParserWithCsarFile() throws IOException, InvalidArchiveException { - String csarResourceName = "catalog_csar.csar"; - byte[] csarBytes = new ArtifactTestUtils().getCompressedArtifact(csarResourceName); - List<Artifact> ymlFiles = new YamlExtractor().extract(csarBytes, csarResourceName, "1.0"); - + List<Artifact> ymlFiles = CsarTest.VNF_VENDOR_CSAR.extractArtifacts(); Map<String, String> additionalParams = new HashMap<>(); additionalParams.put(AdditionalParams.SERVICE_VERSION.getName(), "1.0"); AaiArtifactGenerator generator = new AaiArtifactGenerator(); - GenerationData data = generator.generateArtifact(csarBytes, ymlFiles, additionalParams); + GenerationData data = generator.generateArtifact(CsarTest.VNF_VENDOR_CSAR.getContent(), ymlFiles, + additionalParams); assertThat(data.getErrorData().size(), is(equalTo(0))); assertThat(data.getResultData().size(), is(equalTo(2))); diff --git a/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java b/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java index 9cfdf9b..67539bb 100644 --- a/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java +++ b/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java @@ -21,6 +21,8 @@ package org.onap.aai.babel.service; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import java.io.IOException; @@ -40,6 +42,7 @@ import org.onap.aai.babel.csar.CsarConverterException; import org.onap.aai.babel.csar.CsarToXmlConverter; import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser; import org.onap.aai.babel.service.data.BabelArtifact; +import org.onap.aai.babel.testdata.CsarTest; import org.onap.aai.babel.util.ArtifactTestUtils; import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; @@ -58,36 +61,6 @@ public class CsarToXmlConverterTest { } } - private enum CsarTest { - VALID_CSAR_FILE( - "service-SdWanServiceTest-csar.csar" - ), - MISSING_METADATA_CSAR( - "service-MissingMetadataTest.csar" - ), - NO_YAML_FILES( - "noYmlFilesArchive.zip" - ), - PORT_MIRROR_CSAR( - "service_PortMirror.csar" - ); - - private String filename; - private ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); - - CsarTest(String filename) { - this.filename = filename; - } - - public String getName() { - return filename; - } - - public byte[] getContent() throws IOException { - return artifactTestUtils.getCompressedArtifact(filename); - } - } - // The class to be tested. private CsarToXmlConverter converter; @@ -113,12 +86,12 @@ public class CsarToXmlConverterTest { @Test(expected = NullPointerException.class) public void testMissingName() throws CsarConverterException, IOException { - converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), null, null); + converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), null, null); } @Test(expected = NullPointerException.class) public void testMissingVersion() throws CsarConverterException, IOException { - converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null); + converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null); } @Test(expected = CsarConverterException.class) @@ -128,18 +101,28 @@ public class CsarToXmlConverterTest { @Test(expected = CsarConverterException.class) public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException { - converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), "noYmlFilesArchive.zip", SERVICE_VERSION); + converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), CsarTest.NO_YAML_FILES.getName(), + SERVICE_VERSION); } + /** + * Test that an Exception is thrown when the Artifact Generator properties are not present. + * + * @throws CsarConverterException if there is an error either extracting the YAML files or generating XML artifacts + * @throws IOException if an I/O exception occurs loading the test CSAR file + */ @Test - public void testArtifactGeneratorConfigMissing() - throws IOException, XmlArtifactGenerationException, CsarConverterException { + public void testArtifactGeneratorConfigMissing() throws CsarConverterException, IOException { exception.expect(CsarConverterException.class); - exception.expectMessage("Cannot generate artifacts. artifactgenerator.config system property not configured"); + exception.expectMessage( + "An error occurred trying to generate XML files from a collection of YAML files :" + + " org.onap.aai.babel.xml.generator.XmlArtifactGenerationException: " + + "Error occurred during artifact generation: " + + "{AAI=[Cannot generate artifacts. artifactgenerator.config system property not configured]}"); // Unset the required system property System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE); - converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), CsarTest.VALID_CSAR_FILE.getName(), + converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(), SERVICE_VERSION); } @@ -151,26 +134,22 @@ public class CsarToXmlConverterTest { } @Test - public void generateXmlFromCsar() throws CsarConverterException, IOException, XmlArtifactGenerationException { - Map<String, String> expectedXmlFiles = createExpectedXmlFiles(); - List<BabelArtifact> generatedArtifacts = converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), - CsarTest.VALID_CSAR_FILE.getName(), SERVICE_VERSION); - - generatedArtifacts - .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content", - ga.getPayload(), matches(expectedXmlFiles.get(ga.getName())))); + public void generateXmlFromSdWanCsar() throws IOException, CsarConverterException { + List<String> filesToLoad = new ArrayList<>(); + filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml"); + filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml"); + filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml"); + filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml"); + assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SD_WAN_CSAR_FILE); } @Test public void generatePortMirrorConfigurationModel() throws CsarConverterException, IOException, XmlArtifactGenerationException { - Map<String, String> expectedXmlFiles = createExpectedXmlFiles(); - List<BabelArtifact> generatedArtifacts = converter.generateXmlFromCsar(CsarTest.PORT_MIRROR_CSAR.getContent(), - CsarTest.PORT_MIRROR_CSAR.getName(), SERVICE_VERSION); - - generatedArtifacts - .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content", - ga.getPayload(), matches(expectedXmlFiles.get(ga.getName())))); + List<String> filesToLoad = new ArrayList<>(); + filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml"); + filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml"); + assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.PORT_MIRROR_CSAR); } public Matcher<String> matches(final String expected) { @@ -189,21 +168,22 @@ public class CsarToXmlConverterTest { }; } - private Map<String, String> createExpectedXmlFiles() throws IOException { + private Map<String, String> createExpectedXmlFiles(List<String> filesToLoad) throws IOException { Map<String, String> xmlMap = new HashMap<>(); - - List<String> filesToLoad = new ArrayList<>(); - filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml"); - filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml"); - filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml"); - filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml"); - filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml"); - filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml"); - for (String filename : filesToLoad) { xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename)); } - return xmlMap; } + + private void assertThatGeneratedFilesMatchExpected(Map<String, String> expectedXmlFiles, CsarTest csarFile) + throws CsarConverterException, IOException { + List<BabelArtifact> generatedArtifacts = converter.generateXmlFromCsar(csarFile.getContent(), + csarFile.getName(), SERVICE_VERSION); + assertThat("Incorrect number of files generated", // + generatedArtifacts.size(), is(equalTo(expectedXmlFiles.size()))); + generatedArtifacts + .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content", + ga.getPayload(), matches(expectedXmlFiles.get(ga.getName())))); + } } diff --git a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java index 66a36ad..5bd5484 100644 --- a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java +++ b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java @@ -44,8 +44,8 @@ import org.mockito.Mockito; import org.onap.aai.auth.AAIMicroServiceAuth; import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser; import org.onap.aai.babel.service.data.BabelRequest; +import org.onap.aai.babel.testdata.CsarTest; import org.onap.aai.babel.util.ArtifactTestUtils; -import org.onap.aai.babel.xml.generator.data.GeneratorUtil; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -78,7 +78,7 @@ public class TestGenerateArtifactsServiceImpl { @Test public void testGenerateArtifacts() throws Exception { - Response response = processJsonRequest(getRequestJson("success_request_vnf_catalog.json")); + Response response = processJsonRequest(CsarTest.VNF_VENDOR_CSAR); assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); assertThat(response.getEntity(), is(getResponseJson("response.json"))); } @@ -90,49 +90,55 @@ public class TestGenerateArtifactsServiceImpl { */ @Test public void testGenerateArtifactsWithoutVnfConfiguration() throws Exception { - final byte[] csarContent = new ArtifactTestUtils().getCompressedArtifact("noVnfConfiguration.csar"); - - BabelRequest babelRequest = new BabelRequest(); - babelRequest.setCsar(new String(GeneratorUtil.encode(csarContent))); - babelRequest.setArtifactVersion("3.0"); - babelRequest.setArtifactName("service-Vscpass-Test"); - - Response response = processJsonRequest(new Gson().toJson(babelRequest)); + Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR); assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); assertThat(response.getEntity(), is(getResponseJson("validNoVnfConfigurationResponse.json"))); } @Test public void testInvalidCsarFile() throws URISyntaxException, IOException { - Response response = processJsonRequest(getRequestJson("invalid_csar_request.json")); + BabelRequest request = new BabelRequest(); + request.setArtifactName("hello"); + request.setArtifactVersion("1.0"); + request.setCsar("xxxx"); + Response response = invokeService(new Gson().toJson(request)); assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model.")); } @Test public void testInvalidJsonFile() throws URISyntaxException, IOException { - Response response = processJsonRequest(getRequestJson("invalid_json_request.json")); + Response response = invokeService("{\"csar:\"xxxx\""); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("Malformed request.")); } @Test public void testMissingArtifactName() throws Exception { - Response response = processJsonRequest(getRequestJson("missing_artifact_name_request.json")); + BabelRequest request = new BabelRequest(); + request.setArtifactVersion("1.0"); + request.setCsar(""); + Response response = invokeService(new Gson().toJson(request)); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("No artifact name attribute found in the request body.")); } @Test public void testMissingArtifactVersion() throws Exception { - Response response = processJsonRequest(getRequestJson("missing_artifact_version_request.json")); + BabelRequest request = new BabelRequest(); + request.setArtifactName("hello"); + request.setCsar(""); + Response response = invokeService(new Gson().toJson(request)); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("No artifact version attribute found in the request body.")); } @Test public void testMissingCsarFile() throws Exception { - Response response = processJsonRequest(getRequestJson("missing_csar_request.json")); + BabelRequest request = new BabelRequest(); + request.setArtifactName("test-name"); + request.setArtifactVersion("1.0"); + Response response = invokeService(new Gson().toJson(request)); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("No csar attribute found in the request body.")); } @@ -140,12 +146,16 @@ public class TestGenerateArtifactsServiceImpl { /** * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API. * - * @param resource path to the incoming JSON request + * @param csar * @return the Response from the HTTP API * @throws URISyntaxException if the URI cannot be created * @throws IOException if the resource cannot be loaded */ - private Response processJsonRequest(String jsonString) throws URISyntaxException, IOException { + private Response processJsonRequest(CsarTest csar) throws IOException, URISyntaxException { + String jsonString = csar.getJsonRequest(); + return invokeService(jsonString); + } + private Response invokeService(String jsonString) throws URISyntaxException { UriInfo mockUriInfo = Mockito.mock(UriInfo.class); Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked) Mockito.when(mockUriInfo.getPath(false)).thenReturn("validate"); // URI prefix is stripped by AJSC routing @@ -181,10 +191,6 @@ public class TestGenerateArtifactsServiceImpl { return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString); } - private String getRequestJson(String resource) throws IOException, URISyntaxException { - return new ArtifactTestUtils().getRequestJson(resource); - } - private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException { return new ArtifactTestUtils().getResponseJson(jsonResponse); } diff --git a/src/test/java/org/onap/aai/babel/testdata/CsarTest.java b/src/test/java/org/onap/aai/babel/testdata/CsarTest.java new file mode 100644 index 0000000..d884e4f --- /dev/null +++ b/src/test/java/org/onap/aai/babel/testdata/CsarTest.java @@ -0,0 +1,94 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.babel.testdata; + +import com.google.gson.Gson; +import java.io.IOException; +import java.util.List; +import org.onap.aai.babel.csar.extractor.InvalidArchiveException; +import org.onap.aai.babel.csar.extractor.YamlExtractor; +import org.onap.aai.babel.csar.vnfcatalog.ToscaToCatalogException; +import org.onap.aai.babel.csar.vnfcatalog.VnfVendorImageExtractor; +import org.onap.aai.babel.service.data.BabelArtifact; +import org.onap.aai.babel.service.data.BabelRequest; +import org.onap.aai.babel.util.ArtifactTestUtils; +import org.onap.aai.babel.xml.generator.data.Artifact; +import org.onap.aai.babel.xml.generator.data.GeneratorUtil; + +public enum CsarTest { + // @formatter:off + VNF_VENDOR_CSAR("catalog_csar.csar"), + NO_VNF_CONFIG_CSAR("noVnfConfiguration.csar"), + SD_WAN_CSAR_FILE("service-SdWanServiceTest-csar.csar"), + MISSING_METADATA_CSAR("service-MissingMetadataTest.csar"), + NO_YAML_FILES("noYmlFilesArchive.zip"), + PORT_MIRROR_CSAR("service_PortMirror.csar"), + VNFOD_SERVICE("service-Dev2devnfodservice17July-csar.csar"), + MULTIPLE_VNF_CSAR("catalog_csar_too_many_vnfConfigurations.csar"); + // @formatter:on + + private String filename; + private ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); + + CsarTest(String filename) { + this.filename = filename; + } + + public String getName() { + return filename; + } + + public byte[] getContent() throws IOException { + return artifactTestUtils.getCompressedArtifact(filename); + } + + /** + * Extract YAML Artifacts. + * + * @return the extracted artifacts + * @throws InvalidArchiveException if the CSAR is invalid + * @throws IOException for I/O errors + */ + public List<Artifact> extractArtifacts() throws InvalidArchiveException, IOException { + return new YamlExtractor().extract(getContent(), getName(), "v1"); + } + + /** + * Extract VNF Vendor Image Artifacts. + * + * @return the extracted artifacts + * @throws IOException + * @throws ToscaToCatalogException + * + */ + public BabelArtifact extractVnfVendorImages() throws ToscaToCatalogException, IOException { + return new VnfVendorImageExtractor().extract(getContent()); + } + + public String getJsonRequest() throws IOException { + BabelRequest request = new BabelRequest(); + request.setArtifactName(getName()); + request.setArtifactVersion("1.0"); + request.setCsar(new String(GeneratorUtil.encode(getContent()))); + return new Gson().toJson(request); + } +} |