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