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.onap.sdnc.config.params.transformer.tosca;
27 import java.io.BufferedReader;
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;
47 public class TestGenerateArtifactObject {
49 public TemporaryFolder temporaryFolder = new TemporaryFolder();
52 public void testObjectArtifactProcessor() throws IOException, ArtifactProcessorException {
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";
66 PropertyDefinition pd = new PropertyDefinition();
69 pd.setParameters(createParameters());
71 // Call ArtifactProcessor
72 OutputStream outstream = null;
74 File toscaFile = temporaryFolder.newFile("TestTosca.yml");
75 outstream = new FileOutputStream(toscaFile);
76 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
77 arp.generateArtifact(pd, outstream);
81 String toscaString = getFileContent(toscaFile);
82 Assert.assertEquals(expectedTosca, toscaString);
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";
93 PropertyDefinition pd = new PropertyDefinition();
94 pd.setKind("PropertyDefinition");
96 OutputStream outstream = null;
98 File toscaFile = temporaryFolder.newFile("TestTosca.yml");
99 outstream = new FileOutputStream(toscaFile);
101 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
102 arp.generateArtifact(pd, outstream);
106 String toscaString = getFileContent(toscaFile);
107 Assert.assertEquals(expectedTosca, toscaString);
111 public void testArtifactGeneratorInvalidStream() throws IOException {
112 String expectedMsg = "java.io.IOException: Stream Closed";
113 PropertyDefinition pd = new PropertyDefinition();
116 pd.setParameters(createParameters());
118 // Call ArtifactProcessor
119 OutputStream outstream = null;
121 File toscaFile = temporaryFolder.newFile("TestTosca.yml");
122 outstream = new FileOutputStream(toscaFile);
124 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
125 arp.generateArtifact(pd, outstream);
127 } catch (ArtifactProcessorException e) {
128 Assert.assertEquals(expectedMsg, e.getMessage());
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());
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());
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;
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");
165 // Create RequestKey object 2
166 RequestKey requestKey2 = new RequestKey();
167 requestKey2.setKeyName("address_fqdn");
168 requestKey2.setKeyValue("someVal");
170 // Create RequestKey object 3
171 RequestKey requestKey3 = new RequestKey();
172 requestKey3.setKeyName("address_type");
173 requestKey3.setKeyValue("v4");
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;
183 private List<ResponseKey> createResponseKeys() {
184 // Create RequestKey object 1
185 ResponseKey responseKey1 = new ResponseKey();
187 responseKey1.setUniqueKeyName("name1");
188 responseKey1.setUniqueKeyValue("value1");
189 responseKey1.setFieldKeyName("field1");
191 // Add the RequestKey Objects to the List
192 List<ResponseKey> responseKeyList = new ArrayList<ResponseKey>();
193 responseKeyList.add(responseKey1);
195 return responseKeyList;
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;
209 @Test(expected = Exception.class)
210 public void testPDnull() throws IOException, ArtifactProcessorException {
211 PropertyDefinition pd = null;
212 OutputStream outstream = null;
214 outstream = new FileOutputStream(".\\TestTosca.yml");
215 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
216 arp.generateArtifact(pd, outstream);
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();
227 while (line != null) {
228 sb.append(line).append("\n");
229 line = buf.readLine();
231 String fileString = sb.toString();