f828297b074df3880230f1ac9241bd7daf538c35
[appc.git] /
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 /**
37  * Created by pranavdi on 3/21/2017.
38  */
39 public class TestGenerateArtifactString{
40
41     @Rule
42     public TemporaryFolder temporaryFolder = new TemporaryFolder();
43
44 //    @Test
45     public void testStringArtifactGenerator() throws IOException, ArtifactProcessorException {
46
47         String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
48         OutputStream outstream=null;
49
50         File tempFile = temporaryFolder.newFile("TestTosca.yml");
51         outstream = new FileOutputStream(tempFile);
52         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
53         arp.generateArtifact(pdString,outstream);
54         outstream.flush();
55         outstream.close();
56
57         String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
58         String toscaString = getFileContent(tempFile);
59         Assert.assertEquals(expectedTosca,toscaString);
60
61     }
62
63  //   @Test
64     public void testArtifactGeneratorWithParameterNameBlank() throws IOException, ArtifactProcessorException {
65
66         String pdString = getFileContent("tosca/ExamplePropertyDefinition2.yml");
67         OutputStream outstream=null;
68         String expectedMsg ="Parameter name is empty,null or contains whitespace";
69
70         File tempFile = temporaryFolder.newFile("TestTosca.yml");
71         outstream = new FileOutputStream(tempFile);
72         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
73         try {
74             arp.generateArtifact(pdString, outstream);
75         }
76         catch (ArtifactProcessorException e)
77         {
78             Assert.assertEquals(expectedMsg,e.getMessage());
79         }
80         outstream.flush();
81         outstream.close();
82     }
83
84   //  @Test
85     public void testArtifactGeneratorWithParameterNameNull() throws IOException, ArtifactProcessorException {
86
87         String pdString = getFileContent("tosca/ExamplePropertyDefinition3.yml");
88         OutputStream outstream=null;
89         String expectedMsg ="Parameter name is empty,null or contains whitespace";
90
91         File tempFile = temporaryFolder.newFile("TestTosca.yml");
92         outstream = new FileOutputStream(tempFile);
93         ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
94         try {
95             arp.generateArtifact(pdString, outstream);
96         }
97         catch (ArtifactProcessorException e)
98         {
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         }
118         catch (ArtifactProcessorException e)
119         {
120             Assert.assertEquals(expectedMsg,e.getMessage());
121         }
122         outstream.flush();
123         outstream.close();
124     }
125
126     private String getFileContent(String fileName) throws IOException
127     {
128         ClassLoader classLoader = new TestGenerateArtifactString().getClass().getClassLoader();
129         InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
130         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
131         String line = buf.readLine();
132         StringBuilder sb = new StringBuilder();
133
134         while (line != null) {
135             sb.append(line).append("\n");
136             line = buf.readLine();
137         }
138         String fileString = sb.toString();
139         is.close();
140         return fileString;
141     }
142
143     private String getFileContent(File file) throws IOException
144     {
145         InputStream is = new FileInputStream(file);
146         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
147         String line = buf.readLine();
148         StringBuilder sb = new StringBuilder();
149
150         while (line != null) {
151             sb.append(line).append("\n");
152             line = buf.readLine();
153         }
154         String fileString = sb.toString();
155         is.close();
156         return fileString;
157     }
158 }