b8a86b47f94471ddcdb9d00bb13a06ad17cf6892
[aai/babel.git] / src / test / java / org / onap / aai / babel / testdata / CsarTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.babel.testdata;
23
24 import com.google.gson.Gson;
25 import java.io.IOException;
26 import java.util.List;
27 import org.onap.aai.babel.csar.extractor.InvalidArchiveException;
28 import org.onap.aai.babel.csar.extractor.YamlExtractor;
29 import org.onap.aai.babel.csar.vnfcatalog.ToscaToCatalogException;
30 import org.onap.aai.babel.csar.vnfcatalog.VnfVendorImageExtractor;
31 import org.onap.aai.babel.service.data.BabelArtifact;
32 import org.onap.aai.babel.service.data.BabelRequest;
33 import org.onap.aai.babel.util.ArtifactTestUtils;
34 import org.onap.aai.babel.xml.generator.data.Artifact;
35 import org.onap.aai.babel.xml.generator.data.GeneratorUtil;
36
37 public enum CsarTest {
38     // @formatter:off
39     VNF_VENDOR_CSAR("catalog_csar.csar"),
40     PNF_VENDOR_CSAR("service-Testpnfsvc-csar.csar"),
41     NO_VNF_CONFIG_CSAR("noVnfConfiguration.csar"),
42     SD_WAN_CSAR_FILE("service-SdWanServiceTest-csar.csar"),
43     COS_AVPN_CSAR_FILE("service_CosAvpn_csar.csar"),
44     MISSING_METADATA_CSAR("service-MissingMetadataTest.csar"),
45     NO_YAML_FILES("noYmlFilesArchive.zip"),
46     PORT_MIRROR_CSAR("service_PortMirror.csar"),
47     MULTIPLE_VNF_CSAR("catalog_csar_too_many_vnfConfigurations.csar"),
48     NETWORK_COLLECTION_CSAR_FILE("service_NetworkCollection.csar"),
49     RG_COLLECTOR_615_CSAR_FILE("service-RgCollector615-csar.csar"),
50     VDBE_SERVICE_CSAR_FILE("service-VdbeSrv-csar.csar"),
51     VNFOD_SERVICE("service-Dev2devnfodservice17July-csar.csar"),
52     CHILD_RESOURCE_CSAR_FILE("service-NetworkCloudVnfServiceMock-csar.csar"),
53     SERVICE_PROXY_CSAR_FILE("service-S1-csar.csar");
54
55     // @formatter:on
56
57     private String filename;
58     private ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
59
60     CsarTest(String filename) {
61         this.filename = filename;
62     }
63
64     public String getName() {
65         return filename;
66     }
67
68     public byte[] getContent() throws IOException {
69         return artifactTestUtils.getCompressedArtifact(filename);
70     }
71
72     /**
73      * Extract YAML Artifacts.
74      *
75      * @return the extracted artifacts
76      * @throws InvalidArchiveException
77      *             if the CSAR is invalid
78      * @throws IOException
79      *             for I/O errors
80      */
81     public List<Artifact> extractArtifacts() throws InvalidArchiveException, IOException {
82         return new YamlExtractor().extract(getContent(), getName(), "v1");
83     }
84
85     /**
86      * Extract VNF Vendor Image Artifacts.
87      *
88      * @return the extracted artifacts
89      * @throws ToscaToCatalogException
90      *             if the CSAR content is not valid
91      * @throws IOException
92      *             if an I/O exception occursSince:
93      */
94     public BabelArtifact extractVnfVendorImages() throws ToscaToCatalogException, IOException {
95         return new VnfVendorImageExtractor().extract(getContent());
96     }
97
98     /**
99      * Create a BabelRequest containing the encoded CSAR content.
100      * 
101      * @return a new Babel request for this CSAR
102      * @throws IOException
103      *             if an I/O exception occurs
104      */
105     public String getJsonRequest() throws IOException {
106         BabelRequest request = new BabelRequest();
107         request.setArtifactName(getName());
108         request.setArtifactVersion("1.0");
109         request.setCsar(new String(GeneratorUtil.encode(getContent())));
110         return new Gson().toJson(request);
111     }
112     
113     /**
114      * Create a BabelRequest containing the encoded CSAR content by passing in the artifact version.
115      * 
116      * @return a new Babel request for this CSAR
117      * @throws IOException
118      *             if an I/O exception occurs
119      */
120     public String getJsonRequestWithArtifactVersion(String artifactVersion) throws IOException {
121         BabelRequest request = new BabelRequest();
122         request.setArtifactName(getName());
123         request.setArtifactVersion(artifactVersion);
124         request.setCsar(new String(GeneratorUtil.encode(getContent())));
125         return new Gson().toJson(request);
126     }
127 }