2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.openecomp.sdnc.config.params.transformer.tosca;
25 import org.junit.Assert;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.openecomp.sdnc.config.params.data.Parameter;
30 import org.openecomp.sdnc.config.params.data.PropertyDefinition;
31 import org.openecomp.sdnc.config.params.data.RequestKey;
32 import org.openecomp.sdnc.config.params.data.ResponseKey;
33 import org.openecomp.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
36 import java.util.ArrayList;
37 import java.util.List;
41 * Created by pranavdi on 3/15/2017.
43 public class TestGenerateArtifactObject
46 public TemporaryFolder temporaryFolder = new TemporaryFolder();
49 public void testObjectArtifactProcessor() throws IOException, ArtifactProcessorException {
51 String expectedTosca="node_types:\n" +
53 " derived_from: org.openecomp.genericvnf\n" +
55 " description: ''\n" +
57 " LOCAL_ACCESS_IP_ADDR:\n" +
59 " required: false\n" +
60 " default: 192.168.30.1\n" +
61 " status: SUPPORTED\n" +
62 " LOCAL_CORE_ALT_IP_ADDR:\n" +
64 " required: false\n" +
65 " default: fd00:f4d5:ea06:1:0:110:254\n" +
66 " status: SUPPORTED\n" +
67 "topology_template:\n" +
68 " node_templates:\n" +
72 " LOCAL_ACCESS_IP_ADDR: <rule-type = myRule1> <response-keys = > <source-system = INSTAR> <request-keys = class-type:interface-ip-address , address_fqdn:someVal , address_type:v4>\n" +
73 " LOCAL_CORE_ALT_IP_ADDR: <rule-type = myRule2> <response-keys = name1:value1:field1> <source-system = INSTAR> <request-keys = >\n";
75 PropertyDefinition pd = new PropertyDefinition();
78 pd.setParameters(createParameters());
80 //Call ArtifactProcessor
81 OutputStream outstream=null;
83 File toscaFile =temporaryFolder.newFile("TestTosca.yml");
84 outstream = new FileOutputStream(toscaFile);
85 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
86 arp.generateArtifact(pd,outstream);
90 String toscaString = getFileContent(toscaFile);
91 Assert.assertEquals(expectedTosca,toscaString);
96 public void testPDpropertiesSetNull() throws IOException, ArtifactProcessorException {
97 String expectedTosca = "node_types:\n" +
98 " PropertyDefinition:\n" +
99 " derived_from: org.openecomp.genericvnf\n" +
101 " description: ''\n" +
102 "topology_template:\n" +
103 " node_templates:\n" +
104 " PropertyDefinition_Template:\n" +
105 " type: PropertyDefinition\n";
107 PropertyDefinition pd = new PropertyDefinition();
108 pd.setKind("PropertyDefinition");
110 // pd.setParameters(createParameters());
112 //Call ArtifactProcessor
113 OutputStream outstream=null;
115 File toscaFile =temporaryFolder.newFile("TestTosca.yml");
116 outstream = new FileOutputStream(toscaFile);
118 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
119 arp.generateArtifact(pd,outstream);
123 String toscaString = getFileContent(toscaFile);
124 Assert.assertEquals(expectedTosca,toscaString);
128 public void testArtifactGeneratorInvalidStream() throws IOException {
129 String expectedMsg = "java.io.IOException: Stream Closed";
130 PropertyDefinition pd = new PropertyDefinition();
133 pd.setParameters(createParameters());
135 //Call ArtifactProcessor
136 OutputStream outstream=null;
138 File toscaFile =temporaryFolder.newFile("TestTosca.yml");
139 outstream = new FileOutputStream(toscaFile);
141 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
142 arp.generateArtifact(pd,outstream);
145 catch (ArtifactProcessorException e)
147 Assert.assertEquals(expectedMsg,e.getMessage());
151 private List<Parameter> createParameters()
153 //Create single Parameter object 1
154 Parameter singleParameter1 = new Parameter();
155 singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
156 // singleParameter1.setList(false);
157 singleParameter1.setRequired(false);
158 singleParameter1.setSource("INSTAR");
159 singleParameter1.setDefaultValue("192.168.30.1");
160 singleParameter1.setRuleType("myRule1");
161 singleParameter1.setRequestKeys(createRequestKeys());
163 //Create single Parameter object 2
164 Parameter singleParameter2 = new Parameter();
165 singleParameter2.setName("LOCAL_CORE_ALT_IP_ADDR");
166 singleParameter2.setType("String");
167 // singleParameter2.setList(false);
168 singleParameter2.setRequired(false);
169 singleParameter2.setSource("INSTAR");
170 singleParameter2.setDefaultValue("fd00:f4d5:ea06:1:0:110:254");
171 singleParameter2.setRuleType("myRule2");
172 singleParameter2.setResponseKeys(createResponseKeys());
175 //Add the Parameter objects to the List
176 List<Parameter> parameterList = new ArrayList<Parameter>();
177 parameterList.add(singleParameter1);
178 parameterList.add(singleParameter2);
179 return parameterList;
182 private List<RequestKey> createRequestKeys()
184 //Create RequestKey object 1
185 RequestKey requestKey1 = new RequestKey();
186 requestKey1.setKeyName("class-type");
187 requestKey1.setKeyValue("interface-ip-address");
189 //Create RequestKey object 2
190 RequestKey requestKey2 = new RequestKey();
191 requestKey2.setKeyName("address_fqdn");
192 requestKey2.setKeyValue("someVal");
194 //Create RequestKey object 3
195 RequestKey requestKey3 = new RequestKey();
196 requestKey3.setKeyName("address_type");
197 requestKey3.setKeyValue("v4");
199 //Add the RequestKey Objects to the List
200 List<RequestKey> requestKeyList = new ArrayList<RequestKey>();
201 requestKeyList.add(requestKey1);
202 requestKeyList.add(requestKey2);
203 requestKeyList.add(requestKey3);
204 return requestKeyList;
207 private List<ResponseKey> createResponseKeys()
209 //Create RequestKey object 1
210 ResponseKey responseKey1 = new ResponseKey();
212 responseKey1.setUniqueKeyName("name1");
213 responseKey1.setUniqueKeyValue("value1");
214 responseKey1.setFieldKeyName("field1");
216 //Add the RequestKey Objects to the List
217 List<ResponseKey> responseKeyList = new ArrayList<ResponseKey>();
218 responseKeyList.add(responseKey1);
220 return responseKeyList;
223 private Parameter createParameter()
225 Parameter singleParameter1 = new Parameter();
226 singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
227 //singleParameter1.setList(false);
228 singleParameter1.setRequired(false);
229 singleParameter1.setSource("INSTAR");
230 singleParameter1.setDefaultValue("192.168.30.1");
231 singleParameter1.setRequestKeys(createRequestKeys());
232 singleParameter1.setResponseKeys(createResponseKeys());
233 return singleParameter1;
237 public void testPDnull() throws IOException, ArtifactProcessorException {
238 PropertyDefinition pd = null;
239 OutputStream outstream=null;
241 outstream = new FileOutputStream(".\\TestTosca.yml");
242 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
243 arp.generateArtifact(pd,outstream);
250 private String getFileContent(File file) throws IOException
252 InputStream is = new FileInputStream(file);
253 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
254 String line = buf.readLine();
255 StringBuilder sb = new StringBuilder();
257 while (line != null) {
258 sb.append(line).append("\n");
259 line = buf.readLine();
261 String fileString = sb.toString();