2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.sdnc.config.params.transformer.tosca;
27 import org.junit.Assert;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.TemporaryFolder;
31 import org.openecomp.sdnc.config.params.data.Parameter;
32 import org.openecomp.sdnc.config.params.data.PropertyDefinition;
33 import org.openecomp.sdnc.config.params.data.RequestKey;
34 import org.openecomp.sdnc.config.params.data.ResponseKey;
35 import org.openecomp.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
38 import java.util.ArrayList;
39 import java.util.List;
43 * Created by pranavdi on 3/15/2017.
45 public class TestGenerateArtifactObject
48 public TemporaryFolder temporaryFolder = new TemporaryFolder();
51 public void testObjectArtifactProcessor() throws IOException, ArtifactProcessorException {
53 String expectedTosca="node_types:\n" +
55 " derived_from: org.openecomp.genericvnf\n" +
57 " description: ''\n" +
59 " LOCAL_ACCESS_IP_ADDR:\n" +
61 " required: false\n" +
62 " default: 192.168.30.1\n" +
63 " status: SUPPORTED\n" +
64 " LOCAL_CORE_ALT_IP_ADDR:\n" +
66 " required: false\n" +
67 " default: fd00:f4d5:ea06:1:0:110:254\n" +
68 " status: SUPPORTED\n" +
69 "topology_template:\n" +
70 " node_templates:\n" +
74 " 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" +
75 " LOCAL_CORE_ALT_IP_ADDR: <rule-type = myRule2> <response-keys = name1:value1:field1> <source-system = INSTAR> <request-keys = >\n";
77 PropertyDefinition pd = new PropertyDefinition();
80 pd.setParameters(createParameters());
82 //Call ArtifactProcessor
83 OutputStream outstream=null;
85 File toscaFile =temporaryFolder.newFile("TestTosca.yml");
86 outstream = new FileOutputStream(toscaFile);
87 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
88 arp.generateArtifact(pd,outstream);
92 String toscaString = getFileContent(toscaFile);
93 Assert.assertEquals(expectedTosca,toscaString);
98 public void testPDpropertiesSetNull() throws IOException, ArtifactProcessorException {
99 String expectedTosca = "node_types:\n" +
100 " PropertyDefinition:\n" +
101 " derived_from: org.openecomp.genericvnf\n" +
103 " description: ''\n" +
104 "topology_template:\n" +
105 " node_templates:\n" +
106 " PropertyDefinition_Template:\n" +
107 " type: PropertyDefinition\n";
109 PropertyDefinition pd = new PropertyDefinition();
110 pd.setKind("PropertyDefinition");
112 // pd.setParameters(createParameters());
114 //Call ArtifactProcessor
115 OutputStream outstream=null;
117 File toscaFile =temporaryFolder.newFile("TestTosca.yml");
118 outstream = new FileOutputStream(toscaFile);
120 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
121 arp.generateArtifact(pd,outstream);
125 String toscaString = getFileContent(toscaFile);
126 Assert.assertEquals(expectedTosca,toscaString);
130 public void testArtifactGeneratorInvalidStream() throws IOException {
131 String expectedMsg = "java.io.IOException: Stream Closed";
132 PropertyDefinition pd = new PropertyDefinition();
135 pd.setParameters(createParameters());
137 //Call ArtifactProcessor
138 OutputStream outstream=null;
140 File toscaFile =temporaryFolder.newFile("TestTosca.yml");
141 outstream = new FileOutputStream(toscaFile);
143 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
144 arp.generateArtifact(pd,outstream);
147 catch (ArtifactProcessorException e)
149 Assert.assertEquals(expectedMsg,e.getMessage());
153 private List<Parameter> createParameters()
155 //Create single Parameter object 1
156 Parameter singleParameter1 = new Parameter();
157 singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
158 // singleParameter1.setList(false);
159 singleParameter1.setRequired(false);
160 singleParameter1.setSource("INSTAR");
161 singleParameter1.setDefaultValue("192.168.30.1");
162 singleParameter1.setRuleType("myRule1");
163 singleParameter1.setRequestKeys(createRequestKeys());
165 //Create single Parameter object 2
166 Parameter singleParameter2 = new Parameter();
167 singleParameter2.setName("LOCAL_CORE_ALT_IP_ADDR");
168 singleParameter2.setType("String");
169 // singleParameter2.setList(false);
170 singleParameter2.setRequired(false);
171 singleParameter2.setSource("INSTAR");
172 singleParameter2.setDefaultValue("fd00:f4d5:ea06:1:0:110:254");
173 singleParameter2.setRuleType("myRule2");
174 singleParameter2.setResponseKeys(createResponseKeys());
177 //Add the Parameter objects to the List
178 List<Parameter> parameterList = new ArrayList<Parameter>();
179 parameterList.add(singleParameter1);
180 parameterList.add(singleParameter2);
181 return parameterList;
184 private List<RequestKey> createRequestKeys()
186 //Create RequestKey object 1
187 RequestKey requestKey1 = new RequestKey();
188 requestKey1.setKeyName("class-type");
189 requestKey1.setKeyValue("interface-ip-address");
191 //Create RequestKey object 2
192 RequestKey requestKey2 = new RequestKey();
193 requestKey2.setKeyName("address_fqdn");
194 requestKey2.setKeyValue("someVal");
196 //Create RequestKey object 3
197 RequestKey requestKey3 = new RequestKey();
198 requestKey3.setKeyName("address_type");
199 requestKey3.setKeyValue("v4");
201 //Add the RequestKey Objects to the List
202 List<RequestKey> requestKeyList = new ArrayList<RequestKey>();
203 requestKeyList.add(requestKey1);
204 requestKeyList.add(requestKey2);
205 requestKeyList.add(requestKey3);
206 return requestKeyList;
209 private List<ResponseKey> createResponseKeys()
211 //Create RequestKey object 1
212 ResponseKey responseKey1 = new ResponseKey();
214 responseKey1.setUniqueKeyName("name1");
215 responseKey1.setUniqueKeyValue("value1");
216 responseKey1.setFieldKeyName("field1");
218 //Add the RequestKey Objects to the List
219 List<ResponseKey> responseKeyList = new ArrayList<ResponseKey>();
220 responseKeyList.add(responseKey1);
222 return responseKeyList;
225 private Parameter createParameter()
227 Parameter singleParameter1 = new Parameter();
228 singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
229 //singleParameter1.setList(false);
230 singleParameter1.setRequired(false);
231 singleParameter1.setSource("INSTAR");
232 singleParameter1.setDefaultValue("192.168.30.1");
233 singleParameter1.setRequestKeys(createRequestKeys());
234 singleParameter1.setResponseKeys(createResponseKeys());
235 return singleParameter1;
239 public void testPDnull() throws IOException, ArtifactProcessorException {
240 PropertyDefinition pd = null;
241 OutputStream outstream=null;
243 outstream = new FileOutputStream(".\\TestTosca.yml");
244 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
245 arp.generateArtifact(pd,outstream);
252 private String getFileContent(File file) throws IOException
254 InputStream is = new FileInputStream(file);
255 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
256 String line = buf.readLine();
257 StringBuilder sb = new StringBuilder();
259 while (line != null) {
260 sb.append(line).append("\n");
261 line = buf.readLine();
263 String fileString = sb.toString();