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