5a8930fa7da2341c89a66f639fe0b8dc3d542e28
[appc.git] / appc-config / appc-config-params / provider / src / test / java / org / openecomp / sdnc / config / params / transformer / tosca / TestArtifactProcessor.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
33 public class TestArtifactProcessor{
34     @Rule
35     public TemporaryFolder temporaryFolder = new TemporaryFolder();
36
37 //    @Test
38     public void testArtifactProcessor() throws IOException, ArtifactProcessorException {
39
40         ArtifactProcessor arp = ArtifactProcessorFactory.getArtifactProcessor();
41
42         String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
43         OutputStream outstream=null;
44
45         File tempFile = temporaryFolder.newFile("TestTosca.yml");
46         outstream = new FileOutputStream(tempFile);
47         arp.generateArtifact(pdString,outstream);
48         outstream.flush();
49         outstream.close();
50
51         String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
52         String toscaString = getFileContent(tempFile);
53         Assert.assertEquals(expectedTosca,toscaString);
54     }
55
56  //   @Test
57     public void testArtifactProcessorWithStringOutput() throws IOException, ArtifactProcessorException {
58
59         ArtifactProcessor arp = ArtifactProcessorFactory.getArtifactProcessor();
60
61         String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
62         OutputStream outstream=null;
63
64         outstream = new ByteArrayOutputStream();
65         arp.generateArtifact(pdString,outstream);
66         outstream.flush();
67         outstream.close();
68
69         String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
70         String toscaString = outstream.toString();
71         Assert.assertEquals(expectedTosca,toscaString);
72     }
73
74     private String getFileContent(String fileName) throws IOException{
75         ClassLoader classLoader = new TestArtifactProcessor().getClass().getClassLoader();
76         InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
77         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
78         String line = buf.readLine();
79         StringBuilder sb = new StringBuilder();
80
81         while (line != null) {
82             sb.append(line).append("\n");
83             line = buf.readLine();
84         }
85         String fileString = sb.toString();
86         is.close();
87         return fileString;
88     }
89
90     private String getFileContent(File file) throws IOException{
91         InputStream is = new FileInputStream(file);
92         BufferedReader buf = new BufferedReader(new InputStreamReader(is));
93         String line = buf.readLine();
94         StringBuilder sb = new StringBuilder();
95
96         while (line != null) {
97             sb.append(line).append("\n");
98             line = buf.readLine();
99         }
100         String fileString = sb.toString();
101         is.close();
102         return fileString;
103     }
104 }