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