2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (C) 2018 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.sdnc.config.params.transformer.tosca;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.nio.charset.StandardCharsets;
33 import java.util.ArrayList;
34 import java.util.List;
35 import org.apache.commons.io.FileUtils;
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 = FileUtils.readFileToString(toscaFile, StandardCharsets.UTF_8);
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 = FileUtils.readFileToString(toscaFile, StandardCharsets.UTF_8);
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 @Test(expected = Exception.class)
199 public void testPDnull() throws IOException, ArtifactProcessorException {
200 PropertyDefinition pd = null;
201 OutputStream outstream = null;
203 outstream = new FileOutputStream(".\\TestTosca.yml");
204 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
205 arp.generateArtifact(pd, outstream);