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