2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.openecomp.sdnc.config.params.transformer.tosca;
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;
35 * Created by pranavdi on 3/21/2017.
37 public class TestGenerateArtifactString{
40 public TemporaryFolder temporaryFolder = new TemporaryFolder();
43 public void testStringArtifactGenerator() throws IOException, ArtifactProcessorException {
45 String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
46 OutputStream outstream=null;
48 File tempFile = temporaryFolder.newFile("TestTosca.yml");
49 outstream = new FileOutputStream(tempFile);
50 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
51 arp.generateArtifact(pdString,outstream);
55 String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
56 String toscaString = getFileContent(tempFile);
57 Assert.assertEquals(expectedTosca,toscaString);
62 public void testArtifactGeneratorWithParameterNameBlank() throws IOException, ArtifactProcessorException {
64 String pdString = getFileContent("tosca/ExamplePropertyDefinition2.yml");
65 OutputStream outstream=null;
66 String expectedMsg ="Parameter name is empty,null or contains whitespace";
68 File tempFile = temporaryFolder.newFile("TestTosca.yml");
69 outstream = new FileOutputStream(tempFile);
70 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
72 arp.generateArtifact(pdString, outstream);
74 catch (ArtifactProcessorException e)
76 Assert.assertEquals(expectedMsg,e.getMessage());
83 public void testArtifactGeneratorWithParameterNameNull() throws IOException, ArtifactProcessorException {
85 String pdString = getFileContent("tosca/ExamplePropertyDefinition3.yml");
86 OutputStream outstream=null;
87 String expectedMsg ="Parameter name is empty,null or contains whitespace";
89 File tempFile = temporaryFolder.newFile("TestTosca.yml");
90 outstream = new FileOutputStream(tempFile);
91 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
93 arp.generateArtifact(pdString, outstream);
95 catch (ArtifactProcessorException e)
97 Assert.assertEquals(expectedMsg,e.getMessage());
104 public void testArtifactGeneratorWithKindNull() throws IOException, ArtifactProcessorException {
106 String pdString = getFileContent("tosca/ExamplePropertyDefinition4.yml");
107 OutputStream outstream=null;
108 String expectedMsg ="Kind in PropertyDefinition is blank or null";
110 File tempFile = temporaryFolder.newFile("TestTosca.yml");
111 outstream = new FileOutputStream(tempFile);
112 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
114 arp.generateArtifact(pdString, outstream);
116 catch (ArtifactProcessorException e)
118 Assert.assertEquals(expectedMsg,e.getMessage());
124 private String getFileContent(String fileName) throws IOException
126 ClassLoader classLoader = new TestGenerateArtifactString().getClass().getClassLoader();
127 InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
128 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
129 String line = buf.readLine();
130 StringBuilder sb = new StringBuilder();
132 while (line != null) {
133 sb.append(line).append("\n");
134 line = buf.readLine();
136 String fileString = sb.toString();
141 private String getFileContent(File file) throws IOException
143 InputStream is = new FileInputStream(file);
144 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
145 String line = buf.readLine();
146 StringBuilder sb = new StringBuilder();
148 while (line != null) {
149 sb.append(line).append("\n");
150 line = buf.readLine();
152 String fileString = sb.toString();