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