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