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