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