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