Initial commit for appc-config-params
[appc.git] / appc-config / appc-config-params / provider / src / test / java / org / openecomp / sdnc / config / params / transformer / tosca / TestGenerateArtifactString.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.transformer.tosca.exceptions.ArtifactProcessorException;
30
31 import java.io.*;
32 import java.net.URL;
33
34 /**
35  * Created by pranavdi on 3/21/2017.
36  */
37 public class TestGenerateArtifactString{
38
39     @Rule
40     public TemporaryFolder temporaryFolder = new TemporaryFolder();
41
42 //    @Test
43     public void testStringArtifactGenerator() throws IOException, ArtifactProcessorException {
44
45         String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
46         OutputStream outstream=null;
47
48         File tempFile = temporaryFolder.newFile("TestTosca.yml");
49         outstream = new FileOutputStream(tempFile);
50         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
51         arp.generateArtifact(pdString,outstream);
52         outstream.flush();
53         outstream.close();
54
55         String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
56         String toscaString = getFileContent(tempFile);
57         Assert.assertEquals(expectedTosca,toscaString);
58
59     }
60
61  //   @Test
62     public void testArtifactGeneratorWithParameterNameBlank() throws IOException, ArtifactProcessorException {
63
64         String pdString = getFileContent("tosca/ExamplePropertyDefinition2.yml");
65         OutputStream outstream=null;
66         String expectedMsg ="Parameter name is empty,null or contains whitespace";
67
68         File tempFile = temporaryFolder.newFile("TestTosca.yml");
69         outstream = new FileOutputStream(tempFile);
70         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
71         try {
72             arp.generateArtifact(pdString, outstream);
73         }
74         catch (ArtifactProcessorException e)
75         {
76             Assert.assertEquals(expectedMsg,e.getMessage());
77         }
78         outstream.flush();
79         outstream.close();
80     }
81
82   //  @Test
83     public void testArtifactGeneratorWithParameterNameNull() throws IOException, ArtifactProcessorException {
84
85         String pdString = getFileContent("tosca/ExamplePropertyDefinition3.yml");
86         OutputStream outstream=null;
87         String expectedMsg ="Parameter name is empty,null or contains whitespace";
88
89         File tempFile = temporaryFolder.newFile("TestTosca.yml");
90         outstream = new FileOutputStream(tempFile);
91         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
92         try {
93             arp.generateArtifact(pdString, outstream);
94         }
95         catch (ArtifactProcessorException e)
96         {
97             Assert.assertEquals(expectedMsg,e.getMessage());
98         }
99         outstream.flush();
100         outstream.close();
101     }
102
103    // @Test
104     public void testArtifactGeneratorWithKindNull() throws IOException, ArtifactProcessorException {
105
106         String pdString = getFileContent("tosca/ExamplePropertyDefinition4.yml");
107         OutputStream outstream=null;
108         String expectedMsg ="Kind in PropertyDefinition is blank or null";
109
110         File tempFile = temporaryFolder.newFile("TestTosca.yml");
111         outstream = new FileOutputStream(tempFile);
112         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
113         try {
114             arp.generateArtifact(pdString, outstream);
115         }
116         catch (ArtifactProcessorException e)
117         {
118             Assert.assertEquals(expectedMsg,e.getMessage());
119         }
120         outstream.flush();
121         outstream.close();
122     }
123
124     private String getFileContent(String fileName) throws IOException
125     {
126         ClassLoader classLoader = new TestGenerateArtifactString().getClass().getClassLoader();
127         InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
128         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
129         String line = buf.readLine();
130         StringBuilder sb = new StringBuilder();
131
132         while (line != null) {
133             sb.append(line).append("\n");
134             line = buf.readLine();
135         }
136         String fileString = sb.toString();
137         is.close();
138         return fileString;
139     }
140
141     private String getFileContent(File file) throws IOException
142     {
143         InputStream is = new FileInputStream(file);
144         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
145         String line = buf.readLine();
146         StringBuilder sb = new StringBuilder();
147
148         while (line != null) {
149             sb.append(line).append("\n");
150             line = buf.readLine();
151         }
152         String fileString = sb.toString();
153         is.close();
154         return fileString;
155     }
156 }