Initial commit for appc-config-params
[appc.git] / appc-config / appc-config-params / provider / src / test / java / org / openecomp / sdnc / config / params / transformer / tosca / TestGenerateArtifactObject.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  * 
11  *         http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  * 
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.openecomp.sdnc.config.params.transformer.tosca;
24
25 import org.junit.Assert;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.openecomp.sdnc.config.params.data.Parameter;
30 import org.openecomp.sdnc.config.params.data.PropertyDefinition;
31 import org.openecomp.sdnc.config.params.data.RequestKey;
32 import org.openecomp.sdnc.config.params.data.ResponseKey;
33 import org.openecomp.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
34
35 import java.io.*;
36 import java.util.ArrayList;
37 import java.util.List;
38
39
40 /**
41  * Created by pranavdi on 3/15/2017.
42  */
43 public class TestGenerateArtifactObject
44 {
45     @Rule
46     public TemporaryFolder temporaryFolder = new TemporaryFolder();
47
48     @Test
49     public void testObjectArtifactProcessor() throws IOException, ArtifactProcessorException {
50
51         String expectedTosca="node_types:\n" +
52                 "  VNF:\n" +
53                 "    derived_from: org.openecomp.genericvnf\n" +
54                 "    version: V1\n" +
55                 "    description: ''\n" +
56                 "    properties:\n" +
57                 "      LOCAL_ACCESS_IP_ADDR:\n" +
58                 "        type: string\n" +
59                 "        required: false\n" +
60                 "        default: 192.168.30.1\n" +
61                 "        status: SUPPORTED\n" +
62                 "      LOCAL_CORE_ALT_IP_ADDR:\n" +
63                 "        type: String\n" +
64                 "        required: false\n" +
65                 "        default: fd00:f4d5:ea06:1:0:110:254\n" +
66                 "        status: SUPPORTED\n" +
67                 "topology_template:\n" +
68                 "  node_templates:\n" +
69                 "    VNF_Template:\n" +
70                 "      type: VNF\n" +
71                 "      properties:\n" +
72                 "        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" +
73                 "        LOCAL_CORE_ALT_IP_ADDR: <rule-type = myRule2> <response-keys = name1:value1:field1> <source-system = INSTAR> <request-keys = >\n";
74         //Create object
75         PropertyDefinition pd = new PropertyDefinition();
76         pd.setKind("VNF");
77         pd.setVersion("V1");
78         pd.setParameters(createParameters());
79
80         //Call ArtifactProcessor
81         OutputStream outstream=null;
82
83         File toscaFile =temporaryFolder.newFile("TestTosca.yml");
84         outstream = new FileOutputStream(toscaFile);
85         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
86         arp.generateArtifact(pd,outstream);
87         outstream.flush();
88         outstream.close();
89
90         String toscaString = getFileContent(toscaFile);
91         Assert.assertEquals(expectedTosca,toscaString);
92
93     }
94
95 //    @Test
96     public void testPDpropertiesSetNull() throws IOException, ArtifactProcessorException {
97         String expectedTosca = "node_types:\n" +
98                 "  PropertyDefinition:\n" +
99                 "    derived_from: org.openecomp.genericvnf\n" +
100                 "    version: V1\n" +
101                 "    description: ''\n" +
102                 "topology_template:\n" +
103                 "  node_templates:\n" +
104                 "    PropertyDefinition_Template:\n" +
105                 "      type: PropertyDefinition\n";
106         //Create object
107         PropertyDefinition pd = new PropertyDefinition();
108         pd.setKind("PropertyDefinition");
109         pd.setVersion("V1");
110 //        pd.setParameters(createParameters());
111
112         //Call ArtifactProcessor
113         OutputStream outstream=null;
114
115         File toscaFile =temporaryFolder.newFile("TestTosca.yml");
116         outstream = new FileOutputStream(toscaFile);
117
118         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
119         arp.generateArtifact(pd,outstream);
120         outstream.flush();
121         outstream.close();
122
123         String toscaString = getFileContent(toscaFile);
124         Assert.assertEquals(expectedTosca,toscaString);
125     }
126
127  //   @Test
128     public void testArtifactGeneratorInvalidStream() throws IOException {
129         String expectedMsg = "java.io.IOException: Stream Closed";
130         PropertyDefinition pd = new PropertyDefinition();
131         pd.setKind("VNF");
132         pd.setVersion("V1");
133         pd.setParameters(createParameters());
134
135         //Call ArtifactProcessor
136         OutputStream outstream=null;
137         try {
138             File toscaFile =temporaryFolder.newFile("TestTosca.yml");
139             outstream = new FileOutputStream(toscaFile);
140             outstream.close();
141             ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
142             arp.generateArtifact(pd,outstream);
143             Assert.fail();
144         }
145         catch (ArtifactProcessorException e)
146         {
147             Assert.assertEquals(expectedMsg,e.getMessage());
148         }
149     }
150
151     private List<Parameter> createParameters()
152     {
153         //Create single Parameter object 1
154         Parameter singleParameter1 = new Parameter();
155         singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
156 //        singleParameter1.setList(false);
157         singleParameter1.setRequired(false);
158         singleParameter1.setSource("INSTAR");
159         singleParameter1.setDefaultValue("192.168.30.1");
160         singleParameter1.setRuleType("myRule1");
161         singleParameter1.setRequestKeys(createRequestKeys());
162
163         //Create single Parameter object 2
164         Parameter singleParameter2 = new Parameter();
165         singleParameter2.setName("LOCAL_CORE_ALT_IP_ADDR");
166         singleParameter2.setType("String");
167 //        singleParameter2.setList(false);
168         singleParameter2.setRequired(false);
169         singleParameter2.setSource("INSTAR");
170         singleParameter2.setDefaultValue("fd00:f4d5:ea06:1:0:110:254");
171         singleParameter2.setRuleType("myRule2");
172         singleParameter2.setResponseKeys(createResponseKeys());
173
174
175         //Add the Parameter objects to the List
176         List<Parameter> parameterList = new ArrayList<Parameter>();
177         parameterList.add(singleParameter1);
178         parameterList.add(singleParameter2);
179         return parameterList;
180     }
181
182     private List<RequestKey> createRequestKeys()
183     {
184         //Create RequestKey object 1
185         RequestKey requestKey1 = new RequestKey();
186         requestKey1.setKeyName("class-type");
187         requestKey1.setKeyValue("interface-ip-address");
188
189         //Create RequestKey object 2
190         RequestKey requestKey2 = new RequestKey();
191         requestKey2.setKeyName("address_fqdn");
192         requestKey2.setKeyValue("someVal");
193
194         //Create RequestKey object 3
195         RequestKey requestKey3 = new RequestKey();
196         requestKey3.setKeyName("address_type");
197         requestKey3.setKeyValue("v4");
198
199         //Add the RequestKey Objects to the List
200         List<RequestKey> requestKeyList = new ArrayList<RequestKey>();
201         requestKeyList.add(requestKey1);
202         requestKeyList.add(requestKey2);
203         requestKeyList.add(requestKey3);
204         return  requestKeyList;
205     }
206
207     private List<ResponseKey> createResponseKeys()
208     {
209         //Create RequestKey object 1
210         ResponseKey responseKey1 = new ResponseKey();
211
212         responseKey1.setUniqueKeyName("name1");
213         responseKey1.setUniqueKeyValue("value1");
214         responseKey1.setFieldKeyName("field1");
215
216         //Add the RequestKey Objects to the List
217         List<ResponseKey> responseKeyList = new ArrayList<ResponseKey>();
218         responseKeyList.add(responseKey1);
219
220         return  responseKeyList;
221     }
222
223     private Parameter createParameter()
224     {
225         Parameter singleParameter1 = new Parameter();
226         singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
227         //singleParameter1.setList(false);
228         singleParameter1.setRequired(false);
229         singleParameter1.setSource("INSTAR");
230         singleParameter1.setDefaultValue("192.168.30.1");
231         singleParameter1.setRequestKeys(createRequestKeys());
232         singleParameter1.setResponseKeys(createResponseKeys());
233         return singleParameter1;
234     }
235
236     //@Test
237     public void testPDnull() throws IOException, ArtifactProcessorException {
238         PropertyDefinition pd = null;
239         OutputStream outstream=null;
240
241         outstream = new FileOutputStream(".\\TestTosca.yml");
242         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
243         arp.generateArtifact(pd,outstream);
244         outstream.flush();
245         outstream.close();
246
247
248     }
249
250     private String getFileContent(File file) throws IOException
251     {
252         InputStream is = new FileInputStream(file);
253         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
254         String line = buf.readLine();
255         StringBuilder sb = new StringBuilder();
256
257         while (line != null) {
258             sb.append(line).append("\n");
259             line = buf.readLine();
260         }
261         String fileString = sb.toString();
262         is.close();
263         return fileString;
264     }
265 }