More license header updates to appc-config files
[appc.git] / appc-config / appc-config-params / provider / src / test / java / org / onap / sdnc / config / params / transformer / tosca / TestGenerateArtifactObject.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.sdnc.config.params.transformer.tosca;
25
26 import java.io.BufferedReader;
27 import java.io.File;
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;
45
46 public class TestGenerateArtifactObject {
47     @Rule
48     public TemporaryFolder temporaryFolder = new TemporaryFolder();
49
50     @Test
51     public void testObjectArtifactProcessor() throws IOException, ArtifactProcessorException {
52
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";
64         // Create object
65         PropertyDefinition pd = new PropertyDefinition();
66         pd.setKind("VNF");
67         pd.setVersion("V1");
68         pd.setParameters(createParameters());
69
70         // Call ArtifactProcessor
71         OutputStream outstream = null;
72
73         File toscaFile = temporaryFolder.newFile("TestTosca.yml");
74         outstream = new FileOutputStream(toscaFile);
75         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
76         arp.generateArtifact(pd, outstream);
77         outstream.flush();
78         outstream.close();
79
80         String toscaString = getFileContent(toscaFile);
81         Assert.assertEquals(expectedTosca, toscaString);
82
83     }
84
85     @Test
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";
91         // Create object
92         PropertyDefinition pd = new PropertyDefinition();
93         pd.setKind("PropertyDefinition");
94         pd.setVersion("V1");
95         OutputStream outstream = null;
96
97         File toscaFile = temporaryFolder.newFile("TestTosca.yml");
98         outstream = new FileOutputStream(toscaFile);
99
100         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
101         arp.generateArtifact(pd, outstream);
102         outstream.flush();
103         outstream.close();
104
105         String toscaString = getFileContent(toscaFile);
106         Assert.assertEquals(expectedTosca, toscaString);
107     }
108
109     @Test
110     public void testArtifactGeneratorInvalidStream() throws IOException {
111         String expectedMsg = "java.io.IOException: Stream Closed";
112         PropertyDefinition pd = new PropertyDefinition();
113         pd.setKind("VNF");
114         pd.setVersion("V1");
115         pd.setParameters(createParameters());
116
117         // Call ArtifactProcessor
118         OutputStream outstream = null;
119         try {
120             File toscaFile = temporaryFolder.newFile("TestTosca.yml");
121             outstream = new FileOutputStream(toscaFile);
122             outstream.close();
123             ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
124             arp.generateArtifact(pd, outstream);
125             Assert.fail();
126         } catch (ArtifactProcessorException e) {
127             Assert.assertEquals(expectedMsg, e.getMessage());
128         }
129     }
130
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());
140
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());
150
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;
156     }
157
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");
163
164         // Create RequestKey object 2
165         RequestKey requestKey2 = new RequestKey();
166         requestKey2.setKeyName("address_fqdn");
167         requestKey2.setKeyValue("someVal");
168
169         // Create RequestKey object 3
170         RequestKey requestKey3 = new RequestKey();
171         requestKey3.setKeyName("address_type");
172         requestKey3.setKeyValue("v4");
173
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;
180     }
181
182     private List<ResponseKey> createResponseKeys() {
183         // Create RequestKey object 1
184         ResponseKey responseKey1 = new ResponseKey();
185
186         responseKey1.setUniqueKeyName("name1");
187         responseKey1.setUniqueKeyValue("value1");
188         responseKey1.setFieldKeyName("field1");
189
190         // Add the RequestKey Objects to the List
191         List<ResponseKey> responseKeyList = new ArrayList<ResponseKey>();
192         responseKeyList.add(responseKey1);
193
194         return responseKeyList;
195     }
196
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;
206     }
207
208     @Test(expected = Exception.class)
209     public void testPDnull() throws IOException, ArtifactProcessorException {
210         PropertyDefinition pd = null;
211         OutputStream outstream = null;
212
213         outstream = new FileOutputStream(".\\TestTosca.yml");
214         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
215         arp.generateArtifact(pd, outstream);
216         outstream.flush();
217         outstream.close();
218     }
219
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();
225
226         while (line != null) {
227             sb.append(line).append("\n");
228             line = buf.readLine();
229         }
230         String fileString = sb.toString();
231         is.close();
232         return fileString;
233     }
234 }