2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.sdnc.config.params.transformer.tosca;
26 import java.io.BufferedReader;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.OutputStream;
34 import java.util.ArrayList;
35 import java.util.List;
36 import org.junit.Assert;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.TemporaryFolder;
40 import org.onap.sdnc.config.params.data.Parameter;
41 import org.onap.sdnc.config.params.data.PropertyDefinition;
42 import org.onap.sdnc.config.params.data.RequestKey;
43 import org.onap.sdnc.config.params.data.ResponseKey;
44 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
46 public class TestGenerateArtifactObject {
48 public TemporaryFolder temporaryFolder = new TemporaryFolder();
51 public void testObjectArtifactProcessor() throws IOException, ArtifactProcessorException {
53 String expectedTosca = "node_types:\n" + " VNF:\n"
54 + " derived_from: org.openecomp.genericvnf\n" + " version: V1\n"
55 + " description: ''\n" + " properties:\n" + " LOCAL_ACCESS_IP_ADDR:\n"
56 + " type: string\n" + " required: false\n"
57 + " default: 0.0.0.0\n" + " status: SUPPORTED\n"
58 + " LOCAL_CORE_ALT_IP_ADDR:\n" + " type: String\n"
59 + " required: false\n" + " default: value\n"
60 + " status: SUPPORTED\n" + "topology_template:\n" + " node_templates:\n"
61 + " VNF_Template:\n" + " type: VNF\n" + " properties:\n"
62 + " 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"
63 + " LOCAL_CORE_ALT_IP_ADDR: <rule-type = myRule2> <response-keys = name1:value1:field1> <source-system = source> <request-keys = >\n";
65 PropertyDefinition pd = new PropertyDefinition();
68 pd.setParameters(createParameters());
70 // Call ArtifactProcessor
71 OutputStream outstream = null;
73 File toscaFile = temporaryFolder.newFile("TestTosca.yml");
74 outstream = new FileOutputStream(toscaFile);
75 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
76 arp.generateArtifact(pd, outstream);
80 String toscaString = getFileContent(toscaFile);
81 Assert.assertEquals(expectedTosca, toscaString);
86 public void testPDpropertiesSetNull() throws IOException, ArtifactProcessorException {
87 String expectedTosca = "node_types:\n" + " PropertyDefinition:\n"
88 + " derived_from: org.openecomp.genericvnf\n" + " version: V1\n"
89 + " description: ''\n" + "topology_template:\n" + " node_templates:\n"
90 + " PropertyDefinition_Template:\n" + " type: PropertyDefinition\n";
92 PropertyDefinition pd = new PropertyDefinition();
93 pd.setKind("PropertyDefinition");
95 OutputStream outstream = null;
97 File toscaFile = temporaryFolder.newFile("TestTosca.yml");
98 outstream = new FileOutputStream(toscaFile);
100 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
101 arp.generateArtifact(pd, outstream);
105 String toscaString = getFileContent(toscaFile);
106 Assert.assertEquals(expectedTosca, toscaString);
110 public void testArtifactGeneratorInvalidStream() throws IOException {
111 String expectedMsg = "java.io.IOException: Stream Closed";
112 PropertyDefinition pd = new PropertyDefinition();
115 pd.setParameters(createParameters());
117 // Call ArtifactProcessor
118 OutputStream outstream = null;
120 File toscaFile = temporaryFolder.newFile("TestTosca.yml");
121 outstream = new FileOutputStream(toscaFile);
123 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
124 arp.generateArtifact(pd, outstream);
126 } catch (ArtifactProcessorException e) {
127 Assert.assertEquals(expectedMsg, e.getMessage());
131 private List<Parameter> createParameters() {
132 // Create single Parameter object 1
133 Parameter singleParameter1 = new Parameter();
134 singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
135 singleParameter1.setRequired(false);
136 singleParameter1.setSource("source");
137 singleParameter1.setDefaultValue("0.0.0.0");
138 singleParameter1.setRuleType("myRule1");
139 singleParameter1.setRequestKeys(createRequestKeys());
141 // Create single Parameter object 2
142 Parameter singleParameter2 = new Parameter();
143 singleParameter2.setName("LOCAL_CORE_ALT_IP_ADDR");
144 singleParameter2.setType("String");
145 singleParameter2.setRequired(false);
146 singleParameter2.setSource("source");
147 singleParameter2.setDefaultValue("value");
148 singleParameter2.setRuleType("myRule2");
149 singleParameter2.setResponseKeys(createResponseKeys());
151 // Add the Parameter objects to the List
152 List<Parameter> parameterList = new ArrayList<Parameter>();
153 parameterList.add(singleParameter1);
154 parameterList.add(singleParameter2);
155 return parameterList;
158 private List<RequestKey> createRequestKeys() {
159 // Create RequestKey object 1
160 RequestKey requestKey1 = new RequestKey();
161 requestKey1.setKeyName("class-type");
162 requestKey1.setKeyValue("interface-ip-address");
164 // Create RequestKey object 2
165 RequestKey requestKey2 = new RequestKey();
166 requestKey2.setKeyName("address_fqdn");
167 requestKey2.setKeyValue("someVal");
169 // Create RequestKey object 3
170 RequestKey requestKey3 = new RequestKey();
171 requestKey3.setKeyName("address_type");
172 requestKey3.setKeyValue("v4");
174 // Add the RequestKey Objects to the List
175 List<RequestKey> requestKeyList = new ArrayList<RequestKey>();
176 requestKeyList.add(requestKey1);
177 requestKeyList.add(requestKey2);
178 requestKeyList.add(requestKey3);
179 return requestKeyList;
182 private List<ResponseKey> createResponseKeys() {
183 // Create RequestKey object 1
184 ResponseKey responseKey1 = new ResponseKey();
186 responseKey1.setUniqueKeyName("name1");
187 responseKey1.setUniqueKeyValue("value1");
188 responseKey1.setFieldKeyName("field1");
190 // Add the RequestKey Objects to the List
191 List<ResponseKey> responseKeyList = new ArrayList<ResponseKey>();
192 responseKeyList.add(responseKey1);
194 return responseKeyList;
197 private Parameter createParameter() {
198 Parameter singleParameter1 = new Parameter();
199 singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
200 singleParameter1.setRequired(false);
201 singleParameter1.setSource("source");
202 singleParameter1.setDefaultValue("0.0.0.0");
203 singleParameter1.setRequestKeys(createRequestKeys());
204 singleParameter1.setResponseKeys(createResponseKeys());
205 return singleParameter1;
208 @Test(expected = Exception.class)
209 public void testPDnull() throws IOException, ArtifactProcessorException {
210 PropertyDefinition pd = null;
211 OutputStream outstream = null;
213 outstream = new FileOutputStream(".\\TestTosca.yml");
214 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
215 arp.generateArtifact(pd, outstream);
220 private String getFileContent(File file) throws IOException {
221 InputStream is = new FileInputStream(file);
222 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
223 String line = buf.readLine();
224 StringBuilder sb = new StringBuilder();
226 while (line != null) {
227 sb.append(line).append("\n");
228 line = buf.readLine();
230 String fileString = sb.toString();