2  * ============LICENSE_START=======================================================
 
   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
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.openecomp.sdnc.config.params.transformer.tosca;
 
  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;
 
  38 import java.util.ArrayList;
 
  39 import java.util.List;
 
  41 public class TestGenerateArtifactObject
 
  44     public TemporaryFolder temporaryFolder = new TemporaryFolder();
 
  47     public void testObjectArtifactProcessor() throws IOException, ArtifactProcessorException {
 
  49         String expectedTosca="node_types:\n" +
 
  51                 "    derived_from: org.openecomp.genericvnf\n" +
 
  53                 "    description: ''\n" +
 
  55                 "      LOCAL_ACCESS_IP_ADDR:\n" +
 
  57                 "        required: false\n" +
 
  58                 "        default: 0.0.0.0\n" +
 
  59                 "        status: SUPPORTED\n" +
 
  60                 "      LOCAL_CORE_ALT_IP_ADDR:\n" +
 
  62                 "        required: false\n" +
 
  64                 "        status: SUPPORTED\n" +
 
  65                 "topology_template:\n" +
 
  66                 "  node_templates:\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";
 
  73         PropertyDefinition pd = new PropertyDefinition();
 
  76         pd.setParameters(createParameters());
 
  78         //Call ArtifactProcessor
 
  79         OutputStream outstream=null;
 
  81         File toscaFile =temporaryFolder.newFile("TestTosca.yml");
 
  82         outstream = new FileOutputStream(toscaFile);
 
  83         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
 
  84         arp.generateArtifact(pd,outstream);
 
  88         String toscaString = getFileContent(toscaFile);
 
  89         Assert.assertEquals(expectedTosca,toscaString);
 
  94     public void testPDpropertiesSetNull() throws IOException, ArtifactProcessorException {
 
  95         String expectedTosca = "node_types:\n" +
 
  96                 "  PropertyDefinition:\n" +
 
  97                 "    derived_from: org.openecomp.genericvnf\n" +
 
  99                 "    description: ''\n" +
 
 100                 "topology_template:\n" +
 
 101                 "  node_templates:\n" +
 
 102                 "    PropertyDefinition_Template:\n" +
 
 103                 "      type: PropertyDefinition\n";
 
 105         PropertyDefinition pd = new PropertyDefinition();
 
 106         pd.setKind("PropertyDefinition");
 
 108         OutputStream outstream=null;
 
 110         File toscaFile =temporaryFolder.newFile("TestTosca.yml");
 
 111         outstream = new FileOutputStream(toscaFile);
 
 113         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
 
 114         arp.generateArtifact(pd,outstream);
 
 118         String toscaString = getFileContent(toscaFile);
 
 119         Assert.assertEquals(expectedTosca,toscaString);
 
 123     public void testArtifactGeneratorInvalidStream() throws IOException {
 
 124         String expectedMsg = "java.io.IOException: Stream Closed";
 
 125         PropertyDefinition pd = new PropertyDefinition();
 
 128         pd.setParameters(createParameters());
 
 130         //Call ArtifactProcessor
 
 131         OutputStream outstream=null;
 
 133             File toscaFile =temporaryFolder.newFile("TestTosca.yml");
 
 134             outstream = new FileOutputStream(toscaFile);
 
 136             ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
 
 137             arp.generateArtifact(pd,outstream);
 
 140         catch (ArtifactProcessorException e)
 
 142             Assert.assertEquals(expectedMsg,e.getMessage());
 
 146     private List<Parameter> createParameters()
 
 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());
 
 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());
 
 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;
 
 174     private List<RequestKey> createRequestKeys()
 
 176         //Create RequestKey object 1
 
 177         RequestKey requestKey1 = new RequestKey();
 
 178         requestKey1.setKeyName("class-type");
 
 179         requestKey1.setKeyValue("interface-ip-address");
 
 181         //Create RequestKey object 2
 
 182         RequestKey requestKey2 = new RequestKey();
 
 183         requestKey2.setKeyName("address_fqdn");
 
 184         requestKey2.setKeyValue("someVal");
 
 186         //Create RequestKey object 3
 
 187         RequestKey requestKey3 = new RequestKey();
 
 188         requestKey3.setKeyName("address_type");
 
 189         requestKey3.setKeyValue("v4");
 
 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;
 
 199     private List<ResponseKey> createResponseKeys()
 
 201         //Create RequestKey object 1
 
 202         ResponseKey responseKey1 = new ResponseKey();
 
 204         responseKey1.setUniqueKeyName("name1");
 
 205         responseKey1.setUniqueKeyValue("value1");
 
 206         responseKey1.setFieldKeyName("field1");
 
 208         //Add the RequestKey Objects to the List
 
 209         List<ResponseKey> responseKeyList = new ArrayList<ResponseKey>();
 
 210         responseKeyList.add(responseKey1);
 
 212         return  responseKeyList;
 
 215     private Parameter createParameter()
 
 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;
 
 227     @Test(expected =Exception.class)
 
 228     public void testPDnull() throws IOException, ArtifactProcessorException {
 
 229         PropertyDefinition pd = null;
 
 230         OutputStream outstream=null;
 
 232         outstream = new FileOutputStream(".\\TestTosca.yml");
 
 233         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
 
 234         arp.generateArtifact(pd,outstream);
 
 239     private String getFileContent(File file) throws IOException
 
 241         InputStream is = new FileInputStream(file);
 
 242         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
 
 243         String line = buf.readLine();
 
 244         StringBuilder sb = new StringBuilder();
 
 246         while (line != null) {
 
 247             sb.append(line).append("\n");
 
 248             line = buf.readLine();
 
 250         String fileString = sb.toString();