7477d748d631721c3c3caeaa10bd76f20081f655
[appc.git] / appc-config / appc-config-params / provider / src / test / java / org / onap / sdnc / config / params / transformer / tosca / TestGenerateArtifactObject.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.config.params.transformer.tosca;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.io.OutputStream;
35 import java.util.ArrayList;
36 import java.util.List;
37 import org.junit.Assert;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.TemporaryFolder;
41 import org.onap.sdnc.config.params.data.Parameter;
42 import org.onap.sdnc.config.params.data.PropertyDefinition;
43 import org.onap.sdnc.config.params.data.RequestKey;
44 import org.onap.sdnc.config.params.data.ResponseKey;
45 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
46
47 public class TestGenerateArtifactObject {
48     @Rule
49     public TemporaryFolder temporaryFolder = new TemporaryFolder();
50
51     @Test
52     public void testObjectArtifactProcessor() throws IOException, ArtifactProcessorException {
53
54         String expectedTosca = "node_types:\n" + "  VNF:\n"
55                 + "    derived_from: org.openecomp.genericvnf\n" + "    version: V1\n"
56                 + "    description: ''\n" + "    properties:\n" + "      LOCAL_ACCESS_IP_ADDR:\n"
57                 + "        type: string\n" + "        required: false\n"
58                 + "        default: 0.0.0.0\n" + "        status: SUPPORTED\n"
59                 + "      LOCAL_CORE_ALT_IP_ADDR:\n" + "        type: String\n"
60                 + "        required: false\n" + "        default: value\n"
61                 + "        status: SUPPORTED\n" + "topology_template:\n" + "  node_templates:\n"
62                 + "    VNF_Template:\n" + "      type: VNF\n" + "      properties:\n"
63                 + "        LOCAL_ACCESS_IP_ADDR: <rule-type = myRule1> <response-keys = > <source-system = source> <request-keys = class-type:interface-ip-address , address_fqdn:someVal , address_type:v4>\n"
64                 + "        LOCAL_CORE_ALT_IP_ADDR: <rule-type = myRule2> <response-keys = name1:value1:field1> <source-system = source> <request-keys = >\n";
65         // Create object
66         PropertyDefinition pd = new PropertyDefinition();
67         pd.setKind("VNF");
68         pd.setVersion("V1");
69         pd.setParameters(createParameters());
70
71         // Call ArtifactProcessor
72         OutputStream outstream = null;
73
74         File toscaFile = temporaryFolder.newFile("TestTosca.yml");
75         outstream = new FileOutputStream(toscaFile);
76         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
77         arp.generateArtifact(pd, outstream);
78         outstream.flush();
79         outstream.close();
80
81         String toscaString = getFileContent(toscaFile);
82         Assert.assertEquals(expectedTosca, toscaString);
83
84     }
85
86     @Test
87     public void testPDpropertiesSetNull() throws IOException, ArtifactProcessorException {
88         String expectedTosca = "node_types:\n" + "  PropertyDefinition:\n"
89                 + "    derived_from: org.openecomp.genericvnf\n" + "    version: V1\n"
90                 + "    description: ''\n" + "topology_template:\n" + "  node_templates:\n"
91                 + "    PropertyDefinition_Template:\n" + "      type: PropertyDefinition\n";
92         // Create object
93         PropertyDefinition pd = new PropertyDefinition();
94         pd.setKind("PropertyDefinition");
95         pd.setVersion("V1");
96         OutputStream outstream = null;
97
98         File toscaFile = temporaryFolder.newFile("TestTosca.yml");
99         outstream = new FileOutputStream(toscaFile);
100
101         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
102         arp.generateArtifact(pd, outstream);
103         outstream.flush();
104         outstream.close();
105
106         String toscaString = getFileContent(toscaFile);
107         Assert.assertEquals(expectedTosca, toscaString);
108     }
109
110     @Test
111     public void testArtifactGeneratorInvalidStream() throws IOException {
112         String expectedMsg = "java.io.IOException: Stream Closed";
113         PropertyDefinition pd = new PropertyDefinition();
114         pd.setKind("VNF");
115         pd.setVersion("V1");
116         pd.setParameters(createParameters());
117
118         // Call ArtifactProcessor
119         OutputStream outstream = null;
120         try {
121             File toscaFile = temporaryFolder.newFile("TestTosca.yml");
122             outstream = new FileOutputStream(toscaFile);
123             outstream.close();
124             ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
125             arp.generateArtifact(pd, outstream);
126             Assert.fail();
127         } catch (ArtifactProcessorException e) {
128             Assert.assertEquals(expectedMsg, e.getMessage());
129         }
130     }
131
132     private List<Parameter> createParameters() {
133         // Create single Parameter object 1
134         Parameter singleParameter1 = new Parameter();
135         singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
136         singleParameter1.setRequired(false);
137         singleParameter1.setSource("source");
138         singleParameter1.setDefaultValue("0.0.0.0");
139         singleParameter1.setRuleType("myRule1");
140         singleParameter1.setRequestKeys(createRequestKeys());
141
142         // Create single Parameter object 2
143         Parameter singleParameter2 = new Parameter();
144         singleParameter2.setName("LOCAL_CORE_ALT_IP_ADDR");
145         singleParameter2.setType("String");
146         singleParameter2.setRequired(false);
147         singleParameter2.setSource("source");
148         singleParameter2.setDefaultValue("value");
149         singleParameter2.setRuleType("myRule2");
150         singleParameter2.setResponseKeys(createResponseKeys());
151
152         // Add the Parameter objects to the List
153         List<Parameter> parameterList = new ArrayList<Parameter>();
154         parameterList.add(singleParameter1);
155         parameterList.add(singleParameter2);
156         return parameterList;
157     }
158
159     private List<RequestKey> createRequestKeys() {
160         // Create RequestKey object 1
161         RequestKey requestKey1 = new RequestKey();
162         requestKey1.setKeyName("class-type");
163         requestKey1.setKeyValue("interface-ip-address");
164
165         // Create RequestKey object 2
166         RequestKey requestKey2 = new RequestKey();
167         requestKey2.setKeyName("address_fqdn");
168         requestKey2.setKeyValue("someVal");
169
170         // Create RequestKey object 3
171         RequestKey requestKey3 = new RequestKey();
172         requestKey3.setKeyName("address_type");
173         requestKey3.setKeyValue("v4");
174
175         // Add the RequestKey Objects to the List
176         List<RequestKey> requestKeyList = new ArrayList<RequestKey>();
177         requestKeyList.add(requestKey1);
178         requestKeyList.add(requestKey2);
179         requestKeyList.add(requestKey3);
180         return requestKeyList;
181     }
182
183     private List<ResponseKey> createResponseKeys() {
184         // Create RequestKey object 1
185         ResponseKey responseKey1 = new ResponseKey();
186
187         responseKey1.setUniqueKeyName("name1");
188         responseKey1.setUniqueKeyValue("value1");
189         responseKey1.setFieldKeyName("field1");
190
191         // Add the RequestKey Objects to the List
192         List<ResponseKey> responseKeyList = new ArrayList<ResponseKey>();
193         responseKeyList.add(responseKey1);
194
195         return responseKeyList;
196     }
197
198     private Parameter createParameter() {
199         Parameter singleParameter1 = new Parameter();
200         singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
201         singleParameter1.setRequired(false);
202         singleParameter1.setSource("source");
203         singleParameter1.setDefaultValue("0.0.0.0");
204         singleParameter1.setRequestKeys(createRequestKeys());
205         singleParameter1.setResponseKeys(createResponseKeys());
206         return singleParameter1;
207     }
208
209     @Test(expected = Exception.class)
210     public void testPDnull() throws IOException, ArtifactProcessorException {
211         PropertyDefinition pd = null;
212         OutputStream outstream = null;
213
214         outstream = new FileOutputStream(".\\TestTosca.yml");
215         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
216         arp.generateArtifact(pd, outstream);
217         outstream.flush();
218         outstream.close();
219     }
220
221     private String getFileContent(File file) throws IOException {
222         InputStream is = new FileInputStream(file);
223         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
224         String line = buf.readLine();
225         StringBuilder sb = new StringBuilder();
226
227         while (line != null) {
228             sb.append(line).append("\n");
229             line = buf.readLine();
230         }
231         String fileString = sb.toString();
232         is.close();
233         return fileString;
234     }
235 }