Fix for APPC-1263
[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  * 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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.sdnc.config.params.transformer.tosca;
27
28 import java.io.File;
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;
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 = FileUtils.readFileToString(toscaFile, StandardCharsets.UTF_8);
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 = FileUtils.readFileToString(toscaFile, StandardCharsets.UTF_8);
106
107         Assert.assertEquals(expectedTosca, toscaString);
108     }
109
110     @Test
111     public void testArtifactGeneratorInvalidStream() throws IOException {
112         String expectedMsg = "java.io.IOException: Stream Closed";
113         PropertyDefinition pd = new PropertyDefinition();
114         pd.setKind("VNF");
115         pd.setVersion("V1");
116         pd.setParameters(createParameters());
117
118         // Call ArtifactProcessor
119         OutputStream outstream = null;
120         try {
121             File toscaFile = temporaryFolder.newFile("TestTosca.yml");
122             outstream = new FileOutputStream(toscaFile);
123             outstream.close();
124             ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
125             arp.generateArtifact(pd, outstream);
126             Assert.fail();
127         } catch (ArtifactProcessorException e) {
128             Assert.assertEquals(expectedMsg, e.getMessage());
129         }
130     }
131
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());
141
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());
151
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;
157     }
158
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");
164
165         // Create RequestKey object 2
166         RequestKey requestKey2 = new RequestKey();
167         requestKey2.setKeyName("address_fqdn");
168         requestKey2.setKeyValue("someVal");
169
170         // Create RequestKey object 3
171         RequestKey requestKey3 = new RequestKey();
172         requestKey3.setKeyName("address_type");
173         requestKey3.setKeyValue("v4");
174
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;
181     }
182
183     private List<ResponseKey> createResponseKeys() {
184         // Create RequestKey object 1
185         ResponseKey responseKey1 = new ResponseKey();
186
187         responseKey1.setUniqueKeyName("name1");
188         responseKey1.setUniqueKeyValue("value1");
189         responseKey1.setFieldKeyName("field1");
190
191         // Add the RequestKey Objects to the List
192         List<ResponseKey> responseKeyList = new ArrayList<ResponseKey>();
193         responseKeyList.add(responseKey1);
194
195         return responseKeyList;
196     }
197
198     @Test(expected = Exception.class)
199     public void testPDnull() throws IOException, ArtifactProcessorException {
200         PropertyDefinition pd = null;
201         OutputStream outstream = null;
202
203         outstream = new FileOutputStream(".\\TestTosca.yml");
204         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
205         arp.generateArtifact(pd, outstream);
206         outstream.flush();
207         outstream.close();
208     }
209 }